Skip to content

Commit

Permalink
atsam: update smarteeprom implementation
Browse files Browse the repository at this point in the history
- Use define for SmartEEPROM buffer address
- Check buffer overflow
- Do not perform operation when timeout occurs

Signed-off-by: Alexandre d'Alton <alex@alexdalton.org>
  • Loading branch information
daltona committed Apr 27, 2020
1 parent 6548381 commit eac2938
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions tmk_core/common/arm_atsam/eeprom.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,29 @@
#endif

__attribute__((aligned(4))) static uint8_t buffer[EEPROM_SIZE];
volatile uint8_t *SmartEEPROM8 = (uint8_t *) 0x44000000;
volatile uint8_t *SmartEEPROM8 = (uint8_t *) SEEPROM_ADDR;

uint8_t eeprom_read_byte(const uint8_t *addr) {
uintptr_t offset = (uintptr_t)addr;
if (offset >= EEPROM_SIZE)
return 0xff;

if (NVMCTRL->SEESTAT.bit.PSZ == 0 || NVMCTRL->SEESTAT.bit.SBLK == 0)
return buffer[offset];

int timeout = 10000;
while (NVMCTRL->SEESTAT.bit.BUSY && timeout-- > 0)
;
return SmartEEPROM8[offset];
if (!NVMCTRL->SEESTAT.bit.BUSY)
return SmartEEPROM8[offset];

return 0xff;
}

void eeprom_write_byte(uint8_t *addr, uint8_t value) {
uintptr_t offset = (uintptr_t)addr;
if (offset >= EEPROM_SIZE)
return;

if (NVMCTRL->SEESTAT.bit.PSZ == 0 || NVMCTRL->SEESTAT.bit.SBLK == 0) {
buffer[offset] = value;
Expand All @@ -49,8 +56,8 @@ void eeprom_write_byte(uint8_t *addr, uint8_t value) {
int timeout = 10000;
while (NVMCTRL->SEESTAT.bit.BUSY && timeout-- > 0)
;

SmartEEPROM8[offset] = value;
if (!NVMCTRL->SEESTAT.bit.BUSY)
SmartEEPROM8[offset] = value;
}

uint16_t eeprom_read_word(const uint16_t *addr) {
Expand Down

0 comments on commit eac2938

Please sign in to comment.