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