Sorry to bother, but I have hit a heavy roadblock on a school project:
-
I have an Arduino Due
-
I have multiple arrays of type "const volatile unsigned char" which are all pretty big, these arrays are stored in the Programm Memory
-
I need to load one of these arrays into the SRAM to do some operations with it, the SRAM is big enough to fit one or two of these arrays, but not all of them
-
Therefore my goal is to load one array into the SRAM, use it, have it "forgotten" and load in the next array, however I am unable to do this
const volatile unsigned char* pointer_data = 0; // points to array to be used
uint32_t data_length = 0; // length of array
const volatile unsigned char data_0[] PROGMEM = {
0x80,0x66,0x77,0x58 //and so on
};
const volatile unsigned char data_1[] PROGMEM = {
// lots more elements
};
// I have about 20 of these arrays, not all listed above for simplicity
void setup()
{
// unrelated stuff
}
void loop()
{
pointer_data=data_0;
data_length=sizeof(data_0);
doStuff(); // my function that works with the array that I point to
pointer_data=data_1;
data_length=sizeof(data_1);
doStuff();
pointer_data=data_2;
data_length=sizeof(data_2);
doStuff();
// and so on
}
This gives me a ".bss not within field of ram" compile error, which as far as I know means I have used to much SRAM. Additional notes:
-
the error goes away if I only call do stuff on one or two arrays, but I need to call it on all of them
-
there is also no error when calling "doStuff()" multiple times with the same array
-
pointer_data and data_length are global variables that are used by "doStuff()" to find the correct array
-
my assumption is that the arrays get loaded into the SRAM when I assign it to "data_pointer" and then stays around after the function call, which I don't want, but also don't know how to get rid off.
-
I already tried putting data_pointer = 0; after I'm done with the array, which didn't work
-
I have also tried moving the assignment of the pointer into a function to create a temporary scope, that also didn't work
-
my endgoal is to "doStuff();" with all of the arrays in any possible order
Thank u very much!
Edit: Thank you all for your anwsers, the problem was making the PROGMEM arrays volatile. Having all the PROGMEM arrays as const unsigned char data_x[] PROGMEM
did the trick for me, the rest of my code remained unchanged.