MINI51DE_BSP V3.02.004
The Board Support Package for Mini51DE Series MCU
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
21
27int32_t I2C_SW_Open(uint32_t u32BusClock)
28{
29 if(u32BusClock>500000)
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 */
36
37 I2C_SW_SDA = 1;
38 I2C_SW_CLK = 1;
39 return 0;
40}
41
48int32_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);
55 I2C_SW_CLK = 1;
57 I2C_SW_CLK = 0;
58 }
59 I2C_SW_SDA = 1;
61 I2C_SW_CLK = 1;
63 u32count = I2C_SW_SDA;
64 I2C_SW_CLK = 0;
65
66 return u32count;
67}
68
76int32_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;
85 I2C_SW_SDA = 0;
87 I2C_SW_CLK = 0;
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 }
98I2C_SW_Stop_Send:
99 I2C_SW_SDA = 0;
101 I2C_SW_CLK = 1;
103 I2C_SW_SDA = 1;
104
105 return u32count;
106}
107
113uint8_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 {
122 I2C_SW_CLK = 1;
124 u8Data |= I2C_SW_SDA << (7-u32count);
125 I2C_SW_CLK = 0;
126 }
127 I2C_SW_SDA = u32Ack;
129 I2C_SW_CLK = 1;
130 u32count = I2C_SW_SDA;
132 I2C_SW_CLK = 0;
133 I2C_SW_SDA = 1;
134
135 return u8Data;
136}
137
145int32_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;
154 I2C_SW_SDA = 0;
156 I2C_SW_CLK = 0;
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);
167I2C_SW_Stop_Get:
168 I2C_SW_SDA = 0;
170 I2C_SW_CLK = 1;
172 I2C_SW_SDA = 1;
173
174 return u32count;
175}
176
177
void CLK_SysTickDelay(uint32_t us)
This function execute delay function.
Definition: clk.c:336
#define GPIO_PMD_OPEN_DRAIN
Definition: gpio.h:39
void GPIO_SetMode(GPIO_T *gpio, uint32_t u32PinMask, uint32_t u32Mode)
Set GPIO operation mode.
Definition: gpio.c:40
#define P1
Pointer to GPIO port 1 register structure.
#define I2C_SW_CLK
int32_t I2C_SW_Open(uint32_t u32BusClock)
Prepare to start software I2C.
int32_t I2C_SW_Send(uint8_t u8Address, uint8_t *p8Data, uint32_t u32ByteSize)
Send data.
uint32_t u32_I2C_SW_Delay
#define I2C_SW_SDA
int32_t I2C_SW_Get(uint8_t u8Address, uint8_t *p8Data, uint32_t u32ByteSize)
Read data.
uint8_t I2C_SW_Get_byte(uint32_t u32Ack)
Read a byte.
int32_t I2C_SW_Send_byte(uint8_t u8Data)
Send a byte.
This is the header file of i2c_software_gpio.c.