From e88bb51fb5d5e00ac0bf1a56841d42475bbff0c8 Mon Sep 17 00:00:00 2001 From: wirano Date: Thu, 11 Apr 2024 18:07:09 +0800 Subject: [PATCH] bsp/tm4c123: move i2c clk config to tm4c123_config.c --- bsp/tm4c123bsp/board/tm4c123_config.c | 27 ++++++++++++++++++++++ bsp/tm4c123bsp/board/tm4c123_config.h | 4 ++++ bsp/tm4c123bsp/libraries/Drivers/drv_i2c.c | 24 ++----------------- 3 files changed, 33 insertions(+), 22 deletions(-) diff --git a/bsp/tm4c123bsp/board/tm4c123_config.c b/bsp/tm4c123bsp/board/tm4c123_config.c index cc03988adf..da8b1c1c43 100644 --- a/bsp/tm4c123bsp/board/tm4c123_config.c +++ b/bsp/tm4c123bsp/board/tm4c123_config.c @@ -6,12 +6,14 @@ * Change Logs: * Date Author Notes * 2020-06-27 AHTYDHD the first version + * 2024-04-11 Astrozen add i2c support */ #include #include #include #include "inc/hw_memmap.h" +#include "driverlib/rom.h" #include "driverlib/pin_map.h" #include "driverlib/sysctl.h" #include "driverlib/gpio.h" @@ -29,6 +31,9 @@ #ifdef RT_USING_SPI #include "driverlib/ssi.h" #endif /* RT_USING_SPI */ +#ifdef RT_USING_I2C +#include "driverlib/i2c.h" +#endif /* RT_USING_I2C */ #ifdef RT_USING_SERIAL @@ -85,4 +90,26 @@ void spi_hw_config(void) } #endif /* RT_USING_SPI */ +#ifdef RT_USING_I2C +void i2c_hw_config(void) +{ + /* I2C0 */ + ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); + + ROM_GPIOPinConfigure(GPIO_PB2_I2C0SCL); + ROM_GPIOPinConfigure(GPIO_PB3_I2C0SDA); + + ROM_GPIOPinTypeI2CSCL(GPIO_PORTB_BASE, GPIO_PIN_2); + ROM_GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_3); + + ROM_SysCtlPeripheralDisable(SYSCTL_PERIPH_I2C0); + ROM_SysCtlPeripheralReset(SYSCTL_PERIPH_I2C0); + ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0); + while (!SysCtlPeripheralReady(SYSCTL_PERIPH_I2C0)); + + // timeout:5ms + ROM_I2CMasterTimeoutSet(I2C0_BASE, 0x7d); +} +#endif /* RT_USING_I2C */ + /************************** end of file ******************/ diff --git a/bsp/tm4c123bsp/board/tm4c123_config.h b/bsp/tm4c123bsp/board/tm4c123_config.h index 0153babcfa..e62134c00e 100644 --- a/bsp/tm4c123bsp/board/tm4c123_config.h +++ b/bsp/tm4c123bsp/board/tm4c123_config.h @@ -6,6 +6,7 @@ * Change Logs: * Date Author Notes * 2020-06-27 AHTYDHD the first version + * 2024-04-11 Astrozen add i2c support */ #ifndef __TM4C123GH6PZ_CONFIG_H__ @@ -27,6 +28,9 @@ void pwm_hw_config(void); #ifdef RT_USING_SPI void spi_hw_config(void); #endif /* RT_USING_SPI */ +#ifdef RT_USING_I2C +void i2c_hw_config(void); +#endif /* RT_USING_I2C */ #ifdef __cplusplus } diff --git a/bsp/tm4c123bsp/libraries/Drivers/drv_i2c.c b/bsp/tm4c123bsp/libraries/Drivers/drv_i2c.c index 066e16fa22..ec35596349 100644 --- a/bsp/tm4c123bsp/libraries/Drivers/drv_i2c.c +++ b/bsp/tm4c123bsp/libraries/Drivers/drv_i2c.c @@ -19,13 +19,7 @@ #include "drv_i2c.h" #include "inc/hw_memmap.h" -#include #include "i2c_config.h" -#include "driverlib/rom.h" -#include "driverlib/sysctl.h" -#include "driverlib/pin_map.h" -#include "driverlib/gpio.h" -#include "driverlib/i2c.h" enum { #ifdef BSP_USING_I2C0 @@ -184,23 +178,9 @@ static rt_ssize_t tm4c123_i2c_xfer(struct rt_i2c_bus_device *bus, struct rt_i2c_ int rt_hw_i2c_init(void) { rt_err_t ret = RT_EOK; + i2c_hw_config(); + for (uint32_t i = 0; i < sizeof(tm4c123_i2cs) / sizeof(tm4c123_i2cs[0]); i++) { - ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); - - ROM_GPIOPinConfigure(GPIO_PB2_I2C0SCL); - ROM_GPIOPinConfigure(GPIO_PB3_I2C0SDA); - - ROM_GPIOPinTypeI2CSCL(GPIO_PORTB_BASE, GPIO_PIN_2); - ROM_GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_3); - - ROM_SysCtlPeripheralDisable(SYSCTL_PERIPH_I2C0); - ROM_SysCtlPeripheralReset(SYSCTL_PERIPH_I2C0); - ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0); - while (!SysCtlPeripheralReady(SYSCTL_PERIPH_I2C0)); - - // timeout:5ms - ROM_I2CMasterTimeoutSet(I2C0_BASE, 0x7d); - if (tm4c123_i2cs[i].clk_freq == 400000) { ROM_I2CMasterInitExpClk(tm4c123_i2cs[i].base, ROM_SysCtlClockGet(), RT_TRUE); } else {