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

eeprom_24lc64.c
Go to the documentation of this file.
1 /**************************************************************************/
13 #include <stdio.h>
14 #include "Mini51Series.h"
15 #include "eeprom_24lc64.h"
16
17 #define EEPROM_READ_ADDR 0xA1 /* Address of slave for read */
18 #define EEPROM_WRITE_ADDR 0xA0 /* Address of slave for write */
19
25 void EEPROM_Init(void)
26 {
27 /* Open I2C module and set bus clock */
28 I2C_Open(I2C, 100000);
29 }
30
37 void EEPROM_Write(uint32_t u32Addr, uint8_t u8Data)
38 {
39 int32_t i32Err;
40
41 do
42 {
43 i32Err = 0;
44
45 /* Send start */
46 I2C_START(I2C);
47 I2C_WAIT_READY(I2C);
48
49 /* Send control byte */
50 I2C_SET_DATA(I2C, EEPROM_WRITE_ADDR);
51 I2C_SET_CONTROL_REG(I2C, I2C_SI);
52 I2C_WAIT_READY(I2C);
53
54 if(I2C_GET_STATUS(I2C) == 0x18)
55 {
56 /* ACK */
57
58 /* Send high address */
59 I2C_SET_DATA(I2C, (u32Addr >> 8) & 0xFFUL); // high address
60 I2C_SET_CONTROL_REG(I2C, I2C_SI);
61 I2C_WAIT_READY(I2C);
62 if(I2C_GET_STATUS(I2C) == 0x28)
63 {
64 /* ACK */
65
66 /* Send low address */
67 I2C_SET_DATA(I2C, u32Addr & 0xFFUL); // low address
68 I2C_SET_CONTROL_REG(I2C, I2C_SI);
69 I2C_WAIT_READY(I2C);
70 if(I2C_GET_STATUS(I2C) == 0x28)
71 {
72 /* ACK */
73
74 /* Send data */
75 I2C_SET_DATA(I2C, u8Data); // data
76 I2C_SET_CONTROL_REG(I2C, I2C_SI);
77 I2C_WAIT_READY(I2C);
78 if(I2C_GET_STATUS(I2C) == 0x28)
79 {
80 /* ACK */
81
82 /* Send stop */
83 I2C_SET_CONTROL_REG(I2C, I2C_STO | I2C_SI);
84
85 }
86 else
87 {
88 /* NACK */
89
90 /* Send data error */
91 i32Err = 4;
92 }
93 }
94 else
95 {
96 /* NACK */
97
98 /* Send low address error */
99 i32Err = 3;
100 }
101 }
102 else
103 {
104 /* NACK */
105
106 /* Send high address error */
107 i32Err = 2;
108 }
109 }
110 else
111 {
112 /* NACK */
113
114 /* Send control error */
115 i32Err = 1;
116 }
117
118 if(i32Err)
119 {
120
121 /* Send stop */
122 I2C_SET_CONTROL_REG(I2C, I2C_STO | I2C_SI);
123
124 CLK_SysTickDelay(100);
125 }
126
127 }
128 while(i32Err);
129
130 }
131
137 uint8_t EEPROM_Read(uint32_t u32Addr)
138 {
139 int32_t i32Err;
140 uint8_t u8Data;
141
142 u8Data = 0;
143 do
144 {
145 i32Err = 0;
146
147 /* Send start */
148 I2C_START(I2C);
149 I2C_WAIT_READY(I2C);
150
151 /* Send control byte */
152 I2C_SET_DATA(I2C, EEPROM_WRITE_ADDR);
153 I2C_SET_CONTROL_REG(I2C, I2C_SI);
154 I2C_WAIT_READY(I2C);
155 if(I2C_GET_STATUS(I2C) == 0x18)
156 {
157 /* ACK */
158
159 /* Send high address */
160 I2C_SET_DATA(I2C, (u32Addr >> 8) & 0xFFUL); // high address
161 I2C_SET_CONTROL_REG(I2C, I2C_SI);
162 I2C_WAIT_READY(I2C);
163 if(I2C_GET_STATUS(I2C) == 0x28)
164 {
165 /* ACK */
166
167 /* Send low address */
168 I2C_SET_DATA(I2C, u32Addr & 0xFFUL); // low address
169 I2C_SET_CONTROL_REG(I2C, I2C_SI);
170 I2C_WAIT_READY(I2C);
171 if(I2C_GET_STATUS(I2C) == 0x28)
172 {
173 /* ACK */
174
175 /* Send data */
176 I2C_SET_CONTROL_REG(I2C, I2C_STA | I2C_SI);
177 I2C_WAIT_READY(I2C);
178 if(I2C_GET_STATUS(I2C) == 0x10)
179 {
180 /* ACK */
181
182 /* Send control byte */
183 I2C_SET_DATA(I2C, EEPROM_READ_ADDR);
184 I2C_SET_CONTROL_REG(I2C, I2C_SI);
185 I2C_WAIT_READY(I2C);
186 if(I2C_GET_STATUS(I2C) == 0x40)
187 {
188 I2C_SET_CONTROL_REG(I2C, I2C_SI);
189 I2C_WAIT_READY(I2C);
190
191 /* Read data */
192 u8Data = I2C_GET_DATA(I2C);
193 if(I2C_GET_STATUS(I2C) == 0x58)
194 {
195 /* NACK */
196 /* Send stop */
197 I2C_SET_CONTROL_REG(I2C, I2C_STO | I2C_SI);
198 }
199 else
200 {
201 /* ACK */
202
203 /* read data error */
204 i32Err = 6;
205 }
206 }
207 else
208 {
209 /* NACK */
210
211 /* Send control read error */
212 i32Err = 5;
213 }
214 }
215 else
216 {
217 /* NACK */
218
219 /* Send start error */
220 i32Err = 4;
221 }
222 }
223 else
224 {
225 /* NACK */
226
227 /* Send low address error */
228 i32Err = 3;
229 }
230 }
231 else
232 {
233 /* NACK */
234
235 /* Send high address error */
236 i32Err = 2;
237 }
238 }
239 else
240 {
241 /* NACK */
242
243 /* Send control write error */
244 i32Err = 1;
245
246 }
247
248 if(i32Err)
249 {
250 /* Send stop */
251 I2C_SET_CONTROL_REG(I2C, I2C_STO | I2C_SI);
252
253 CLK_SysTickDelay(10);
254 }
255
256 }
257 while(i32Err);
258
259 return u8Data;
260 }
261
269 uint8_t EEPROM_SequentialRead(uint32_t u32Addr, uint8_t *pu8Buf, uint32_t u32Size)
270 {
271 int32_t i32Err;
272 int32_t i;
273
274 do
275 {
276 i32Err = 0;
277
278 /* Send start */
279 I2C_START(I2C);
280 I2C_WAIT_READY(I2C);
281
282 /* Send control byte */
283 I2C_SET_DATA(I2C, EEPROM_WRITE_ADDR);
284 I2C_SET_CONTROL_REG(I2C, I2C_SI);
285 I2C_WAIT_READY(I2C);
286 if(I2C_GET_STATUS(I2C) == 0x18)
287 {
288 /* ACK */
289
290 /* Send high address */
291 I2C_SET_DATA(I2C, (u32Addr >> 8) & 0xFFUL); // high address
292 I2C_SET_CONTROL_REG(I2C, I2C_SI);
293 I2C_WAIT_READY(I2C);
294 if(I2C_GET_STATUS(I2C) == 0x28)
295 {
296 /* ACK */
297
298 /* Send low address */
299 I2C_SET_DATA(I2C, u32Addr & 0xFFUL); // low address
300 I2C_SET_CONTROL_REG(I2C, I2C_SI);
301 I2C_WAIT_READY(I2C);
302 if(I2C_GET_STATUS(I2C) == 0x28)
303 {
304 /* ACK */
305
306 /* Send data */
307 I2C_SET_CONTROL_REG(I2C, I2C_STA | I2C_SI);
308 I2C_WAIT_READY(I2C);
309 if(I2C_GET_STATUS(I2C) == 0x10)
310 {
311 /* ACK */
312
313 /* Send control byte */
314 I2C_SET_DATA(I2C, EEPROM_READ_ADDR);
315 I2C_SET_CONTROL_REG(I2C, I2C_SI);
316 I2C_WAIT_READY(I2C);
317 if(I2C_GET_STATUS(I2C) == 0x40)
318 {
319 for(i=0; i<u32Size-1; i++)
320 {
321 I2C_SET_CONTROL_REG(I2C, I2C_SI | I2C_AA);
322 I2C_WAIT_READY(I2C);
323
324 /* Read data */
325 pu8Buf[i] = I2C_GET_DATA(I2C);
326 }
327
328 I2C_SET_CONTROL_REG(I2C, I2C_SI);
329 I2C_WAIT_READY(I2C);
330 pu8Buf[i] = I2C_GET_DATA(I2C);
331
332 /* Send stop */
333 I2C_SET_CONTROL_REG(I2C, I2C_STO | I2C_SI);
334 }
335 else
336 {
337 /* NACK */
338
339 /* Send control read error */
340 i32Err = 5;
341 }
342 }
343 else
344 {
345 /* NACK */
346
347 /* Send start error */
348 i32Err = 4;
349 }
350 }
351 else
352 {
353 /* NACK */
354
355 /* Send low address error */
356 i32Err = 3;
357 }
358 }
359 else
360 {
361 /* NACK */
362
363 /* Send high address error */
364 i32Err = 2;
365 }
366 }
367 else
368 {
369 /* NACK */
370
371 /* Send control write error */
372 i32Err = 1;
373
374 }
375
376 if(i32Err)
377 {
378 /* Send stop */
379 I2C_SET_CONTROL_REG(I2C, I2C_STO | I2C_SI);
380
381 CLK_SysTickDelay(100);
382 }
383
384 }
385 while(i32Err);
386
387 return u32Size;
388 }
389
396 void EEPROM_PageWrite(uint32_t u32Addr, uint8_t *pu8Buf)
397 {
398 int32_t i32Err;
399 int32_t i;
400
401 do
402 {
403 i32Err = 0;
404
405 /* Send start */
406 I2C_START(I2C);
407 I2C_WAIT_READY(I2C);
408
409 /* Send control byte */
410 I2C_SET_DATA(I2C, EEPROM_WRITE_ADDR);
411 I2C_SET_CONTROL_REG(I2C, I2C_SI);
412 I2C_WAIT_READY(I2C);
413 if(I2C_GET_STATUS(I2C) == 0x18)
414 {
415 /* ACK */
416
417 /* Send high address */
418 I2C_SET_DATA(I2C, (u32Addr >> 8) & 0xFFUL); // high address
419 I2C_SET_CONTROL_REG(I2C, I2C_SI);
420 I2C_WAIT_READY(I2C);
421 if(I2C_GET_STATUS(I2C) == 0x28)
422 {
423 /* ACK */
424
425 /* Send low address */
426 I2C_SET_DATA(I2C, u32Addr & 0xFFUL); // low address
427 I2C_SET_CONTROL_REG(I2C, I2C_SI);
428 I2C_WAIT_READY(I2C);
429 if(I2C_GET_STATUS(I2C) == 0x28)
430 {
431 /* ACK */
432
433 for(i=0; i<32; i++)
434 {
435 /* Send data */
436 I2C_SET_DATA(I2C, pu8Buf[i]); // data
437 I2C_SET_CONTROL_REG(I2C, I2C_SI);
438 I2C_WAIT_READY(I2C);
439 if(I2C_GET_STATUS(I2C) == 0x30)
440 {
441 /* NACK */
442
443 /* Send data error */
444 i32Err = 4;
445 }
446 }
447
448 /* Send stop when no any error */
449 if(i32Err == 0)
450 {
451 /* Send stop */
452 I2C_SET_CONTROL_REG(I2C, I2C_STO | I2C_SI);
453 }
454 }
455 else
456 {
457 /* NACK */
458
459 /* Send low address error */
460 i32Err = 3;
461 }
462 }
463 else
464 {
465 /* NACK */
466
467 /* Send high address error */
468 i32Err = 2;
469 }
470 }
471 else
472 {
473 /* NACK */
474
475 /* Send control error */
476 i32Err = 1;
477 }
478
479 if(i32Err)
480 {
481 /* Send stop */
482 I2C_SET_CONTROL_REG(I2C, I2C_STO | I2C_SI);
483
484 CLK_SysTickDelay(100);
485 }
486
487 }
488 while(i32Err);
489
490 }
491
EEPROM_Init
void EEPROM_Init(void)
Open I2C interface to access EEPROM.
Definition: eeprom_24lc64.c:25
I2C_WAIT_READY
#define I2C_WAIT_READY(i2c)
This macro will return when I2C module is ready.
Definition: i2c.h:77
I2C_GET_STATUS
#define I2C_GET_STATUS(i2c)
This macro returns the status of I2C module.
Definition: i2c.h:155
EEPROM_Read
uint8_t EEPROM_Read(uint32_t u32Addr)
Read data from EEPROM.
Definition: eeprom_24lc64.c:137
I2C
#define I2C
Pointer to I2C register structure.
Definition: Mini51Series.h:9926
EEPROM_READ_ADDR
#define EEPROM_READ_ADDR
Definition: eeprom_24lc64.c:17
I2C_AA
#define I2C_AA
Definition: i2c.h:37
Mini51Series.h
Mini51 series peripheral access layer header file. This file contains all the peripheral register's d...
I2C_STO
#define I2C_STO
Definition: i2c.h:35
EEPROM_SequentialRead
uint8_t EEPROM_SequentialRead(uint32_t u32Addr, uint8_t *pu8Buf, uint32_t u32Size)
Read data from EEPROM using sequential read method.
Definition: eeprom_24lc64.c:269
EEPROM_WRITE_ADDR
#define EEPROM_WRITE_ADDR
Definition: eeprom_24lc64.c:18
EEPROM_Write
void EEPROM_Write(uint32_t u32Addr, uint8_t u8Data)
Write data to EEPROM.
Definition: eeprom_24lc64.c:37
EEPROM_PageWrite
void EEPROM_PageWrite(uint32_t u32Addr, uint8_t *pu8Buf)
Write page data to EEPROM.
Definition: eeprom_24lc64.c:396
I2C_SET_CONTROL_REG
#define I2C_SET_CONTROL_REG(i2c, u8Ctrl)
This macro sets the I2C control register at one time.
Definition: i2c.h:52
eeprom_24lc64.h
MINI51 series 24LC64 EEPROM library header file.
I2C_SI
#define I2C_SI
Definition: i2c.h:36
I2C_GET_DATA
#define I2C_GET_DATA(i2c)
This macro returns the data stored in data register of I2C module.
Definition: i2c.h:140
I2C_Open
uint32_t I2C_Open(I2C_T *i2c, uint32_t u32BusClock)
This function make I2C module be ready and set the wanted bus clock.
Definition: i2c.c:33
CLK_SysTickDelay
void CLK_SysTickDelay(uint32_t us)
This function execute delay function.
Definition: clk.c:336
I2C_START
#define I2C_START(i2c)
This macro only set START bit to the control register of I2C module.
Definition: i2c.h:59
I2C_STA
#define I2C_STA
Definition: i2c.h:34
I2C_SET_DATA
#define I2C_SET_DATA(i2c, u8Data)
This macro writes the data to data register of I2C module.
Definition: i2c.h:148