Add implement verify and erase functions and support for old api. Add cfg for eeprom.
[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 bool eeprom_erase(Eeprom *eep, e2addr_t addr, e2_size_t size)
112 {
113         uint8_t tmp[CHUNCK_SIZE] = { [0 ... (CHUNCK_SIZE - 1)] = 0xFF };
114
115         while (size)
116         {
117                 block_idx_t idx = addr / eep->blk.blk_size;
118                 size_t offset = addr % eep->blk.blk_size;
119                 size_t count = MIN(size, (e2_size_t)CHUNCK_SIZE);
120                 size_t ret_len = eep->blk.priv.vt->writeDirect((KBlock *)eep, idx, tmp, offset, count);
121                 size -= ret_len;
122                 addr += ret_len;
123
124                 if (ret_len != count)
125                         return false;
126         }
127         return true;
128 }
129
130 bool eeprom_verify(Eeprom *eep, e2addr_t addr, const void *buf, size_t size)
131 {
132     uint8_t verify_buf[CHUNCK_SIZE];
133         while (size)
134         {
135                 block_idx_t idx = addr / eep->blk.blk_size;
136                 size_t offset = addr % eep->blk.blk_size;
137                 size_t count = MIN(size, (size_t)CHUNCK_SIZE);
138
139                 size_t ret_len = eep->blk.priv.vt->readDirect((KBlock *)eep, idx, verify_buf, offset, count);
140
141                 if (ret_len != count)
142                 {
143                         LOG_ERR("Verify read fail.\n");
144                         return false;
145                 }
146
147                 if (memcmp(buf, verify_buf, ret_len) != 0)
148                 {
149                         LOG_ERR("Data mismatch!\n");
150                         return false;
151                 }
152
153                 size -= ret_len;
154                 addr += ret_len;
155                 buf = ((const char *)buf) + ret_len;
156         }
157         return true;
158 }
159
160
161 static size_t eeprom_write(KBlock *blk, block_idx_t idx, const void *buf, size_t offset, size_t size)
162 {
163         Eeprom *eep = EEPROM_CAST_KBLOCK(blk);
164         e2dev_addr_t dev_addr;
165         uint8_t addr_buf[2];
166         uint8_t addr_len;
167         uint32_t abs_addr = blk->blk_size * idx + offset;
168
169         STATIC_ASSERT(countof(addr_buf) <= sizeof(e2addr_t));
170
171
172         /* clamp size to memory limit (otherwise may roll back) */
173         ASSERT(idx <= blk->blk_cnt);
174         size = MIN(size, blk->blk_size - offset);
175
176         if (mem_info[eep->type].has_dev_addr)
177         {
178                 dev_addr = eep->addr;
179                 addr_len = 2;
180         }
181         else
182         {
183                 dev_addr = (e2dev_addr_t)((abs_addr >> 8) & 0x07);
184                 addr_len = 1;
185         }
186
187         if (mem_info[eep->type].has_dev_addr)
188         {
189                 addr_buf[0] = (abs_addr >> 8) & 0xFF;
190                 addr_buf[1] = (abs_addr & 0xFF);
191         }
192         else
193         {
194                 dev_addr = (e2dev_addr_t)((abs_addr >> 8) & 0x07);
195                 addr_buf[0] = (abs_addr & 0xFF);
196         }
197
198         i2c_start_w(eep->i2c, EEPROM_ADDR(dev_addr),  addr_len + size, I2C_STOP);
199         i2c_write(eep->i2c, addr_buf, addr_len);
200         i2c_write(eep->i2c, buf, size);
201
202         if (i2c_error(eep->i2c))
203                 return 0;
204
205         return size;
206 }
207
208 static size_t eeprom_readDirect(struct KBlock *_blk, block_idx_t idx, void *_buf, size_t offset, size_t size)
209 {
210         Eeprom *blk = EEPROM_CAST_KBLOCK(_blk);
211         uint8_t addr_buf[2];
212         uint8_t addr_len;
213         size_t rd_len = 0;
214         uint8_t *buf = (uint8_t *)_buf;
215         uint32_t abs_addr = mem_info[blk->type].blk_size * idx + offset;
216
217         STATIC_ASSERT(countof(addr_buf) <= sizeof(e2addr_t));
218
219         /* clamp size to memory limit (otherwise may roll back) */
220         ASSERT(idx <= blk->blk.blk_cnt);
221         size = MIN(size, blk->blk.blk_size - offset);
222
223         e2dev_addr_t dev_addr;
224         if (mem_info[blk->type].has_dev_addr)
225         {
226                 dev_addr = blk->addr;
227                 addr_len = 2;
228                 addr_buf[0] = (abs_addr >> 8) & 0xFF;
229                 addr_buf[1] = (abs_addr & 0xFF);
230         }
231         else
232         {
233                 dev_addr = (e2dev_addr_t)((abs_addr >> 8) & 0x07);
234                 addr_len = 1;
235                 addr_buf[0] = (abs_addr & 0xFF);
236         }
237
238
239         i2c_start_w(blk->i2c, EEPROM_ADDR(dev_addr),  addr_len, I2C_NOSTOP);
240         i2c_write(blk->i2c, addr_buf, addr_len);
241
242         i2c_start_r(blk->i2c, EEPROM_ADDR(dev_addr), size, I2C_STOP);
243         i2c_read(blk->i2c, buf, size);
244
245         if (i2c_error(blk->i2c))
246                    return rd_len;
247
248         rd_len += size;
249
250         return rd_len;
251 }
252
253 static size_t eeprom_writeDirect(KBlock *blk, block_idx_t idx, const void *buf, size_t offset, size_t size)
254 {
255         Eeprom *eep = EEPROM_CAST_KBLOCK(blk);
256         if (!eep->verify)
257                 return eeprom_write(blk, idx, buf, offset, size);
258         else
259         {
260                 int retries = 5;
261                 while (retries--)
262                 {
263                         uint8_t verify_buf[CHUNCK_SIZE];
264                         size_t wr_len = 0;
265                         size_t len = 0;
266                         while (size)
267                         {
268                                 /* Split read in smaller pieces */
269                                 size_t count = MIN(size, (size_t)CHUNCK_SIZE);
270                                 if ((wr_len = eeprom_write(blk, idx, buf, offset, count)) != 0)
271                                 {
272                                         if (eeprom_readDirect(blk, idx, verify_buf, offset, count) != wr_len)
273                                         {
274                                                 LOG_ERR("Verify read fail.\n");
275                                                 return 0;
276                                         }
277                                         else if (memcmp(buf, verify_buf, wr_len) != 0)
278                                         {
279                                                 LOG_ERR("Data mismatch!\n");
280                                                 continue;
281                                         }
282                                 }
283                                 else
284                                 {
285                                         LOG_ERR("Write fail.\n");
286                                         return 0;
287                                 }
288                                 size -= wr_len;
289                                 len += wr_len;
290                                 buf = ((const char *)buf) + wr_len;
291                         }
292                         return len;
293                 }
294         }
295
296         return 0;
297 }
298
299 static int kblockEeprom_dummy(UNUSED_ARG(struct KBlock *,b))
300 {
301         return 0;
302 }
303
304
305 static const KBlockVTable eeprom_unbuffered_vt =
306 {
307         .readDirect = eeprom_readDirect,
308         .writeDirect = eeprom_writeDirect,
309
310         .error = kblockEeprom_dummy,
311         .clearerr = (kblock_clearerr_t)kblockEeprom_dummy,
312 };
313
314 /**
315  * Initialize EEPROM module.
316  * \param b is the Kblock context.
317  * \param type is the eeprom device we want to initialize (\see EepromType)
318  * \param i2c context for i2c channel
319  * \param addr is the i2c devide address (usually pins A0, A1, A2).
320  */
321 void eeprom_init_5(Eeprom *eep, I2c *i2c, EepromType type, e2dev_addr_t addr, bool verify)
322 {
323         ASSERT(type < EEPROM_CNT);
324
325         memset(eep, 0, sizeof(*eep));
326         DB(eep->blk.priv.type = KBT_EEPROM);
327
328         eep->type = type;
329         eep->addr = addr;
330         eep->i2c = i2c;
331         eep->verify = verify;
332
333         eep->blk.blk_size = mem_info[type].blk_size;
334         eep->blk.blk_cnt = mem_info[type].e2_size / mem_info[type].blk_size;
335         eep->blk.priv.flags |= KB_PARTIAL_WRITE;
336         eep->blk.priv.vt = &eeprom_unbuffered_vt;
337 }
338
339