|
1 | 1 | #include "i2c_soft_init.h" |
2 | 2 |
|
| 3 | +#ifdef SOC_STM32F1 |
| 4 | +#include <stm32f10x.h> |
| 5 | +#endif |
| 6 | + |
3 | 7 | void stm32_set_sda(void *data, rt_int32_t state) |
4 | 8 | { |
| 9 | + // examples for STM32F103x |
| 10 | + #ifdef SOC_STM32F1 |
5 | 11 | if(state == 1) |
6 | 12 | GPIO_SetBits(I2C1_GPIO , I2C1_GPIO_SDA); //GPIOB->BSRRL = I2C1_GPIO_SDA |
7 | 13 | // rt_pin_write(I2C1_GPIO_SDA,PIN_HIGH); |
8 | 14 | else if(state == 0) |
9 | 15 | GPIO_ResetBits(I2C1_GPIO , I2C1_GPIO_SDA); //GPIOB->BSRRH = I2C1_GPIO_SDA |
10 | 16 | // rt_pin_write(I2C1_GPIO_SDA,PIN_LOW); |
| 17 | + #endif |
11 | 18 | } |
12 | 19 |
|
13 | 20 | void stm32_set_scl(void *data, rt_int32_t state) |
14 | 21 | { |
| 22 | + // examples for STM32F103x |
| 23 | + #ifdef SOC_STM32F1 |
15 | 24 | if(state == 1) |
16 | 25 | GPIO_SetBits(I2C1_GPIO , I2C1_GPIO_SCL); //GPIOB->BSRRL = I2C1_GPIO_SCL |
17 | 26 | // rt_pin_write(I2C1_GPIO_SCL,PIN_HIGH); |
18 | 27 | else if(state == 0) |
19 | 28 | GPIO_ResetBits(I2C1_GPIO , I2C1_GPIO_SCL); //GPIOB->BSRRH = I2C1_GPIO_SCL |
20 | 29 | // rt_pin_write(I2C1_GPIO_SCL,PIN_LOW); |
| 30 | + #endif |
21 | 31 | } |
22 | 32 |
|
23 | 33 | rt_int32_t stm32_get_sda(void *data) |
24 | 34 | { |
| 35 | + // examples for STM32F103x |
| 36 | + #ifdef SOC_STM32F1 |
25 | 37 | return (rt_int32_t)GPIO_ReadInputDataBit(I2C1_GPIO , I2C1_GPIO_SDA);//return(GPIOB->IDR & I2C1_GPIO_SDA) |
26 | | - // return rt_pin_read(I2C1_GPIO_SDA); |
| 38 | + #endif |
| 39 | + // return rt_pin_read(I2C1_GPIO_SDA); |
27 | 40 | } |
28 | 41 |
|
29 | 42 | rt_int32_t stm32_get_scl(void *data) |
30 | 43 | { |
| 44 | + // examples for STM32F103x |
| 45 | + #ifdef SOC_STM32F1 |
31 | 46 | return (rt_int32_t)GPIO_ReadInputDataBit(I2C1_GPIO , I2C1_GPIO_SCL);//return(GPIOB->IDR & I2C1_GPIO_SCL) |
32 | | - // return rt_pin_read(I2C1_GPIO_SCL); |
| 47 | + #endif |
| 48 | + // return rt_pin_read(I2C1_GPIO_SCL); |
33 | 49 | } |
34 | 50 |
|
35 | 51 | void stm32_udelay(rt_uint32_t us) |
36 | 52 | { |
| 53 | + // examples for STM32F103x |
| 54 | + #ifdef SOC_STM32F1 |
37 | 55 | rt_uint32_t delta; |
38 | 56 | /* sysTick->LOAD=21000, RT_TICK_PER_SECOND=1000 */ |
39 | 57 | us = us * (SysTick->LOAD/(1000000/RT_TICK_PER_SECOND)); |
40 | 58 | delta = SysTick->VAL; |
41 | 59 | /* delay us */ |
42 | 60 | while (delta - SysTick->VAL< us); |
| 61 | + #endif |
43 | 62 | } |
44 | 63 |
|
45 | 64 | void stm32_mdelay(rt_uint32_t ms) |
46 | 65 | { |
47 | | - stm32_udelay(ms * 1000); |
| 66 | + // examples for STM32F103x |
| 67 | + #ifdef SOC_STM32F1 |
| 68 | + stm32_udelay(ms * 1000); |
| 69 | + #endif |
48 | 70 | } |
49 | 71 |
|
50 | 72 | static const struct rt_i2c_bit_ops stm32_i2c_bit_ops = |
51 | 73 | { |
| 74 | + // examples for STM32F103x |
| 75 | + #ifdef SOC_STM32F1 |
52 | 76 | (void*)0xaa, //no use in set_sda,set_scl,get_sda,get_scl |
53 | 77 | stm32_set_sda, |
54 | 78 | stm32_set_scl, |
55 | 79 | stm32_get_sda, |
56 | 80 | stm32_get_scl, |
57 | 81 | stm32_udelay, |
58 | | - 20, |
| 82 | + 20, |
| 83 | + #endif |
59 | 84 | }; |
60 | 85 |
|
61 | 86 | static void RCC_Configuration(void) |
62 | 87 | { |
| 88 | + // examples for STM32F103x |
| 89 | + #ifdef SOC_STM32F1 |
63 | 90 | RCC->APB2ENR|=1<<4; |
64 | 91 | RCC_APB2PeriphClockCmd( RCC_I2C, ENABLE ); |
| 92 | + #endif |
65 | 93 | } |
66 | 94 |
|
67 | 95 |
|
68 | 96 | static void GPIO_Configuration(void) |
69 | 97 | { |
70 | | - |
| 98 | + // examples for STM32F103x |
| 99 | + #ifdef SOC_STM32F1 |
71 | 100 | GPIO_InitTypeDef GPIO_InitStructure; |
72 | 101 | GPIO_InitStructure.GPIO_Pin = I2C1_GPIO_SDA | I2C1_GPIO_SCL; |
73 | 102 | GPIO_InitStructure.GPIO_Mode =GPIO_Mode_Out_OD ; |
74 | 103 | GPIO_InitStructure.GPIO_Speed =GPIO_Speed_50MHz; |
75 | 104 |
|
76 | 105 | GPIO_Init(I2C1_GPIO, &GPIO_InitStructure); |
77 | | - |
| 106 | + #endif |
| 107 | + |
78 | 108 | // rt_pin_mode(I2C1_GPIO_SDA, PIN_MODE_OUTPUT_OD); |
79 | 109 | // rt_pin_mode(I2C1_GPIO_SCL, PIN_MODE_OUTPUT_OD); |
80 | 110 |
|
|
0 commit comments