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 !CONFIG_I2C_DISABLE_OLD_API
111 * Copy \a size bytes from buffer \a buf to
114 static size_t eeprom_writeRaw(struct KFile *_fd, const void *buf, size_t size)
116 Eeprom *fd = EEPROM_CAST(_fd);
117 e2dev_addr_t dev_addr;
122 e2blk_size_t blk_size = mem_info[fd->type].blk_size;
124 STATIC_ASSERT(countof(addr_buf) <= sizeof(e2addr_t));
126 /* clamp size to memory limit (otherwise may roll back) */
127 ASSERT(_fd->seek_pos + (kfile_off_t)size <= (kfile_off_t)_fd->size);
128 size = MIN((kfile_off_t)size, _fd->size - _fd->seek_pos);
130 if (mem_info[fd->type].has_dev_addr)
137 dev_addr = (e2dev_addr_t)((fd->fd.seek_pos >> 8) & 0x07);
144 * Split write in multiple sequential mode operations that
145 * don't cross page boundaries.
147 size_t count = MIN(size, (size_t)(blk_size - (fd->fd.seek_pos & (blk_size - 1))));
149 if (mem_info[fd->type].has_dev_addr)
151 addr_buf[0] = (fd->fd.seek_pos >> 8) & 0xFF;
152 addr_buf[1] = (fd->fd.seek_pos & 0xFF);
156 dev_addr = (e2dev_addr_t)((fd->fd.seek_pos >> 8) & 0x07);
157 addr_buf[0] = (fd->fd.seek_pos & 0xFF);
161 if (!(i2c_start_w(EEPROM_ADDR(dev_addr))
162 && i2c_send(addr_buf, addr_len)
163 && i2c_send(buf, count)))
171 /* Update count and addr for next operation */
173 fd->fd.seek_pos += count;
174 buf = ((const char *)buf) + count;
182 * Copy \a size bytes from buffer \a _buf to
184 * \note Writes are verified and if buffer content
185 * is not matching we retry 5 times max.
187 static size_t eeprom_writeVerify(struct KFile *_fd, const void *_buf, size_t size)
189 Eeprom *fd = EEPROM_CAST(_fd);
195 wr_len = eeprom_writeRaw(_fd, _buf, size);
196 /* rewind to verify what we have just written */
197 kfile_seek(_fd, -(kfile_off_t)wr_len, KSM_SEEK_CUR);
199 && eeprom_verify(fd, _buf, wr_len))
201 /* Forward to go after what we have written*/
202 kfile_seek(_fd, wr_len, KSM_SEEK_CUR);
212 * from eeprom to RAM to buffer \a _buf.
214 * \return the number of bytes read.
216 static size_t eeprom_read(struct KFile *_fd, void *_buf, size_t size)
218 Eeprom *fd = EEPROM_CAST(_fd);
222 uint8_t *buf = (uint8_t *)_buf;
224 STATIC_ASSERT(countof(addr_buf) <= sizeof(e2addr_t));
226 /* clamp size to memory limit (otherwise may roll back) */
227 ASSERT(_fd->seek_pos + (kfile_off_t)size <= (kfile_off_t)_fd->size);
228 size = MIN((kfile_off_t)size, _fd->size - _fd->seek_pos);
230 e2dev_addr_t dev_addr;
231 if (mem_info[fd->type].has_dev_addr)
235 addr_buf[0] = (fd->fd.seek_pos >> 8) & 0xFF;
236 addr_buf[1] = (fd->fd.seek_pos & 0xFF);
240 dev_addr = (e2dev_addr_t)((fd->fd.seek_pos >> 8) & 0x07);
242 addr_buf[0] = (fd->fd.seek_pos & 0xFF);
246 if (!(i2c_start_w(EEPROM_ADDR(dev_addr))
247 && i2c_send(addr_buf, addr_len)
248 && i2c_start_r(EEPROM_ADDR(dev_addr))))
255 if (i2c_recv(buf, size))
257 fd->fd.seek_pos += size;
266 * Check that the contents of an EEPROM range
267 * match with a provided data buffer.
269 * \return true on success.
270 * \note Seek position of \a fd will not change.
272 bool eeprom_verify(Eeprom *fd, const void *buf, size_t count)
274 uint8_t verify_buf[16];
277 /* Save seek position */
278 kfile_off_t prev_seek = fd->fd.seek_pos;
280 while (count && result)
282 /* Split read in smaller pieces */
283 size_t size = MIN(count, sizeof verify_buf);
285 /* Read back buffer */
286 if (eeprom_read(&fd->fd, verify_buf, size))
288 if (memcmp(buf, verify_buf, size) != 0)
290 TRACEMSG("Data mismatch!");
296 TRACEMSG("Read error!");
300 /* Update count and addr for next operation */
302 buf = ((const char *)buf) + size;
305 /* Restore previous seek position */
306 fd->fd.seek_pos = prev_seek;
311 * Erase specified part of eeprom, writing 0xFF.
313 * \a addr starting address
314 * \a count length of block to erase
315 * \note Seek position is unchanged.
316 * \return true if ok, false otherwise.
318 bool eeprom_erase(Eeprom *fd, e2addr_t addr, e2_size_t count)
320 e2blk_size_t blk_size = mem_info[fd->type].blk_size;
321 uint8_t buf[blk_size];
322 kfile_off_t prev_off = fd->fd.seek_pos;
326 memset(buf, 0xFF, blk_size);
329 kfile_seek(&fd->fd, addr, KSM_SEEK_SET);
332 * Optimization: this first write id used to realign
333 * current address to block boundaries.
337 size = MIN(count, (e2_size_t)(blk_size - (addr & (blk_size - 1))));
338 if (kfile_write(&fd->fd, buf, size) != size)
340 fd->fd.seek_pos = prev_off;
348 /* Long operation, reset watchdog */
351 size = MIN(count, (e2_size_t)sizeof buf);
352 if (kfile_write(&fd->fd, buf, size) != size)
360 fd->fd.seek_pos = prev_off;
366 * Initialize EEPROM module.
367 * \a fd is the Kfile context.
368 * \a type is the eeprom device we want to initialize (\see EepromType)
369 * \a addr is the i2c devide address (usually pins A0, A1, A2).
370 * \a verify is true if you want that every write operation will be verified.
372 void eeprom_init(Eeprom *fd, EepromType type, e2dev_addr_t addr, bool verify)
375 ASSERT(type < EEPROM_CNT);
377 memset(fd, 0, sizeof(*fd));
378 DB(fd->fd._type = KFT_EEPROM);
382 fd->fd.size = mem_info[fd->type].e2_size;
384 // Setup eeprom programming functions.
385 fd->fd.read = eeprom_read;
387 fd->fd.write = eeprom_writeVerify;
389 fd->fd.write = eeprom_writeRaw;
390 fd->fd.close = kfile_genericClose;
392 fd->fd.seek = kfile_genericSeek;
395 #endif /* !CONFIG_I2C_DISABLE_OLD_API */