diff --git a/port/i2c_soft_init.c b/port/i2c_soft_init.c index 89565a91..abe8e4f1 100644 --- a/port/i2c_soft_init.c +++ b/port/i2c_soft_init.c @@ -15,6 +15,9 @@ void stm32_set_sda(void *data, rt_int32_t state) GPIO_ResetBits(I2C1_GPIO , I2C1_GPIO_SDA); //GPIOB->BSRRH = I2C1_GPIO_SDA // rt_pin_write(I2C1_GPIO_SDA,PIN_LOW); #endif + + // If you are going to use software I2C, implement here + } void stm32_set_scl(void *data, rt_int32_t state) @@ -28,68 +31,89 @@ void stm32_set_scl(void *data, rt_int32_t state) GPIO_ResetBits(I2C1_GPIO , I2C1_GPIO_SCL); //GPIOB->BSRRH = I2C1_GPIO_SCL // rt_pin_write(I2C1_GPIO_SCL,PIN_LOW); #endif + + // If you are going to use software I2C, implement here + } rt_int32_t stm32_get_sda(void *data) { // examples for STM32F103x #ifdef SOC_STM32F1 - return (rt_int32_t)GPIO_ReadInputDataBit(I2C1_GPIO , I2C1_GPIO_SDA);//return(GPIOB->IDR & I2C1_GPIO_SDA) + return (rt_int32_t)GPIO_ReadInputDataBit(I2C1_GPIO , I2C1_GPIO_SDA);//return(GPIOB->IDR & I2C1_GPIO_SDA) #endif // return rt_pin_read(I2C1_GPIO_SDA); + + // If you are going to use software I2C, implement here + } rt_int32_t stm32_get_scl(void *data) { // examples for STM32F103x #ifdef SOC_STM32F1 - return (rt_int32_t)GPIO_ReadInputDataBit(I2C1_GPIO , I2C1_GPIO_SCL);//return(GPIOB->IDR & I2C1_GPIO_SCL) + return (rt_int32_t)GPIO_ReadInputDataBit(I2C1_GPIO , I2C1_GPIO_SCL);//return(GPIOB->IDR & I2C1_GPIO_SCL) #endif // return rt_pin_read(I2C1_GPIO_SCL); + + // If you are going to use software I2C, implement here + } void stm32_udelay(rt_uint32_t us) { // examples for STM32F103x #ifdef SOC_STM32F1 - rt_uint32_t delta; - /* sysTick->LOAD=21000, RT_TICK_PER_SECOND=1000 */ - us = us * (SysTick->LOAD/(1000000/RT_TICK_PER_SECOND)); - delta = SysTick->VAL; - /* delay us */ - while (delta - SysTick->VAL< us); + rt_uint32_t delta; + /* sysTick->LOAD=21000, RT_TICK_PER_SECOND=1000 */ + us = us * (SysTick->LOAD/(1000000/RT_TICK_PER_SECOND)); + delta = SysTick->VAL; + /* delay us */ + while (delta - SysTick->VAL< us); #endif + + // If you are going to use software I2C, implement here + } void stm32_mdelay(rt_uint32_t ms) { // examples for STM32F103x #ifdef SOC_STM32F1 - stm32_udelay(ms * 1000); + stm32_udelay(ms * 1000); #endif + + // If you are going to use software I2C, implement here + } static const struct rt_i2c_bit_ops stm32_i2c_bit_ops = { // examples for STM32F103x #ifdef SOC_STM32F1 - (void*)0xaa, //no use in set_sda,set_scl,get_sda,get_scl - stm32_set_sda, - stm32_set_scl, - stm32_get_sda, - stm32_get_scl, - stm32_udelay, - 20, + (void*)0xaa, //no use in set_sda,set_scl,get_sda,get_scl + stm32_set_sda, + stm32_set_scl, + stm32_get_sda, + stm32_get_scl, + stm32_udelay, + 20, #endif + + // If you are going to use software I2C, implement here + }; static void RCC_Configuration(void) { // examples for STM32F103x #ifdef SOC_STM32F1 - RCC->APB2ENR|=1<<4; - RCC_APB2PeriphClockCmd( RCC_I2C, ENABLE ); + RCC->APB2ENR|=1<<4; + RCC_APB2PeriphClockCmd( RCC_I2C, ENABLE ); #endif + + // If you are going to use software I2C, implement here + } @@ -97,17 +121,19 @@ static void GPIO_Configuration(void) { // examples for STM32F103x #ifdef SOC_STM32F1 - GPIO_InitTypeDef GPIO_InitStructure; - GPIO_InitStructure.GPIO_Pin = I2C1_GPIO_SDA | I2C1_GPIO_SCL; - GPIO_InitStructure.GPIO_Mode =GPIO_Mode_Out_OD ; - GPIO_InitStructure.GPIO_Speed =GPIO_Speed_50MHz; + GPIO_InitTypeDef GPIO_InitStructure; + GPIO_InitStructure.GPIO_Pin = I2C1_GPIO_SDA | I2C1_GPIO_SCL; + GPIO_InitStructure.GPIO_Mode =GPIO_Mode_Out_OD ; + GPIO_InitStructure.GPIO_Speed =GPIO_Speed_50MHz; - GPIO_Init(I2C1_GPIO, &GPIO_InitStructure); + GPIO_Init(I2C1_GPIO, &GPIO_InitStructure); #endif // rt_pin_mode(I2C1_GPIO_SDA, PIN_MODE_OUTPUT_OD); // rt_pin_mode(I2C1_GPIO_SCL, PIN_MODE_OUTPUT_OD); + // If you are going to use software I2C, implement here + } int rt_hw_i2c_init(void) @@ -123,5 +149,8 @@ int rt_hw_i2c_init(void) return 0; } -INIT_BOARD_EXPORT(rt_hw_i2c_init); -//rt_hw_i2c_init will be called in rt_components_board_init() + +#ifdef SOC_STM32F1 + // rt_hw_i2c_init will be called in rt_components_board_init() + INIT_BOARD_EXPORT(rt_hw_i2c_init); +#endif diff --git a/port/i2c_soft_init.h b/port/i2c_soft_init.h index 60bc55d0..2384f939 100644 --- a/port/i2c_soft_init.h +++ b/port/i2c_soft_init.h @@ -5,8 +5,8 @@ #include #include -#define RCC_I2C RCC_APB2Periph_GPIOB -#define I2C1_GPIO GPIOB +#define RCC_I2C RCC_APB2Periph_GPIOB +#define I2C1_GPIO GPIOB #define I2C1_GPIO_SDA GPIO_Pin_7 #define I2C1_GPIO_SCL GPIO_Pin_6 diff --git a/port/u8g_port.c b/port/u8g_port.c index 89e69571..25090a34 100644 --- a/port/u8g_port.c +++ b/port/u8g_port.c @@ -34,8 +34,8 @@ uint8_t u8x8_byte_rt_hw_i2c(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *ar /* u8g2/u8x8 will never send more than 32 bytes between START_TRANSFER and END_TRANSFER */ struct rt_i2c_msg msgs; static uint8_t buffer[32]; - static uint8_t buf_idx; - uint8_t *data; + static uint8_t buf_idx; + uint8_t *data; rt_uint8_t t = 0; @@ -43,12 +43,12 @@ uint8_t u8x8_byte_rt_hw_i2c(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *ar { case U8X8_MSG_BYTE_SEND: data = (uint8_t *)arg_ptr; - while( arg_int > 0 ) - { + while( arg_int > 0 ) + { buffer[buf_idx++] = *data; data++; arg_int--; - } + } break; case U8X8_MSG_BYTE_INIT: i2c_bus = rt_i2c_bus_device_find(I2C_DEVICE_NAME); @@ -62,7 +62,7 @@ uint8_t u8x8_byte_rt_hw_i2c(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *ar break; case U8X8_MSG_BYTE_END_TRANSFER: // I2C Data Transfer - msgs.addr = u8x8_GetI2CAddress(u8x8)>>1; + msgs.addr = u8x8_GetI2CAddress(u8x8)>>1; msgs.flags = RT_I2C_WR; msgs.buf = buffer; msgs.len = buf_idx;