Force variables to be written to FLASH instead of RAM?

Greetings, I’m new to platform IO and I’ve been making a small program with an Arduino UNO, my program is supposed to perform certain actions based on changing parameters, I’ve a structure and functions to quickly change this parameters.

Because these are predefined, they should be loaded from FLASH memory, I’m used to codewarrior eclipse (using nxp microcontrollers) where to force data to the ROM you would write the const keyword when declaring it, however on platform IO using the const keyword does not seem to force memory to the flash.

How does one force variables to the flash on PlatformIO?

here are some definitions I’m using that don’t result in data being written to flash:

struct s_data_motor_config_general{
	const bool dir;
	const unsigned int pulse_width;
}typedef motor_config;

struct s_data_motor_config_indexed{
	const motor_index idx;
	const motor_config mot_cfg;
}typedef motor_config_indexed;

struct s_data_motor_config_group{
	const motor_config_indexed config_array[TOTAL_MOTORS];
	const unsigned int amount;
}typedef motor_config_group;

const motor_config_group mcfgg_array[6]={
{ {{ mot_x, {ADVANCE, 2} }, { mot_y, {ADVANCE, 4} }, { mot_z, {ADVANCE, 8} }}, 3 },
{ {{ mot_x, {RECEDE, 2} }, { mot_y, {RECEDE, 4} }, { mot_z, {RECEDE, 8} }}, 3 },
{ {{ mot_x, {ADVANCE, 8} }, { mot_y, {RECEDE, 2} }, { mot_z, {ADVANCE, 8} }}, 3 },
{ {{ mot_x, {RECEDE, 1} }, { mot_y, {RECEDE, 2} }, { mot_z, {RECEDE, 3} }}, 3 },
{ {{ mot_x, {ADVANCE, 2} }, { mot_y, {RECEDE, 3} }, { mot_z, {RECEDE, 4} }}, 3 },
{ {{ mot_x, {RECEDE, 3} }, { mot_y, {ADVANCE, 4} }, { mot_z, {RECEDE, 5} }}, 3 }
};

  1. Full platformio.ini?
  2. Full compilable code? You are using types in there (motor_index) that you don’t say how you define them, along with enums or macros like ADVANCE, …

however on platform IO using the const keyword does not seem to force memory to the flash.

  1. How have you verified that?

What you really want to ask is “how to force variables to be put in flash on GCC”. PlatformIO as a fancy build system that calls into avr-gcc in your case.

And final question: Why not use PROGMEM? This macro maps to __attribute__((__progmem__)) (source) and is GCC attribute that will put a symbol into program memory, aka flash. Just write

const motor_config_group mcfgg_array[6] PROGMEM ={
..
};

and retry.

1:
platform.ini:

; PlatformIO Project Configuration File
;
;   Build options: build flags, source filter
;   Upload options: custom upload port, speed and extra flags
;   Library options: dependencies, extra library storages
;   Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html

[env:uno]
platform = atmelavr
board = uno
framework = arduino

build_flags =
-I/Users/Santiago/Documents/Projects/Test11/headers
-I/Users/Santiago/Documents/Projects/Test11/headers/01_controllers
-I/Users/Santiago/Documents/Projects/Test11/headers/02_managers
-I/Users/Santiago/Documents/Projects/Test11/headers/03_physical_elements
-I/Users/Santiago/Documents/Projects/Test11/headers/00_globals

-Wno-reorder

2:
ADVANCE and RECEIVE are booleans (true & false)
I forgot to paste that motor_index is an enumeration of unsigned char, but all the rest of the definitions are there the last structure is basically 3 structures nested into the last and everything else is primitive types, I pasted them more to show the sintaxis.

I’d paste the compilabe code but it’s stretched over several files, classes, headers and so on, I’ve pushed it to this git repository: GitHub - CrainFartor/Test11: This is a repository where i pushed code i was working on when i asked a question on a forum, I'm archiving it, and keeping it up in case someone finds the post and wants to follow the link to check something, for some reason.

3:
The way I checked this was by commenting out the variables and all calls to functions that accesed them and seeing how much more or less flash and ram the program was occupying what’s curious and what I hadn’t noticed is that both the ram and flash grow when i take this variables in or out

I did not use progmem becouse I didn’t know it was a thing, I was used to codewarrior where writting const was just enough, but using it fixed it, though i’m still perplexed by the fact that i seem to be having all data duplicated both on flash and ram when using only the const keyword and not progmem

This does make sense – if you have a const initialized variable that is stored in RAM, you have to store the initialized data into flash and allocate space in RAM that you will copy this init data to. It’s like having a const variable that is not directly accessed from flash but from RAM – or, just any RAM variable with initialized data and compiler checks to see if that variable isn’t changed.

Also note that accessing program memory under AVR is special – you have to use the pgm_read_word and friends functions to retrieve their contents again. This isn’t a nice direct access – hence also why the compiler doesn’t put variables into flash even if they’re const. There has to be special code to read the variable again.

Yeah, using normal const_cast worked fine for a bit but since i’ve nested structures eventually it broke and then i found out you need to use special reads, which, is bs, though complaining won’t change anything so I guess now I know for next time; For now i think i can pull it out with just RAM since I don’t really think I need a lot of sctructs.

I can’t believe I forgot something so basic:

This does make sense – if you have a const initialized variable that is stored in RAM, you have to store the initialized data into flash and allocate space in RAM that you will copy this init data to

Thanks a lot for the all help.