4 * Copyright 2003, 2004 Develer S.r.l. (http://www.develer.com/)
5 * This file is part of DevLib - See devlib/README 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.15 2005/01/06 16:09:40 aleph
20 *#* Split twi/eeprom functions from eeprom module in separate twi module
22 *#* Revision 1.14 2004/12/13 12:07:06 bernie
23 *#* DISABLE_IRQSAVE/ENABLE_IRQRESTORE: Convert to IRQ_SAVE_DISABLE/IRQ_RESTORE.
25 *#* Revision 1.13 2004/11/16 20:58:51 bernie
28 *#* Revision 1.12 2004/11/02 17:50:01 bernie
29 *#* CONFIG_EEPROM_VERIFY: New config option.
31 *#* Revision 1.11 2004/10/26 08:35:31 bernie
32 *#* Reset watchdog for long operations.
34 *#* Revision 1.10 2004/09/20 03:31:22 bernie
37 *#* Revision 1.9 2004/09/14 21:03:46 bernie
38 *#* Use debug.h instead of kdebug.h.
40 *#* Revision 1.8 2004/08/25 14:12:08 rasky
41 *#* Aggiornato il comment block dei log RCS
43 *#* Revision 1.7 2004/08/24 16:48:40 bernie
44 *#* Note reason for including <macros.h>
46 *#* Revision 1.6 2004/08/24 14:27:20 bernie
49 *#* Revision 1.5 2004/08/24 13:46:48 bernie
50 *#* Include <macros.h>.
52 *#* Revision 1.4 2004/08/10 06:57:22 bernie
53 *#* eeprom_erase(): New function.
55 *#* Revision 1.3 2004/07/29 22:57:09 bernie
56 *#* Add 24LC16 support.
58 *#* Revision 1.2 2004/07/22 01:24:43 bernie
59 *#* Document AVR dependency.
61 *#* Revision 1.1 2004/07/20 17:11:18 bernie
62 *#* Import into DevLib.
69 #include <config.h> // CONFIG_EEPROM_VERIFY
70 #include <macros.h> // MIN()
73 #include <mware/byteorder.h> // cpu_to_be16()
75 #include <string.h> // memset()
78 // Configuration sanity checks
79 #if !defined(CONFIG_EEPROM_VERIFY) || (CONFIG_EEPROM_VERIFY != 0 && CONFIG_EEPROM_VERIFY != 1)
80 #error CONFIG_EEPROM_VERIFY must be defined to either 0 or 1
85 * Copy \c count bytes from buffer \c buf to
86 * eeprom at address \c addr.
88 static bool eeprom_writeRaw(e2addr_t addr, const void *buf, size_t count)
91 ASSERT(addr + count <= EEPROM_SIZE);
93 while (count && result)
96 * Split write in multiple sequential mode operations that
97 * don't cross page boundaries.
100 MIN(count, (size_t)(EEPROM_BLKSIZE - (addr & (EEPROM_BLKSIZE - 1))));
102 #if CONFIG_EEPROM_TYPE == EEPROM_24XX16
104 * The 24LC16 uses the slave address as a 3-bit
107 uint8_t blk_addr = (uint8_t)((addr >> 8) & 0x07);
108 uint8_t blk_offs = (uint8_t)addr;
111 twi_start_w(blk_addr)
112 && twi_send(&blk_offs, sizeof blk_offs)
113 && twi_send(buf, size);
115 #elif CONFIG_EEPROM_TYPE == EEPROM_24XX256
117 // 24LC256 wants big-endian addresses
118 uint16_t addr_be = cpu_to_be16(addr);
122 && twi_send((uint8_t *)&addr_be, sizeof addr_be)
123 && twi_send(buf, size);
126 #error Unknown device type
132 //kprintf("addr=%d, count=%d, size=%d, *#?=%d\n",
133 // addr, count, size,
134 // (EEPROM_BLKSIZE - (addr & (EEPROM_BLKSIZE - 1)))
137 /* Update count and addr for next operation */
140 buf = ((const char *)buf) + size;
144 TRACEMSG("Write error!");
149 #if CONFIG_EEPROM_VERIFY
151 * Check that the contents of an EEPROM range
152 * match with a provided data buffer.
154 * \return true on success.
156 static bool eeprom_verify(e2addr_t addr, const void *buf, size_t count)
158 uint8_t verify_buf[16];
161 while (count && result)
163 /* Split read in smaller pieces */
164 size_t size = MIN(count, sizeof verify_buf);
166 /* Read back buffer */
167 if (eeprom_read(addr, verify_buf, size))
169 if (memcmp(buf, verify_buf, size) != 0)
171 TRACEMSG("Data mismatch!");
177 TRACEMSG("Read error!");
181 /* Update count and addr for next operation */
184 buf = ((const char *)buf) + size;
189 #endif /* CONFIG_EEPROM_VERIFY */
192 bool eeprom_write(e2addr_t addr, const void *buf, size_t count)
194 #if CONFIG_EEPROM_VERIFY
198 if (eeprom_writeRaw(addr, buf, count)
199 && eeprom_verify(addr, buf, count))
204 #else /* !CONFIG_EEPROM_VERIFY */
205 return eeprom_writeRaw(addr, buf, count);
206 #endif /* !CONFIG_EEPROM_VERIFY */
211 * Copy \c count bytes at address \c addr
212 * from eeprom to RAM to buffer \c buf.
214 * \return true on success.
216 bool eeprom_read(e2addr_t addr, void *buf, size_t count)
218 ASSERT(addr + count <= EEPROM_SIZE);
220 #if CONFIG_EEPROM_TYPE == EEPROM_24XX16
222 * The 24LC16 uses the slave address as a 3-bit
225 uint8_t blk_addr = (uint8_t)((addr >> 8) & 0x07);
226 uint8_t blk_offs = (uint8_t)addr;
229 twi_start_w(blk_addr)
230 && twi_send(&blk_offs, sizeof blk_offs)
231 && twi_start_r(blk_addr)
232 && twi_recv(buf, count);
234 #elif CONFIG_EEPROM_TYPE == EEPROM_24XX256
236 // 24LC256 wants big-endian addresses
237 addr = cpu_to_be16(addr);
241 && twi_send((uint8_t *)&addr, sizeof(addr))
243 && twi_recv(buf, count);
245 #error Unknown device type
251 TRACEMSG("Read error!");
257 * Write a single character \a c at address \a addr.
259 bool eeprom_write_char(e2addr_t addr, char c)
261 return eeprom_write(addr, &c, 1);
266 * Read a single character at address \a addr.
268 * \return the requested character or -1 in case of failure.
270 int eeprom_read_char(e2addr_t addr)
274 if (eeprom_read(addr, &c, 1))
282 * Erase specified part of eeprom, writing 0xFF.
284 * \param addr starting address
285 * \param count length of block to erase
287 void eeprom_erase(e2addr_t addr, size_t count)
289 uint8_t buf[EEPROM_BLKSIZE];
290 memset(buf, 0xFF, sizeof buf);
292 // Clear all but struct hw_info at start of eeprom
295 // Long operation, reset watchdog
298 size_t size = MIN(count, sizeof buf);
299 eeprom_write(addr, buf, size);
307 * Initialize TWI module.
309 void eeprom_init(void)
319 void eeprom_test(void)
321 static const char magic[14] = "Humpty Dumpty";
322 char buf[sizeof magic];
325 // Write something to EEPROM using unaligned sequential writes
326 for (i = 0; i < 42; ++i)
329 eeprom_write(i * sizeof magic, magic, sizeof magic);
332 // Read back with single-byte reads
333 for (i = 0; i < 42 * sizeof magic; ++i)
336 eeprom_read(i, buf, 1);
337 kprintf("EEPROM byte read: %c (%d)\n", buf[0], buf[0]);
338 ASSERT(buf[0] == magic[i % sizeof magic]);
341 // Read back again using sequential reads
342 for (i = 0; i < 42; ++i)
345 memset(buf, 0, sizeof buf);
346 eeprom_read(i * sizeof magic, buf, sizeof magic);
347 kprintf("EEPROM seq read @ 0x%x: '%s'\n", i * sizeof magic, buf);
348 ASSERT(memcmp(buf, magic, sizeof magic) == 0);