Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PROGMEM

Guide to PROGMEM on ESP8266 and Arduino IDE

Intro

On low memory devices like the arduino and esp8266 you do not want strings to be stored in RAM. This occurs by default on these systems. Declare a string const char * xyz = "this is a string" and it will use up RAM.

The solution on these devices is to allow strings to be stored in read only memory, in Arduino this is the PROGMEM macro. Most of my experience is with the ESP8266 which is a 32bit micros controller. This device stores PROGMEM data in flash. The macro PROGMEM on ESP8266 is simply

#define PROGMEM   ICACHE_RODATA_ATTR

Which in turn is defined by:

#define ICACHE_RODATA_ATTR  __attribute__((section(".irom.text")))

Which places the variable in the .irom.text section ie flash.

The key to understanding PROGMEM is to understand how the strings are stored and then how they are retrieved from flash.

Declare a global string to be stored in flash.

static const char xyz[] PROGMEM = "This is a string stored in flash";

Declare a flash string within code block.

For this you can use the PSTR macro. Which are all defined in pgmspace.h

#define PGM_P  		const char *
#define PGM_VOID_P  const void *
#define PSTR(s) (__extension__({static const char __c[] PROGMEM = (s); &__c[0];}))

In practice:

void myfunction(void) {
PGM_P xyz = PSTR("Store this string in flash");
const char * abc = PSTR("Also Store this string in flash");
}

The two examples above will store these strings in flash. To retrieve and manipulate flash strings is not straight forward as the esp8266 must read from flash in 4byte words. In the Arduino IDE for esp8266 there are several functions that can help retrieve strings from flash that have been stored using PROGMEM. Both of the examples above will give you a const char * back, however if you try to do anything with these pointers, without correct 32bit alignment you will get a seg fault and the esp will crash. You must read from the flash 32 bit aligned.

Functions to read back from PROGMEM

Which are all defined in pgmspace.h

int memcmp_P(const void* buf1, PGM_VOID_P buf2P, size_t size);
void* memccpy_P(void* dest, PGM_VOID_P src, int c, size_t count);
void* memmem_P(const void* buf, size_t bufSize, PGM_VOID_P findP, size_t findPSize);
void* memcpy_P(void* dest, PGM_VOID_P src, size_t count);
char* strncpy_P(char* dest, PGM_P src, size_t size);
#define strcpy_P(dest, src)          strncpy_P((dest), (src), SIZE_IRRELEVANT)
char* strncat_P(char* dest, PGM_P src, size_t size);
#define strcat_P(dest, src)          strncat_P((dest), (src), SIZE_IRRELEVANT)
int strncmp_P(const char* str1, PGM_P str2P, size_t size);
#define strcmp_P(str1, str2P)          strncmp_P((str1), (str2P), SIZE_IRRELEVANT)s
int strncasecmp_P(const char* str1, PGM_P str2P, size_t size);
#define strcasecmp_P(str1, str2P)          strncasecmp_P((str1), (str2P), SIZE_IRRELEVANT)
size_t strnlen_P(PGM_P s, size_t size);
#define strlen_P(strP)          strnlen_P((strP), SIZE_IRRELEVANT)
char* strstr_P(const char* haystack, PGM_P needle);
int	printf_P(PGM_P formatP, ...);
int	sprintf_P(char *str, PGM_P formatP, ...);
int	snprintf_P(char *str, size_t strSize, PGM_P formatP, ...);
int	vsnprintf_P(char *str, size_t strSize, PGM_P formatP, va_list ap);

There are a lot of functions there but in reality they are _P versions of standard c functions that are adapted to read from the esp8266 32bit aligned flash. All of them take a PGM_P which is essentially a const char *. Under the hood these functions all use:

#define pgm_read_byte(addr) 		                                           \
(__extension__({                                                               \
    PGM_P __local = (PGM_P)(addr);  /* isolate varible for macro expansion */         \
    ptrdiff_t __offset = ((uint32_t)__local & 0x00000003); /* byte aligned mask */            \
    const uint32_t* __addr32 = (const uint32_t*)((const uint8_t*)(__local)-__offset); \
    uint8_t __result = ((*__addr32) >> (__offset * 8));                        \
    __result;                                                                  \
}))

which reads backs the bytes without causing a seg fault.

This works well when you have designed a function as above that is specialised for dealing with PROGMEM pointers but there is no type checking except against const char *. This means that it is totally legitimate, as far as the compiler is concerned, for you to pass it any const char * string, which is obviously not true and will lead to undefined behaviour. This makes it impossible to create any overloaded functions that can use flash strings when they are defined as PGM_P. If you try you will get an ambiguous overload error as PGM_P == const char *.

Enter the __FlashStringHelper...

This is a wrapper class that allows flash strings to be used as a class, this means that type checking and function overloading can be used with flash strings. Most people will be familiar with the F() macro and possibly the FPSTR() macro. These are defined in WString.h:

#define FPSTR(pstr_pointer) (reinterpret_cast<const __FlashStringHelper *>(pstr_pointer))
#define F(string_literal) (FPSTR(PSTR(string_literal)))

So FSPTR() takes a PROGMEM pointer to a string and casts it to this __FlashStringHelper class. Thus if you have defined a string as above xyz you can use FPSTR() to convert it to __FlashStringHelper for passing into functions that take it.

static const char xyz[] PROGMEM = "This is a string stored in flash";
Serial.println(FPSTR(xyz));

The F() combines both of these methods to create an easy and quick way to store an inline string in flash, and return the type __FlashStringHelper. For example:

Serial.println(F("This is a string stored in flash"));

