March 13, 2016

Arduino Due Flash Memory Access

The Arduino Due does not have an EEPROM like the earlier Arduinos had. Persistent memory locations, i.e. memory that can cold variable values which will be available even after power loss, watchdog reset, or after switching the box off and on again, are however available in the form of the Due's flash memory that also holds the program code and static data.

In my box, I want to store persistent data that holds the user's most recent configuration. This includes favorite channel settings (but not amplitude, which will be set to zero initially for safety), preferences, etc. I will also store certain indicators that can be read during start() and will tell whether the box was re-booted due to an intermittent power loss or a watchdog reset.

The Due has 512 kBytes of flash memory, of which usually only a part is occupied by the program code. The remainder should be usable to store and restore persistent data. Flash memory starts at address 0x00080000 (IFLASH0_ADDR) in the Due's 32-bit flat address space, and at 512k size (IFLASH0_SIZE+IFLASH1_SIZE) ranges until address 0x000fffff. The lower addresses will likely contain boot loader and program code.

The entire flash memory address space can usually only be read, and doing so is no different that ready any other (SRAM) memory location. The following function translates a flash memory address (from 0x00000 to 0x7ffff) to a C++ pointer pointing into the Due's 32-bit flat address space: To write it, the Due needs to be told explicitly that write access is permitted: After this, writing a buffer of given length to a flash memory address is possible using the following instruction sequence: boolean The above code snippets are taken and modified from a very nice library called DueFlashStorage to be found at [1]. It includes Atmel's header and source files (efc.h, efc.cpp, flash_efc.h, flash_efc.cpp) required to access the SAM 3x8E's embedded flash service component (EFC), and it builds a little C++ class for flash memory write access on top.

References

[1] https://github.com/sebnil/DueFlashStorage

No comments:

Post a Comment