Skip to content

Commit 1aba10c

Browse files
author
Wu Han
committed
Do not automatically add software i2c bus
1 parent ba2e707 commit 1aba10c

File tree

3 files changed

+62
-33
lines changed

3 files changed

+62
-33
lines changed

port/i2c_soft_init.c

Lines changed: 54 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ void stm32_set_sda(void *data, rt_int32_t state)
1515
GPIO_ResetBits(I2C1_GPIO , I2C1_GPIO_SDA); //GPIOB->BSRRH = I2C1_GPIO_SDA
1616
// rt_pin_write(I2C1_GPIO_SDA,PIN_LOW);
1717
#endif
18+
19+
// If you are going to use software I2C, implement here
20+
1821
}
1922

2023
void stm32_set_scl(void *data, rt_int32_t state)
@@ -28,86 +31,109 @@ void stm32_set_scl(void *data, rt_int32_t state)
2831
GPIO_ResetBits(I2C1_GPIO , I2C1_GPIO_SCL); //GPIOB->BSRRH = I2C1_GPIO_SCL
2932
// rt_pin_write(I2C1_GPIO_SCL,PIN_LOW);
3033
#endif
34+
35+
// If you are going to use software I2C, implement here
36+
3137
}
3238

3339
rt_int32_t stm32_get_sda(void *data)
3440
{
3541
// examples for STM32F103x
3642
#ifdef SOC_STM32F1
37-
return (rt_int32_t)GPIO_ReadInputDataBit(I2C1_GPIO , I2C1_GPIO_SDA);//return(GPIOB->IDR & I2C1_GPIO_SDA)
43+
return (rt_int32_t)GPIO_ReadInputDataBit(I2C1_GPIO , I2C1_GPIO_SDA);//return(GPIOB->IDR & I2C1_GPIO_SDA)
3844
#endif
3945
// return rt_pin_read(I2C1_GPIO_SDA);
46+
47+
// If you are going to use software I2C, implement here
48+
4049
}
4150

4251
rt_int32_t stm32_get_scl(void *data)
4352
{
4453
// examples for STM32F103x
4554
#ifdef SOC_STM32F1
46-
return (rt_int32_t)GPIO_ReadInputDataBit(I2C1_GPIO , I2C1_GPIO_SCL);//return(GPIOB->IDR & I2C1_GPIO_SCL)
55+
return (rt_int32_t)GPIO_ReadInputDataBit(I2C1_GPIO , I2C1_GPIO_SCL);//return(GPIOB->IDR & I2C1_GPIO_SCL)
4756
#endif
4857
// return rt_pin_read(I2C1_GPIO_SCL);
58+
59+
// If you are going to use software I2C, implement here
60+
4961
}
5062

5163
void stm32_udelay(rt_uint32_t us)
5264
{
5365
// examples for STM32F103x
5466
#ifdef SOC_STM32F1
55-
rt_uint32_t delta;
56-
/* sysTick->LOAD=21000, RT_TICK_PER_SECOND=1000 */
57-
us = us * (SysTick->LOAD/(1000000/RT_TICK_PER_SECOND));
58-
delta = SysTick->VAL;
59-
/* delay us */
60-
while (delta - SysTick->VAL< us);
67+
rt_uint32_t delta;
68+
/* sysTick->LOAD=21000, RT_TICK_PER_SECOND=1000 */
69+
us = us * (SysTick->LOAD/(1000000/RT_TICK_PER_SECOND));
70+
delta = SysTick->VAL;
71+
/* delay us */
72+
while (delta - SysTick->VAL< us);
6173
#endif
74+
75+
// If you are going to use software I2C, implement here
76+
6277
}
6378

6479
void stm32_mdelay(rt_uint32_t ms)
6580
{
6681
// examples for STM32F103x
6782
#ifdef SOC_STM32F1
68-
stm32_udelay(ms * 1000);
83+
stm32_udelay(ms * 1000);
6984
#endif
85+
86+
// If you are going to use software I2C, implement here
87+
7088
}
7189

7290
static const struct rt_i2c_bit_ops stm32_i2c_bit_ops =
7391
{
7492
// examples for STM32F103x
7593
#ifdef SOC_STM32F1
76-
(void*)0xaa, //no use in set_sda,set_scl,get_sda,get_scl
77-
stm32_set_sda,
78-
stm32_set_scl,
79-
stm32_get_sda,
80-
stm32_get_scl,
81-
stm32_udelay,
82-
20,
94+
(void*)0xaa, //no use in set_sda,set_scl,get_sda,get_scl
95+
stm32_set_sda,
96+
stm32_set_scl,
97+
stm32_get_sda,
98+
stm32_get_scl,
99+
stm32_udelay,
100+
20,
83101
#endif
102+
103+
// If you are going to use software I2C, implement here
104+
84105
};
85106

