0

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?

5
  • Whatever the function pgm_read_byte returns, it can't be used as a pointer. And if it returns an int (as shown in the first code snippet) then why would you pass it as an array of bytes to the callPGM2 function? Commented Sep 25, 2016 at 10:13
  • Also, in C (as well as in C++) arrays naturally decays to pointers to their first element. If a byte * is expected, and you have an array of byte, then passing the array as is will work just as fine as doing e.g. &array[0]. Commented Sep 25, 2016 at 10:15
  • pgm_read_byte is a function in the avr/pgmspace lib. The code snippet above I have made a mistake, i think as it returns byte. Commented Sep 25, 2016 at 10:21
  • The reason to want to pass the array to the function is so the function can perform operation and write values defined in the array to secondary device through i2c Commented Sep 25, 2016 at 10:23
  • I think you may use pointer to PROGMEM as function argument, but don't mix it with non PROGMEM pointer. Would you mind to test this in real hardware, since I don't have it in my hand right now. If this is not what you mean, please ignore my comment. Commented Sep 25, 2016 at 11:19

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, i did come across another post referring to the Harvard architecture , but no one confirmed if a pointer to flash memory could be used as an argument in a function.
I'm little bit confused with the explanation. Isn't function like memcpy_P take the pointer to PROGMEM as one of its argument? So technically, we can define similar function which accept pointer to PROGMEM? Of course, inside the function, we must used PROGMEM functions to access the data.
Sure - A progmem pointer is just a value - can be handed over to PROGMEM functions to have it dereferenced into program space. Working with "normal" and PROGMEM pointers in parallel can, however, be extremely confusing (you can dereference one, but not the other), so it is recommended to copy the memory to RAM temporarily and work on it with "normal" pointers.
@tofro can I ask you for a little more information?
0

To copy a string from flash memory to RAM

#include<avr/pgmspace.h>
const byte Update_1[5]  PROGMEM = {0x01, 0xB2, 0x02, 0xFF, 0xFF};
byte buffer2[5];
setup {
  memcpy_P (buffer2, &(Update_1),5);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.