-->
Page 1 of 1

Having such a hard time with PROGMEM.......resets ESP....

PostPosted: Mon Oct 26, 2015 9:02 pm
by winneymj
Hi,
I am having such a hard time reading from PROGMEM. Below is a sample program that I though would work, but it causes the ESP to reboot/reset. Any ideas what I am doing wrong here. FYI this is being compiled and uploaded in the Arduino IDE.
Appreciated
Mark.

Code: Select all#include <pgmspace.h>

static const char PROGMEM Line1[] = {"String 1"};
static const char PROGMEM Line2[] = {"String 2"};
static const char PROGMEM Line3[] = {"String 3"};

const char * const Lines [] PROGMEM = { Line1, Line2, Line3 };

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  delay(5000);

  char buffer[20] = {0};
  strcpy_P(buffer, (char*) pgm_read_word( &(Lines[0]) ) );
   
  Serial.print("buffer=");
  Serial.println(buffer);
}

void loop() {
  // put your main code here, to run repeatedly:
}

Re: Having such a hard time with PROGMEM.......resets ESP...

PostPosted: Tue Oct 27, 2015 1:22 am
by eduperez
If "Lines" is defined as an array of "char *", then "Lines[0]" is a pointer to the first item in the array; thus, in the call to pgm_read_word, you do not need to add and extra "&". I would guess that you should do "pgm_read_word(Lines[0])" instead.

Hope my rusty C is not that rusty...

Re: Having such a hard time with PROGMEM.......resets ESP...

PostPosted: Tue Oct 27, 2015 5:11 pm
by winneymj
eduperez wrote:If "Lines" is defined as an array of "char *", then "Lines[0]" is a pointer to the first item in the array; thus, in the call to pgm_read_word, you do not need to add and extra "&". I would guess that you should do "pgm_read_word(Lines[0])" instead.

Hope my rusty C is not that rusty...

Hi,
I figured out the problem. It seems that pointers on the ESP8266 are 4 bytes (32 bits) and the call pgm_read_word only returns a 16 bit number. It needs to be changed to pgm_read_dword to get back a 32 bit pointer. New code sample is below.

Code: Select all#include <pgmspace.h>

static const char PROGMEM Line1[] = {"String 1"};
static const char PROGMEM Line2[] = {"String 2"};
static const char PROGMEM Line3[] = {"String 3"};

const char * const Lines [] PROGMEM = { Line1, Line2, Line3 };

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  delay(5000);

  char *ptrSize;
 
  Serial.print("prtSize=");
  Serial.println(sizeof(ptrSize));

  char buffer[20] = {0};
  strcpy_P(buffer, (char*) pgm_read_dword(&(Lines[0])));
   
  Serial.print("buffer=");
  Serial.println(buffer);
}

void loop() {
  // put your main code here, to run repeatedly:

}