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