Using Flash space error

I am leading with a strange error using the PROGMEM space that I can't understand. Here is the temp code that I have been using to figure out the problem:

char stop0to[] PROGMEM= "Terminal Inicio" ;
char stop1to[] PROGMEM= "Juan B Justo" ;
char stop2to[] PROGMEM= "Oro" ;
char stop3to[] PROGMEM= "Charcas" ;

char* StopsNameTo[] PROGMEM={
  stop0to,stop1to,stop2to,stop3to};

char stop0from[] PROGMEM= "Terminal Inicio" ;
char stop1from[] PROGMEM= "Ayacucho" ;
char stop2from[] PROGMEM= "Uriburu" ;
char stop3from[] PROGMEM= "Pueyrredon" ;

char* StopsNameFrom[] PROGMEM={
stop0from,stop1from,stop2from,stop3from};

char** StopsName[] PROGMEM= {
  StopsNameTo, StopsNameFrom};

char stop_name_aux[30];

void setup(){
  Serial.begin(9600);
  Serial.println(GetStopName(1,0));
}

void loop(){
}

char* GetStopName(int Stop_Id, char route)
{  
  strcpy_P(stop_name_aux, (PGM_P)pgm_read_word(&(StopsName[route][Stop_Id])));  
  return stop_name_aux;
}

route could be 0,1 and Stop_Id could be 0,1,2,3.

So basically, I want to retrieve the strings that are in the Flash memory, but the code above is not working. It prints random characters. The weiredest part is that if I hardcode the "route" var with 0 or 1 like this in the GetStopName function:

char* GetStopName(int Stop_Id, char route)
{  
  strcpy_P(stop_name_aux, (PGM_P)pgm_read_word(&(StopsName[0][Stop_Id])));  
  return stop_name_aux;
}

then it works, but it won't work if I pass the 0 value to the function through the route parameter like in the original code.

Also I've found out that if the code is like this:

char* GetStopName(int Stop_Id, char route)
{  
  if(!route)
  strcpy_P(stop_name_aux, (PGM_P)pgm_read_word(&(StopsName[route][Stop_Id])));  
  return stop_name_aux;
}

then it also works!

I think it's a PGMSPACE pointer and var issue. But I can't work it out.

Thanks!

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

char* StopsNameTo[] PROGMEM={
  stop0to,stop1to,stop2to,stop3to};

Is it really necessary to store the small pointers in PROGMEM? The 4 pointers only use 8 bytes.

PaulS:

char* StopsNameTo[] PROGMEM={

stop0to,stop1to,stop2to,stop3to};



Is it really necessary to store the small pointers in PROGMEM? The 4 pointers only use 8 bytes.

I guess not. I am doing what the reference website says avr-libc: Data in Program Space.

Also this example code has just 4 stops, but the original code might have up to 200 stops.

In general, the PROGMEM in older AVR-Toolchains contains a macro in avr/pgmspace.h that contains the following definition:

#define PROGMEM __ATTR_PROGMEM__
...

typedef char prog_char PROGMEM;

This is since a couple of Versions of the Toolchain, this "type" declaration marked as "Depricated". The reason is, that the GNU-C/C++ Compiler does'nt supports memory attribute on a type definitions. The recommended usage is, to use the attribute on the variable instead of the type definition. The explain and further programming of attribute are found here:http://www.codeforge.com/article/41705 .

if you define a uint8_t array of values, you need to declare them as const instead static.

const uint8_t arr[] = PROGMEM  {"this is just a test that shoud stored into the Programm memory space\n"};
uint8_t char_from_progmem;


uint8_t readstring_P (const uint8_t *str) 
 { 
    retbyte = pgm_read_byte (str)  ;
 return (retbyte);
 }


// ############# call that function ######################
char_from_progmem = readstring_P(arr);

The compiler does'nt need to know what memory segment the pointer of str points, just the address is needed. The access to the program memory do the
pgm_read_byte (str) function for us. This need just the address and assume, that the memory is the program space instead data segment.
The document from Dean Camera describe further details about the progmem handling.

bye the way, the depricated SIGNAL() function for ISR callbacks are also describted by Dean Camera and i would recomment his "actual" Documentation of all the hints about the newer toolchain and recommended programming.

rasco22862:
I am leading with a strange error using the PROGMEM space that I can't understand. Here is the temp code that I have been using to figure out the problem: