Switch to full style
Understanding the language, error messages, etc.
Post a reply

Pointers and PROGMEM

Fri Mar 20, 2015 12:06 pm

So yeah....I have some weird issue with PROGMEM and pointers.
I have these const byte arrays which store map data, and i have this const byte *maps which holds the pointers to the arrays, and i have const byte * currentmap which is the pointer to the current map loaded.
This all works fine like this:
Code:
const byte map1[] = {data};
const byte *maps = {map1};
const byte *currentmap;

currentmap = maps[0];

And now i can reference the current map as expected with currentmap[0] etc.

But now, as soon as I do
Code:
const byte map1[] PROGMEM = {data};
To free up coding space, i don't know what it is doing, but the gamebuino is freezing, so i suspect that the pointer is wrong.
Any way how to get the map data into PROGMEM and the program is still working?

Re: Pointers and PROGMEM

Fri Mar 20, 2015 1:10 pm

This is how to delcare it:
Code:
#include <avr/pgmspace.h>
...
const byte map1[] PROGMEM = {data};


Now to e.g. read a byte from index 3 of the map1 use this:
Code:
byte yourdata = pgm_read_byte(&(map1[3]));

Re: Pointers and PROGMEM

Fri Mar 20, 2015 1:23 pm

Then why does this work, while my pointer mapping stuff doesn't?

Code:
const byte arr[] PROGMEM = {42};
arr[0]; // returns 42

Re: Pointers and PROGMEM

Fri Mar 20, 2015 1:33 pm

Code:
#include <avr/pgmspace.h>
...
const byte level1[] PROGMEM = {data};
const byte level2[] PROGMEM = {data};
uint16_t levels[] PROGMEM = {&(level1), &(level2)};


Now you should be able to access it in this way:
Code:
byte b = pgm_read_byte(pgm_read_ptr(&(levels[levelindex]))+dataindex);

Re: Pointers and PROGMEM

Fri Mar 20, 2015 3:29 pm

ok it worked with pgm_read_byte(currentmap+offset);

But now I'm having a new issue:

Code:
CORE: *** Invalid read address PC=083e SP=08f8 O=3081 Address dc01 out of ram (08ff)

I get that in gbsim and it crashes, it tries to read flash at about 15048 or something.

Is there a limit of flash usage? o.O
Post a reply