4 * Copyright 2003, 2004, 2005 Develer S.r.l. (http://www.develer.com/)
5 * This file is part of DevLib - See README.devlib for information.
8 * \brief Driver for the 24xx16 and 24xx256 I2C EEPROMS (implementation)
10 * \note This implementation is AVR specific.
13 * \author Stefano Fedrigo <aleph@develer.com>
14 * \author Bernardo Innocenti <bernie@develer.com>
19 *#* Revision 1.16 2005/03/01 23:25:09 bernie
22 *#* Revision 1.11 2004/10/26 08:35:31 bernie
23 *#* Reset watchdog for long operations.
25 *#* Revision 1.10 2004/09/20 03:31:22 bernie
28 *#* Revision 1.9 2004/09/14 21:03:46 bernie
29 *#* Use debug.h instead of kdebug.h.
35 #include <config.h> // CONFIG_EEPROM_VERIFY
36 #include <macros.h> // MIN()
39 #include <mware/byteorder.h> // cpu_to_be16()
41 #include <string.h> // memset()
44 // Configuration sanity checks
45 #if !defined(CONFIG_EEPROM_VERIFY) || (CONFIG_EEPROM_VERIFY != 0 && CONFIG_EEPROM_VERIFY != 1)
46 #error CONFIG_EEPROM_VERIFY must be defined to either 0 or 1
51 * Copy \c count bytes from buffer \c buf to
52 * eeprom at address \c addr.
54 static bool eeprom_writeRaw(e2addr_t addr, const void *buf, size_t count)
57 ASSERT(addr + count <= EEPROM_SIZE);
59 while (count && result)
62 * Split write in multiple sequential mode operations that
63 * don't cross page boundaries.
66 MIN(count, (size_t)(EEPROM_BLKSIZE - (addr & (EEPROM_BLKSIZE - 1))));
68 #if CONFIG_EEPROM_TYPE == EEPROM_24XX16
70 * The 24LC16 uses the slave address as a 3-bit
73 uint8_t blk_addr = (uint8_t)((addr >> 8) & 0x07);
74 uint8_t blk_offs = (uint8_t)addr;
78 && twi_send(&blk_offs, sizeof blk_offs)
79 && twi_send(buf, size);
81 #elif CONFIG_EEPROM_TYPE == EEPROM_24XX256
83 // 24LC256 wants big-endian addresses
84 uint16_t addr_be = cpu_to_be16(addr);
88 && twi_send((uint8_t *)&addr_be, sizeof addr_be)
89 && twi_send(buf, size);
92 #error Unknown device type
98 //kprintf("addr=%d, count=%d, size=%d, *#?=%d\n",
100 // (EEPROM_BLKSIZE - (addr & (EEPROM_BLKSIZE - 1)))
103 /* Update count and addr for next operation */
106 buf = ((const char *)buf) + size;
110 TRACEMSG("Write error!");
115 #if CONFIG_EEPROM_VERIFY
117 * Check that the contents of an EEPROM range
118 * match with a provided data buffer.
120 * \return true on success.
122 static bool eeprom_verify(e2addr_t addr, const void *buf, size_t count)
124 uint8_t verify_buf[16];
127 while (count && result)
129 /* Split read in smaller pieces */
130 size_t size = MIN(count, sizeof verify_buf);
132 /* Read back buffer */
133 if (eeprom_read(addr, verify_buf, size))
135 if (memcmp(buf, verify_buf, size) != 0)
137 TRACEMSG("Data mismatch!");
143 TRACEMSG("Read error!");
147 /* Update count and addr for next operation */
150 buf = ((const char *)buf) + size;
155 #endif /* CONFIG_EEPROM_VERIFY */
158 bool eeprom_write(e2addr_t addr, const void *buf, size_t count)
160 #if CONFIG_EEPROM_VERIFY
164 if (eeprom_writeRaw(addr, buf, count)
165 && eeprom_verify(addr, buf, count))
170 #else /* !CONFIG_EEPROM_VERIFY */
171 return eeprom_writeRaw(addr, buf, count);
172 #endif /* !CONFIG_EEPROM_VERIFY */
177 * Copy \c count bytes at address \c addr
178 * from eeprom to RAM to buffer \c buf.
180 * \return true on success.
182 bool eeprom_read(e2addr_t addr, void *buf, size_t count)
184 ASSERT(addr + count <= EEPROM_SIZE);
186 #if CONFIG_EEPROM_TYPE == EEPROM_24XX16
188 * The 24LC16 uses the slave address as a 3-bit
191 uint8_t blk_addr = (uint8_t)((addr >> 8) & 0x07);
192 uint8_t blk_offs = (uint8_t)addr;
195 twi_start_w(blk_addr)
196 && twi_send(&blk_offs, sizeof blk_offs)
197 && twi_start_r(blk_addr)
198 && twi_recv(buf, count);
200 #elif CONFIG_EEPROM_TYPE == EEPROM_24XX256
202 // 24LC256 wants big-endian addresses
203 addr = cpu_to_be16(addr);
207 && twi_send((uint8_t *)&addr, sizeof(addr))
209 && twi_recv(buf, count);
211 #error Unknown device type
217 TRACEMSG("Read error!");
223 * Write a single character \a c at address \a addr.
225 bool eeprom_write_char(e2addr_t addr, char c)
227 return eeprom_write(addr, &c, 1);
232 * Read a single character at address \a addr.
234 * \return the requested character or -1 in case of failure.
236 int eeprom_read_char(e2addr_t addr)
240 if (eeprom_read(addr, &c, 1))
248 * Erase specified part of eeprom, writing 0xFF.
250 * \param addr starting address
251 * \param count length of block to erase
253 void eeprom_erase(e2addr_t addr, size_t count)
255 uint8_t buf[EEPROM_BLKSIZE];
256 memset(buf, 0xFF, sizeof buf);
258 // Clear all but struct hw_info at start of eeprom
261 // Long operation, reset watchdog
264 size_t size = MIN(count, sizeof buf);
265 eeprom_write(addr, buf, size);
273 * Initialize TWI module.
275 void eeprom_init(void)
285 void eeprom_test(void)
287 static const char magic[14] = "Humpty Dumpty";
288 char buf[sizeof magic];
291 // Write something to EEPROM using unaligned sequential writes
292 for (i = 0; i < 42; ++i)
295 eeprom_write(i * sizeof magic, magic, sizeof magic);
298 // Read back with single-byte reads
299 for (i = 0; i < 42 * sizeof magic; ++i)
302 eeprom_read(i, buf, 1);
303 kprintf("EEPROM byte read: %c (%d)\n", buf[0], buf[0]);
304 ASSERT(buf[0] == magic[i % sizeof magic]);
307 // Read back again using sequential reads
308 for (i = 0; i < 42; ++i)
311 memset(buf, 0, sizeof buf);
312 eeprom_read(i * sizeof magic, buf, sizeof magic);
313 kprintf("EEPROM seq read @ 0x%x: '%s'\n", i * sizeof magic, buf);
314 ASSERT(memcmp(buf, magic, sizeof magic) == 0);