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