Fix warning.
[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 #if !CPU_ARM
45
46 #include <cfg/macros.h>  // MIN()
47 #include <cfg/debug.h>
48 #include <cfg/module.h>  // MOD_CHECK()
49
50 #include <cpu/attr.h>
51 #include <drv/i2c.h>
52
53 #include <drv/wdt.h>
54
55 #include <cpu/byteorder.h> // cpu_to_be16()
56
57 #include <string.h>  // memset()
58
59 /**
60  * EEPROM ID code
61  */
62 #define EEPROM_ID  0xA0
63
64 /**
65  * This macros form the correct slave address for EEPROMs
66  */
67 #define EEPROM_ADDR(x) (EEPROM_ID | (((uint8_t)((x) & 0x07)) << 1))
68
69
70 /**
71  * Array used to describe EEPROM memory devices currently supported.
72  */
73 static const EepromInfo mem_info[] =
74 {
75         {
76                 /* 24XX16 */
77                 .has_dev_addr = false,
78                 .blk_size = 0x10,
79                 .e2_size = 0x800,
80         },
81         {
82                 /* 24XX256 */
83                 .has_dev_addr = true,
84                 .blk_size = 0x40,
85                 .e2_size = 0x8000,
86         },
87         {
88                 /* 24XX512 */
89                 .has_dev_addr = true,
90                 .blk_size = 0x80,
91                 .e2_size = 0x10000,
92         },
93         /* Add other memories here */
94 };
95
96 STATIC_ASSERT(countof(mem_info) == EEPROM_CNT);
97
98
99 /**
100  * Copy \a size bytes from buffer \a buf to
101  * eeprom.
102  */
103 static size_t eeprom_writeRaw(struct KFile *_fd, const void *buf, size_t size)
104 {
105         Eeprom *fd = EEPROM_CAST(_fd);
106         e2dev_addr_t dev_addr;
107         uint8_t addr_buf[2];
108         uint8_t addr_len;
109         size_t wr_len = 0;
110
111         e2blk_size_t blk_size = mem_info[fd->type].blk_size;
112
113         STATIC_ASSERT(countof(addr_buf) <= sizeof(e2addr_t));
114
115         /* clamp size to memory limit (otherwise may roll back) */
116         ASSERT(_fd->seek_pos + size <= (kfile_off_t)_fd->size);
117         size = MIN((kfile_off_t)size, _fd->size - _fd->seek_pos);
118
119         if (mem_info[fd->type].has_dev_addr)
120         {
121                 dev_addr = fd->addr;
122                 addr_len = 2;
123         }
124         else
125         {
126                 dev_addr = (e2dev_addr_t)((fd->fd.seek_pos >> 8) & 0x07);
127                 addr_len = 1;
128         }
129
130         while (size)
131         {
132                 /*
133                  * Split write in multiple sequential mode operations that
134                  * don't cross page boundaries.
135                  */
136                 size_t count = MIN(size, (size_t)(blk_size - (fd->fd.seek_pos & (blk_size - 1))));
137
138                 if (mem_info[fd->type].has_dev_addr)
139                 {
140                         addr_buf[0] = (fd->fd.seek_pos >> 8) & 0xFF;
141                         addr_buf[1] = (fd->fd.seek_pos & 0xFF);
142                 }
143                 else
144                 {
145                         dev_addr = (e2dev_addr_t)((fd->fd.seek_pos >> 8) & 0x07);
146                         addr_buf[0] = (fd->fd.seek_pos & 0xFF);
147                 }
148
149
150                 if (!(i2c_start_w(EEPROM_ADDR(dev_addr))
151                         && i2c_send(addr_buf, addr_len)
152                         && i2c_send(buf, count)))
153                 {
154                         i2c_stop();
155                         return wr_len;
156                 }
157
158                 i2c_stop();
159
160                 /* Update count and addr for next operation */
161                 size -= count;
162                 fd->fd.seek_pos += count;
163                 buf = ((const char *)buf) + count;
164                 wr_len += count;
165         }
166
167         return wr_len;
168 }
169
170 /**
171  * Copy \a size bytes from buffer \a _buf to
172  * eeprom.
173  * \note Writes are verified and if buffer content
174  *       is not matching we retry 5 times max.
175  */
176 static size_t eeprom_writeVerify(struct KFile *_fd, const void *_buf, size_t size)
177 {
178         Eeprom *fd = EEPROM_CAST(_fd);
179         int retries = 5;
180         size_t wr_len = 0;
181
182         while (retries--)
183         {
184                 wr_len = eeprom_writeRaw(_fd, _buf, size);
185                 /* rewind to verify what we have just written */
186                 kfile_seek(_fd, -(kfile_off_t)wr_len, KSM_SEEK_CUR);
187                 if (wr_len == size
188                  && eeprom_verify(fd, _buf, wr_len))
189                 {
190                         /* Forward to go after what we have written*/
191                         kfile_seek(_fd, wr_len, KSM_SEEK_CUR);
192                         return wr_len;
193                 }
194         }
195         return wr_len;
196 }
197
198
199 /**
200  * Copy \a size bytes
201  * from eeprom to RAM to buffer \a _buf.
202  *
203  * \return the number of bytes read.
204  */
205 static size_t eeprom_read(struct KFile *_fd, void *_buf, size_t size)
206 {
207         Eeprom *fd = EEPROM_CAST(_fd);
208         uint8_t addr_buf[2];
209         uint8_t addr_len;
210         size_t rd_len = 0;
211         uint8_t *buf = (uint8_t *)_buf;
212
213         STATIC_ASSERT(countof(addr_buf) <= sizeof(e2addr_t));
214
215         /* clamp size to memory limit (otherwise may roll back) */
216         ASSERT(_fd->seek_pos + size <= (kfile_off_t)_fd->size);
217         size = MIN((kfile_off_t)size, _fd->size - _fd->seek_pos);
218
219         e2dev_addr_t dev_addr;
220         if (mem_info[fd->type].has_dev_addr)
221         {
222                 dev_addr = fd->addr;
223                 addr_len = 2;
224                 addr_buf[0] = (fd->fd.seek_pos >> 8) & 0xFF;
225                 addr_buf[1] = (fd->fd.seek_pos & 0xFF);
226         }
227         else
228         {
229                 dev_addr = (e2dev_addr_t)((fd->fd.seek_pos >> 8) & 0x07);
230                 addr_len = 1;
231                 addr_buf[0] = (fd->fd.seek_pos & 0xFF);
232         }
233
234
235         if (!(i2c_start_w(EEPROM_ADDR(dev_addr))
236            && i2c_send(addr_buf, addr_len)
237            && i2c_start_r(EEPROM_ADDR(dev_addr))))
238         {
239                 i2c_stop();
240                 return 0;
241         }
242
243         while (size--)
244         {
245                 /*
246                  * The last byte read does not have an ACK
247                  * to stop communication.
248                  */
249                 int c = i2c_get(size);
250
251                 if (c == EOF)
252                         break;
253
254                 *buf++ = c;
255                 fd->fd.seek_pos++;
256                 rd_len++;
257         }
258
259         return rd_len;
260 }
261
262 /**
263  * Check that the contents of an EEPROM range
264  * match with a provided data buffer.
265  *
266  * \return true on success.
267  * \note Seek position of \a fd will not change.
268  */
269 bool eeprom_verify(Eeprom *fd, const void *buf, size_t count)
270 {
271         uint8_t verify_buf[16];
272         bool result = true;
273
274         /* Save seek position */
275         kfile_off_t prev_seek = fd->fd.seek_pos;
276
277         while (count && result)
278         {
279                 /* Split read in smaller pieces */
280                 size_t size = MIN(count, sizeof verify_buf);
281
282                 /* Read back buffer */
283                 if (eeprom_read(&fd->fd, verify_buf, size))
284                 {
285                         if (memcmp(buf, verify_buf, size) != 0)
286                         {
287                                 TRACEMSG("Data mismatch!");
288                                 result = false;
289                         }
290                 }
291                 else
292                 {
293                         TRACEMSG("Read error!");
294                         result = false;
295                 }
296
297                 /* Update count and addr for next operation */
298                 count -= size;
299                 buf = ((const char *)buf) + size;
300         }
301
302         /* Restore previous seek position */
303         fd->fd.seek_pos = prev_seek;
304         return result;
305 }
306
307 /**
308  * Erase specified part of eeprom, writing 0xFF.
309  *
310  * \a addr   starting address
311  * \a count  length of block to erase
312  * \note Seek position is unchanged.
313  * \return true if ok, false otherwise.
314  */
315 bool eeprom_erase(Eeprom *fd, e2addr_t addr, e2_size_t count)
316 {
317         e2blk_size_t blk_size = mem_info[fd->type].blk_size;
318         uint8_t buf[blk_size];
319         kfile_off_t prev_off = fd->fd.seek_pos;
320         bool res = true;
321         size_t size;
322
323         memset(buf, 0xFF, blk_size);
324
325
326         kfile_seek(&fd->fd, addr, KSM_SEEK_SET);
327
328         /*
329          * Optimization: this first write id used to realign
330          * current address to block boundaries.
331          */
332
333         wdt_reset();
334         size = MIN(count, (e2_size_t)(blk_size - (addr & (blk_size - 1))));
335         if (kfile_write(&fd->fd, buf, size) != size)
336         {
337                 fd->fd.seek_pos = prev_off;
338                 return false;
339         }
340         count -= size;
341
342         /* Clear all */
343         while (count)
344         {
345                 /* Long operation, reset watchdog */
346                 wdt_reset();
347
348                 size = MIN(count, (e2_size_t)sizeof buf);
349                 if (kfile_write(&fd->fd, buf, size) != size)
350                 {
351                         res = false;
352                         break;
353                 }
354
355                 count -= size;
356         }
357         fd->fd.seek_pos = prev_off;
358         return res;
359 }
360
361
362 /**
363  * Initialize EEPROM module.
364  * \a fd is the Kfile context.
365  * \a type is the eeprom device we want to initialize (\see EepromType)
366  * \a addr is the i2c devide address (usually pins A0, A1, A2).
367  * \a verify is true if you want that every write operation will be verified.
368  */
369 void eeprom_init(Eeprom *fd, EepromType type, e2dev_addr_t addr, bool verify)
370 {
371         MOD_CHECK(i2c);
372         ASSERT(type < EEPROM_CNT);
373
374         memset(fd, 0, sizeof(*fd));
375         DB(fd->fd._type = KFT_EEPROM);
376
377         fd->type = type;
378         fd->addr = addr;
379         fd->fd.size = mem_info[fd->type].e2_size;
380
381         // Setup eeprom programming functions.
382         fd->fd.read = eeprom_read;
383         if (verify)
384                 fd->fd.write = eeprom_writeVerify;
385         else
386                 fd->fd.write = eeprom_writeRaw;
387         fd->fd.close = kfile_genericClose;
388
389         fd->fd.seek = kfile_genericSeek;
390 }
391
392 #endif