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