4 * Copyright 2003, 2004 Develer S.r.l. (http://www.develer.com/)
10 * \author Stefano Fedrigo <aleph@develer.com>
11 * \author Bernardo Innocenti <bernie@develer.com>
13 * \brief Driver for the 24xx16 and 24xx256 I2C EEPROMS (implementation)
15 * \note This implementation is AVR specific.
20 * Revision 1.3 2004/07/29 22:57:09 bernie
23 * Revision 1.2 2004/07/22 01:24:43 bernie
24 * Document AVR dependency.
26 * Revision 1.1 2004/07/20 17:11:18 bernie
31 #include <mware/byteorder.h> /* cpu_to_be16() */
32 #include <drv/kdebug.h>
37 /* Wait for TWINT flag set: bus is ready */
38 #define WAIT_TWI_READY do {} while (!(TWCR & BV(TWINT)))
40 /*! \name EEPROM control codes */
48 * Send START condition on the bus.
50 * \return true on success, false otherwise.
52 static bool twi_start(void)
54 TWCR = BV(TWINT) | BV(TWSTA) | BV(TWEN);
57 if (TW_STATUS == TW_START || TW_STATUS == TW_REP_START)
60 DB(kprintf("!TW_(REP)START: %x\n", TWSR);)
66 * Send START condition and select slave for write.
68 * \return true on success, false otherwise.
70 static bool twi_start_w(uint8_t slave_addr)
72 ASSERT(slave_addr < 8);
75 * Loop on the select write sequence: when the eeprom is busy
76 * writing previously sent data it will reply to the SLA_W
77 * control byte with a NACK. In this case, we must
78 * keep trying until the eeprom responds with an ACK.
82 TWDR = SLA_W | (slave_addr << 1);
83 TWCR = BV(TWINT) | BV(TWEN);
86 if (TW_STATUS == TW_MT_SLA_ACK)
88 else if (TW_STATUS != TW_MT_SLA_NACK)
90 DB(kprintf("!TW_MT_SLA_(N)ACK: %x\n", TWSR);)
100 * Send START condition and select slave for read.
102 * \return true on success, false otherwise.
104 static bool twi_start_r(uint8_t slave_addr)
106 ASSERT(slave_addr < 8);
110 TWDR = SLA_R | (slave_addr << 1);
111 TWCR = BV(TWINT) | BV(TWEN);
114 if (TW_STATUS == TW_MR_SLA_ACK)
117 DB(kprintf("!TW_MR_SLA_ACK: %x\n", TWSR);)
125 * Send STOP condition.
127 static void twi_stop(void)
129 TWCR = BV(TWINT) | BV(TWEN) | BV(TWSTO);
134 * Send a sequence of bytes in master transmitter mode
135 * to the selected slave device through the TWI bus.
137 * \return true on success, false on error.
139 static bool twi_send(const uint8_t *buf, size_t count)
144 TWCR = BV(TWINT) | BV(TWEN);
146 if (TW_STATUS != TW_MT_DATA_ACK)
148 DB(kprintf("!TW_MT_DATA_ACK: %x\n", TWSR);)
158 * Receive a sequence of one or more bytes from the
159 * selected slave device in master receive mode through
162 * Received data is placed in \c buf.
164 * \return true on success, false on error
166 static bool twi_recv(uint8_t *buf, size_t count)
169 * When reading the last byte the TWEA bit is not
170 * set, and the eeprom should answer with NACK
174 TWCR = BV(TWINT) | BV(TWEN) | (count ? BV(TWEA) : 0);
179 if (TW_STATUS != TW_MR_DATA_ACK)
181 DB(kprintf("!TW_MR_DATA_ACK: %x\n", TWSR);)
187 if (TW_STATUS != TW_MR_DATA_NACK)
189 DB(kprintf("!TW_MR_DATA_NACK: %x\n", TWSR);)
200 * Copy \c count bytes from buffer \c buf to
201 * eeprom at address \c addr.
203 bool eeprom_write(e2addr_t addr, const void *buf, size_t count)
206 ASSERT(addr + count <= EEPROM_SIZE);
208 while (count && result)
211 * Split write in multiple sequential mode operations that
212 * don't cross page boundaries.
215 MIN(count, (size_t)(EEPROM_BLKSIZE - (addr & (EEPROM_BLKSIZE - 1))));
217 #if CONFIG_EEPROM_TYPE == EEPROM_24XX16
219 * The 24LC16 uses the slave address as a 3-bit
222 uint8_t blk_addr = (uint8_t)((addr >> 8) & 0x07);
223 uint8_t blk_offs = (uint8_t)addr;
226 twi_start_w(blk_addr)
227 && twi_send(&blk_offs, sizeof blk_offs)
228 && twi_send(buf, size);
230 #elif CONFIG_EEPROM_TYPE == EEPROM_24XX256
232 // 24LC256 wants big-endian addresses
233 uint16_t addr_be = cpu_to_be16(addr);
237 && twi_send((uint8_t *)&addr_be, sizeof addr_be)
238 && twi_send(buf, size);
241 #error Unknown device type
247 //kprintf("addr=%d, count=%d, size=%d, *#?=%d\n",
248 // addr, count, size,
249 // (EEPROM_BLKSIZE - (addr & (EEPROM_BLKSIZE - 1)))
252 /* Update count and addr for next operation */
255 buf = ((const char *)buf) + size;
263 * Copy \c count bytes at address \c addr
264 * from eeprom to RAM to buffer \c buf.
266 bool eeprom_read(e2addr_t addr, void *buf, size_t count)
268 ASSERT(addr + count <= EEPROM_SIZE);
270 #if CONFIG_EEPROM_TYPE == EEPROM_24XX16
272 * The 24LC16 uses the slave address as a 3-bit
275 uint8_t blk_addr = (uint8_t)((addr >> 8) & 0x07);
276 uint8_t blk_offs = (uint8_t)addr;
279 twi_start_w(blk_addr)
280 && twi_send(&blk_offs, sizeof blk_offs)
281 && twi_start_r(blk_addr)
282 && twi_recv(buf, count);
284 #elif CONFIG_EEPROM_TYPE == EEPROM_24XX256
286 // 24LC256 wants big-endian addresses
287 addr = cpu_to_be16(addr);
291 && twi_send((uint8_t *)&addr, sizeof(addr))
293 && twi_recv(buf, count);
295 #error Unknown device type
305 * Write a single character \a c at address \a addr.
307 bool eeprom_write_char(e2addr_t addr, char c)
309 return eeprom_write(addr, &c, 1);
314 * Read a single character at address \a addr.
316 * \return the requested character or -1 in case of failure.
318 int eeprom_read_char(e2addr_t addr)
322 if (eeprom_read(addr, &c, 1))
330 * Initialize TWI module.
332 void eeprom_init(void)
335 DISABLE_IRQSAVE(flags);
337 #if defined(__AVR_ATmega64__)
338 PORTD |= BV(PD0) | BV(PD1);
339 DDRD |= BV(PD0) | BV(PD1);
340 #elif defined(__AVR_ATmega8__)
341 PORTC |= BV(PC4) | BV(PC5);
342 DDRC |= BV(PC4) | BV(PC5);
344 #error Unsupported architecture
349 * F = CLOCK_FREQ / (16 + 2*TWBR * 4^TWPS)
351 #define TWI_FREQ 300000 /* 300 kHz */
352 #define TWI_PRESC 1 /* 4 ^ TWPS */
354 TWBR = (CLOCK_FREQ / (2 * TWI_FREQ * TWI_PRESC)) - (8 / TWI_PRESC);
358 ENABLE_IRQRESTORE(flags);
366 void eeprom_test(void)
368 static const char magic[13] = "Humpty Dumpty";
369 char buf[sizeof magic + 1];
372 // Write something to EEPROM using unaligned sequential writes
373 for (i = 0; i < 42; ++i)
374 eeprom_write(i * sizeof magic, magic, sizeof magic);
376 // Read back with single-byte reads
377 for (i = 0; i < 42 * sizeof magic; ++i)
379 eeprom_read(i, buf, 1);
380 kprintf("EEPROM byte read: %c (%d)\n", buf[0], buf[0]);
381 ASSERT(buf[0] == magic[i % sizeof magic]);
384 // Read back again using sequential reads
385 for (i = 0; i < 42; ++i)
387 memset(buf, 0, sizeof buf);
388 eeprom_read(i * sizeof magic, buf, sizeof magic);
389 kprintf("EEPROM seq read @ 0x%x: '%s'\n", i * sizeof magic, buf);
390 ASSERT(memcmp(buf, magic, sizeof magic) == 0);