- */
-
-/*
- * $Log$
- * Revision 1.7 2004/08/24 16:48:40 bernie
- * Note reason for including <macros.h>
- *
- * Revision 1.6 2004/08/24 14:27:20 bernie
- * Doxygen fix.
- *
- * Revision 1.5 2004/08/24 13:46:48 bernie
- * Include <macros.h>.
- *
- * Revision 1.4 2004/08/10 06:57:22 bernie
- * eeprom_erase(): New function.
- *
- * Revision 1.3 2004/07/29 22:57:09 bernie
- * Add 24LC16 support.
- *
- * Revision 1.2 2004/07/22 01:24:43 bernie
- * Document AVR dependency.
- *
- * Revision 1.1 2004/07/20 17:11:18 bernie
- * Import into DevLib.
- *
- */
-
-#include "eeprom.h"
-
-#include <mware/byteorder.h> /* cpu_to_be16() */
-#include <drv/kdebug.h>
-#include <hw.h>
-#include <macros.h> // MIN()
-
-#include <string.h> // memset()
-
-#include <avr/twi.h>
-
-
-/* Wait for TWINT flag set: bus is ready */
-#define WAIT_TWI_READY do {} while (!(TWCR & BV(TWINT)))
-
-/*! \name EEPROM control codes */
-/*@{*/
-#define SLA_W 0xA0
-#define SLA_R 0xA1
-/*@}*/
-
-
-/*!
- * Send START condition on the bus.
- *
- * \return true on success, false otherwise.
- */
-static bool twi_start(void)
-{
- TWCR = BV(TWINT) | BV(TWSTA) | BV(TWEN);
- WAIT_TWI_READY;
-
- if (TW_STATUS == TW_START || TW_STATUS == TW_REP_START)
- return true;
-
- DB(kprintf("!TW_(REP)START: %x\n", TWSR);)
- return false;
-}
-
-
-/*!
- * Send START condition and select slave for write.
- *
- * \return true on success, false otherwise.
- */
-static bool twi_start_w(uint8_t slave_addr)
-{
- ASSERT(slave_addr < 8);
-
- /*
- * Loop on the select write sequence: when the eeprom is busy
- * writing previously sent data it will reply to the SLA_W
- * control byte with a NACK. In this case, we must
- * keep trying until the eeprom responds with an ACK.
- */
- while (twi_start())
- {
- TWDR = SLA_W | (slave_addr << 1);
- TWCR = BV(TWINT) | BV(TWEN);
- WAIT_TWI_READY;
-
- if (TW_STATUS == TW_MT_SLA_ACK)
- return true;
- else if (TW_STATUS != TW_MT_SLA_NACK)
- {
- DB(kprintf("!TW_MT_SLA_(N)ACK: %x\n", TWSR);)
- break;
- }
- }
-
- return false;
-}
-
-
-/*!
- * Send START condition and select slave for read.