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 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/macros.h> // MIN()
42 #include <cfg/debug.h>
43 #include <cfg/module.h> // MOD_CHECK()
50 #include <cpu/byteorder.h> // cpu_to_be16()
52 #include <string.h> // memset()
57 #define EEPROM_ID 0xA0
60 * This macros form the correct slave address for EEPROMs
62 #define EEPROM_ADDR(x) (EEPROM_ID | (((uint8_t)((x) & 0x07)) << 1))
66 * Array used to describe EEPROM memory devices currently supported.
68 static const EepromInfo mem_info[] =
72 .has_dev_addr = false,
78 .has_dev_addr = false,
101 /* Add other memories here */
104 STATIC_ASSERT(countof(mem_info) == EEPROM_CNT);
108 * Copy \a size bytes from buffer \a buf to
111 static size_t eeprom_writeRaw(struct KFile *_fd, const void *buf, size_t size)
113 Eeprom *fd = EEPROM_CAST(_fd);
114 e2dev_addr_t dev_addr;
119 e2blk_size_t blk_size = mem_info[fd->type].blk_size;
121 STATIC_ASSERT(countof(addr_buf) <= sizeof(e2addr_t));
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);
127 if (mem_info[fd->type].has_dev_addr)
134 dev_addr = (e2dev_addr_t)((fd->fd.seek_pos >> 8) & 0x07);
141 * Split write in multiple sequential mode operations that
142 * don't cross page boundaries.
144 size_t count = MIN(size, (size_t)(blk_size - (fd->fd.seek_pos & (blk_size - 1))));
146 if (mem_info[fd->type].has_dev_addr)
148 addr_buf[0] = (fd->fd.seek_pos >> 8) & 0xFF;
149 addr_buf[1] = (fd->fd.seek_pos & 0xFF);
153 dev_addr = (e2dev_addr_t)((fd->fd.seek_pos >> 8) & 0x07);
154 addr_buf[0] = (fd->fd.seek_pos & 0xFF);
158 if (!(i2c_start_w(EEPROM_ADDR(dev_addr))
159 && i2c_send(addr_buf, addr_len)
160 && i2c_send(buf, count)))
168 /* Update count and addr for next operation */
170 fd->fd.seek_pos += count;
171 buf = ((const char *)buf) + count;
179 * Copy \a size bytes from buffer \a _buf to
181 * \note Writes are verified and if buffer content
182 * is not matching we retry 5 times max.
184 static size_t eeprom_writeVerify(struct KFile *_fd, const void *_buf, size_t size)
186 Eeprom *fd = EEPROM_CAST(_fd);
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);
196 && eeprom_verify(fd, _buf, wr_len))
198 /* Forward to go after what we have written*/
199 kfile_seek(_fd, wr_len, KSM_SEEK_CUR);
209 * from eeprom to RAM to buffer \a _buf.
211 * \return the number of bytes read.
213 static size_t eeprom_read(struct KFile *_fd, void *_buf, size_t size)
215 Eeprom *fd = EEPROM_CAST(_fd);
219 uint8_t *buf = (uint8_t *)_buf;
221 STATIC_ASSERT(countof(addr_buf) <= sizeof(e2addr_t));
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);
227 e2dev_addr_t dev_addr;
228 if (mem_info[fd->type].has_dev_addr)
232 addr_buf[0] = (fd->fd.seek_pos >> 8) & 0xFF;
233 addr_buf[1] = (fd->fd.seek_pos & 0xFF);
237 dev_addr = (e2dev_addr_t)((fd->fd.seek_pos >> 8) & 0x07);
239 addr_buf[0] = (fd->fd.seek_pos & 0xFF);
243 if (!(i2c_start_w(EEPROM_ADDR(dev_addr))
244 && i2c_send(addr_buf, addr_len)
245 && i2c_start_r(EEPROM_ADDR(dev_addr))))
252 if (i2c_recv(buf, size))
254 fd->fd.seek_pos += size;
263 * Check that the contents of an EEPROM range
264 * match with a provided data buffer.
266 * \return true on success.
267 * \note Seek position of \a fd will not change.
269 bool eeprom_verify(Eeprom *fd, const void *buf, size_t count)
271 uint8_t verify_buf[16];
274 /* Save seek position */
275 kfile_off_t prev_seek = fd->fd.seek_pos;
277 while (count && result)
279 /* Split read in smaller pieces */
280 size_t size = MIN(count, sizeof verify_buf);
282 /* Read back buffer */
283 if (eeprom_read(&fd->fd, verify_buf, size))
285 if (memcmp(buf, verify_buf, size) != 0)
287 TRACEMSG("Data mismatch!");
293 TRACEMSG("Read error!");
297 /* Update count and addr for next operation */
299 buf = ((const char *)buf) + size;
302 /* Restore previous seek position */
303 fd->fd.seek_pos = prev_seek;
308 * Erase specified part of eeprom, writing 0xFF.
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.
315 bool eeprom_erase(Eeprom *fd, e2addr_t addr, e2_size_t count)
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;
323 memset(buf, 0xFF, blk_size);
326 kfile_seek(&fd->fd, addr, KSM_SEEK_SET);
329 * Optimization: this first write id used to realign
330 * current address to block boundaries.
334 size = MIN(count, (e2_size_t)(blk_size - (addr & (blk_size - 1))));
335 if (kfile_write(&fd->fd, buf, size) != size)
337 fd->fd.seek_pos = prev_off;
345 /* Long operation, reset watchdog */
348 size = MIN(count, (e2_size_t)sizeof buf);
349 if (kfile_write(&fd->fd, buf, size) != size)
357 fd->fd.seek_pos = prev_off;
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.
369 void eeprom_init(Eeprom *fd, EepromType type, e2dev_addr_t addr, bool verify)
372 ASSERT(type < EEPROM_CNT);
374 memset(fd, 0, sizeof(*fd));
375 DB(fd->fd._type = KFT_EEPROM);
379 fd->fd.size = mem_info[fd->type].e2_size;
381 // Setup eeprom programming functions.
382 fd->fd.read = eeprom_read;
384 fd->fd.write = eeprom_writeVerify;
386 fd->fd.write = eeprom_writeRaw;
387 fd->fd.close = kfile_genericClose;
389 fd->fd.seek = kfile_genericSeek;