V71: How to configure project for flash-based EEPROM

Go To Last Post
2 posts / 0 new
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have a well running application and now I am planning to enhance the code by saving user settings in the MCU's internal flash, using the flash utility provided via Atmel Start (hal_flash.c).


I have a couple of questions where I'd appreciate some help from the community here:

  1. My linker script samv71q21b_flash.ld says the following about the memory:

       rom (rx)    : ORIGIN = 0x00400000, LENGTH = 0x00200000 /* rom, 2097152K */
       ram (rwx)   : ORIGIN = 0x20400000, LENGTH = 0x00060000 /* ram, 393216K */


    When I build my project, I get the following information about my memory footprint:

       Program Memory Usage     :    391388 bytes   18.7 % Full
       Data Memory Usage        :    153052 bytes   38.9 % Full


    My intent is to use the upper half of my flash for storing the configuration. However, when I change LENGTH of the ROM to 0x00100000, the footprint doesn't seem to change and after recompiling it still says that the Program Memory Usage is 18.7%. Is this to be expected? What may I be doing wrong here?

  2. I want to store my configuration data starting with address 0x00410000 by using code something like this:

       #define FLASH_MEMORY 0x00410000UL
       #define example_A (*(volatile int16_t*) (FLASH_MEMORY + 0UL))
       #define example_B (*(volatile int16_t*) (FLASH_MEMORY + 2UL))


    Somewhere else in my code I can then conveniently access the contents of the variables by something like

       int16_t = example_A * example_B;

    However, it is not clear to me how to initialize my variable during the build. In other words, when I flash my MCU, these addresses are initialized with an arbitrary value but I need these addresses initialized with specific values during flashing my firmware. How is this properly done? In more general terms, I need to have a default configuration available in flash upon the first start of the application after the MCU was flashed and I am not sure how to do this best.


I am using Atmel Studio 7 (version 7.0.2389) on a Windows 10 computer.


Last Edited: Fri. Mar 18, 2022 - 06:36 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

OK, in the meantime, I found the solution.


  1. I adapted the linker script in two places:


       /* Memory Spaces Definitions */
           rom  (rx) : ORIGIN = 0x00400000, LENGTH = 0x00100000 /* rom, 1048576 bytes                    */
           rom2 (rx) : ORIGIN = 0x00500000, LENGTH = 0x00100000 /* used for flash-based EEPROM emulation */
           ram (rwx) : ORIGIN = 0x20400000, LENGTH = 0x00060000 /* ram,  393216 bytes                    */


    Further below, where the ram section ends, I added the following:

        .eeprom :
            . = ALIGN(4);
            __eeprom_START__ = .;
            . = ALIGN(4);
            __eeprom_END__ = .;
        } > rom2


  2. In my C code, I initialized a variable as follows:


    __attribute__((section(".eeprom"))) static uint32_t xyz = 0xDEADBEEF;


When I run the debugger, I see value 0xDEADBEEF stored at address 0x500000.

Last Edited: Fri. Mar 18, 2022 - 08:28 PM