@@ -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
2023void 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
3339rt_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
4251rt_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
5163void 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
6479void 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
7290static 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
86107static 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
96120static 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
113139int 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
0 commit comments