BLE5.2 1.0
开发文档说明
xc_drv_sw_i2c.c
浏览该文件的文档.
1
25/*-----------------------------------------------------------------------------------
26 INCLUDE HEADE FILES
27 ------------------------------------------------------------------------------------*/
28#include "xc_drv_sw_i2c.h"
29
30/*------------------------------------------------------------------------------------
31 Macros
32 -------------------------------------------------------------------------------------*/
33#define AT24C01 128
34#define AT24C02 256
35#define AT24C04 512
36#define AT24C08 1024
37#define AT24C16 2048
38#define AT24C32 4096
39#define AT24C64 8192
40#define AT24C128 16384
41#define AT24C256 32768
42
43#define EE_PAGE_ADDR_UNIT 256
44#define EE_PAGE_BYTE 8
45#define EE_SIZE AT24C08
46#define I2C_SLAVE_ADDR (0x50)
47
48#define AT24XX_SR1_ADDR (0x50)
49/*------------------------------------------------------------------------------------
50 Local Variables
51 -------------------------------------------------------------------------------------*/
52
53/*------------------------------------------------------------------------------------
54 Global Variables
55 -------------------------------------------------------------------------------------*/
56
57/*------------------------------------------------------------------------------------
58 Func Prototype
59 -------------------------------------------------------------------------------------*/
60
61/*------------------------------------------------------------------------------------
62 Functions
63-------------------------------------------------------------------------------------*/
64
66{
67 GPIO_InitCfg_t gpio_cfg = {0};
68
69 gpio_cfg.FunSel = GPIO_Dx;
70 gpio_cfg.Dir = GPIO_DIR_OUTPUT;
71 gpio_cfg.Pull = GPIO_PULLDOWN;
72 gpio_cfg.Int = NOT_INT;
73
74 gpio_cfg.Pin = SCL_PIN;
75 xc_gpio_init(&gpio_cfg);
76
77 gpio_cfg.Pin = SDA_PIN;
78 xc_gpio_init(&gpio_cfg);
79}
80
81void i2c_start(void)
82{
83 SDA_OUT_HIGH(SDA_PIN);
84 SCL_OUT_HIGH(SCL_PIN);
85 delay_us(2);
86 SDA_OUT_LOW(SDA_PIN); // START:when CLK is high,DATA change form high to low
87 delay_us(2);
88 SCL_OUT_LOW(SCL_PIN);
89}
90
91void i2c_stop(void)
92{
93 SCL_OUT_LOW(SCL_PIN);
94 SDA_OUT_LOW(SDA_PIN); // STOP:when CLK is high DATA change form low to high
95 delay_us(2);
96
97 SCL_OUT_HIGH(SCL_PIN);
98 delay_us(2);
99 SDA_OUT_HIGH(SDA_PIN);
100 delay_us(2);
101}
102
103uint8_t i2c_wait_ack(void)
104{
105 uint8_t ucErrTime = 0;
106
107 SDA_INPUT(SDA_PIN, GPIO_PULLUP);
108 delay_us(7);
109 SCL_OUT_HIGH(SCL_PIN);
110 delay_us(7);
111
112 while (SDA_INPUT_VAL(SDA_PIN)) {
113 ucErrTime++;
114 if (ucErrTime > 250) {
115 i2c_stop();
116 return 1;
117 }
118 }
119 SCL_OUT_LOW(SCL_PIN);
120 delay_us(2);
121 return 0;
122}
123
124void i2c_ack(void)
125{
126 SCL_OUT_LOW(SCL_PIN);
127 SDA_OUT_LOW(SDA_PIN);
128 delay_us(2);
129 SCL_OUT_HIGH(SCL_PIN);
130 delay_us(2);
131 SCL_OUT_LOW(SCL_PIN);
132}
133
134void i2c_nack(void)
135{
136 SCL_OUT_LOW(SCL_PIN);
137 SDA_OUT_HIGH(SDA_PIN);
138 delay_us(2);
139 SCL_OUT_HIGH(SCL_PIN);
140 delay_us(2);
141 SCL_OUT_LOW(SCL_PIN);
142}
143
144void i2c_send_byte(uint8_t txd)
145{
146 uint8_t t;
147 SCL_OUT_LOW(SCL_PIN);
148 for (t = 0; t < 8; t++) {
149 if ((txd & 0x80) >> 7) {
150 SDA_OUT_HIGH(SDA_PIN);
151 } else {
152 SDA_OUT_LOW(SDA_PIN);
153 }
154 txd <<= 1;
155 delay_us(2);
156 SCL_OUT_HIGH(SCL_PIN);
157 delay_us(2);
158 SCL_OUT_LOW(SCL_PIN);
159 delay_us(2);
160 }
161}
162
163uint8_t i2c_read_byte(unsigned char ack)
164{
165 unsigned char i, receive = 0;
166 SDA_INPUT(SDA_PIN, GPIO_PULLUP);
167 for (i = 0; i < 8; i++) {
168 SCL_OUT_LOW(SCL_PIN);
169 delay_us(5);
170 SCL_OUT_HIGH(SCL_PIN);
171 receive <<= 1;
172 if (SDA_INPUT_VAL(SDA_PIN)) {
173 receive++;
174 }
175 delay_us(5);
176 }
177 if (!ack)
178 i2c_nack();
179 else
180 i2c_ack();
181 return receive;
182}
183
184uint8_t at24cxx_read_byte(uint16_t addr)
185{
186 uint8_t temp = 0;
187 i2c_start();
188
189 if (EE_SIZE > AT24C16) {
190 i2c_send_byte(0XA0);
191 i2c_wait_ack();
192 i2c_send_byte(addr >> 8);
193 i2c_wait_ack();
194 } else
195 i2c_send_byte(0XA0 + ((addr / 256) << 1));
196
197 i2c_wait_ack();
198 i2c_send_byte(addr % 256);
199 i2c_wait_ack();
200 i2c_start();
201 i2c_send_byte(0XA0 + ((addr / 256) << 1) + 1);
202 i2c_wait_ack();
203 temp = i2c_read_byte(0);
204 i2c_stop();
205 return temp;
206}
207
208void at24cxx_write_byte(uint16_t addr, uint8_t data)
209{
210 i2c_start();
211 if (EE_SIZE > AT24C16) {
212 i2c_send_byte(0XA0);
213 i2c_wait_ack();
214 i2c_send_byte(addr >> 8);
215 } else {
216 i2c_send_byte(0XA0 + ((addr / 256) << 1));
217 }
218 i2c_wait_ack();
219 i2c_send_byte(addr % 256);
220 i2c_wait_ack();
221 i2c_send_byte(data);
222 i2c_wait_ack();
223 i2c_stop();
224 delay_ms(10);
225}
226
227void at24cxx_write_nbytes(uint16_t addr, uint8_t *buff, uint32_t num)
228{
229 while (num--) {
230 at24cxx_write_byte(addr, *buff);
231 addr++;
232 buff++;
233 }
234}
235
236void at24cxx_read_nbytes(uint16_t addr, uint8_t *buff, uint32_t num)
237{
238 while (num) {
239 *buff++ = at24cxx_read_byte(addr++);
240 num--;
241 }
242}
GPIO_DIR_TypeDef Dir
GPIO_FUN_SEL_TypeDef FunSel
GPIO_PULL_TypeDef Pull
GPIO_INT_TypeDef Int
void xc_gpio_init(GPIO_InitCfg_t *init_cfg)
xc_gpio_init
Definition xc_drv_gpio.c:63
@ GPIO_DIR_OUTPUT
@ GPIO_Dx
@ GPIO_PULLDOWN
@ GPIO_PULLUP
@ NOT_INT
void i2c_send_byte(uint8_t txd)
uint8_t i2c_read_byte(unsigned char ack)
void at24cxx_read_nbytes(uint16_t addr, uint8_t *buff, uint32_t num)
void i2c_ack(void)
void at24cxx_write_byte(uint16_t addr, uint8_t data)
uint8_t at24cxx_read_byte(uint16_t addr)
void at24cxx_write_nbytes(uint16_t addr, uint8_t *buff, uint32_t num)
uint8_t i2c_wait_ack(void)
void i2c_stop(void)
void i2c_software_init(void)
void i2c_nack(void)
void i2c_start(void)
void delay_ms(uint32_t nms)
void delay_us(uint32_t nus)