Memory layout for PROGMEM

Instead of moving the application code forward another 1k after the bootloader, I've decided to move the config data to the end of the Flash.

So a working proof of concept is like this ...

// code in the Arduino sketch
typedef struct {
  byte channel;
  byte id;
} config_test;

config_test  __attribute__((section (".myBufSection")))  configData[2] ;

void setup() {
  pinMode(LED_BUILTIN,  OUTPUT);
  digitalWrite(LED_BUILTIN,  configData[0].id);   
// etc

and definitions in the linker script file

MEMORY
{
  FLASH (rx) : ORIGIN = 0x00000000+0x2000, LENGTH = 0x00040000-0x2000-0x1000 /*-0x400  - First 8KB used by bootloader, last 1k by config data */
  RAM (rwx) : ORIGIN = 0x20000000, LENGTH = 0x00008000
 CONFIG_DATA (ar) :  ORIGIN = 255K, LENGTH = 1K  	
// etc
SECTIONS
{
  /* placing my named section at given address: */
  .myBufBlock 255K : 
  {
/* keep my variable even if not referenced */
    KEEP(*(.myBufSection))  
  } > CONFIG_DATA
 
  /* other placements follow here... */
// etc

This provides the following output hex file

// first text/code lines
:10 2000 00 00800020C5210000AD210000AD210000 AE
:10 2010 00 00000000000000000000000000000000 C0
...
// last text/code lines
:10 33B4 00 40420F00FFFFFFFFDD2000002D210000 31
:10 33C4 00 B5200000000000000000000000000000 24
// reset the extended segment address to 0x3000 * 0x10 (196608 or 192K)
:02 0000 02 3000 CC
// here's the  config data, 4 bytes being 2 array elements each 2 bytes long, watch this change below
:04 FC00 00 00000000 00  // address is added to new base above, so is 0x3FC00 or 261120 or 255K
// and the starting execution address
:04 0000 03 000021C5 13
// followed by the End of File marker
:00 0000 01 FF

The important bit is the config data line. Changing this to output {{01,2},{03,4}} would give configuration data

:04 FC00 00 01020304 F6

So this is the data that needs to be flashed to the mcu to change the configuration. A separate tool will output these lines based on a UI tool.

And removing the section in the linker script will remove the output lines in the HEX file from the specified address, however they are allocated to the next available memory block.

:04 33D4 00 00000000 F5

So probably some intermediate processing of the HEX file to remove the lines allocating memory to the specified area would make sense before uploading. More research required.

And the final part of the puzzle is to mark the linker section definition with the NOLOAD option

SECTIONS
{
  /* placing my named section at given address: */
  .myBufBlock 255K   (NOLOAD) :  { 
    *(.myBufSection) 
  } > CONFIG_DATA  
 
  /* other placements follow here... */

This yields the following HEX file (last few lines shown for brevity )

// last text/code lines
:10 33B4 00 40420F00FFFFFFFFDD2000002D210000 31
:10 33C4 00 B5200000000000000000000000000000 24
// and the starting execution address
:04 0000 03 000021C5 13
// followed by the End of File marker
:00 0000 01 FF

Note that there is no inclusion of the configuration data. This can be uploaded separately.

P.S. marking the original declaration as const will prevent accidental changes to the configuration data in the code from sneaking past the compiler (the linker would ignore them), so the declaration then becomes

const config_test __attribute__((section (".myBufSection")))  configData[2] = {};

Hmmm, this did not work quite as hoped for.

Declaring the config_test element as const means that the compiler then optimises it out of the executable code. So even though I can include it in the Flash memory at a specified location it is never referenced as the values are baked in at compile time. Which kind of makes sense, the compiler will inline the value used in the assignment.

So it has to be declared as a variable, but unfortunately then being a var, the compiler does NOT complain if an assignment is accidentally made to the value in my code, even though it gets ignored at run time because the var is located in Flash. Grrr.

So, not a perfect solution. I think the work around is to use a compiler directive to include the const during development of the code which protects against the accidental changing of the config_tests values.

And then set the compiler directive for the final compilation to convert the element to a variable so it is correctly referenced from the Flash.

My code now looks like this during development

#ifndef FINAL
const 
#endif /* FINAL */
config_test __attribute__((section (".myBufSection")))  configData[2]  = {{01, 1}, {02, 0}};

and like this when the final compilation is performed

#define FINAL 1
#ifndef FINAL
const 
#endif /* FINAL */
config_test __attribute__((section (".myBufSection")))  configData[2]  = {{01, 1}, {02, 0}};

I'll also choose a better name for the memory section :slight_smile:

I've tested this by compiling the code and the manually altering the HEX and uploading the file to the device.
This turns the LED on

:020000023000CC
:04FC000001010200FC

This turns the LED off

:020000023000CC
:04FC000001000200FD