user1232141234213 user1232141234213 - 2 months ago 24
C Question

arduino flash memory array

I have a number of arrays all of different sizes that I have stored in flash memory.
I can access single array entries with

byte j = pgm_read_byte(&(array[x]));


What I want to do is to pass the array from the flash memory as an argument to a function. I have tried giving a pointer to the array, as an argument but this gives a compilation error:

void callPGM2(byte arr_size, byte *arr) {
..
..
}

ptr2 = &pgm_read_byte(&(array_1[0]));
callPGM2(5, &ptr2);


Can full arrays be passed from flash memory as function arguments?

Answer

There's no way to directly pass a pointer to PROGMEM variables, because of the AVR's Harvard architecture with 2 address spaces that C has no way to straightforwardly express - You need to temporarily copy the memory to RAM using memcpy_P, for example.

And you want to learn about the functions provided in the pgmspace library. It holds equivalents to a number of C functions like strcmp, that allow you to work with a constant argument in program space.

Comments