cpu: drivers/eeprom: refactor periph_eeprom implementation

This commit is contained in:
Alexandre Abadie 2018-10-01 15:24:57 +02:00
parent a91b71a581
commit e9a6ebc409
4 changed files with 72 additions and 47 deletions

View File

@ -24,33 +24,45 @@
#include "cpu.h" #include "cpu.h"
#include "periph/eeprom.h" #include "periph/eeprom.h"
uint8_t eeprom_read_byte(uint32_t pos) #define ENABLE_DEBUG (0)
{ #include "debug.h"
assert(pos < EEPROM_SIZE);
/* Wait for completion of previous operation */ size_t eeprom_read(uint32_t pos, uint8_t *data, size_t len)
{
assert(pos + len <= EEPROM_SIZE);
uint8_t *p = data;
DEBUG("Reading data from EEPROM at pos %" PRIu32 ": ", pos);
for (size_t i = 0; i < len; i++) {
while (EECR & (1 << EEPE)) {} while (EECR & (1 << EEPE)) {}
/* Set up address register */ /* Set up address register */
EEAR = pos; EEAR = pos++;
/* Start eeprom read by writing EERE */ /* Start eeprom read by writing EERE */
EECR |= (1 << EERE); EECR |= (1 << EERE);
*p++ = (uint8_t)EEDR;
DEBUG("0x%02X ", EEDR);
}
DEBUG("\n");
/* Return data from Data Register */ return len;
return EEDR;
} }
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;
for (size_t i = 0; i < len; i++) {
/* Wait for completion of previous operation */ /* Wait for completion of previous operation */
while (EECR & (1 << EEPE)) {} while (EECR & (1 << EEPE)) {}
/* Set up address and Data Registers */ /* Set up address and Data Registers */
EEAR = pos; EEAR = pos++;
EEDR = data; EEDR = *p++;
/* Write logical one to EEMPE */ /* Write logical one to EEMPE */
EECR |= (1 << EEMPE); EECR |= (1 << EEMPE);
@ -58,3 +70,6 @@ void eeprom_write_byte(uint32_t pos, uint8_t data)
/* Start eeprom write by setting EEPE */ /* Start eeprom write by setting EEPE */
EECR |= (1 << EEPE); EECR |= (1 << EEPE);
} }
return len;
}

View File

@ -22,12 +22,11 @@
#include <assert.h> #include <assert.h>
#include "cpu.h" #include "cpu.h"
#include "periph/eeprom.h"
#define ENABLE_DEBUG (0) #define ENABLE_DEBUG (0)
#include "debug.h" #include "debug.h"
#include "periph/eeprom.h"
extern void _lock(void); extern void _lock(void);
extern void _unlock(void); extern void _unlock(void);
@ -35,20 +34,35 @@ extern void _unlock(void);
#error "periph/eeprom: EEPROM_START_ADDR is not defined" #error "periph/eeprom: EEPROM_START_ADDR is not defined"
#endif #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); uint8_t *p = data;
return *(uint8_t *)(EEPROM_START_ADDR + pos);
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(); _unlock();
*(uint8_t *)(EEPROM_START_ADDR + pos) = data;
_lock(); for (size_t i = 0; i < len; i++) {
*(uint8_t *)(EEPROM_START_ADDR + pos++) = *p++;
}
_lock();
return len;
} }

View File

@ -46,6 +46,9 @@ uint8_t eeprom_read_byte(uint32_t pos);
/** /**
* @brief Read @p len bytes from the given position * @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[in] pos start position in eeprom
* @param[out] data output byte array to write to * @param[out] data output byte array to write to
* @param[in] len the number of bytes to read * @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 * @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] pos start position in eeprom
* @param[in] data input byte array to read into * @param[in] data input byte array to read into
* @param[in] len the number of bytes to read * @param[in] len the number of bytes to read

View File

@ -28,26 +28,16 @@
#include "periph/eeprom.h" #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); uint8_t byte;
eeprom_read(pos, &byte, 1);
for (size_t i = 0; i < len; i++) { return byte;
data[i] = eeprom_read_byte(pos++);
} }
return len; void eeprom_write_byte(uint32_t pos, uint8_t byte)
}
size_t eeprom_write(uint32_t pos, const uint8_t *data, size_t len)
{ {
assert(pos + len < EEPROM_SIZE); eeprom_write(pos, &byte, 1);
for (size_t i = 0; i < len; i++) {
eeprom_write_byte(pos++, data[i]);
}
return len;
} }
#endif #endif