doc: Refactor documentation and add OOP basics.
[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, 2010 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/cfg_i2c.h"
42 #include "cfg/cfg_eeprom.h"
43
44 /* Define logging setting (for cfg/log.h module). */
45 #define LOG_LEVEL         EEPROM_LOG_LEVEL
46 #define LOG_FORMAT        EEPROM_LOG_FORMAT
47 #include <cfg/log.h>
48 #include <cfg/debug.h>
49 #include <cfg/macros.h>  // MIN()
50
51 #include <cpu/attr.h>
52
53 #include <drv/i2c.h>
54
55 #include <string.h>  // memset()
56
57 /**
58  * EEPROM ID code
59  */
60 #define EEPROM_ID  0xA0
61
62 /**
63  * This macros form the correct slave address for EEPROMs
64  */
65 #define EEPROM_ADDR(x) (EEPROM_ID | (((uint8_t)((x) & 0x07)) << 1))
66
67
68 /**
69  * Array used to describe EEPROM memory devices currently supported.
70  */
71 static const EepromInfo mem_info[] =
72 {
73         {
74                 /* 24XX08 */
75                 .has_dev_addr = false,
76                 .blk_size = 0x10,
77                 .e2_size = 0x400,
78         },
79         {
80                 /* 24XX16 */
81                 .has_dev_addr = false,
82                 .blk_size = 0x10,
83                 .e2_size = 0x800,
84         },
85         {
86                 /* 24XX256 */
87                 .has_dev_addr = true,
88                 .blk_size = 0x40,
89                 .e2_size = 0x8000,
90         },
91         {
92                 /* 24XX512 */
93                 .has_dev_addr = true,
94                 .blk_size = 0x80,
95                 .e2_size = 0x10000,
96         },
97         {
98                 /* 24XX1024 */
99                 .has_dev_addr = true,
100                 .blk_size = 0x100,
101                 .e2_size = 0x20000,
102         },
103
104         /* Add other memories here */
105 };
106
107 STATIC_ASSERT(countof(mem_info) == EEPROM_CNT);
108
109 #define CHUNCK_SIZE     16
110
111 /**
112  * Erase EEPROM.
113  * \param eep is the Kblock context.
114  * \param addr eeprom address where start to erase
115  * \param size number of byte to erase
116  */
117 bool eeprom_erase(Eeprom *eep, e2addr_t addr, e2_size_t size)
118 {
119         uint8_t tmp[CHUNCK_SIZE] = { [0 ... (CHUNCK_SIZE - 1)] = 0xFF };
120
121         while (size)
122         {
123                 block_idx_t idx = addr / eep->blk.blk_size;
124                 size_t offset = addr % eep->blk.blk_size;
125                 size_t count = MIN(size, (e2_size_t)CHUNCK_SIZE);
126                 size_t ret_len = eep->blk.priv.vt->writeDirect((KBlock *)eep, idx, tmp, offset, count);
127                 size -= ret_len;
128                 addr += ret_len;
129
130                 if (ret_len != count)
131                         return false;
132         }
133         return true;
134 }
135
136 /**
137  * Verify EEPROM.
138  * \param eep is the Kblock context.
139  * \param addr eeprom address where start to verify.
140  * \param buf buffer of data to compare with eeprom data read.
141  * \param size number of byte to verify.
142  */
143 bool eeprom_verify(Eeprom *eep, e2addr_t addr, const void *buf, size_t size)
144 {
145     uint8_t verify_buf[CHUNCK_SIZE];
146         while (size)
147         {
148                 block_idx_t idx = addr / eep->blk.blk_size;
149                 size_t offset = addr % eep->blk.blk_size;
150                 size_t count = MIN(size, (size_t)CHUNCK_SIZE);
151
152                 size_t ret_len = eep->blk.priv.vt->readDirect((KBlock *)eep, idx, verify_buf, offset, count);
153
154                 if (ret_len != count)
155                 {
156                         LOG_ERR("Verify read fail.\n");
157                         return false;
158                 }
159
160                 if (memcmp(buf, verify_buf, ret_len) != 0)
161                 {
162                         LOG_ERR("Data mismatch!\n");
163                         return false;
164                 }
165
166                 size -= ret_len;
167                 addr += ret_len;
168                 buf = ((const char *)buf) + ret_len;
169         }
170         return true;
171 }
172
173
174 static size_t eeprom_write(KBlock *blk, block_idx_t idx, const void *buf, size_t offset, size_t size)
175 {
176         Eeprom *eep = EEPROM_CAST_KBLOCK(blk);
177         e2dev_addr_t dev_addr;
178         uint8_t addr_buf[2];
179         uint8_t addr_len;
180         uint32_t abs_addr = blk->blk_size * idx + offset;
181
182         STATIC_ASSERT(countof(addr_buf) <= sizeof(e2addr_t));
183
184
185         /* clamp size to memory limit (otherwise may roll back) */
186         ASSERT(idx <= blk->blk_cnt);
187         size = MIN(size, blk->blk_size - offset);
188
189         if (mem_info[eep->type].has_dev_addr)
190         {
191                 dev_addr = eep->addr;
192                 addr_len = 2;
193         }
194         else
195         {
196                 dev_addr = (e2dev_addr_t)((abs_addr >> 8) & 0x07);
197                 addr_len = 1;
198         }
199
200         if (mem_info[eep->type].has_dev_addr)
201         {
202                 addr_buf[0] = (abs_addr >> 8) & 0xFF;
203                 addr_buf[1] = (abs_addr & 0xFF);
204         }
205         else
206         {
207                 dev_addr = (e2dev_addr_t)((abs_addr >> 8) & 0x07);
208                 addr_buf[0] = (abs_addr & 0xFF);
209         }
210
211         i2c_start_w(eep->i2c, EEPROM_ADDR(dev_addr),  addr_len + size, I2C_STOP);
212         i2c_write(eep->i2c, addr_buf, addr_len);
213         i2c_write(eep->i2c, buf, size);
214
215         if (i2c_error(eep->i2c))
216                 return 0;
217
218         return size;
219 }
220
221 static size_t eeprom_readDirect(struct KBlock *_blk, block_idx_t idx, void *_buf, size_t offset, size_t size)
222 {
223         Eeprom *blk = EEPROM_CAST_KBLOCK(_blk);
224         uint8_t addr_buf[2];
225         uint8_t addr_len;
226         size_t rd_len = 0;
227         uint8_t *buf = (uint8_t *)_buf;
228         uint32_t abs_addr = mem_info[blk->type].blk_size * idx + offset;
229
230         STATIC_ASSERT(countof(addr_buf) <= sizeof(e2addr_t));
231
232         /* clamp size to memory limit (otherwise may roll back) */
233         ASSERT(idx <= blk->blk.blk_cnt);
234         size = MIN(size, blk->blk.blk_size - offset);
235
236         e2dev_addr_t dev_addr;
237         if (mem_info[blk->type].has_dev_addr)
238         {
239                 dev_addr = blk->addr;
240                 addr_len = 2;
241                 addr_buf[0] = (abs_addr >> 8) & 0xFF;
242                 addr_buf[1] = (abs_addr & 0xFF);
243         }
244         else
245         {
246                 dev_addr = (e2dev_addr_t)((abs_addr >> 8) & 0x07);
247                 addr_len = 1;
248                 addr_buf[0] = (abs_addr & 0xFF);
249         }
250
251
252         i2c_start_w(blk->i2c, EEPROM_ADDR(dev_addr),  addr_len, I2C_NOSTOP);
253         i2c_write(blk->i2c, addr_buf, addr_len);
254
255         i2c_start_r(blk->i2c, EEPROM_ADDR(dev_addr), size, I2C_STOP);
256         i2c_read(blk->i2c, buf, size);
257
258         if (i2c_error(blk->i2c))
259                    return rd_len;
260
261         rd_len += size;
262
263         return rd_len;
264 }
265
266 static size_t eeprom_writeDirect(KBlock *blk, block_idx_t idx, const void *buf, size_t offset, size_t size)
267 {
268         Eeprom *eep = EEPROM_CAST_KBLOCK(blk);
269         if (!eep->verify)
270                 return eeprom_write(blk, idx, buf, offset, size);
271         else
272         {
273                 int retries = 5;
274                 while (retries--)
275                 {
276                         uint8_t verify_buf[CHUNCK_SIZE];
277                         size_t wr_len = 0;
278                         size_t len = 0;
279                         while (size)
280                         {
281                                 /* Split read in smaller pieces */
282                                 size_t count = MIN(size, (size_t)CHUNCK_SIZE);
283                                 if ((wr_len = eeprom_write(blk, idx, buf, offset, count)) != 0)
284                                 {
285                                         if (eeprom_readDirect(blk, idx, verify_buf, offset, count) != wr_len)
286                                         {
287                                                 LOG_ERR("Verify read fail.\n");
288                                                 return 0;
289                                         }
290                                         else if (memcmp(buf, verify_buf, wr_len) != 0)
291                                         {
292                                                 LOG_ERR("Data mismatch!\n");
293                                                 continue;
294                                         }
295                                 }
296                                 else
297                                 {
298                                         LOG_ERR("Write fail.\n");
299                                         return 0;
300                                 }
301                                 size -= wr_len;
302                                 len += wr_len;
303                                 buf = ((const char *)buf) + wr_len;
304                         }
305                         return len;
306                 }
307         }
308
309         return 0;
310 }
311
312 static int kblockEeprom_dummy(UNUSED_ARG(struct KBlock *,b))
313 {
314         return 0;
315 }
316
317
318 static const KBlockVTable eeprom_unbuffered_vt =
319 {
320         .readDirect = eeprom_readDirect,
321         .writeDirect = eeprom_writeDirect,
322
323         .error = kblockEeprom_dummy,
324         .clearerr = (kblock_clearerr_t)kblockEeprom_dummy,
325 };
326
327 /**
328  * Initialize EEPROM module.
329  * \param eep is the Kblock context.
330  * \param type is the eeprom device we want to initialize (\see EepromType)
331  * \param i2c context for i2c channel
332  * \param addr is the i2c devide address (usually pins A0, A1, A2).
333  * \param verify enable the write check.
334  */
335 void eeprom_init_5(Eeprom *eep, I2c *i2c, EepromType type, e2dev_addr_t addr, bool verify)
336 {
337         ASSERT(type < EEPROM_CNT);
338
339         memset(eep, 0, sizeof(*eep));
340         DB(eep->blk.priv.type = KBT_EEPROM);
341
342         eep->type = type;
343         eep->addr = addr;
344         eep->i2c = i2c;
345         eep->verify = verify;
346
347         eep->blk.blk_size = mem_info[type].blk_size;
348         eep->blk.blk_cnt = mem_info[type].e2_size / mem_info[type].blk_size;
349         eep->blk.priv.flags |= KB_PARTIAL_WRITE;
350         eep->blk.priv.vt = &eeprom_unbuffered_vt;
351 }
352
353