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.6 2004/08/24 14:27:20 bernie
23 * Revision 1.5 2004/08/24 13:46:48 bernie
26 * Revision 1.4 2004/08/10 06:57:22 bernie
27 * eeprom_erase(): New function.
29 * Revision 1.3 2004/07/29 22:57:09 bernie
32 * Revision 1.2 2004/07/22 01:24:43 bernie
33 * Document AVR dependency.
35 * Revision 1.1 2004/07/20 17:11:18 bernie
42 #include <mware/byteorder.h> /* cpu_to_be16() */
43 #include <drv/kdebug.h>
47 #include <string.h> // memset()
52 /* Wait for TWINT flag set: bus is ready */
53 #define WAIT_TWI_READY do {} while (!(TWCR & BV(TWINT)))
55 /*! \name EEPROM control codes */
63 * Send START condition on the bus.
65 * \return true on success, false otherwise.
67 static bool twi_start(void)
69 TWCR = BV(TWINT) | BV(TWSTA) | BV(TWEN);
72 if (TW_STATUS == TW_START || TW_STATUS == TW_REP_START)
75 DB(kprintf("!TW_(REP)START: %x\n", TWSR);)
81 * Send START condition and select slave for write.
83 * \return true on success, false otherwise.
85 static bool twi_start_w(uint8_t slave_addr)
87 ASSERT(slave_addr < 8);
90 * Loop on the select write sequence: when the eeprom is busy
91 * writing previously sent data it will reply to the SLA_W
92 * control byte with a NACK. In this case, we must
93 * keep trying until the eeprom responds with an ACK.
97 TWDR = SLA_W | (slave_addr << 1);
98 TWCR = BV(TWINT) | BV(TWEN);
101 if (TW_STATUS == TW_MT_SLA_ACK)
103 else if (TW_STATUS != TW_MT_SLA_NACK)
105 DB(kprintf("!TW_MT_SLA_(N)ACK: %x\n", TWSR);)
115 * Send START condition and select slave for read.
117 * \return true on success, false otherwise.
119 static bool twi_start_r(uint8_t slave_addr)
121 ASSERT(slave_addr < 8);
125 TWDR = SLA_R | (slave_addr << 1);
126 TWCR = BV(TWINT) | BV(TWEN);
129 if (TW_STATUS == TW_MR_SLA_ACK)
132 DB(kprintf("!TW_MR_SLA_ACK: %x\n", TWSR);)
140 * Send STOP condition.
142 static void twi_stop(void)
144 TWCR = BV(TWINT) | BV(TWEN) | BV(TWSTO);
149 * Send a sequence of bytes in master transmitter mode
150 * to the selected slave device through the TWI bus.
152 * \return true on success, false on error.
154 static bool twi_send(const uint8_t *buf, size_t count)
159 TWCR = BV(TWINT) | BV(TWEN);
161 if (TW_STATUS != TW_MT_DATA_ACK)
163 DB(kprintf("!TW_MT_DATA_ACK: %x\n", TWSR);)
173 * Receive a sequence of one or more bytes from the
174 * selected slave device in master receive mode through
177 * Received data is placed in \c buf.
179 * \return true on success, false on error
181 static bool twi_recv(uint8_t *buf, size_t count)
184 * When reading the last byte the TWEA bit is not
185 * set, and the eeprom should answer with NACK
189 TWCR = BV(TWINT) | BV(TWEN) | (count ? BV(TWEA) : 0);
194 if (TW_STATUS != TW_MR_DATA_ACK)
196 DB(kprintf("!TW_MR_DATA_ACK: %x\n", TWSR);)
202 if (TW_STATUS != TW_MR_DATA_NACK)
204 DB(kprintf("!TW_MR_DATA_NACK: %x\n", TWSR);)
215 * Copy \c count bytes from buffer \c buf to
216 * eeprom at address \c addr.
218 bool eeprom_write(e2addr_t addr, const void *buf, size_t count)
221 ASSERT(addr + count <= EEPROM_SIZE);
223 while (count && result)
226 * Split write in multiple sequential mode operations that
227 * don't cross page boundaries.
230 MIN(count, (size_t)(EEPROM_BLKSIZE - (addr & (EEPROM_BLKSIZE - 1))));
232 #if CONFIG_EEPROM_TYPE == EEPROM_24XX16
234 * The 24LC16 uses the slave address as a 3-bit
237 uint8_t blk_addr = (uint8_t)((addr >> 8) & 0x07);
238 uint8_t blk_offs = (uint8_t)addr;
241 twi_start_w(blk_addr)
242 && twi_send(&blk_offs, sizeof blk_offs)
243 && twi_send(buf, size);
245 #elif CONFIG_EEPROM_TYPE == EEPROM_24XX256
247 // 24LC256 wants big-endian addresses
248 uint16_t addr_be = cpu_to_be16(addr);
252 && twi_send((uint8_t *)&addr_be, sizeof addr_be)
253 && twi_send(buf, size);
256 #error Unknown device type
262 //kprintf("addr=%d, count=%d, size=%d, *#?=%d\n",
263 // addr, count, size,
264 // (EEPROM_BLKSIZE - (addr & (EEPROM_BLKSIZE - 1)))
267 /* Update count and addr for next operation */
270 buf = ((const char *)buf) + size;
278 * Copy \c count bytes at address \c addr
279 * from eeprom to RAM to buffer \c buf.
281 bool eeprom_read(e2addr_t addr, void *buf, size_t count)
283 ASSERT(addr + count <= EEPROM_SIZE);
285 #if CONFIG_EEPROM_TYPE == EEPROM_24XX16
287 * The 24LC16 uses the slave address as a 3-bit
290 uint8_t blk_addr = (uint8_t)((addr >> 8) & 0x07);
291 uint8_t blk_offs = (uint8_t)addr;
294 twi_start_w(blk_addr)
295 && twi_send(&blk_offs, sizeof blk_offs)
296 && twi_start_r(blk_addr)
297 && twi_recv(buf, count);
299 #elif CONFIG_EEPROM_TYPE == EEPROM_24XX256
301 // 24LC256 wants big-endian addresses
302 addr = cpu_to_be16(addr);
306 && twi_send((uint8_t *)&addr, sizeof(addr))
308 && twi_recv(buf, count);
310 #error Unknown device type
320 * Write a single character \a c at address \a addr.
322 bool eeprom_write_char(e2addr_t addr, char c)
324 return eeprom_write(addr, &c, 1);
329 * Read a single character at address \a addr.
331 * \return the requested character or -1 in case of failure.
333 int eeprom_read_char(e2addr_t addr)
337 if (eeprom_read(addr, &c, 1))
345 * Erase specified part of eeprom, writing 0xFF.
347 * \param addr starting address
348 * \param count length of block to erase
350 void eeprom_erase(e2addr_t addr, size_t count)
352 uint8_t buf[EEPROM_BLKSIZE];
353 memset(buf, 0xFF, sizeof buf);
355 // Clear all but struct hw_info at start of eeprom
358 size_t size = MIN(count, sizeof buf);
359 eeprom_write(addr, buf, size);
367 * Initialize TWI module.
369 void eeprom_init(void)
372 DISABLE_IRQSAVE(flags);
374 #if defined(__AVR_ATmega64__)
375 PORTD |= BV(PD0) | BV(PD1);
376 DDRD |= BV(PD0) | BV(PD1);
377 #elif defined(__AVR_ATmega8__)
378 PORTC |= BV(PC4) | BV(PC5);
379 DDRC |= BV(PC4) | BV(PC5);
381 #error Unsupported architecture
386 * F = CLOCK_FREQ / (16 + 2*TWBR * 4^TWPS)
388 #define TWI_FREQ 300000 /* 300 kHz */
389 #define TWI_PRESC 1 /* 4 ^ TWPS */
391 TWBR = (CLOCK_FREQ / (2 * TWI_FREQ * TWI_PRESC)) - (8 / TWI_PRESC);
395 ENABLE_IRQRESTORE(flags);
403 void eeprom_test(void)
405 static const char magic[13] = "Humpty Dumpty";
406 char buf[sizeof magic + 1];
409 // Write something to EEPROM using unaligned sequential writes
410 for (i = 0; i < 42; ++i)
411 eeprom_write(i * sizeof magic, magic, sizeof magic);
413 // Read back with single-byte reads
414 for (i = 0; i < 42 * sizeof magic; ++i)
416 eeprom_read(i, buf, 1);
417 kprintf("EEPROM byte read: %c (%d)\n", buf[0], buf[0]);
418 ASSERT(buf[0] == magic[i % sizeof magic]);
421 // Read back again using sequential reads
422 for (i = 0; i < 42; ++i)
424 memset(buf, 0, sizeof buf);
425 eeprom_read(i * sizeof magic, buf, sizeof magic);
426 kprintf("EEPROM seq read @ 0x%x: '%s'\n", i * sizeof magic, buf);
427 ASSERT(memcmp(buf, magic, sizeof magic) == 0);