In this use case, the EEPROM emulator module is configured and a sample page of data read and written.
The first byte of the first EEPROM page is toggled, and a LED is turned on or off to reflect the new state. Each time the device is reset, the LED should toggle to a different state to indicate correct non-volatile storage and retrieval.
Prerequisites
The device's fuses must be configured to reserve a sufficient number of FLASH memory rows for use by the EEPROM emulator service, before the service can be used. That is: NVMCTRL_FUSES_EEPROM_SIZE
has to be set to less than 0x5 in the fuse setting, then there will be more than 8 pages size for EEPROM. Atmel Studio can be used to set this fuse(Tools->Device Programming).
Setup
Prerequisites
There are no special setup requirements for this use-case.
Code
Copy-paste the following setup code to your user application:
void configure_eeprom(void)
{
while (true) {
}
}
}
}
#if (SAMD || SAMR21)
void SYSCTRL_Handler(void)
{
if (SYSCTRL->INTFLAG.reg & SYSCTRL_INTFLAG_BOD33DET) {
SYSCTRL->INTFLAG.reg = SYSCTRL_INTFLAG_BOD33DET;
}
}
#endif
static void configure_bod(void)
{
#if (SAMD || SAMR21)
config_bod33.level = 48;
SYSCTRL->INTENSET.reg = SYSCTRL_INTENCLR_BOD33DET;
#endif
}
Add to user application initialization (typically the start of main()
):
Workflow
- Attempt to initialize the EEPROM emulator service, storing the error code from the initialization function into a temporary variable.
- Check if the emulator failed to initialize due to the device fuses not being configured to reserve enough of the main FLASH memory rows for emulated EEPROM usage - abort if the fuses are mis-configured.
- Check if the emulator service failed to initialize for any other reason; if so assume the emulator physical memory is unformatted or corrupt and erase/re-try initialization. Config BOD to give an early warning, so that we could prevent data loss.
Use Case
Code
Copy-paste the following code to your user application:
page_data[0] = !page_data[0];
page_data[1]=0x1;
while (true) {
}
Workflow
- Create a buffer to hold a single emulated EEPROM page of memory, and read out logical EEPROM page zero into it.
- Toggle the first byte of the read page.
page_data[0] = !page_data[0];
- Output the toggled LED state onto the board LED.
- Write the modified page back to logical EEPROM page zero, flushing the internal emulator write cache afterwards to ensure it is immediately written to physical non-volatile memory.
- Modify data and write back to logical EEPROM page zero. The data is not committed and should call
eeprom_emulator_commit_page_buffer
to ensure that any outstanding cache data is fully written to prevent data loss when detecting a BOD early warning.