From 8c175db1086a6938f3c45303715c0094ce1b7555 Mon Sep 17 00:00:00 2001 From: bernie Date: Tue, 2 Nov 2004 17:50:02 +0000 Subject: [PATCH] CONFIG_EEPROM_VERIFY: New config option. git-svn-id: https://src.develer.com/svnoss/bertos/trunk@266 38d2e660-2303-0410-9eaa-f027e97ec537 --- drv/eeprom.c | 74 +++++++++++++++++++++++++++++++++++++++++++++++++--- drv/eeprom.h | 5 +++- 2 files changed, 75 insertions(+), 4 deletions(-) diff --git a/drv/eeprom.c b/drv/eeprom.c index 0f301eff..cf5da31a 100755 --- a/drv/eeprom.c +++ b/drv/eeprom.c @@ -2,7 +2,7 @@ * \file * * * \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. *#* @@ -57,12 +60,18 @@ #include /* cpu_to_be16() */ #include #include +#include // CONFIG_EEPROM_VERIFY #include // MIN() -#include // memset() +#include // memset() #include +// 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. diff --git a/drv/eeprom.h b/drv/eeprom.h index 6d2caf03..cec8bbd7 100755 --- a/drv/eeprom.h +++ b/drv/eeprom.h @@ -2,7 +2,7 @@ * \file * * * \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 *#* -- 2.25.1