cpu: drivers/eeprom: refactor periph_eeprom implementation
This commit is contained in:
parent
a91b71a581
commit
e9a6ebc409
@ -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;
|
||||
}
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user