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 Bernardo Innocenti <bernie@develer.com>
43 #warning TODO:Test and complete this module for arm platform.
46 #include "cfg/cfg_eeprom.h" // CONFIG_EEPROM_VERIFY
47 #include <cfg/macros.h> // MIN()
48 #include <cfg/debug.h>
51 #include CPU_HEADER(twi)
55 #include <mware/byteorder.h> // cpu_to_be16()
57 #include <string.h> // memset()
60 // Configuration sanity checks
61 #if !defined(CONFIG_EEPROM_VERIFY) || (CONFIG_EEPROM_VERIFY != 0 && CONFIG_EEPROM_VERIFY != 1)
62 #error CONFIG_EEPROM_VERIFY must be defined to either 0 or 1
68 #define EEPROM_ID 0xA0
71 * This macros form the correct slave address for EEPROMs
73 #define EEPROM_ADDR(x) (EEPROM_ID | (((uint8_t)(x)) << 1))
79 * Copy \c count bytes from buffer \c buf to
80 * eeprom at address \c addr.
82 static bool eeprom_writeRaw(e2addr_t addr, const void *buf, size_t count)
85 ASSERT(addr + count <= EEPROM_SIZE);
87 while (count && result)
90 * Split write in multiple sequential mode operations that
91 * don't cross page boundaries.
94 MIN(count, (size_t)(EEPROM_BLKSIZE - (addr & (EEPROM_BLKSIZE - 1))));
96 #if CONFIG_EEPROM_TYPE == EEPROM_24XX16
98 * The 24LC16 uses the slave address as a 3-bit
101 uint8_t blk_addr = (uint8_t)((addr >> 8) & 0x07);
102 uint8_t blk_offs = (uint8_t)addr;
105 twi_start_w(EEPROM_ADDR(blk_addr))
106 && twi_send(&blk_offs, sizeof blk_offs)
107 && twi_send(buf, size);
109 #elif CONFIG_EEPROM_TYPE == EEPROM_24XX256
111 // 24LC256 wants big-endian addresses
112 uint16_t addr_be = cpu_to_be16(addr);
115 twi_start_w(EEPROM_ID)
116 && twi_send((uint8_t *)&addr_be, sizeof addr_be)
117 && twi_send(buf, size);
120 #error Unknown device type
126 //kprintf("addr=%d, count=%d, size=%d, *#?=%d\n",
127 // addr, count, size,
128 // (EEPROM_BLKSIZE - (addr & (EEPROM_BLKSIZE - 1)))
131 /* Update count and addr for next operation */
134 buf = ((const char *)buf) + size;
138 TRACEMSG("Write error!");
143 #if CONFIG_EEPROM_VERIFY
145 * Check that the contents of an EEPROM range
146 * match with a provided data buffer.
148 * \return true on success.
150 static bool eeprom_verify(e2addr_t addr, const void *buf, size_t count)
152 uint8_t verify_buf[16];
155 while (count && result)
157 /* Split read in smaller pieces */
158 size_t size = MIN(count, sizeof verify_buf);
160 /* Read back buffer */
161 if (eeprom_read(addr, verify_buf, size))
163 if (memcmp(buf, verify_buf, size) != 0)
165 TRACEMSG("Data mismatch!");
171 TRACEMSG("Read error!");
175 /* Update count and addr for next operation */
178 buf = ((const char *)buf) + size;
183 #endif /* CONFIG_EEPROM_VERIFY */
186 bool eeprom_write(e2addr_t addr, const void *buf, size_t count)
188 #if CONFIG_EEPROM_VERIFY
192 if (eeprom_writeRaw(addr, buf, count)
193 && eeprom_verify(addr, buf, count))
198 #else /* !CONFIG_EEPROM_VERIFY */
199 return eeprom_writeRaw(addr, buf, count);
200 #endif /* !CONFIG_EEPROM_VERIFY */
205 * Copy \c count bytes at address \c addr
206 * from eeprom to RAM to buffer \c buf.
208 * \return true on success.
210 bool eeprom_read(e2addr_t addr, void *buf, size_t count)
212 ASSERT(addr + count <= EEPROM_SIZE);
214 #if CONFIG_EEPROM_TYPE == EEPROM_24XX16
216 * The 24LC16 uses the slave address as a 3-bit
219 uint8_t blk_addr = (uint8_t)((addr >> 8) & 0x07);
220 uint8_t blk_offs = (uint8_t)addr;
223 twi_start_w(EEPROM_ADDR(blk_addr))
224 && twi_send(&blk_offs, sizeof blk_offs)
225 && twi_start_r(EEPROM_ADDR(blk_addr))
226 && twi_recv(buf, count);
228 #elif CONFIG_EEPROM_TYPE == EEPROM_24XX256
230 // 24LC256 wants big-endian addresses
231 addr = cpu_to_be16(addr);
234 twi_start_w(EEPROM_ID)
235 && twi_send((uint8_t *)&addr, sizeof(addr))
236 && twi_start_r(EEPROM_ID)
237 && twi_recv(buf, count);
239 #error Unknown device type
245 TRACEMSG("Read error!");
251 * Write a single character \a c at address \a addr.
253 bool eeprom_write_char(e2addr_t addr, char c)
255 return eeprom_write(addr, &c, 1);
260 * Read a single character at address \a addr.
262 * \return the requested character or -1 in case of failure.
264 int eeprom_read_char(e2addr_t addr)
268 if (eeprom_read(addr, &c, 1))
276 * Erase specified part of eeprom, writing 0xFF.
278 * \param addr starting address
279 * \param count length of block to erase
281 void eeprom_erase(e2addr_t addr, size_t count)
283 uint8_t buf[EEPROM_BLKSIZE];
284 memset(buf, 0xFF, sizeof buf);
286 // Clear all but struct hw_info at start of eeprom
289 // Long operation, reset watchdog
292 size_t size = MIN(count, sizeof buf);
293 eeprom_write(addr, buf, size);
301 * Initialize TWI module.
303 void eeprom_init(void)
313 void eeprom_test(void)
315 static const char magic[14] = "Humpty Dumpty";
316 char buf[sizeof magic];
319 // Write something to EEPROM using unaligned sequential writes
320 for (i = 0; i < 42; ++i)
323 eeprom_write(i * sizeof magic, magic, sizeof magic);
326 // Read back with single-byte reads
327 for (i = 0; i < 42 * sizeof magic; ++i)
330 eeprom_read(i, buf, 1);
331 kprintf("EEPROM byte read: %c (%d)\n", buf[0], buf[0]);
332 ASSERT(buf[0] == magic[i % sizeof magic]);
335 // Read back again using sequential reads
336 for (i = 0; i < 42; ++i)
339 memset(buf, 0, sizeof buf);
340 eeprom_read(i * sizeof magic, buf, sizeof magic);
341 kprintf("EEPROM seq read @ 0x%x: '%s'\n", i * sizeof magic, buf);
342 ASSERT(memcmp(buf, magic, sizeof magic) == 0);