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