| Mini51 BSP V3.02.002 The Board Support Package for Mini51 Series

i2c_software_gpio.c
Go to the documentation of this file.
1 /**************************************************************************/
13 #include <stdio.h>
14 #include "i2c_software_gpio.h"
15
16 /* SDA = GP1.4, CLK = GP1.5 */
17 #define I2C_SW_SDA P14
18 #define I2C_SW_CLK P15
19
20 uint32_t u32_I2C_SW_Delay;
21
27 int32_t I2C_SW_Open(uint32_t u32BusClock)
28 {
29 if(u32BusClock>500000)
30 u32_I2C_SW_Delay = 1;
31 else
32 u32_I2C_SW_Delay =500000/u32BusClock; /* Compute proper divider for I2C clock */
33
34 /* Configure P1.4 and P1.5 as open-drain mode */
35 GPIO_SetMode(P1, 0x30, GPIO_PMD_OPEN_DRAIN);
36
37 I2C_SW_SDA = 1;
38 I2C_SW_CLK = 1;
39 return 0;
40 }
41
48 int32_t I2C_SW_Send_byte(uint8_t u8Data)
49 {
50 uint32_t u32count;
51 for(u32count=0; u32count<8; u32count++)
52 {
53 I2C_SW_SDA = u8Data>>(7-u32count);
54 CLK_SysTickDelay(u32_I2C_SW_Delay);
55 I2C_SW_CLK = 1;
56 CLK_SysTickDelay(u32_I2C_SW_Delay);
57 I2C_SW_CLK = 0;
58 }
59 I2C_SW_SDA = 1;
60 CLK_SysTickDelay(u32_I2C_SW_Delay);
61 I2C_SW_CLK = 1;
62 CLK_SysTickDelay(u32_I2C_SW_Delay);
63 u32count = I2C_SW_SDA;
64 I2C_SW_CLK = 0;
65
66 return u32count;
67 }
68
76 int32_t I2C_SW_Send(uint8_t u8Address, uint8_t* p8Data, uint32_t u32ByteSize)
77 {
78 uint32_t u32count = 0;
79 if(u32ByteSize == 0)
80 return 0;
81
82 I2C_SW_SDA = 1;
83 I2C_SW_CLK = 1;
84 CLK_SysTickDelay(u32_I2C_SW_Delay);
85 I2C_SW_SDA = 0;
86 CLK_SysTickDelay(u32_I2C_SW_Delay);
87 I2C_SW_CLK = 0;
88 CLK_SysTickDelay(u32_I2C_SW_Delay);
89
90 if(I2C_SW_Send_byte(u8Address<<1))
91 goto I2C_SW_Stop_Send;
92
93 while(u32count<u32ByteSize)
94 {
95 if(I2C_SW_Send_byte(*(p8Data+u32count++)))
96 goto I2C_SW_Stop_Send;
97 }
98 I2C_SW_Stop_Send:
99 I2C_SW_SDA = 0;
100 CLK_SysTickDelay(u32_I2C_SW_Delay);
101 I2C_SW_CLK = 1;
102 CLK_SysTickDelay(u32_I2C_SW_Delay);
103 I2C_SW_SDA = 1;
104
105 return u32count;
106 }
107
113 uint8_t I2C_SW_Get_byte(uint32_t u32Ack)
114 {
115 uint32_t u32count;
116 uint8_t u8Data=0;
117
118
119 for(u32count=0; u32count<8; u32count++)
120 {
121 CLK_SysTickDelay(u32_I2C_SW_Delay);
122 I2C_SW_CLK = 1;
123 CLK_SysTickDelay(u32_I2C_SW_Delay);
124 u8Data |= I2C_SW_SDA << (7-u32count);
125 I2C_SW_CLK = 0;
126 }
127 I2C_SW_SDA = u32Ack;
128 CLK_SysTickDelay(u32_I2C_SW_Delay);
129 I2C_SW_CLK = 1;
130 u32count = I2C_SW_SDA;
131 CLK_SysTickDelay(u32_I2C_SW_Delay);
132 I2C_SW_CLK = 0;
133 I2C_SW_SDA = 1;
134
135 return u8Data;
136 }
137
145 int32_t I2C_SW_Get(uint8_t u8Address, uint8_t* p8Data, uint32_t u32ByteSize)
146 {
147 uint32_t u32count = 0;
148 if(u32ByteSize == 0)
149 return 0;
150
151 I2C_SW_SDA = 1;
152 I2C_SW_CLK = 1;
153 CLK_SysTickDelay(u32_I2C_SW_Delay);
154 I2C_SW_SDA = 0;
155 CLK_SysTickDelay(u32_I2C_SW_Delay);
156 I2C_SW_CLK = 0;
157 CLK_SysTickDelay(u32_I2C_SW_Delay);
158
159 if(I2C_SW_Send_byte((u8Address<<1)|1))
160 goto I2C_SW_Stop_Get;
161
162 while(u32count<(u32ByteSize-1))
163 {
164 *(p8Data+u32count++) = I2C_SW_Get_byte(0);
165 }
166 *(p8Data+u32count++) = I2C_SW_Get_byte(1);
167 I2C_SW_Stop_Get:
168 I2C_SW_SDA = 0;
169 CLK_SysTickDelay(u32_I2C_SW_Delay);
170 I2C_SW_CLK = 1;
171 CLK_SysTickDelay(u32_I2C_SW_Delay);
172 I2C_SW_SDA = 1;
173
174 return u32count;
175 }
176
177
i2c_software_gpio.h
This is the header file of i2c_software_gpio.c.
u32_I2C_SW_Delay
uint32_t u32_I2C_SW_Delay
Definition: i2c_software_gpio.c:20
I2C_SW_Get
int32_t I2C_SW_Get(uint8_t u8Address, uint8_t *p8Data, uint32_t u32ByteSize)
Read data.
Definition: i2c_software_gpio.c:145
I2C_SW_SDA
#define I2C_SW_SDA
Definition: i2c_software_gpio.c:17
GPIO_SetMode
void GPIO_SetMode(GPIO_T *gpio, uint32_t u32PinMask, uint32_t u32Mode)
Set GPIO operation mode.
Definition: gpio.c:40
CLK_SysTickDelay
void CLK_SysTickDelay(uint32_t us)
This function execute delay function.
Definition: clk.c:336
P1
#define P1
Pointer to GPIO port 1 register structure.
Definition: Mini51Series.h:9940
GPIO_PMD_OPEN_DRAIN
#define GPIO_PMD_OPEN_DRAIN
Definition: gpio.h:39
I2C_SW_Open
int32_t I2C_SW_Open(uint32_t u32BusClock)
Prepare to start software I2C.
Definition: i2c_software_gpio.c:27
I2C_SW_Send
int32_t I2C_SW_Send(uint8_t u8Address, uint8_t *p8Data, uint32_t u32ByteSize)
Send data.
Definition: i2c_software_gpio.c:76
I2C_SW_CLK
#define I2C_SW_CLK
Definition: i2c_software_gpio.c:18
I2C_SW_Send_byte
int32_t I2C_SW_Send_byte(uint8_t u8Data)
Send a byte.
Definition: i2c_software_gpio.c:48
I2C_SW_Get_byte
uint8_t I2C_SW_Get_byte(uint32_t u32Ack)
Read a byte.
Definition: i2c_software_gpio.c:113