How can I put an array of functions in PROGMEM

Go To Last Post
11 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I know I can use __flash, but everything else int his code uses PROGMEM so I'm trying to do be consistent:

 

declared:

void (*configurefunctions[9])() = { init, version, version, version, version, version, version, version, version, };

 

called:

  configurefunctions[0]();

 

DOES not compile:

const void (*configurefunctions[9])() PROGMEM = { init, version, version, version, version, version, version, version, version, };

 

call:

((void (*)())pgm_read_word(&configurefunctions[0]))();

 

I get:

</p>
<p>Severity    Code    Description    Project    File    Line<br />
Error        variable 'configurefunctions' must be const in order to be put into read-only section by means of '__attribute__((progmem))'</p>
<p> </p>
<p>Severity    Code    Description    Project    File    Line<br />
Warning        initialization from incompatible pointer type [-Wincompatible-pointer-types]    binaryEnigma

 

How can I specify that the const isn't part of the function, but is part of the function pointer?

This topic has a solution.
Last Edited: Mon. Sep 28, 2020 - 02:51 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

So I got this working:

 

typedef void (*mycfg)();

const mycfg cfg[] PROGMEM = { version, none, none, init, none, none, none, none, none };

 

Can it be done without the typedef in one line?

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

alank2 wrote:

Can it be done without the typedef in one line?

try

void (* const configurefunctions[9])() =

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes, that works!  Thanks!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

You probably know this but I'll just mention it anyway. If you have a simple pointer like:

int * pInt;

there is a potential to add up to two "const" to that:

const int * pInt;
int * const pInt;
const int * const pInt;

The first is a pointer to const int. The second is a const pointer to int. The third is a const pointer to const int. So when you have any form of pointer either the datatype to which it points may be const. Or the pointer itself can be const. Or both. 

 

PROGMEM (and __flash) require the things that are stored to be const whether they are base types of pointers to them.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
The first is a pointer to const int. The second is a const pointer to int. The third is a const pointer to const int.

Absolutely.

With a function pointer, it's only the equivalent of the second version that makes sense ie. a const pointer to function

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks clawson/MrKendo - I was getting the idea that this was possible when trying to figure it out, but thank you for spelling it out!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Time to mark the solution ... ?

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes - can you mark more than one as solution?  Or just one?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

alank2 wrote:
can you mark more than one as solution?

It's all in Tip #5

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Super!