From e9a6ebc409ec6896e6e4ecbe9cdd8ffd6677907f Mon Sep 17 00:00:00 2001 From: Alexandre Abadie Date: Mon, 1 Oct 2018 15:24:57 +0200 Subject: [PATCH] cpu: drivers/eeprom: refactor periph_eeprom implementation --- cpu/atmega_common/periph/eeprom.c | 57 +++++++++++++++++++------------ cpu/stm32_common/periph/eeprom.c | 34 ++++++++++++------ drivers/include/periph/eeprom.h | 6 ++++ drivers/periph_common/eeprom.c | 22 ++++-------- 4 files changed, 72 insertions(+), 47 deletions(-) diff --git a/cpu/atmega_common/periph/eeprom.c b/cpu/atmega_common/periph/eeprom.c index 2a3827336d..15129e53f6 100644 --- a/cpu/atmega_common/periph/eeprom.c +++ b/cpu/atmega_common/periph/eeprom.c @@ -24,37 +24,52 @@ #include "cpu.h" #include "periph/eeprom.h" -uint8_t eeprom_read_byte(uint32_t pos) +#define ENABLE_DEBUG (0) +#include "debug.h" + +size_t eeprom_read(uint32_t pos, uint8_t *data, size_t len) { - assert(pos < EEPROM_SIZE); + assert(pos + len <= EEPROM_SIZE); - /* Wait for completion of previous operation */ - while (EECR & (1 << EEPE)) {} + uint8_t *p = data; - /* Set up address register */ - EEAR = pos; + DEBUG("Reading data from EEPROM at pos %" PRIu32 ": ", pos); + for (size_t i = 0; i < len; i++) { + while (EECR & (1 << EEPE)) {} - /* Start eeprom read by writing EERE */ - EECR |= (1 << EERE); + /* Set up address register */ + EEAR = pos++; - /* Return data from Data Register */ - return EEDR; + /* Start eeprom read by writing EERE */ + EECR |= (1 << EERE); + *p++ = (uint8_t)EEDR; + DEBUG("0x%02X ", EEDR); + } + DEBUG("\n"); + + return len; } -void eeprom_write_byte(uint32_t pos, uint8_t data) +size_t eeprom_write(uint32_t pos, const uint8_t *data, size_t len) { - assert(pos < EEPROM_SIZE); + assert(pos + len <= EEPROM_SIZE); - /* Wait for completion of previous operation */ - while (EECR & (1 << EEPE)) {} + uint8_t *p = (uint8_t *)data; - /* Set up address and Data Registers */ - EEAR = pos; - EEDR = data; + for (size_t i = 0; i < len; i++) { + /* Wait for completion of previous operation */ + while (EECR & (1 << EEPE)) {} - /* Write logical one to EEMPE */ - EECR |= (1 << EEMPE); + /* Set up address and Data Registers */ + EEAR = pos++; + EEDR = *p++; - /* Start eeprom write by setting EEPE */ - EECR |= (1 << EEPE); + /* Write logical one to EEMPE */ + EECR |= (1 << EEMPE); + + /* Start eeprom write by setting EEPE */ + EECR |= (1 << EEPE); + } + + return len; } diff --git a/cpu/stm32_common/periph/eeprom.c b/cpu/stm32_common/periph/eeprom.c index bef71291b3..962b543bab 100644 --- a/cpu/stm32_common/periph/eeprom.c +++ b/cpu/stm32_common/periph/eeprom.c @@ -22,12 +22,11 @@ #include #include "cpu.h" +#include "periph/eeprom.h" #define ENABLE_DEBUG (0) #include "debug.h" -#include "periph/eeprom.h" - extern void _lock(void); extern void _unlock(void); @@ -35,20 +34,35 @@ extern void _unlock(void); #error "periph/eeprom: EEPROM_START_ADDR is not defined" #endif -uint8_t eeprom_read_byte(uint32_t pos) +size_t eeprom_read(uint32_t pos, uint8_t *data, size_t len) { - assert(pos < EEPROM_SIZE); + assert(pos + len <= EEPROM_SIZE); - DEBUG("Reading data from EEPROM at pos %" PRIu32 "\n", pos); - return *(uint8_t *)(EEPROM_START_ADDR + pos); + uint8_t *p = data; + + DEBUG("Reading data from EEPROM at pos %" PRIu32 ": ", pos); + for (size_t i = 0; i < len; i++) { + *p++ = *(uint8_t *)(EEPROM_START_ADDR + pos++); + DEBUG("0x%02X ", *p); + } + DEBUG("\n"); + + return len; } -void eeprom_write_byte(uint32_t pos, uint8_t data) +size_t eeprom_write(uint32_t pos, const uint8_t *data, size_t len) { - assert(pos < EEPROM_SIZE); + assert(pos + len <= EEPROM_SIZE); + + uint8_t *p = (uint8_t *)data; - DEBUG("Writing data '%c' to EEPROM at pos %" PRIu32 "\n", data, pos); _unlock(); - *(uint8_t *)(EEPROM_START_ADDR + pos) = data; + + for (size_t i = 0; i < len; i++) { + *(uint8_t *)(EEPROM_START_ADDR + pos++) = *p++; + } + _lock(); + + return len; } diff --git a/drivers/include/periph/eeprom.h b/drivers/include/periph/eeprom.h index 09dd5034ee..affdf13572 100644 --- a/drivers/include/periph/eeprom.h +++ b/drivers/include/periph/eeprom.h @@ -46,6 +46,9 @@ uint8_t eeprom_read_byte(uint32_t pos); /** * @brief Read @p len bytes from the given position * + * This function must be implemented by each CPU that provides an internal + * EEPROM. + * * @param[in] pos start position in eeprom * @param[out] data output byte array to write to * @param[in] len the number of bytes to read @@ -65,6 +68,9 @@ void eeprom_write_byte(uint32_t pos, uint8_t data); /** * @brief Write @p len bytes at the given position * + * This function must be implemented by each CPU that provides an internal + * EEPROM. + * * @param[in] pos start position in eeprom * @param[in] data input byte array to read into * @param[in] len the number of bytes to read diff --git a/drivers/periph_common/eeprom.c b/drivers/periph_common/eeprom.c index 07fd94e6db..d22c512701 100644 --- a/drivers/periph_common/eeprom.c +++ b/drivers/periph_common/eeprom.c @@ -28,26 +28,16 @@ #include "periph/eeprom.h" -size_t eeprom_read(uint32_t pos, uint8_t *data, size_t len) +uint8_t eeprom_read_byte(uint32_t pos) { - assert(pos + len < EEPROM_SIZE); - - for (size_t i = 0; i < len; i++) { - data[i] = eeprom_read_byte(pos++); - } - - return len; + uint8_t byte; + eeprom_read(pos, &byte, 1); + return byte; } -size_t eeprom_write(uint32_t pos, const uint8_t *data, size_t len) +void eeprom_write_byte(uint32_t pos, uint8_t byte) { - assert(pos + len < EEPROM_SIZE); - - for (size_t i = 0; i < len; i++) { - eeprom_write_byte(pos++, data[i]); - } - - return len; + eeprom_write(pos, &byte, 1); } #endif