Although these two functions provide a similar function, they serve different roles. FPSTR() allows you to define a global flash string and then use it in any function that takes __FlashStringHelper. F() allows you to define these flash strings in place, but you can't use them anywhere else. The consequence of this is sharing common strings is possible using FPSTR() but not F(). __FlashStringHelper is what the String class uses to overload its constructor:

String(const char *cstr = ""); // constructor from const char * 
String(const String &str); // copy constructor
String(const __FlashStringHelper *str); // constructor for flash strings 

This allows you to write:

String mystring(F("This string is stored in flash"));

How do I write a function to use __FlashStringHelper?

Simples: cast the pointer back to a PGM_P and use the _P functions shown above. This an example implementation for String for the concat function.

unsigned char String::concat(const __FlashStringHelper * str) {
    if (!str) return 0; // return if the pointer is void
    int length = strlen_P((PGM_P)str); // cast it to PGM_P, which is basically const char *, and measure it using the _P version of strlen.
    if (length == 0) return 1;
    unsigned int newlen = len + length;
    if (!reserve(newlen)) return 0; // create a buffer of the correct length
    strcpy_P(buffer + len, (PGM_P)str); //copy the string in using strcpy_P
    len = newlen;
    return 1;
}

How do I declare a global flash string and use it?

static const char xyz[] PROGMEM = "This is a string stored in flash. Len = %u";

void setup() {
    Serial.begin(115200); Serial.println(); 
    Serial.println( FPSTR(xyz) ); // just prints the string, must convert it to FlashStringHelper first using FPSTR(). 
    Serial.printf_P( xyz, strlen_P(xyz)); // use printf with PROGMEM string
}

How do I use inline flash strings?

void setup() {
    Serial.begin(115200); Serial.println(); 
    Serial.println( F("This is an inline string")); // 
    Serial.printf_P( PSTR("This is an inline string using printf %s"), "hello");
}

How do I declare and use data in PROGMEM?

const size_t len_xyz = 30;
const uint8_t xyz[] PROGMEM = {
  0x53, 0x61, 0x79, 0x20, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 
  0x74, 0x6f, 0x20, 0x4d, 0x79, 0x20, 0x4c, 0x69, 0x74, 0x74, 
  0x6c, 0x65, 0x20, 0x46, 0x72, 0x69, 0x65, 0x6e, 0x64, 0x00};

 void setup() {
     Serial.begin(115200); Serial.println(); 
     uint8_t * buf = new uint8_t[len_xyz];
     if (buf) {
      memcpy_P(buf, xyz, len_xyz);
      Serial.write(buf, len_xyz); // output the buffer. 
     }
 }

How do I declare some data in PROGMEM, and retrieve one byte from it.

Declare the data as done previously, then use pgm_read_byte to get the value back.

const size_t len_xyz = 30;
const uint8_t xyz[] PROGMEM = {
  0x53, 0x61, 0x79, 0x20, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20,
  0x74, 0x6f, 0x20, 0x4d, 0x79, 0x20, 0x4c, 0x69, 0x74, 0x74,
  0x6c, 0x65, 0x20, 0x46, 0x72, 0x69, 0x65, 0x6e, 0x64, 0x00
};

void setup() {
  Serial.begin(115200); Serial.println();
  for (int i = 0; i < len_xyz; i++) {
    uint8_t byteval = pgm_read_byte(xyz + i);
    Serial.write(byteval); // output the buffer.
  }
}

In summary

It is easy to store strings in flash using PROGMEM and PSTR but you have to create functions that specifically use the pointers they generate as they are basically const char *. On the other hand FPSTR and F() give you a class that you can do implicit conversions from, very useful when overloading functions, and doing implicit type conversions. It is worth adding that if you wish to store an int, float or pointer these can be stored and read back directly as they are 4 bytes in size and therefor will be always aligned!

Hope this helps.

@SizL75

This comment has been minimized.

Copy link

commented Mar 13, 2017

Great gist! Last code block seems like it's a duplicate of the previous block, not the pgm_read_byte example though..

@milkpirate

This comment has been minimized.

Copy link

commented Apr 14, 2017

Hey,

I would like to put my wifi crdentials into flash like so:

static const char ssid[] PROGMEM = "ssid";
static const char pass[] PROGMEM = "pass";
...
WiFi.begin(FPSTR(ssid), FPSTR(pass));

But the compiler doesnt seem to be happy about that:

no matching function for call to 'ESP8266WiFiClass::begin(const __FlashStringHelper*, const __FlashStringHelper*)'

and WiFi.begin((const char*)FPSTR(ssid), (const char*)FPSTR(pass)); causes segfaults. Any suggestions?

@ClaudioHutte

This comment has been minimized.

Copy link

commented Apr 26, 2017

@milkpirate You can pass the pointer to a string stored in flash only if the function is designed to deal with it, WiFi.begin does not seem to accept strings stored in flash. Look at the example of the String.concat provided in the guide above, the function accept an argument str declared as const __FlashStringHelper* because internally it uses the specific functions (such as strcpy_P) that provide for the correct alignment to retrieve the actual string. I haven't looked at it, but evidently WiFi.begin does not perform these tasks so you have to deal with them by yourself such as:

strcpy_P(_ssid, ssid);
strcpy_P(_pass, pass);
WiFi.begin(_ssid, _pass);
@sticilface

This comment has been minimized.

Copy link
Owner Author

commented May 3, 2017

updated to correct the pgm read bit

@JamesNewton

This comment has been minimized.

Copy link

commented Feb 2, 2018

In the last example, why not use sizeof(xyz) instead of manually declaring len_xyz

@uDude

This comment has been minimized.

Copy link

commented Apr 18, 2019

Why not just alter the ld script? Note: that ram is also on the SPI flash chip. The ESP8266 does not have ram in the chip, it is just partitioned from the flash.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.