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.4 2004/08/10 06:57:22 bernie
21 * eeprom_erase(): New function.
23 * Revision 1.3 2004/07/29 22:57:09 bernie
26 * Revision 1.2 2004/07/22 01:24:43 bernie
27 * Document AVR dependency.
29 * Revision 1.1 2004/07/20 17:11:18 bernie
34 #include <mware/byteorder.h> /* cpu_to_be16() */
35 #include <drv/kdebug.h>
37 #include <string.h> // memset()
41 /* Wait for TWINT flag set: bus is ready */
42 #define WAIT_TWI_READY do {} while (!(TWCR & BV(TWINT)))
44 /*! \name EEPROM control codes */
52 * Send START condition on the bus.
54 * \return true on success, false otherwise.
56 static bool twi_start(void)
58 TWCR = BV(TWINT) | BV(TWSTA) | BV(TWEN);
61 if (TW_STATUS == TW_START || TW_STATUS == TW_REP_START)
64 DB(kprintf("!TW_(REP)START: %x\n", TWSR);)
70 * Send START condition and select slave for write.
72 * \return true on success, false otherwise.
74 static bool twi_start_w(uint8_t slave_addr)
76 ASSERT(slave_addr < 8);
79 * Loop on the select write sequence: when the eeprom is busy
80 * writing previously sent data it will reply to the SLA_W
81 * control byte with a NACK. In this case, we must
82 * keep trying until the eeprom responds with an ACK.
86 TWDR = SLA_W | (slave_addr << 1);
87 TWCR = BV(TWINT) | BV(TWEN);
90 if (TW_STATUS == TW_MT_SLA_ACK)
92 else if (TW_STATUS != TW_MT_SLA_NACK)
94 DB(kprintf("!TW_MT_SLA_(N)ACK: %x\n", TWSR);)
104 * Send START condition and select slave for read.
106 * \return true on success, false otherwise.
108 static bool twi_start_r(uint8_t slave_addr)
110 ASSERT(slave_addr < 8);
114 TWDR = SLA_R | (slave_addr << 1);
115 TWCR = BV(TWINT) | BV(TWEN);
118 if (TW_STATUS == TW_MR_SLA_ACK)
121 DB(kprintf("!TW_MR_SLA_ACK: %x\n", TWSR);)
129 * Send STOP condition.
131 static void twi_stop(void)
133 TWCR = BV(TWINT) | BV(TWEN) | BV(TWSTO);
138 * Send a sequence of bytes in master transmitter mode
139 * to the selected slave device through the TWI bus.
141 * \return true on success, false on error.
143 static bool twi_send(const uint8_t *buf, size_t count)
148 TWCR = BV(TWINT) | BV(TWEN);
150 if (TW_STATUS != TW_MT_DATA_ACK)
152 DB(kprintf("!TW_MT_DATA_ACK: %x\n", TWSR);)
162 * Receive a sequence of one or more bytes from the
163 * selected slave device in master receive mode through
166 * Received data is placed in \c buf.
168 * \return true on success, false on error
170 static bool twi_recv(uint8_t *buf, size_t count)
173 * When reading the last byte the TWEA bit is not
174 * set, and the eeprom should answer with NACK
178 TWCR = BV(TWINT) | BV(TWEN) | (count ? BV(TWEA) : 0);
183 if (TW_STATUS != TW_MR_DATA_ACK)
185 DB(kprintf("!TW_MR_DATA_ACK: %x\n", TWSR);)
191 if (TW_STATUS != TW_MR_DATA_NACK)
193 DB(kprintf("!TW_MR_DATA_NACK: %x\n", TWSR);)
204 * Copy \c count bytes from buffer \c buf to
205 * eeprom at address \c addr.
207 bool eeprom_write(e2addr_t addr, const void *buf, size_t count)
210 ASSERT(addr + count <= EEPROM_SIZE);
212 while (count && result)
215 * Split write in multiple sequential mode operations that
216 * don't cross page boundaries.
219 MIN(count, (size_t)(EEPROM_BLKSIZE - (addr & (EEPROM_BLKSIZE - 1))));
221 #if CONFIG_EEPROM_TYPE == EEPROM_24XX16
223 * The 24LC16 uses the slave address as a 3-bit
226 uint8_t blk_addr = (uint8_t)((addr >> 8) & 0x07);
227 uint8_t blk_offs = (uint8_t)addr;
230 twi_start_w(blk_addr)
231 && twi_send(&blk_offs, sizeof blk_offs)
232 && twi_send(buf, size);
234 #elif CONFIG_EEPROM_TYPE == EEPROM_24XX256
236 // 24LC256 wants big-endian addresses
237 uint16_t addr_be = cpu_to_be16(addr);
241 && twi_send((uint8_t *)&addr_be, sizeof addr_be)
242 && twi_send(buf, size);
245 #error Unknown device type
251 //kprintf("addr=%d, count=%d, size=%d, *#?=%d\n",
252 // addr, count, size,
253 // (EEPROM_BLKSIZE - (addr & (EEPROM_BLKSIZE - 1)))
256 /* Update count and addr for next operation */
259 buf = ((const char *)buf) + size;
267 * Copy \c count bytes at address \c addr
268 * from eeprom to RAM to buffer \c buf.
270 bool eeprom_read(e2addr_t addr, void *buf, size_t count)
272 ASSERT(addr + count <= EEPROM_SIZE);
274 #if CONFIG_EEPROM_TYPE == EEPROM_24XX16
276 * The 24LC16 uses the slave address as a 3-bit
279 uint8_t blk_addr = (uint8_t)((addr >> 8) & 0x07);
280 uint8_t blk_offs = (uint8_t)addr;
283 twi_start_w(blk_addr)
284 && twi_send(&blk_offs, sizeof blk_offs)
285 && twi_start_r(blk_addr)
286 && twi_recv(buf, count);
288 #elif CONFIG_EEPROM_TYPE == EEPROM_24XX256
290 // 24LC256 wants big-endian addresses
291 addr = cpu_to_be16(addr);
295 && twi_send((uint8_t *)&addr, sizeof(addr))
297 && twi_recv(buf, count);
299 #error Unknown device type
309 * Write a single character \a c at address \a addr.
311 bool eeprom_write_char(e2addr_t addr, char c)
313 return eeprom_write(addr, &c, 1);
318 * Read a single character at address \a addr.
320 * \return the requested character or -1 in case of failure.
322 int eeprom_read_char(e2addr_t addr)
326 if (eeprom_read(addr, &c, 1))
334 * Erase specified part of eeprom, writing 0xFF.
336 * \param addr starting address
337 * \param len length of block to erase
339 void eeprom_erase(e2addr_t addr, size_t count)
341 uint8_t buf[EEPROM_BLKSIZE];
342 memset(buf, 0xFF, sizeof buf);
344 // Clear all but struct hw_info at start of eeprom
347 size_t size = MIN(count, sizeof buf);
348 eeprom_write(addr, buf, size);
356 * Initialize TWI module.
358 void eeprom_init(void)
361 DISABLE_IRQSAVE(flags);
363 #if defined(__AVR_ATmega64__)
364 PORTD |= BV(PD0) | BV(PD1);
365 DDRD |= BV(PD0) | BV(PD1);
366 #elif defined(__AVR_ATmega8__)
367 PORTC |= BV(PC4) | BV(PC5);
368 DDRC |= BV(PC4) | BV(PC5);
370 #error Unsupported architecture
375 * F = CLOCK_FREQ / (16 + 2*TWBR * 4^TWPS)
377 #define TWI_FREQ 300000 /* 300 kHz */
378 #define TWI_PRESC 1 /* 4 ^ TWPS */
380 TWBR = (CLOCK_FREQ / (2 * TWI_FREQ * TWI_PRESC)) - (8 / TWI_PRESC);
384 ENABLE_IRQRESTORE(flags);
392 void eeprom_test(void)
394 static const char magic[13] = "Humpty Dumpty";
395 char buf[sizeof magic + 1];
398 // Write something to EEPROM using unaligned sequential writes
399 for (i = 0; i < 42; ++i)
400 eeprom_write(i * sizeof magic, magic, sizeof magic);
402 // Read back with single-byte reads
403 for (i = 0; i < 42 * sizeof magic; ++i)
405 eeprom_read(i, buf, 1);
406 kprintf("EEPROM byte read: %c (%d)\n", buf[0], buf[0]);
407 ASSERT(buf[0] == magic[i % sizeof magic]);
410 // Read back again using sequential reads
411 for (i = 0; i < 42; ++i)
413 memset(buf, 0, sizeof buf);
414 eeprom_read(i * sizeof magic, buf, sizeof magic);
415 kprintf("EEPROM seq read @ 0x%x: '%s'\n", i * sizeof magic, buf);
416 ASSERT(memcmp(buf, magic, sizeof magic) == 0);