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.10 2004/09/20 03:31:22 bernie
22 *#* Revision 1.9 2004/09/14 21:03:46 bernie
23 *#* Use debug.h instead of kdebug.h.
25 *#* Revision 1.8 2004/08/25 14:12:08 rasky
26 *#* Aggiornato il comment block dei log RCS
28 *#* Revision 1.7 2004/08/24 16:48:40 bernie
29 *#* Note reason for including <macros.h>
31 *#* Revision 1.6 2004/08/24 14:27:20 bernie
34 *#* Revision 1.5 2004/08/24 13:46:48 bernie
35 *#* Include <macros.h>.
37 *#* Revision 1.4 2004/08/10 06:57:22 bernie
38 *#* eeprom_erase(): New function.
40 *#* Revision 1.3 2004/07/29 22:57:09 bernie
41 *#* Add 24LC16 support.
43 *#* Revision 1.2 2004/07/22 01:24:43 bernie
44 *#* Document AVR dependency.
46 *#* Revision 1.1 2004/07/20 17:11:18 bernie
47 *#* Import into DevLib.
53 #include <mware/byteorder.h> /* cpu_to_be16() */
56 #include <macros.h> // MIN()
58 #include <string.h> // memset()
63 /* Wait for TWINT flag set: bus is ready */
64 #define WAIT_TWI_READY do {} while (!(TWCR & BV(TWINT)))
66 /*! \name EEPROM control codes */
74 * Send START condition on the bus.
76 * \return true on success, false otherwise.
78 static bool twi_start(void)
80 TWCR = BV(TWINT) | BV(TWSTA) | BV(TWEN);
83 if (TW_STATUS == TW_START || TW_STATUS == TW_REP_START)
86 DB(kprintf("!TW_(REP)START: %x\n", TWSR);)
92 * Send START condition and select slave for write.
94 * \return true on success, false otherwise.
96 static bool twi_start_w(uint8_t slave_addr)
98 ASSERT(slave_addr < 8);
101 * Loop on the select write sequence: when the eeprom is busy
102 * writing previously sent data it will reply to the SLA_W
103 * control byte with a NACK. In this case, we must
104 * keep trying until the eeprom responds with an ACK.
108 TWDR = SLA_W | (slave_addr << 1);
109 TWCR = BV(TWINT) | BV(TWEN);
112 if (TW_STATUS == TW_MT_SLA_ACK)
114 else if (TW_STATUS != TW_MT_SLA_NACK)
116 DB(kprintf("!TW_MT_SLA_(N)ACK: %x\n", TWSR);)
126 * Send START condition and select slave for read.
128 * \return true on success, false otherwise.
130 static bool twi_start_r(uint8_t slave_addr)
132 ASSERT(slave_addr < 8);
136 TWDR = SLA_R | (slave_addr << 1);
137 TWCR = BV(TWINT) | BV(TWEN);
140 if (TW_STATUS == TW_MR_SLA_ACK)
143 DB(kprintf("!TW_MR_SLA_ACK: %x\n", TWSR);)
151 * Send STOP condition.
153 static void twi_stop(void)
155 TWCR = BV(TWINT) | BV(TWEN) | BV(TWSTO);
160 * Send a sequence of bytes in master transmitter mode
161 * to the selected slave device through the TWI bus.
163 * \return true on success, false on error.
165 static bool twi_send(const void *_buf, size_t count)
167 const uint8_t *buf = (const uint8_t *)_buf;
172 TWCR = BV(TWINT) | BV(TWEN);
174 if (TW_STATUS != TW_MT_DATA_ACK)
176 DB(kprintf("!TW_MT_DATA_ACK: %x\n", TWSR);)
186 * Receive a sequence of one or more bytes from the
187 * selected slave device in master receive mode through
190 * Received data is placed in \c buf.
192 * \return true on success, false on error
194 static bool twi_recv(void *_buf, size_t count)
196 uint8_t *buf = (uint8_t *)_buf;
199 * When reading the last byte the TWEA bit is not
200 * set, and the eeprom should answer with NACK
204 TWCR = BV(TWINT) | BV(TWEN) | (count ? BV(TWEA) : 0);
209 if (TW_STATUS != TW_MR_DATA_ACK)
211 DB(kprintf("!TW_MR_DATA_ACK: %x\n", TWSR);)
217 if (TW_STATUS != TW_MR_DATA_NACK)
219 DB(kprintf("!TW_MR_DATA_NACK: %x\n", TWSR);)
230 * Copy \c count bytes from buffer \c buf to
231 * eeprom at address \c addr.
233 bool eeprom_write(e2addr_t addr, const void *buf, size_t count)
236 ASSERT(addr + count <= EEPROM_SIZE);
238 while (count && result)
241 * Split write in multiple sequential mode operations that
242 * don't cross page boundaries.
245 MIN(count, (size_t)(EEPROM_BLKSIZE - (addr & (EEPROM_BLKSIZE - 1))));
247 #if CONFIG_EEPROM_TYPE == EEPROM_24XX16
249 * The 24LC16 uses the slave address as a 3-bit
252 uint8_t blk_addr = (uint8_t)((addr >> 8) & 0x07);
253 uint8_t blk_offs = (uint8_t)addr;
256 twi_start_w(blk_addr)
257 && twi_send(&blk_offs, sizeof blk_offs)
258 && twi_send(buf, size);
260 #elif CONFIG_EEPROM_TYPE == EEPROM_24XX256
262 // 24LC256 wants big-endian addresses
263 uint16_t addr_be = cpu_to_be16(addr);
267 && twi_send((uint8_t *)&addr_be, sizeof addr_be)
268 && twi_send(buf, size);
271 #error Unknown device type
277 //kprintf("addr=%d, count=%d, size=%d, *#?=%d\n",
278 // addr, count, size,
279 // (EEPROM_BLKSIZE - (addr & (EEPROM_BLKSIZE - 1)))
282 /* Update count and addr for next operation */
285 buf = ((const char *)buf) + size;
293 * Copy \c count bytes at address \c addr
294 * from eeprom to RAM to buffer \c buf.
296 bool eeprom_read(e2addr_t addr, void *buf, size_t count)
298 ASSERT(addr + count <= EEPROM_SIZE);
300 #if CONFIG_EEPROM_TYPE == EEPROM_24XX16
302 * The 24LC16 uses the slave address as a 3-bit
305 uint8_t blk_addr = (uint8_t)((addr >> 8) & 0x07);
306 uint8_t blk_offs = (uint8_t)addr;
309 twi_start_w(blk_addr)
310 && twi_send(&blk_offs, sizeof blk_offs)
311 && twi_start_r(blk_addr)
312 && twi_recv(buf, count);
314 #elif CONFIG_EEPROM_TYPE == EEPROM_24XX256
316 // 24LC256 wants big-endian addresses
317 addr = cpu_to_be16(addr);
321 && twi_send((uint8_t *)&addr, sizeof(addr))
323 && twi_recv(buf, count);
325 #error Unknown device type
335 * Write a single character \a c at address \a addr.
337 bool eeprom_write_char(e2addr_t addr, char c)
339 return eeprom_write(addr, &c, 1);
344 * Read a single character at address \a addr.
346 * \return the requested character or -1 in case of failure.
348 int eeprom_read_char(e2addr_t addr)
352 if (eeprom_read(addr, &c, 1))
360 * Erase specified part of eeprom, writing 0xFF.
362 * \param addr starting address
363 * \param count length of block to erase
365 void eeprom_erase(e2addr_t addr, size_t count)
367 uint8_t buf[EEPROM_BLKSIZE];
368 memset(buf, 0xFF, sizeof buf);
370 // Clear all but struct hw_info at start of eeprom
373 size_t size = MIN(count, sizeof buf);
374 eeprom_write(addr, buf, size);
382 * Initialize TWI module.
384 void eeprom_init(void)
387 DISABLE_IRQSAVE(flags);
389 #if defined(__AVR_ATmega64__)
390 PORTD |= BV(PD0) | BV(PD1);
391 DDRD |= BV(PD0) | BV(PD1);
392 #elif defined(__AVR_ATmega8__)
393 PORTC |= BV(PC4) | BV(PC5);
394 DDRC |= BV(PC4) | BV(PC5);
396 #error Unsupported architecture
401 * F = CLOCK_FREQ / (16 + 2*TWBR * 4^TWPS)
403 #define TWI_FREQ 300000 /* 300 kHz */
404 #define TWI_PRESC 1 /* 4 ^ TWPS */
406 TWBR = (CLOCK_FREQ / (2 * TWI_FREQ * TWI_PRESC)) - (8 / TWI_PRESC);
410 ENABLE_IRQRESTORE(flags);
418 void eeprom_test(void)
420 static const char magic[14] = "Humpty Dumpty";
421 char buf[sizeof magic];
424 // Write something to EEPROM using unaligned sequential writes
425 for (i = 0; i < 42; ++i)
426 eeprom_write(i * sizeof magic, magic, sizeof magic);
428 // Read back with single-byte reads
429 for (i = 0; i < 42 * sizeof magic; ++i)
431 eeprom_read(i, buf, 1);
432 kprintf("EEPROM byte read: %c (%d)\n", buf[0], buf[0]);
433 ASSERT(buf[0] == magic[i % sizeof magic]);
436 // Read back again using sequential reads
437 for (i = 0; i < 42; ++i)
439 memset(buf, 0, sizeof buf);
440 eeprom_read(i * sizeof magic, buf, sizeof magic);
441 kprintf("EEPROM seq read @ 0x%x: '%s'\n", i * sizeof magic, buf);
442 ASSERT(memcmp(buf, magic, sizeof magic) == 0);