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