86107
static void RCC_Configuration(void)
87108
{
88109
// examples for STM32F103x
89110
#ifdef SOC_STM32F1
90-
RCC->APB2ENR|=1<<4;
91-
RCC_APB2PeriphClockCmd( RCC_I2C, ENABLE );
111+
RCC->APB2ENR|=1<<4;
112+
RCC_APB2PeriphClockCmd( RCC_I2C, ENABLE );
92113
#endif
114+
115+
// If you are going to use software I2C, implement here
116+
93117
}
94118

95119

96120
static void GPIO_Configuration(void)
97121
{
98122
// examples for STM32F103x
99123
#ifdef SOC_STM32F1
100-
GPIO_InitTypeDef GPIO_InitStructure;
101-
GPIO_InitStructure.GPIO_Pin = I2C1_GPIO_SDA | I2C1_GPIO_SCL;
102-
GPIO_InitStructure.GPIO_Mode =GPIO_Mode_Out_OD ;
103-
GPIO_InitStructure.GPIO_Speed =GPIO_Speed_50MHz;
124+
GPIO_InitTypeDef GPIO_InitStructure;
125+
GPIO_InitStructure.GPIO_Pin = I2C1_GPIO_SDA | I2C1_GPIO_SCL;
126+
GPIO_InitStructure.GPIO_Mode =GPIO_Mode_Out_OD ;
127+
GPIO_InitStructure.GPIO_Speed =GPIO_Speed_50MHz;
104128

105-
GPIO_Init(I2C1_GPIO, &GPIO_InitStructure);
129+
GPIO_Init(I2C1_GPIO, &GPIO_InitStructure);
106130
#endif
107131

108132
// rt_pin_mode(I2C1_GPIO_SDA, PIN_MODE_OUTPUT_OD);
109133
// rt_pin_mode(I2C1_GPIO_SCL, PIN_MODE_OUTPUT_OD);
110134

135+
// If you are going to use software I2C, implement here
136+
111137
}
112138

113139
int rt_hw_i2c_init(void)
@@ -123,5 +149,8 @@ int rt_hw_i2c_init(void)
123149

124150
return 0;
125151
}
126-
INIT_BOARD_EXPORT(rt_hw_i2c_init);
127-
//rt_hw_i2c_init will be called in rt_components_board_init()
152+
153+
#ifdef SOC_STM32F1
154+
// rt_hw_i2c_init will be called in rt_components_board_init()
155+
INIT_BOARD_EXPORT(rt_hw_i2c_init);
156+
#endif

port/i2c_soft_init.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
#include <rtdevice.h>
66
#include <drivers/i2c-bit-ops.h>
77

8-
#define RCC_I2C RCC_APB2Periph_GPIOB
9-
#define I2C1_GPIO GPIOB
8+
#define RCC_I2C RCC_APB2Periph_GPIOB
9+
#define I2C1_GPIO GPIOB
1010
#define I2C1_GPIO_SDA GPIO_Pin_7
1111
#define I2C1_GPIO_SCL GPIO_Pin_6
1212

port/u8g_port.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,21 @@ uint8_t u8x8_byte_rt_hw_i2c(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *ar
3434
/* u8g2/u8x8 will never send more than 32 bytes between START_TRANSFER and END_TRANSFER */
3535
struct rt_i2c_msg msgs;
3636
static uint8_t buffer[32];
37-
static uint8_t buf_idx;
38-
uint8_t *data;
37+
static uint8_t buf_idx;
38+
uint8_t *data;
3939

4040

4141
rt_uint8_t t = 0;
4242
switch(msg)
4343
{
4444
case U8X8_MSG_BYTE_SEND:
4545
data = (uint8_t *)arg_ptr;
46-
while( arg_int > 0 )
47-
{
46+
while( arg_int > 0 )
47+
{
4848
buffer[buf_idx++] = *data;
4949
data++;
5050
arg_int--;
51-
}
51+
}
5252
break;
5353
case U8X8_MSG_BYTE_INIT:
5454
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
6262
break;
6363
case U8X8_MSG_BYTE_END_TRANSFER:
6464
// I2C Data Transfer
65-
msgs.addr = u8x8_GetI2CAddress(u8x8)>>1;
65+
msgs.addr = u8x8_GetI2CAddress(u8x8)>>1;
6666
msgs.flags = RT_I2C_WR;
6767
msgs.buf = buffer;
6868
msgs.len = buf_idx;

0 commit comments

Comments
 (0)