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.17 2005/04/11 19:10:27 bernie
20 *#* Include top-level headers from cfg/ subdir.
22 *#* Revision 1.16 2005/03/01 23:25:09 bernie
25 *#* Revision 1.11 2004/10/26 08:35:31 bernie
26 *#* Reset watchdog for long operations.
28 *#* Revision 1.10 2004/09/20 03:31:22 bernie
31 *#* Revision 1.9 2004/09/14 21:03:46 bernie
32 *#* Use debug.h instead of kdebug.h.
37 #include <cfg/debug.h>
38 #include <cfg/config.h> // CONFIG_EEPROM_VERIFY
39 #include <cfg/macros.h> // MIN()
42 #include <mware/byteorder.h> // cpu_to_be16()
44 #include <string.h> // memset()
47 // Configuration sanity checks
48 #if !defined(CONFIG_EEPROM_VERIFY) || (CONFIG_EEPROM_VERIFY != 0 && CONFIG_EEPROM_VERIFY != 1)
49 #error CONFIG_EEPROM_VERIFY must be defined to either 0 or 1
54 * Copy \c count bytes from buffer \c buf to
55 * eeprom at address \c addr.
57 static bool eeprom_writeRaw(e2addr_t addr, const void *buf, size_t count)
60 ASSERT(addr + count <= EEPROM_SIZE);
62 while (count && result)
65 * Split write in multiple sequential mode operations that
66 * don't cross page boundaries.
69 MIN(count, (size_t)(EEPROM_BLKSIZE - (addr & (EEPROM_BLKSIZE - 1))));
71 #if CONFIG_EEPROM_TYPE == EEPROM_24XX16
73 * The 24LC16 uses the slave address as a 3-bit
76 uint8_t blk_addr = (uint8_t)((addr >> 8) & 0x07);
77 uint8_t blk_offs = (uint8_t)addr;
81 && twi_send(&blk_offs, sizeof blk_offs)
82 && twi_send(buf, size);
84 #elif CONFIG_EEPROM_TYPE == EEPROM_24XX256
86 // 24LC256 wants big-endian addresses
87 uint16_t addr_be = cpu_to_be16(addr);
91 && twi_send((uint8_t *)&addr_be, sizeof addr_be)
92 && twi_send(buf, size);
95 #error Unknown device type
101 //kprintf("addr=%d, count=%d, size=%d, *#?=%d\n",
102 // addr, count, size,
103 // (EEPROM_BLKSIZE - (addr & (EEPROM_BLKSIZE - 1)))
106 /* Update count and addr for next operation */
109 buf = ((const char *)buf) + size;
113 TRACEMSG("Write error!");
118 #if CONFIG_EEPROM_VERIFY
120 * Check that the contents of an EEPROM range
121 * match with a provided data buffer.
123 * \return true on success.
125 static bool eeprom_verify(e2addr_t addr, const void *buf, size_t count)
127 uint8_t verify_buf[16];
130 while (count && result)
132 /* Split read in smaller pieces */
133 size_t size = MIN(count, sizeof verify_buf);
135 /* Read back buffer */
136 if (eeprom_read(addr, verify_buf, size))
138 if (memcmp(buf, verify_buf, size) != 0)
140 TRACEMSG("Data mismatch!");
146 TRACEMSG("Read error!");
150 /* Update count and addr for next operation */
153 buf = ((const char *)buf) + size;
158 #endif /* CONFIG_EEPROM_VERIFY */
161 bool eeprom_write(e2addr_t addr, const void *buf, size_t count)
163 #if CONFIG_EEPROM_VERIFY
167 if (eeprom_writeRaw(addr, buf, count)
168 && eeprom_verify(addr, buf, count))
173 #else /* !CONFIG_EEPROM_VERIFY */
174 return eeprom_writeRaw(addr, buf, count);
175 #endif /* !CONFIG_EEPROM_VERIFY */
180 * Copy \c count bytes at address \c addr
181 * from eeprom to RAM to buffer \c buf.
183 * \return true on success.
185 bool eeprom_read(e2addr_t addr, void *buf, size_t count)
187 ASSERT(addr + count <= EEPROM_SIZE);
189 #if CONFIG_EEPROM_TYPE == EEPROM_24XX16
191 * The 24LC16 uses the slave address as a 3-bit
194 uint8_t blk_addr = (uint8_t)((addr >> 8) & 0x07);
195 uint8_t blk_offs = (uint8_t)addr;
198 twi_start_w(blk_addr)
199 && twi_send(&blk_offs, sizeof blk_offs)
200 && twi_start_r(blk_addr)
201 && twi_recv(buf, count);
203 #elif CONFIG_EEPROM_TYPE == EEPROM_24XX256
205 // 24LC256 wants big-endian addresses
206 addr = cpu_to_be16(addr);
210 && twi_send((uint8_t *)&addr, sizeof(addr))
212 && twi_recv(buf, count);
214 #error Unknown device type
220 TRACEMSG("Read error!");
226 * Write a single character \a c at address \a addr.
228 bool eeprom_write_char(e2addr_t addr, char c)
230 return eeprom_write(addr, &c, 1);
235 * Read a single character at address \a addr.
237 * \return the requested character or -1 in case of failure.
239 int eeprom_read_char(e2addr_t addr)
243 if (eeprom_read(addr, &c, 1))
251 * Erase specified part of eeprom, writing 0xFF.
253 * \param addr starting address
254 * \param count length of block to erase
256 void eeprom_erase(e2addr_t addr, size_t count)
258 uint8_t buf[EEPROM_BLKSIZE];
259 memset(buf, 0xFF, sizeof buf);
261 // Clear all but struct hw_info at start of eeprom
264 // Long operation, reset watchdog
267 size_t size = MIN(count, sizeof buf);
268 eeprom_write(addr, buf, size);
276 * Initialize TWI module.
278 void eeprom_init(void)
288 void eeprom_test(void)
290 static const char magic[14] = "Humpty Dumpty";
291 char buf[sizeof magic];
294 // Write something to EEPROM using unaligned sequential writes
295 for (i = 0; i < 42; ++i)
298 eeprom_write(i * sizeof magic, magic, sizeof magic);
301 // Read back with single-byte reads
302 for (i = 0; i < 42 * sizeof magic; ++i)
305 eeprom_read(i, buf, 1);
306 kprintf("EEPROM byte read: %c (%d)\n", buf[0], buf[0]);
307 ASSERT(buf[0] == magic[i % sizeof magic]);
310 // Read back again using sequential reads
311 for (i = 0; i < 42; ++i)
314 memset(buf, 0, sizeof buf);
315 eeprom_read(i * sizeof magic, buf, sizeof magic);
316 kprintf("EEPROM seq read @ 0x%x: '%s'\n", i * sizeof magic, buf);
317 ASSERT(memcmp(buf, magic, sizeof magic) == 0);