Sistema l'errore da me commesso in fase di conversione...
[bertos.git] / drv / eeprom.c
1 /**
2  * \file
3  * <!--
4  * Copyright 2003, 2004, 2005 Develer S.r.l. (http://www.develer.com/)
5  * This file is part of DevLib - See README.devlib for information.
6  * -->
7  *
8  * \brief Driver for the 24xx16 and 24xx256 I2C EEPROMS (implementation)
9  *
10  * \note This implementation is AVR specific.
11  *
12  * \version $Id$
13  * \author Stefano Fedrigo <aleph@develer.com>
14  * \author Bernardo Innocenti <bernie@develer.com>
15  */
16
17 /*#*
18  *#* $Log$
19  *#* Revision 1.20  2006/07/19 12:56:25  bernie
20  *#* Convert to new Doxygen style.
21  *#*
22  *#* Revision 1.19  2006/03/20 17:49:50  bernie
23  *#* Make the TWI driver more generic to work with devices other than EEPROMS.
24  *#*
25  *#* Revision 1.18  2005/11/27 23:33:40  bernie
26  *#* Use appconfig.h instead of cfg/config.h.
27  *#*
28  *#* Revision 1.17  2005/04/11 19:10:27  bernie
29  *#* Include top-level headers from cfg/ subdir.
30  *#*
31  *#* Revision 1.16  2005/03/01 23:25:09  bernie
32  *#* Prune CVS log.
33  *#*
34  *#* Revision 1.11  2004/10/26 08:35:31  bernie
35  *#* Reset watchdog for long operations.
36  *#*
37  *#* Revision 1.10  2004/09/20 03:31:22  bernie
38  *#* Sanitize for C++.
39  *#*
40  *#* Revision 1.9  2004/09/14 21:03:46  bernie
41  *#* Use debug.h instead of kdebug.h.
42  *#*/
43
44 #include "eeprom.h"
45
46 #include <cfg/debug.h>
47 #include <appconfig.h>  // CONFIG_EEPROM_VERIFY
48 #include <cfg/macros.h>  // MIN()
49 #include <drv/twi.h>
50 #include <drv/wdt.h>
51 #include <mware/byteorder.h> // cpu_to_be16()
52
53 #include <string.h>  // memset()
54
55
56 // Configuration sanity checks
57 #if !defined(CONFIG_EEPROM_VERIFY) || (CONFIG_EEPROM_VERIFY != 0 && CONFIG_EEPROM_VERIFY != 1)
58         #error CONFIG_EEPROM_VERIFY must be defined to either 0 or 1
59 #endif
60
61 /**
62  * EEPROM ID code
63  */
64 #define EEPROM_ID  0xA0
65
66 /**
67  * This macros form the correct slave address for EEPROMs
68  */
69 #define EEPROM_ADDR(x) (EEPROM_ID | (((uint8_t)(x)) << 1))
70
71
72
73
74 /**
75  * Copy \c count bytes from buffer \c buf to
76  * eeprom at address \c addr.
77  */
78 static bool eeprom_writeRaw(e2addr_t addr, const void *buf, size_t count)
79 {
80         bool result = true;
81         ASSERT(addr + count <= EEPROM_SIZE);
82
83         while (count && result)
84         {
85                 /*
86                  * Split write in multiple sequential mode operations that
87                  * don't cross page boundaries.
88                  */
89                 size_t size =
90                         MIN(count, (size_t)(EEPROM_BLKSIZE - (addr & (EEPROM_BLKSIZE - 1))));
91
92         #if CONFIG_EEPROM_TYPE == EEPROM_24XX16
93                 /*
94                  * The 24LC16 uses the slave address as a 3-bit
95                  * block address.
96                  */
97                 uint8_t blk_addr = (uint8_t)((addr >> 8) & 0x07);
98                 uint8_t blk_offs = (uint8_t)addr;
99
100                 result =
101                         twi_start_w(EEPROM_ADDR(blk_addr))
102                         && twi_send(&blk_offs, sizeof blk_offs)
103                         && twi_send(buf, size);
104
105         #elif CONFIG_EEPROM_TYPE == EEPROM_24XX256
106
107                 // 24LC256 wants big-endian addresses
108                 uint16_t addr_be = cpu_to_be16(addr);
109
110                 result =
111                         twi_start_w(EEPROM_ID)
112                         && twi_send((uint8_t *)&addr_be, sizeof addr_be)
113                         && twi_send(buf, size);
114
115         #else
116                 #error Unknown device type
117         #endif
118
119                 twi_stop();
120
121                 // DEBUG
122                 //kprintf("addr=%d, count=%d, size=%d, *#?=%d\n",
123                 //      addr, count, size,
124                 //      (EEPROM_BLKSIZE - (addr & (EEPROM_BLKSIZE - 1)))
125                 //);
126
127                 /* Update count and addr for next operation */
128                 count -= size;
129                 addr += size;
130                 buf = ((const char *)buf) + size;
131         }
132
133         if (!result)
134                 TRACEMSG("Write error!");
135         return result;
136 }
137
138
139 #if CONFIG_EEPROM_VERIFY
140 /**
141  * Check that the contents of an EEPROM range
142  * match with a provided data buffer.
143  *
144  * \return true on success.
145  */
146 static bool eeprom_verify(e2addr_t addr, const void *buf, size_t count)
147 {
148         uint8_t verify_buf[16];
149         bool result = true;
150
151         while (count && result)
152         {
153                 /* Split read in smaller pieces */
154                 size_t size = MIN(count, sizeof verify_buf);
155
156                 /* Read back buffer */
157                 if (eeprom_read(addr, verify_buf, size))
158                 {
159                         if (memcmp(buf, verify_buf, size) != 0)
160                         {
161                                 TRACEMSG("Data mismatch!");
162                                 result = false;
163                         }
164                 }
165                 else
166                 {
167                         TRACEMSG("Read error!");
168                         result = false;
169                 }
170
171                 /* Update count and addr for next operation */
172                 count -= size;
173                 addr += size;
174                 buf = ((const char *)buf) + size;
175         }
176
177         return result;
178 }
179 #endif /* CONFIG_EEPROM_VERIFY */
180
181
182 bool eeprom_write(e2addr_t addr, const void *buf, size_t count)
183 {
184 #if CONFIG_EEPROM_VERIFY
185         int retries = 5;
186
187         while (retries--)
188                 if (eeprom_writeRaw(addr, buf, count)
189                                 && eeprom_verify(addr, buf, count))
190                         return true;
191
192         return false;
193
194 #else /* !CONFIG_EEPROM_VERIFY */
195         return eeprom_writeRaw(addr, buf, count);
196 #endif /* !CONFIG_EEPROM_VERIFY */
197 }
198
199
200 /**
201  * Copy \c count bytes at address \c addr
202  * from eeprom to RAM to buffer \c buf.
203  *
204  * \return true on success.
205  */
206 bool eeprom_read(e2addr_t addr, void *buf, size_t count)
207 {
208         ASSERT(addr + count <= EEPROM_SIZE);
209
210 #if CONFIG_EEPROM_TYPE == EEPROM_24XX16
211         /*
212          * The 24LC16 uses the slave address as a 3-bit
213          * block address.
214          */
215         uint8_t blk_addr = (uint8_t)((addr >> 8) & 0x07);
216         uint8_t blk_offs = (uint8_t)addr;
217
218         bool res =
219                 twi_start_w(EEPROM_ADDR(blk_addr))
220                 && twi_send(&blk_offs, sizeof blk_offs)
221                 && twi_start_r(EEPROM_ADDR(blk_addr))
222                 && twi_recv(buf, count);
223
224 #elif CONFIG_EEPROM_TYPE == EEPROM_24XX256
225
226         // 24LC256 wants big-endian addresses
227         addr = cpu_to_be16(addr);
228
229         bool res =
230                 twi_start_w(EEPROM_ID)
231                 && twi_send((uint8_t *)&addr, sizeof(addr))
232                 && twi_start_r(EEPROM_ID)
233                 && twi_recv(buf, count);
234 #else
235         #error Unknown device type
236 #endif
237
238         twi_stop();
239
240         if (!res)
241                 TRACEMSG("Read error!");
242         return res;
243 }
244
245
246 /**
247  * Write a single character \a c at address \a addr.
248  */
249 bool eeprom_write_char(e2addr_t addr, char c)
250 {
251         return eeprom_write(addr, &c, 1);
252 }
253
254
255 /**
256  * Read a single character at address \a addr.
257  *
258  * \return the requested character or -1 in case of failure.
259  */
260 int eeprom_read_char(e2addr_t addr)
261 {
262         char c;
263
264         if (eeprom_read(addr, &c, 1))
265                 return c;
266         else
267                 return -1;
268 }
269
270
271 /**
272  * Erase specified part of eeprom, writing 0xFF.
273  *
274  * \param addr   starting address
275  * \param count  length of block to erase
276  */
277 void eeprom_erase(e2addr_t addr, size_t count)
278 {
279         uint8_t buf[EEPROM_BLKSIZE];
280         memset(buf, 0xFF, sizeof buf);
281
282         // Clear all but struct hw_info at start of eeprom
283         while (count)
284         {
285                 // Long operation, reset watchdog
286                 wdt_reset();
287
288                 size_t size = MIN(count, sizeof buf);
289                 eeprom_write(addr, buf, size);
290                 addr += size;
291                 count -= size;
292         }
293 }
294
295
296 /**
297  * Initialize TWI module.
298  */
299 void eeprom_init(void)
300 {
301         twi_init();
302 }
303
304
305 #ifdef _DEBUG
306
307 #include <string.h>
308
309 void eeprom_test(void)
310 {
311         static const char magic[14] = "Humpty Dumpty";
312         char buf[sizeof magic];
313         size_t i;
314
315         // Write something to EEPROM using unaligned sequential writes
316         for (i = 0; i < 42; ++i)
317         {
318                 wdt_reset();
319                 eeprom_write(i * sizeof magic, magic, sizeof magic);
320         }
321
322         // Read back with single-byte reads
323         for (i = 0; i < 42 * sizeof magic; ++i)
324         {
325                 wdt_reset();
326                 eeprom_read(i, buf, 1);
327                 kprintf("EEPROM byte read: %c (%d)\n", buf[0], buf[0]);
328                 ASSERT(buf[0] == magic[i % sizeof magic]);
329         }
330
331         // Read back again using sequential reads
332         for (i = 0; i < 42; ++i)
333         {
334                 wdt_reset();
335                 memset(buf, 0, sizeof buf);
336                 eeprom_read(i * sizeof magic, buf, sizeof magic);
337                 kprintf("EEPROM seq read @ 0x%x: '%s'\n", i * sizeof magic, buf);
338                 ASSERT(memcmp(buf, magic, sizeof magic) == 0);
339         }
340 }
341
342 #endif // _DEBUG