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