4 * Copyright 2003, 2004 Develer S.r.l. (http://www.develer.com/)
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.11 2004/10/26 08:35:31 bernie
20 *#* Reset watchdog for long operations.
22 *#* Revision 1.10 2004/09/20 03:31:22 bernie
25 *#* Revision 1.9 2004/09/14 21:03:46 bernie
26 *#* Use debug.h instead of kdebug.h.
28 *#* Revision 1.8 2004/08/25 14:12:08 rasky
29 *#* Aggiornato il comment block dei log RCS
31 *#* Revision 1.7 2004/08/24 16:48:40 bernie
32 *#* Note reason for including <macros.h>
34 *#* Revision 1.6 2004/08/24 14:27:20 bernie
37 *#* Revision 1.5 2004/08/24 13:46:48 bernie
38 *#* Include <macros.h>.
40 *#* Revision 1.4 2004/08/10 06:57:22 bernie
41 *#* eeprom_erase(): New function.
43 *#* Revision 1.3 2004/07/29 22:57:09 bernie
44 *#* Add 24LC16 support.
46 *#* Revision 1.2 2004/07/22 01:24:43 bernie
47 *#* Document AVR dependency.
49 *#* Revision 1.1 2004/07/20 17:11:18 bernie
50 *#* Import into DevLib.
57 #include <mware/byteorder.h> /* cpu_to_be16() */
60 #include <macros.h> // MIN()
62 #include <string.h> // memset()
67 /* Wait for TWINT flag set: bus is ready */
68 #define WAIT_TWI_READY do {} while (!(TWCR & BV(TWINT)))
70 /*! \name EEPROM control codes */
78 * Send START condition on the bus.
80 * \return true on success, false otherwise.
82 static bool twi_start(void)
84 TWCR = BV(TWINT) | BV(TWSTA) | BV(TWEN);
87 if (TW_STATUS == TW_START || TW_STATUS == TW_REP_START)
90 DB(kprintf("!TW_(REP)START: %x\n", TWSR);)
96 * Send START condition and select slave for write.
98 * \return true on success, false otherwise.
100 static bool twi_start_w(uint8_t slave_addr)
102 ASSERT(slave_addr < 8);
105 * Loop on the select write sequence: when the eeprom is busy
106 * writing previously sent data it will reply to the SLA_W
107 * control byte with a NACK. In this case, we must
108 * keep trying until the eeprom responds with an ACK.
112 TWDR = SLA_W | (slave_addr << 1);
113 TWCR = BV(TWINT) | BV(TWEN);
116 if (TW_STATUS == TW_MT_SLA_ACK)
118 else if (TW_STATUS != TW_MT_SLA_NACK)
120 DB(kprintf("!TW_MT_SLA_(N)ACK: %x\n", TWSR);)
130 * Send START condition and select slave for read.
132 * \return true on success, false otherwise.
134 static bool twi_start_r(uint8_t slave_addr)
136 ASSERT(slave_addr < 8);
140 TWDR = SLA_R | (slave_addr << 1);
141 TWCR = BV(TWINT) | BV(TWEN);
144 if (TW_STATUS == TW_MR_SLA_ACK)
147 DB(kprintf("!TW_MR_SLA_ACK: %x\n", TWSR);)
155 * Send STOP condition.
157 static void twi_stop(void)
159 TWCR = BV(TWINT) | BV(TWEN) | BV(TWSTO);
164 * Send a sequence of bytes in master transmitter mode
165 * to the selected slave device through the TWI bus.
167 * \return true on success, false on error.
169 static bool twi_send(const void *_buf, size_t count)
171 const uint8_t *buf = (const uint8_t *)_buf;
176 TWCR = BV(TWINT) | BV(TWEN);
178 if (TW_STATUS != TW_MT_DATA_ACK)
180 DB(kprintf("!TW_MT_DATA_ACK: %x\n", TWSR);)
190 * Receive a sequence of one or more bytes from the
191 * selected slave device in master receive mode through
194 * Received data is placed in \c buf.
196 * \return true on success, false on error
198 static bool twi_recv(void *_buf, size_t count)
200 uint8_t *buf = (uint8_t *)_buf;
203 * When reading the last byte the TWEA bit is not
204 * set, and the eeprom should answer with NACK
208 TWCR = BV(TWINT) | BV(TWEN) | (count ? BV(TWEA) : 0);
213 if (TW_STATUS != TW_MR_DATA_ACK)
215 DB(kprintf("!TW_MR_DATA_ACK: %x\n", TWSR);)
221 if (TW_STATUS != TW_MR_DATA_NACK)
223 DB(kprintf("!TW_MR_DATA_NACK: %x\n", TWSR);)
234 * Copy \c count bytes from buffer \c buf to
235 * eeprom at address \c addr.
237 bool eeprom_write(e2addr_t addr, const void *buf, size_t count)
240 ASSERT(addr + count <= EEPROM_SIZE);
242 while (count && result)
245 * Split write in multiple sequential mode operations that
246 * don't cross page boundaries.
249 MIN(count, (size_t)(EEPROM_BLKSIZE - (addr & (EEPROM_BLKSIZE - 1))));
251 #if CONFIG_EEPROM_TYPE == EEPROM_24XX16
253 * The 24LC16 uses the slave address as a 3-bit
256 uint8_t blk_addr = (uint8_t)((addr >> 8) & 0x07);
257 uint8_t blk_offs = (uint8_t)addr;
260 twi_start_w(blk_addr)
261 && twi_send(&blk_offs, sizeof blk_offs)
262 && twi_send(buf, size);
264 #elif CONFIG_EEPROM_TYPE == EEPROM_24XX256
266 // 24LC256 wants big-endian addresses
267 uint16_t addr_be = cpu_to_be16(addr);
271 && twi_send((uint8_t *)&addr_be, sizeof addr_be)
272 && twi_send(buf, size);
275 #error Unknown device type
281 //kprintf("addr=%d, count=%d, size=%d, *#?=%d\n",
282 // addr, count, size,
283 // (EEPROM_BLKSIZE - (addr & (EEPROM_BLKSIZE - 1)))
286 /* Update count and addr for next operation */
289 buf = ((const char *)buf) + size;
297 * Copy \c count bytes at address \c addr
298 * from eeprom to RAM to buffer \c buf.
300 * \return true on success.
302 bool eeprom_read(e2addr_t addr, void *buf, size_t count)
304 ASSERT(addr + count <= EEPROM_SIZE);
306 #if CONFIG_EEPROM_TYPE == EEPROM_24XX16
308 * The 24LC16 uses the slave address as a 3-bit
311 uint8_t blk_addr = (uint8_t)((addr >> 8) & 0x07);
312 uint8_t blk_offs = (uint8_t)addr;
315 twi_start_w(blk_addr)
316 && twi_send(&blk_offs, sizeof blk_offs)
317 && twi_start_r(blk_addr)
318 && twi_recv(buf, count);
320 #elif CONFIG_EEPROM_TYPE == EEPROM_24XX256
322 // 24LC256 wants big-endian addresses
323 addr = cpu_to_be16(addr);
327 && twi_send((uint8_t *)&addr, sizeof(addr))
329 && twi_recv(buf, count);
331 #error Unknown device type
341 * Write a single character \a c at address \a addr.
343 bool eeprom_write_char(e2addr_t addr, char c)
345 return eeprom_write(addr, &c, 1);
350 * Read a single character at address \a addr.
352 * \return the requested character or -1 in case of failure.
354 int eeprom_read_char(e2addr_t addr)
358 if (eeprom_read(addr, &c, 1))
366 * Erase specified part of eeprom, writing 0xFF.
368 * \param addr starting address
369 * \param count length of block to erase
371 void eeprom_erase(e2addr_t addr, size_t count)
373 uint8_t buf[EEPROM_BLKSIZE];
374 memset(buf, 0xFF, sizeof buf);
376 // Clear all but struct hw_info at start of eeprom
379 // Long operation, reset watchdog
382 size_t size = MIN(count, sizeof buf);
383 eeprom_write(addr, buf, size);
391 * Initialize TWI module.
393 void eeprom_init(void)
396 DISABLE_IRQSAVE(flags);
398 #if defined(__AVR_ATmega64__)
399 PORTD |= BV(PD0) | BV(PD1);
400 DDRD |= BV(PD0) | BV(PD1);
401 #elif defined(__AVR_ATmega8__)
402 PORTC |= BV(PC4) | BV(PC5);
403 DDRC |= BV(PC4) | BV(PC5);
405 #error Unsupported architecture
410 * F = CLOCK_FREQ / (16 + 2*TWBR * 4^TWPS)
412 #define TWI_FREQ 300000 /* 300 kHz */
413 #define TWI_PRESC 1 /* 4 ^ TWPS */
415 TWBR = (CLOCK_FREQ / (2 * TWI_FREQ * TWI_PRESC)) - (8 / TWI_PRESC);
419 ENABLE_IRQRESTORE(flags);
427 void eeprom_test(void)
429 static const char magic[14] = "Humpty Dumpty";
430 char buf[sizeof magic];
433 // Write something to EEPROM using unaligned sequential writes
434 for (i = 0; i < 42; ++i)
435 eeprom_write(i * sizeof magic, magic, sizeof magic);
437 // Read back with single-byte reads
438 for (i = 0; i < 42 * sizeof magic; ++i)
440 eeprom_read(i, buf, 1);
441 kprintf("EEPROM byte read: %c (%d)\n", buf[0], buf[0]);
442 ASSERT(buf[0] == magic[i % sizeof magic]);
445 // Read back again using sequential reads
446 for (i = 0; i < 42; ++i)
448 memset(buf, 0, sizeof buf);
449 eeprom_read(i * sizeof magic, buf, sizeof magic);
450 kprintf("EEPROM seq read @ 0x%x: '%s'\n", i * sizeof magic, buf);
451 ASSERT(memcmp(buf, magic, sizeof magic) == 0);