PROGMEM: Define to nothing for non-Harvard processors.
[bertos.git] / drv / eeprom.c
index cf5da31ad7f2d8d3d81b993965f16648bdf2d3bb..57861376151b1151ee38817421cd8961273e169d 100755 (executable)
 
 /*#*
  *#* $Log$
+ *#* Revision 1.14  2004/12/13 12:07:06  bernie
+ *#* DISABLE_IRQSAVE/ENABLE_IRQRESTORE: Convert to IRQ_SAVE_DISABLE/IRQ_RESTORE.
+ *#*
+ *#* Revision 1.13  2004/11/16 20:58:51  bernie
+ *#* Add write verify.
+ *#*
  *#* Revision 1.12  2004/11/02 17:50:01  bernie
  *#* CONFIG_EEPROM_VERIFY: New config option.
  *#*
@@ -60,6 +66,7 @@
 #include <mware/byteorder.h> /* cpu_to_be16() */
 #include <debug.h>
 #include <hw.h>
+#include <cpu.h>     // IRQ_SAVE_DISABLE(), IRQ_RESTORE()
 #include <config.h>  // CONFIG_EEPROM_VERIFY
 #include <macros.h>  // MIN()
 
@@ -298,6 +305,8 @@ static bool eeprom_writeRaw(e2addr_t addr, const void *buf, size_t count)
                buf = ((const char *)buf) + size;
        }
 
+       if (!result)
+               TRACEMSG("Write error!");
        return result;
 }
 
@@ -306,6 +315,8 @@ static bool eeprom_writeRaw(e2addr_t addr, const void *buf, size_t count)
 /*!
  * Check that the contents of an EEPROM range
  * match with a provided data buffer.
+ *
+ * \return true on success.
  */
 static bool eeprom_verify(e2addr_t addr, const void *buf, size_t count)
 {
@@ -322,13 +333,13 @@ static bool eeprom_verify(e2addr_t addr, const void *buf, size_t count)
                {
                        if (memcmp(buf, verify_buf, size) != 0)
                        {
-                               TRACEMSG("Data mismatch!\n");
+                               TRACEMSG("Data mismatch!");
                                result = false;
                        }
                }
                else
                {
-                       TRACEMSG("Read error!\n");
+                       TRACEMSG("Read error!");
                        result = false;
                }
 
@@ -401,6 +412,8 @@ bool eeprom_read(e2addr_t addr, void *buf, size_t count)
 
        twi_stop();
 
+       if (!res)
+               TRACEMSG("Read error!");
        return res;
 }
 
@@ -461,8 +474,15 @@ void eeprom_erase(e2addr_t addr, size_t count)
 void eeprom_init(void)
 {
        cpuflags_t flags;
-       DISABLE_IRQSAVE(flags);
+       IRQ_SAVE_DISABLE(flags);
 
+       /*
+        * This is pretty useless according to AVR's datasheet,
+        * but it helps us driving the TWI data lines on boards
+        * where the bus pull-up resistors are missing.  This is
+        * probably due to some unwanted interaction between the
+        * port pin and the TWI lines.
+        */
 #if defined(__AVR_ATmega64__)
        PORTD |= BV(PD0) | BV(PD1);
        DDRD |= BV(PD0) | BV(PD1);
@@ -477,14 +497,14 @@ void eeprom_init(void)
         * Set speed:
         * F = CLOCK_FREQ / (16 + 2*TWBR * 4^TWPS)
         */
-       #define TWI_FREQ  300000  /* 300 kHz */
+       #define TWI_FREQ  100000  /* ~100 kHz */
        #define TWI_PRESC 1       /* 4 ^ TWPS */
 
        TWBR = (CLOCK_FREQ / (2 * TWI_FREQ * TWI_PRESC)) - (8 / TWI_PRESC);
        TWSR = 0;
        TWCR = BV(TWEN);
 
-       ENABLE_IRQRESTORE(flags);
+       IRQ_RESTORE(flags);
 }