CONFIG_EEPROM_VERIFY: New config option.
authorbernie <bernie@38d2e660-2303-0410-9eaa-f027e97ec537>
Tue, 2 Nov 2004 17:50:02 +0000 (17:50 +0000)
committerbernie <bernie@38d2e660-2303-0410-9eaa-f027e97ec537>
Tue, 2 Nov 2004 17:50:02 +0000 (17:50 +0000)
git-svn-id: https://src.develer.com/svnoss/bertos/trunk@266 38d2e660-2303-0410-9eaa-f027e97ec537

drv/eeprom.c
drv/eeprom.h

index 0f301effa709c41d463398d9f45d5033ed69e849..cf5da31ad7f2d8d3d81b993965f16648bdf2d3bb 100755 (executable)
@@ -2,7 +2,7 @@
  * \file
  * <!--
  * Copyright 2003, 2004 Develer S.r.l. (http://www.develer.com/)
- * All Rights Reserved.
+ * This file is part of DevLib - See devlib/README for information.
  * -->
  *
  * \brief Driver for the 24xx16 and 24xx256 I2C EEPROMS (implementation)
@@ -16,6 +16,9 @@
 
 /*#*
  *#* $Log$
+ *#* Revision 1.12  2004/11/02 17:50:01  bernie
+ *#* CONFIG_EEPROM_VERIFY: New config option.
+ *#*
  *#* Revision 1.11  2004/10/26 08:35:31  bernie
  *#* Reset watchdog for long operations.
  *#*
 #include <mware/byteorder.h> /* cpu_to_be16() */
 #include <debug.h>
 #include <hw.h>
+#include <config.h>  // CONFIG_EEPROM_VERIFY
 #include <macros.h>  // MIN()
 
-#include <string.h> // memset()
+#include <string.h>  // memset()
 
 #include <avr/twi.h>
 
+// Configuration sanity checks
+#if !defined(CONFIG_EEPROM_VERIFY) || (CONFIG_EEPROM_VERIFY != 0 && CONFIG_EEPROM_VERIFY != 1)
+       #error CONFIG_EEPROM_VERIFY must be defined to either 0 or 1
+#endif
+
 
 /* Wait for TWINT flag set: bus is ready */
 #define WAIT_TWI_READY  do {} while (!(TWCR & BV(TWINT)))
@@ -234,7 +243,7 @@ static bool twi_recv(void *_buf, size_t count)
  * Copy \c count bytes from buffer \c buf to
  * eeprom at address \c addr.
  */
-bool eeprom_write(e2addr_t addr, const void *buf, size_t count)
+static bool eeprom_writeRaw(e2addr_t addr, const void *buf, size_t count)
 {
        bool result = true;
        ASSERT(addr + count <= EEPROM_SIZE);
@@ -293,6 +302,65 @@ bool eeprom_write(e2addr_t addr, const void *buf, size_t count)
 }
 
 
+#if CONFIG_EEPROM_VERIFY
+/*!
+ * Check that the contents of an EEPROM range
+ * match with a provided data buffer.
+ */
+static bool eeprom_verify(e2addr_t addr, const void *buf, size_t count)
+{
+       uint8_t verify_buf[16];
+       bool result = true;
+
+       while (count && result)
+       {
+               /* Split read in smaller pieces */
+               size_t size = MIN(count, sizeof verify_buf);
+
+               /* Read back buffer */
+               if (eeprom_read(addr, verify_buf, size))
+               {
+                       if (memcmp(buf, verify_buf, size) != 0)
+                       {
+                               TRACEMSG("Data mismatch!\n");
+                               result = false;
+                       }
+               }
+               else
+               {
+                       TRACEMSG("Read error!\n");
+                       result = false;
+               }
+
+               /* Update count and addr for next operation */
+               count -= size;
+               addr += size;
+               buf = ((const char *)buf) + size;
+       }
+
+       return result;
+}
+#endif /* CONFIG_EEPROM_VERIFY */
+
+
+bool eeprom_write(e2addr_t addr, const void *buf, size_t count)
+{
+#if CONFIG_EEPROM_VERIFY
+       int retries = 5;
+
+       while (retries--)
+               if (eeprom_writeRaw(addr, buf, count)
+                               && eeprom_verify(addr, buf, count))
+                       return true;
+
+       return false;
+
+#else /* !CONFIG_EEPROM_VERIFY */
+       return eeprom_writeRaw(addr, buf, count);
+#endif /* !CONFIG_EEPROM_VERIFY */
+}
+
+
 /*!
  * Copy \c count bytes at address \c addr
  * from eeprom to RAM to buffer \c buf.
index 6d2caf032d5ec6860eaeb487170091912b3316aa..cec8bbd717eb8fb0a30134f30270f0406818fd2f 100755 (executable)
@@ -2,7 +2,7 @@
  * \file
  * <!--
  * Copyright 2003, 2004 Develer S.r.l. (http://www.develer.com/)
- * All Rights Reserved.
+ * This file is part of DevLib - See devlib/README for information.
  * -->
  *
  * \version $Id$
@@ -15,6 +15,9 @@
 
 /*#*
  *#* $Log$
+ *#* Revision 1.5  2004/11/02 17:50:02  bernie
+ *#* CONFIG_EEPROM_VERIFY: New config option.
+ *#*
  *#* Revision 1.4  2004/08/25 14:12:08  rasky
  *#* Aggiornato il comment block dei log RCS
  *#*