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