Pointers and PROGMEM

Understanding the language, error messages, etc.

Pointers and PROGMEM

Postby Sorunome » 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: Select all
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: Select all
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?
User avatar
Sorunome
 
Posts: 629
Joined: Sun Mar 01, 2015 1:58 pm

Re: Pointers and PROGMEM

Postby Muessigb » Fri Mar 20, 2015 1:10 pm

This is how to delcare it:
Code: Select all
#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: Select all
byte yourdata = pgm_read_byte(&(map1[3]));
User avatar
Muessigb
 
Posts: 45
Joined: Tue Dec 09, 2014 5:49 pm
Location: Germany

Re: Pointers and PROGMEM

Postby Sorunome » Fri Mar 20, 2015 1:23 pm

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

Code: Select all
const byte arr[] PROGMEM = {42};
arr[0]; // returns 42
User avatar
Sorunome
 
Posts: 629
Joined: Sun Mar 01, 2015 1:58 pm

Re: Pointers and PROGMEM

Postby Muessigb » Fri Mar 20, 2015 1:33 pm

Code: Select all
#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: Select all
byte b = pgm_read_byte(pgm_read_ptr(&(levels[levelindex]))+dataindex);
User avatar
Muessigb
 
Posts: 45
Joined: Tue Dec 09, 2014 5:49 pm
Location: Germany

Re: Pointers and PROGMEM

Postby Sorunome » 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: Select all
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
User avatar
Sorunome
 
Posts: 629
Joined: Sun Mar 01, 2015 1:58 pm


Return to Programming Questions

Who is online

Users browsing this forum: No registered users and 28 guests

cron