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.12 2004/11/02 17:50:01 bernie
20 *#* CONFIG_EEPROM_VERIFY: New config option.
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.
31 *#* Revision 1.8 2004/08/25 14:12:08 rasky
32 *#* Aggiornato il comment block dei log RCS
34 *#* Revision 1.7 2004/08/24 16:48:40 bernie
35 *#* Note reason for including <macros.h>
37 *#* Revision 1.6 2004/08/24 14:27:20 bernie
40 *#* Revision 1.5 2004/08/24 13:46:48 bernie
41 *#* Include <macros.h>.
43 *#* Revision 1.4 2004/08/10 06:57:22 bernie
44 *#* eeprom_erase(): New function.
46 *#* Revision 1.3 2004/07/29 22:57:09 bernie
47 *#* Add 24LC16 support.
49 *#* Revision 1.2 2004/07/22 01:24:43 bernie
50 *#* Document AVR dependency.
52 *#* Revision 1.1 2004/07/20 17:11:18 bernie
53 *#* Import into DevLib.
60 #include <mware/byteorder.h> /* cpu_to_be16() */
63 #include <config.h> // CONFIG_EEPROM_VERIFY
64 #include <macros.h> // MIN()
66 #include <string.h> // memset()
70 // Configuration sanity checks
71 #if !defined(CONFIG_EEPROM_VERIFY) || (CONFIG_EEPROM_VERIFY != 0 && CONFIG_EEPROM_VERIFY != 1)
72 #error CONFIG_EEPROM_VERIFY must be defined to either 0 or 1
76 /* Wait for TWINT flag set: bus is ready */
77 #define WAIT_TWI_READY do {} while (!(TWCR & BV(TWINT)))
79 /*! \name EEPROM control codes */
87 * Send START condition on the bus.
89 * \return true on success, false otherwise.
91 static bool twi_start(void)
93 TWCR = BV(TWINT) | BV(TWSTA) | BV(TWEN);
96 if (TW_STATUS == TW_START || TW_STATUS == TW_REP_START)
99 DB(kprintf("!TW_(REP)START: %x\n", TWSR);)
105 * Send START condition and select slave for write.
107 * \return true on success, false otherwise.
109 static bool twi_start_w(uint8_t slave_addr)
111 ASSERT(slave_addr < 8);
114 * Loop on the select write sequence: when the eeprom is busy
115 * writing previously sent data it will reply to the SLA_W
116 * control byte with a NACK. In this case, we must
117 * keep trying until the eeprom responds with an ACK.
121 TWDR = SLA_W | (slave_addr << 1);
122 TWCR = BV(TWINT) | BV(TWEN);
125 if (TW_STATUS == TW_MT_SLA_ACK)
127 else if (TW_STATUS != TW_MT_SLA_NACK)
129 DB(kprintf("!TW_MT_SLA_(N)ACK: %x\n", TWSR);)
139 * Send START condition and select slave for read.
141 * \return true on success, false otherwise.
143 static bool twi_start_r(uint8_t slave_addr)
145 ASSERT(slave_addr < 8);
149 TWDR = SLA_R | (slave_addr << 1);
150 TWCR = BV(TWINT) | BV(TWEN);
153 if (TW_STATUS == TW_MR_SLA_ACK)
156 DB(kprintf("!TW_MR_SLA_ACK: %x\n", TWSR);)
164 * Send STOP condition.
166 static void twi_stop(void)
168 TWCR = BV(TWINT) | BV(TWEN) | BV(TWSTO);
173 * Send a sequence of bytes in master transmitter mode
174 * to the selected slave device through the TWI bus.
176 * \return true on success, false on error.
178 static bool twi_send(const void *_buf, size_t count)
180 const uint8_t *buf = (const uint8_t *)_buf;
185 TWCR = BV(TWINT) | BV(TWEN);
187 if (TW_STATUS != TW_MT_DATA_ACK)
189 DB(kprintf("!TW_MT_DATA_ACK: %x\n", TWSR);)
199 * Receive a sequence of one or more bytes from the
200 * selected slave device in master receive mode through
203 * Received data is placed in \c buf.
205 * \return true on success, false on error
207 static bool twi_recv(void *_buf, size_t count)
209 uint8_t *buf = (uint8_t *)_buf;
212 * When reading the last byte the TWEA bit is not
213 * set, and the eeprom should answer with NACK
217 TWCR = BV(TWINT) | BV(TWEN) | (count ? BV(TWEA) : 0);
222 if (TW_STATUS != TW_MR_DATA_ACK)
224 DB(kprintf("!TW_MR_DATA_ACK: %x\n", TWSR);)
230 if (TW_STATUS != TW_MR_DATA_NACK)
232 DB(kprintf("!TW_MR_DATA_NACK: %x\n", TWSR);)
243 * Copy \c count bytes from buffer \c buf to
244 * eeprom at address \c addr.
246 static bool eeprom_writeRaw(e2addr_t addr, const void *buf, size_t count)
249 ASSERT(addr + count <= EEPROM_SIZE);
251 while (count && result)
254 * Split write in multiple sequential mode operations that
255 * don't cross page boundaries.
258 MIN(count, (size_t)(EEPROM_BLKSIZE - (addr & (EEPROM_BLKSIZE - 1))));
260 #if CONFIG_EEPROM_TYPE == EEPROM_24XX16
262 * The 24LC16 uses the slave address as a 3-bit
265 uint8_t blk_addr = (uint8_t)((addr >> 8) & 0x07);
266 uint8_t blk_offs = (uint8_t)addr;
269 twi_start_w(blk_addr)
270 && twi_send(&blk_offs, sizeof blk_offs)
271 && twi_send(buf, size);
273 #elif CONFIG_EEPROM_TYPE == EEPROM_24XX256
275 // 24LC256 wants big-endian addresses
276 uint16_t addr_be = cpu_to_be16(addr);
280 && twi_send((uint8_t *)&addr_be, sizeof addr_be)
281 && twi_send(buf, size);
284 #error Unknown device type
290 //kprintf("addr=%d, count=%d, size=%d, *#?=%d\n",
291 // addr, count, size,
292 // (EEPROM_BLKSIZE - (addr & (EEPROM_BLKSIZE - 1)))
295 /* Update count and addr for next operation */
298 buf = ((const char *)buf) + size;
305 #if CONFIG_EEPROM_VERIFY
307 * Check that the contents of an EEPROM range
308 * match with a provided data buffer.
310 static bool eeprom_verify(e2addr_t addr, const void *buf, size_t count)
312 uint8_t verify_buf[16];
315 while (count && result)
317 /* Split read in smaller pieces */
318 size_t size = MIN(count, sizeof verify_buf);
320 /* Read back buffer */
321 if (eeprom_read(addr, verify_buf, size))
323 if (memcmp(buf, verify_buf, size) != 0)
325 TRACEMSG("Data mismatch!\n");
331 TRACEMSG("Read error!\n");
335 /* Update count and addr for next operation */
338 buf = ((const char *)buf) + size;
343 #endif /* CONFIG_EEPROM_VERIFY */
346 bool eeprom_write(e2addr_t addr, const void *buf, size_t count)
348 #if CONFIG_EEPROM_VERIFY
352 if (eeprom_writeRaw(addr, buf, count)
353 && eeprom_verify(addr, buf, count))
358 #else /* !CONFIG_EEPROM_VERIFY */
359 return eeprom_writeRaw(addr, buf, count);
360 #endif /* !CONFIG_EEPROM_VERIFY */
365 * Copy \c count bytes at address \c addr
366 * from eeprom to RAM to buffer \c buf.
368 * \return true on success.
370 bool eeprom_read(e2addr_t addr, void *buf, size_t count)
372 ASSERT(addr + count <= EEPROM_SIZE);
374 #if CONFIG_EEPROM_TYPE == EEPROM_24XX16
376 * The 24LC16 uses the slave address as a 3-bit
379 uint8_t blk_addr = (uint8_t)((addr >> 8) & 0x07);
380 uint8_t blk_offs = (uint8_t)addr;
383 twi_start_w(blk_addr)
384 && twi_send(&blk_offs, sizeof blk_offs)
385 && twi_start_r(blk_addr)
386 && twi_recv(buf, count);
388 #elif CONFIG_EEPROM_TYPE == EEPROM_24XX256
390 // 24LC256 wants big-endian addresses
391 addr = cpu_to_be16(addr);
395 && twi_send((uint8_t *)&addr, sizeof(addr))
397 && twi_recv(buf, count);
399 #error Unknown device type
409 * Write a single character \a c at address \a addr.
411 bool eeprom_write_char(e2addr_t addr, char c)
413 return eeprom_write(addr, &c, 1);
418 * Read a single character at address \a addr.
420 * \return the requested character or -1 in case of failure.
422 int eeprom_read_char(e2addr_t addr)
426 if (eeprom_read(addr, &c, 1))
434 * Erase specified part of eeprom, writing 0xFF.
436 * \param addr starting address
437 * \param count length of block to erase
439 void eeprom_erase(e2addr_t addr, size_t count)
441 uint8_t buf[EEPROM_BLKSIZE];
442 memset(buf, 0xFF, sizeof buf);
444 // Clear all but struct hw_info at start of eeprom
447 // Long operation, reset watchdog
450 size_t size = MIN(count, sizeof buf);
451 eeprom_write(addr, buf, size);
459 * Initialize TWI module.
461 void eeprom_init(void)
464 DISABLE_IRQSAVE(flags);
466 #if defined(__AVR_ATmega64__)
467 PORTD |= BV(PD0) | BV(PD1);
468 DDRD |= BV(PD0) | BV(PD1);
469 #elif defined(__AVR_ATmega8__)
470 PORTC |= BV(PC4) | BV(PC5);
471 DDRC |= BV(PC4) | BV(PC5);
473 #error Unsupported architecture
478 * F = CLOCK_FREQ / (16 + 2*TWBR * 4^TWPS)
480 #define TWI_FREQ 300000 /* 300 kHz */
481 #define TWI_PRESC 1 /* 4 ^ TWPS */
483 TWBR = (CLOCK_FREQ / (2 * TWI_FREQ * TWI_PRESC)) - (8 / TWI_PRESC);
487 ENABLE_IRQRESTORE(flags);
495 void eeprom_test(void)
497 static const char magic[14] = "Humpty Dumpty";
498 char buf[sizeof magic];
501 // Write something to EEPROM using unaligned sequential writes
502 for (i = 0; i < 42; ++i)
503 eeprom_write(i * sizeof magic, magic, sizeof magic);
505 // Read back with single-byte reads
506 for (i = 0; i < 42 * sizeof magic; ++i)
508 eeprom_read(i, buf, 1);
509 kprintf("EEPROM byte read: %c (%d)\n", buf[0], buf[0]);
510 ASSERT(buf[0] == magic[i % sizeof magic]);
513 // Read back again using sequential reads
514 for (i = 0; i < 42; ++i)
516 memset(buf, 0, sizeof buf);
517 eeprom_read(i * sizeof magic, buf, sizeof magic);
518 kprintf("EEPROM seq read @ 0x%x: '%s'\n", i * sizeof magic, buf);
519 ASSERT(memcmp(buf, magic, sizeof magic) == 0);