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

View File

@ -22,12 +22,11 @@
#include <assert.h>
#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;
}

View File

@ -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

View File

@ -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