Skip to content

Commit

Permalink
Do not automatically add software i2c bus
Browse files Browse the repository at this point in the history
  • Loading branch information
Wu Han committed Sep 28, 2018
1 parent ba2e707 commit 1aba10c
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 33 deletions.
79 changes: 54 additions & 25 deletions port/i2c_soft_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -28,86 +31,109 @@ 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

}


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)
Expand All @@ -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
4 changes: 2 additions & 2 deletions port/i2c_soft_init.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
#include <rtdevice.h>
#include <drivers/i2c-bit-ops.h>

#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

Expand Down
12 changes: 6 additions & 6 deletions port/u8g_port.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,21 @@ 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;
switch(msg)
{
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);
Expand All @@ -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;
Expand Down

0 comments on commit 1aba10c

Please # to comment.