4 * This file is part of BeRTOS.
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.
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.
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
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.
29 * Copyright 2003, 2004, 2005, 2010 Develer S.r.l. (http://www.develer.com/)
33 * \brief Driver for the 24xx16 and 24xx256 I2C EEPROMS (implementation)
35 * \author Stefano Fedrigo <aleph@develer.com>
36 * \author Bernie Innocenti <bernie@codewiz.org>
41 #include "cfg/cfg_i2c.h"
43 #include <cfg/macros.h> // MIN()
44 #include <cfg/debug.h>
45 #include <cfg/module.h> // MOD_CHECK()
52 #include <cpu/byteorder.h> // cpu_to_be16()
54 #include <string.h> // memset()
59 #define EEPROM_ID 0xA0
62 * This macros form the correct slave address for EEPROMs
64 #define EEPROM_ADDR(x) (EEPROM_ID | (((uint8_t)((x) & 0x07)) << 1))
68 * Array used to describe EEPROM memory devices currently supported.
70 static const EepromInfo mem_info[] =
74 .has_dev_addr = false,
80 .has_dev_addr = false,
103 /* Add other memories here */
106 STATIC_ASSERT(countof(mem_info) == EEPROM_CNT);
108 #if 0//!CONFIG_I2C_DISABLE_OLD_API
110 * Copy \a size bytes from buffer \a buf to
113 static size_t eeprom_writeRaw(struct KFile *_fd, const void *buf, size_t size)
115 Eeprom *fd = EEPROM_CAST(_fd);
116 e2dev_addr_t dev_addr;
121 e2blk_size_t blk_size = mem_info[fd->type].blk_size;
123 STATIC_ASSERT(countof(addr_buf) <= sizeof(e2addr_t));
125 /* clamp size to memory limit (otherwise may roll back) */
126 ASSERT(_fd->seek_pos + (kfile_off_t)size <= (kfile_off_t)_fd->size);
127 size = MIN((kfile_off_t)size, _fd->size - _fd->seek_pos);
129 if (mem_info[fd->type].has_dev_addr)
136 dev_addr = (e2dev_addr_t)((fd->fd.seek_pos >> 8) & 0x07);
143 * Split write in multiple sequential mode operations that
144 * don't cross page boundaries.
146 size_t count = MIN(size, (size_t)(blk_size - (fd->fd.seek_pos & (blk_size - 1))));
148 if (mem_info[fd->type].has_dev_addr)
150 addr_buf[0] = (fd->fd.seek_pos >> 8) & 0xFF;
151 addr_buf[1] = (fd->fd.seek_pos & 0xFF);
155 dev_addr = (e2dev_addr_t)((fd->fd.seek_pos >> 8) & 0x07);
156 addr_buf[0] = (fd->fd.seek_pos & 0xFF);
160 if (!(i2c_start_w(EEPROM_ADDR(dev_addr))
161 && i2c_send(addr_buf, addr_len)
162 && i2c_send(buf, count)))
170 /* Update count and addr for next operation */
172 fd->fd.seek_pos += count;
173 buf = ((const char *)buf) + count;
181 * Copy \a size bytes from buffer \a _buf to
183 * \note Writes are verified and if buffer content
184 * is not matching we retry 5 times max.
186 static size_t eeprom_writeVerify(struct KFile *_fd, const void *_buf, size_t size)
188 Eeprom *fd = EEPROM_CAST(_fd);
194 wr_len = eeprom_writeRaw(_fd, _buf, size);
195 /* rewind to verify what we have just written */
196 kfile_seek(_fd, -(kfile_off_t)wr_len, KSM_SEEK_CUR);
198 && eeprom_verify(fd, _buf, wr_len))
200 /* Forward to go after what we have written*/
201 kfile_seek(_fd, wr_len, KSM_SEEK_CUR);
211 * from eeprom to RAM to buffer \a _buf.
213 * \return the number of bytes read.
215 static size_t eeprom_read(struct KFile *_fd, void *_buf, size_t size)
217 Eeprom *fd = EEPROM_CAST(_fd);
221 uint8_t *buf = (uint8_t *)_buf;
223 STATIC_ASSERT(countof(addr_buf) <= sizeof(e2addr_t));
225 /* clamp size to memory limit (otherwise may roll back) */
226 ASSERT(_fd->seek_pos + (kfile_off_t)size <= (kfile_off_t)_fd->size);
227 size = MIN((kfile_off_t)size, _fd->size - _fd->seek_pos);
229 e2dev_addr_t dev_addr;
230 if (mem_info[fd->type].has_dev_addr)
234 addr_buf[0] = (fd->fd.seek_pos >> 8) & 0xFF;
235 addr_buf[1] = (fd->fd.seek_pos & 0xFF);
239 dev_addr = (e2dev_addr_t)((fd->fd.seek_pos >> 8) & 0x07);
241 addr_buf[0] = (fd->fd.seek_pos & 0xFF);
245 if (!(i2c_start_w(EEPROM_ADDR(dev_addr))
246 && i2c_send(addr_buf, addr_len)
247 && i2c_start_r(EEPROM_ADDR(dev_addr))))
254 if (i2c_recv(buf, size))
256 fd->fd.seek_pos += size;
265 * Check that the contents of an EEPROM range
266 * match with a provided data buffer.
268 * \return true on success.
269 * \note Seek position of \a fd will not change.
271 bool eeprom_verify(Eeprom *fd, const void *buf, size_t count)
273 uint8_t verify_buf[16];
276 /* Save seek position */
277 kfile_off_t prev_seek = fd->fd.seek_pos;
279 while (count && result)
281 /* Split read in smaller pieces */
282 size_t size = MIN(count, sizeof verify_buf);
284 /* Read back buffer */
285 if (eeprom_read(&fd->fd, verify_buf, size))
287 if (memcmp(buf, verify_buf, size) != 0)
289 TRACEMSG("Data mismatch!");
295 TRACEMSG("Read error!");
299 /* Update count and addr for next operation */
301 buf = ((const char *)buf) + size;
304 /* Restore previous seek position */
305 fd->fd.seek_pos = prev_seek;
310 * Erase specified part of eeprom, writing 0xFF.
312 * \a addr starting address
313 * \a count length of block to erase
314 * \note Seek position is unchanged.
315 * \return true if ok, false otherwise.
317 bool eeprom_erase(Eeprom *fd, e2addr_t addr, e2_size_t count)
319 e2blk_size_t blk_size = mem_info[fd->type].blk_size;
320 uint8_t buf[blk_size];
321 kfile_off_t prev_off = fd->fd.seek_pos;
325 memset(buf, 0xFF, blk_size);
328 kfile_seek(&fd->fd, addr, KSM_SEEK_SET);
331 * Optimization: this first write id used to realign
332 * current address to block boundaries.
336 size = MIN(count, (e2_size_t)(blk_size - (addr & (blk_size - 1))));
337 if (kfile_write(&fd->fd, buf, size) != size)
339 fd->fd.seek_pos = prev_off;
347 /* Long operation, reset watchdog */
350 size = MIN(count, (e2_size_t)sizeof buf);
351 if (kfile_write(&fd->fd, buf, size) != size)
359 fd->fd.seek_pos = prev_off;
365 * Initialize EEPROM module.
366 * \a fd is the Kfile context.
367 * \a type is the eeprom device we want to initialize (\see EepromType)
368 * \a addr is the i2c devide address (usually pins A0, A1, A2).
369 * \a verify is true if you want that every write operation will be verified.
371 void eeprom_init(Eeprom *fd, EepromType type, e2dev_addr_t addr, bool verify)
374 ASSERT(type < EEPROM_CNT);
376 memset(fd, 0, sizeof(*fd));
377 DB(fd->fd._type = KFT_EEPROM);
381 fd->fd.size = mem_info[fd->type].e2_size;
383 // Setup eeprom programming functions.
384 fd->fd.read = eeprom_read;
386 fd->fd.write = eeprom_writeVerify;
388 fd->fd.write = eeprom_writeRaw;
389 fd->fd.close = kfile_genericClose;
391 fd->fd.seek = kfile_genericSeek;
394 #endif /* !CONFIG_I2C_DISABLE_OLD_API */
396 static size_t eeprom_writeDirect(KBlock *b, block_idx_t idx, const void *buf, size_t offset, size_t size)
398 Eeprom *fd = EEPROM_CAST(b);
399 e2dev_addr_t dev_addr;
402 uint32_t abs_addr = b->blk_size * idx + offset;
404 STATIC_ASSERT(countof(addr_buf) <= sizeof(e2addr_t));
407 /* clamp size to memory limit (otherwise may roll back) */
408 ASSERT(idx <= b->blk_cnt);
409 size = MIN(size, b->blk_size - offset);
411 if (mem_info[fd->type].has_dev_addr)
418 dev_addr = (e2dev_addr_t)((abs_addr >> 8) & 0x07);
422 if (mem_info[fd->type].has_dev_addr)
424 addr_buf[0] = (abs_addr >> 8) & 0xFF;
425 addr_buf[1] = (abs_addr & 0xFF);
429 dev_addr = (e2dev_addr_t)((abs_addr >> 8) & 0x07);
430 addr_buf[0] = (abs_addr & 0xFF);
433 i2c_start_w(fd->i2c, EEPROM_ADDR(dev_addr), addr_len + size, I2C_STOP);
434 i2c_write(fd->i2c, addr_buf, addr_len);
435 i2c_write(fd->i2c, buf, size);
437 if (i2c_error(fd->i2c))
444 static size_t eeprom_readDirect(struct KBlock *b, block_idx_t idx, void *_buf, size_t offset, size_t size)
446 Eeprom *fd = EEPROM_CAST(b);
450 uint8_t *buf = (uint8_t *)_buf;
451 uint32_t abs_addr = mem_info[fd->type].blk_size * idx + offset;
453 STATIC_ASSERT(countof(addr_buf) <= sizeof(e2addr_t));
455 /* clamp size to memory limit (otherwise may roll back) */
456 ASSERT(idx <= b->blk_cnt);
457 size = MIN(size, b->blk_size - offset);
459 e2dev_addr_t dev_addr;
460 if (mem_info[fd->type].has_dev_addr)
464 addr_buf[0] = (abs_addr >> 8) & 0xFF;
465 addr_buf[1] = (abs_addr & 0xFF);
469 dev_addr = (e2dev_addr_t)((abs_addr >> 8) & 0x07);
471 addr_buf[0] = (abs_addr & 0xFF);
475 i2c_start_w(fd->i2c, EEPROM_ADDR(dev_addr), addr_len, I2C_NOSTOP);
476 i2c_write(fd->i2c, addr_buf, addr_len);
478 i2c_start_r(fd->i2c, EEPROM_ADDR(dev_addr), size, I2C_STOP);
479 i2c_read(fd->i2c, buf, size);
481 if (i2c_error(fd->i2c))
489 static int kblockEeprom_dummy(UNUSED_ARG(struct KBlock *,b))
495 static const KBlockVTable eeprom_unbuffered_vt =
497 .readDirect = eeprom_readDirect,
498 .writeDirect = eeprom_writeDirect,
500 .error = kblockEeprom_dummy,
501 .clearerr = (kblock_clearerr_t)kblockEeprom_dummy,
505 * Initialize EEPROM module.
506 * \param b is the Kblock context.
507 * \param type is the eeprom device we want to initialize (\see EepromType)
508 * \param i2c context for i2c channel
509 * \param addr is the i2c devide address (usually pins A0, A1, A2).
511 void eeprom_init(Eeprom *b, I2c *i2c, EepromType type, e2dev_addr_t addr)
513 ASSERT(type < EEPROM_CNT);
515 memset(b, 0, sizeof(*b));
516 DB(b->b.priv.type = KBT_EEPROM);
522 b->b.blk_size = mem_info[type].blk_size;
523 b->b.blk_cnt = mem_info[type].e2_size / mem_info[type].blk_size;
524 b->b.priv.flags |= KB_PARTIAL_WRITE;
525 b->b.priv.vt = &eeprom_unbuffered_vt;