This is doing my head in.
I want to store a struct containing nine floats in progmem to free up the ram it would otherwise consume. I’ve followed the discussion on how to do it, all seem to end up placing an array in progmem, not a single struct, and this might be the core of the problem.
Here is some test code:
#include <avr/pgmspace.h>
struct Coeff
{
float b0;
float b1;
float b2;
float b3;
float b4;
float a1;
float a2;
float a3;
float a4;
};
// // This struct in flash
const Coeff C PROGMEM = {0.0048243,
0.0192974,
0.0289461,
0.0192974,
0.0048243,
2.3695159,
-2.3139877,
1.0546647,
-0.1873776};
// // This struct in RAM
// const Coeff C = {0.0048243,
// 0.0192974,
// 0.0289461,
// 0.0192974,
// 0.0048243,
// 2.3695159,
// -2.3139877,
// 1.0546647,
// -0.1873776};
void setup()
{
Serial.begin(9600);
delay(1000);
// Read struct from progmem and print values
Serial.println(pgm_read_float(&C.b0),8);
Serial.println(pgm_read_float(&C.b1),8);
Serial.println(pgm_read_float(&C.b2),8);
Serial.println(pgm_read_float(&C.b3),8);
Serial.println(pgm_read_float(&C.b4),8);
Serial.println(pgm_read_float(&C.a1),8);
Serial.println(pgm_read_float(&C.a2),8);
Serial.println(pgm_read_float(&C.a3),8);
Serial.println(pgm_read_float(&C.a4),8);
// // Read struct from RAM and print values
// Serial.println(C.b0,8);
// Serial.println(C.b1,8);
// Serial.println(C.b2,8);
// Serial.println(C.b3,8);
// Serial.println(C.b4,8);
// Serial.println(C.a1,8);
// Serial.println(C.a2,8);
// Serial.println(C.a3,8);
// Serial.println(C.a4,8);
}
void loop()
{
// put your main code here, to run repeatedly:
}
I run the code first with the struct in progmem, then with it in RAM (by commenting out and in the relevant code) and here are the compile results:
Struct in progmem
Sketch uses 3314 bytes (10%) of program storage space. Maximum is 32256 bytes.
Global variables use 200 bytes (9%) of dynamic memory, leaving 1848 bytes for local variables. Maximum is 2048 bytes.
Struct in RAM uses 72 bytes less flash but still 200 bytes of RAM
Sketch uses 3242 bytes (10%) of program storage space. Maximum is 32256 bytes.
Global variables use 200 bytes (9%) of dynamic memory, leaving 1848 bytes for local variables. Maximum is 2048 bytes.
The flash usage changes by 72 bytes (why not 36?), between the two runs, but the RAM usage does not. It seems that the compiler puts the struct in both memory spaces. How can I get it to release the space used in RAM?
Pogo