MINI51DE_BSP V3.02.004
The Board Support Package for Mini51DE Series MCU
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
25void EEPROM_Init(void)
26{
27 /* Open I2C module and set bus clock */
28 I2C_Open(I2C, 100000);
29}
30
37void EEPROM_Write(uint32_t u32Addr, uint8_t u8Data)
38{
39 int32_t i32Err;
40
41 do
42 {
43 i32Err = 0;
44
45 /* Send start */
48
49 /* Send control byte */
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
62 if(I2C_GET_STATUS(I2C) == 0x28)
63 {
64 /* ACK */
65
66 /* Send low address */
67 I2C_SET_DATA(I2C, u32Addr & 0xFFUL); // low address
70 if(I2C_GET_STATUS(I2C) == 0x28)
71 {
72 /* ACK */
73
74 /* Send data */
75 I2C_SET_DATA(I2C, u8Data); // data
78 if(I2C_GET_STATUS(I2C) == 0x28)
79 {
80 /* ACK */
81
82 /* Send stop */
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 */
123
124 CLK_SysTickDelay(100);
125 }
126
127 }
128 while(i32Err);
129
130}
131
137uint8_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);
150
151 /* Send control byte */
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
163 if(I2C_GET_STATUS(I2C) == 0x28)
164 {
165 /* ACK */
166
167 /* Send low address */
168 I2C_SET_DATA(I2C, u32Addr & 0xFFUL); // low address
171 if(I2C_GET_STATUS(I2C) == 0x28)
172 {
173 /* ACK */
174
175 /* Send data */
178 if(I2C_GET_STATUS(I2C) == 0x10)
179 {
180 /* ACK */
181
182 /* Send control byte */
186 if(I2C_GET_STATUS(I2C) == 0x40)
187 {
190
191 /* Read data */
192 u8Data = I2C_GET_DATA(I2C);
193 if(I2C_GET_STATUS(I2C) == 0x58)
194 {
195 /* NACK */
196 /* Send stop */
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 */
252
254 }
255
256 }
257 while(i32Err);
258
259 return u8Data;
260}
261
269uint8_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);
281
282 /* Send control byte */
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
294 if(I2C_GET_STATUS(I2C) == 0x28)
295 {
296 /* ACK */
297
298 /* Send low address */
299 I2C_SET_DATA(I2C, u32Addr & 0xFFUL); // low address
302 if(I2C_GET_STATUS(I2C) == 0x28)
303 {
304 /* ACK */
305
306 /* Send data */
309 if(I2C_GET_STATUS(I2C) == 0x10)
310 {
311 /* ACK */
312
313 /* Send control byte */
317 if(I2C_GET_STATUS(I2C) == 0x40)
318 {
319 for(i=0; i<u32Size-1; i++)
320 {
323
324 /* Read data */
325 pu8Buf[i] = I2C_GET_DATA(I2C);
326 }
327
330 pu8Buf[i] = I2C_GET_DATA(I2C);
331
332 /* Send stop */
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 */
380
381 CLK_SysTickDelay(100);
382 }
383
384 }
385 while(i32Err);
386
387 return u32Size;
388}
389
396void 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);
408
409 /* Send control byte */
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
421 if(I2C_GET_STATUS(I2C) == 0x28)
422 {
423 /* ACK */
424
425 /* Send low address */
426 I2C_SET_DATA(I2C, u32Addr & 0xFFUL); // low address
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
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 */
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 */
483
484 CLK_SysTickDelay(100);
485 }
486
487 }
488 while(i32Err);
489
490}
491
Mini51 series peripheral access layer header file. This file contains all the peripheral register's d...
uint8_t EEPROM_Read(uint32_t u32Addr)
Read data from EEPROM.
uint8_t EEPROM_SequentialRead(uint32_t u32Addr, uint8_t *pu8Buf, uint32_t u32Size)
Read data from EEPROM using sequential read method.
#define EEPROM_WRITE_ADDR
Definition: eeprom_24lc64.c:18
#define EEPROM_READ_ADDR
Definition: eeprom_24lc64.c:17
void EEPROM_Init(void)
Open I2C interface to access EEPROM.
Definition: eeprom_24lc64.c:25
void EEPROM_Write(uint32_t u32Addr, uint8_t u8Data)
Write data to EEPROM.
Definition: eeprom_24lc64.c:37
void EEPROM_PageWrite(uint32_t u32Addr, uint8_t *pu8Buf)
Write page data to EEPROM.
MINI51 series 24LC64 EEPROM library header file.
void CLK_SysTickDelay(uint32_t us)
This function execute delay function.
Definition: clk.c:336
#define I2C_SI
Definition: i2c.h:36
#define I2C_AA
Definition: i2c.h:37
#define I2C_STO
Definition: i2c.h:35
#define I2C_STA
Definition: i2c.h:34
#define I2C_SET_CONTROL_REG(i2c, u8Ctrl)
This macro sets the I2C control register at one time.
Definition: i2c.h:52
#define I2C_GET_DATA(i2c)
This macro returns the data stored in data register of I2C module.
Definition: i2c.h:156
#define I2C_GET_STATUS(i2c)
This macro returns the status of I2C module.
Definition: i2c.h:171
__STATIC_INLINE int32_t I2C_WAIT_READY(I2C_T *i2c)
This macro will return when I2C module is ready.
Definition: i2c.h:85
#define I2C_SET_DATA(i2c, u8Data)
This macro writes the data to data register of I2C module.
Definition: i2c.h:164
#define I2C_START(i2c)
This macro only set START bit to the control register of I2C module.
Definition: i2c.h:59
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
#define I2C
Pointer to I2C register structure.