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)
37 * \author Stefano Fedrigo <aleph@develer.com>
38 * \author Bernie Innocenti <bernie@codewiz.org>
43 #warning TODO:Test and complete this module for arm platform.
46 #include <cfg/macros.h> // MIN()
47 #include <cfg/debug.h>
48 #include <cfg/module.h> // MOD_CHECK()
55 #include <cpu/byteorder.h> // cpu_to_be16()
57 #include <string.h> // memset()
62 #define EEPROM_ID 0xA0
65 * This macros form the correct slave address for EEPROMs
67 #define EEPROM_ADDR(x) (EEPROM_ID | (((uint8_t)((x) & 0x07)) << 1))
71 * Array used to describe EEPROM memory devices currently supported.
73 static const EepromInfo mem_info[] =
77 .has_dev_addr = false,
93 /* Add other memories here */
96 STATIC_ASSERT(countof(mem_info) == EEPROM_CNT);
100 * Copy \a size bytes from buffer \a buf to
103 static size_t eeprom_writeRaw(struct KFile *_fd, const void *buf, size_t size)
105 Eeprom *fd = EEPROM_CAST(_fd);
106 e2dev_addr_t dev_addr;
111 e2blk_size_t blk_size = mem_info[fd->type].blk_size;
113 STATIC_ASSERT(countof(addr_buf) <= sizeof(e2addr_t));
115 /* clamp size to memory limit (otherwise may roll back) */
116 ASSERT(_fd->seek_pos + size <= (kfile_off_t)_fd->size);
117 size = MIN((kfile_off_t)size, _fd->size - _fd->seek_pos);
119 if (mem_info[fd->type].has_dev_addr)
126 dev_addr = (e2dev_addr_t)((fd->fd.seek_pos >> 8) & 0x07);
133 * Split write in multiple sequential mode operations that
134 * don't cross page boundaries.
136 size_t count = MIN(size, (size_t)(blk_size - (fd->fd.seek_pos & (blk_size - 1))));
138 if (mem_info[fd->type].has_dev_addr)
140 addr_buf[0] = (fd->fd.seek_pos >> 8) & 0xFF;
141 addr_buf[1] = (fd->fd.seek_pos & 0xFF);
145 dev_addr = (e2dev_addr_t)((fd->fd.seek_pos >> 8) & 0x07);
146 addr_buf[0] = (fd->fd.seek_pos & 0xFF);
150 if (!(i2c_start_w(EEPROM_ADDR(dev_addr))
151 && i2c_send(addr_buf, addr_len)
152 && i2c_send(buf, count)))
160 /* Update count and addr for next operation */
162 fd->fd.seek_pos += count;
163 buf = ((const char *)buf) + count;
171 * Copy \a size bytes from buffer \a _buf to
173 * \note Writes are verified and if buffer content
174 * is not matching we retry 5 times max.
176 static size_t eeprom_writeVerify(struct KFile *_fd, const void *_buf, size_t size)
178 Eeprom *fd = EEPROM_CAST(_fd);
184 wr_len = eeprom_writeRaw(_fd, _buf, size);
185 /* rewind to verify what we have just written */
186 kfile_seek(_fd, -(kfile_off_t)wr_len, KSM_SEEK_CUR);
188 && eeprom_verify(fd, _buf, wr_len))
190 /* Forward to go after what we have written*/
191 kfile_seek(_fd, wr_len, KSM_SEEK_CUR);
201 * from eeprom to RAM to buffer \a _buf.
203 * \return the number of bytes read.
205 static size_t eeprom_read(struct KFile *_fd, void *_buf, size_t size)
207 Eeprom *fd = EEPROM_CAST(_fd);
211 uint8_t *buf = (uint8_t *)_buf;
213 STATIC_ASSERT(countof(addr_buf) <= sizeof(e2addr_t));
215 /* clamp size to memory limit (otherwise may roll back) */
216 ASSERT(_fd->seek_pos + size <= (kfile_off_t)_fd->size);
217 size = MIN((kfile_off_t)size, _fd->size - _fd->seek_pos);
219 e2dev_addr_t dev_addr;
220 if (mem_info[fd->type].has_dev_addr)
224 addr_buf[0] = (fd->fd.seek_pos >> 8) & 0xFF;
225 addr_buf[1] = (fd->fd.seek_pos & 0xFF);
229 dev_addr = (e2dev_addr_t)((fd->fd.seek_pos >> 8) & 0x07);
231 addr_buf[0] = (fd->fd.seek_pos & 0xFF);
235 if (!(i2c_start_w(EEPROM_ADDR(dev_addr))
236 && i2c_send(addr_buf, addr_len)
237 && i2c_start_r(EEPROM_ADDR(dev_addr))))
246 * The last byte read does not have an ACK
247 * to stop communication.
249 int c = i2c_get(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;