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