Use recv instead get.
[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
252         if (i2c_recv(buf, size))
253         {
254                 fd->fd.seek_pos += size;
255                 rd_len += size;
256         }
257
258         i2c_stop();
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 }