From 50f19d1d1f0759c12fb3a56a462b695f09b06ee8 Mon Sep 17 00:00:00 2001 From: Alexandre Abadie Date: Thu, 27 Sep 2018 13:22:49 +0200 Subject: [PATCH] drivers/periph_eeprom: add clear and erase functions --- drivers/include/periph/eeprom.h | 19 +++++++++++++++++++ drivers/periph_common/eeprom.c | 15 +++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/drivers/include/periph/eeprom.h b/drivers/include/periph/eeprom.h index affdf13572..d7617ea1ea 100644 --- a/drivers/include/periph/eeprom.h +++ b/drivers/include/periph/eeprom.h @@ -79,6 +79,25 @@ void eeprom_write_byte(uint32_t pos, uint8_t data); */ size_t eeprom_write(uint32_t pos, const uint8_t *data, size_t len); +/** + * @brief Clear @p len bytes from the given position @p pos + * + * Clearing a byte in EEPROM simply consists in setting it to 0 + * + * @param[in] pos start position in eeprom + * @param[in] len the number of bytes to clear + * + * @return the number of bytes cleared + */ +size_t eeprom_clear(uint32_t pos, size_t len); + +/** + * @brief Erase the whole EEPROM content + * + * @return the EEPROM_SIZE + */ +size_t eeprom_erase(void); + #ifdef __cplusplus } #endif diff --git a/drivers/periph_common/eeprom.c b/drivers/periph_common/eeprom.c index d22c512701..35ec3aeaaf 100644 --- a/drivers/periph_common/eeprom.c +++ b/drivers/periph_common/eeprom.c @@ -40,4 +40,19 @@ void eeprom_write_byte(uint32_t pos, uint8_t byte) eeprom_write(pos, &byte, 1); } +size_t eeprom_clear(uint32_t pos, size_t len) +{ + assert(pos + len <= EEPROM_SIZE); + + for (size_t i = 0; i < len; i++) { + eeprom_write_byte(pos++, 0); + } + + return len; +} + +size_t eeprom_erase(void) +{ + return eeprom_clear(0, EEPROM_SIZE); +} #endif