On an ESP32: Is using the F()Macro as pointless as using PROGMEM?

ullixesp
Posts: 83
Joined: Wed Oct 16, 2019 9:34 am
Location: Germany

On an ESP32: Is using the F()Macro as pointless as using PROGMEM?

Postby ullixesp » Mon Apr 26, 2021 4:27 pm

The Arduino pages are full with recommendations on using PROGMEM to keep const data in Flash. However, this is unnecessary, as any const declared data are kept in Flash anyway. PROGMEM on a ESP32 is actually only a dummy, kept for code compatibility. The reason behind it, as I had learned, has to do with von Neumann vs. Harvard architecture.

Equally hotly advised is the use of the F() macro, like in 'F("This is a very long string")'. But my understanding is that the F()macro is only a means to make the string 'progmem'. Which would also be pointless on an ESP32.

Is that true?

ESP_Sprite
Posts: 9730
Joined: Thu Nov 26, 2015 4:08 am

Re: On an ESP32: Is using the F()Macro as pointless as using PROGMEM?

Postby ESP_Sprite » Wed Apr 28, 2021 5:58 am

Yes.

ullixesp
Posts: 83
Joined: Wed Oct 16, 2019 9:34 am
Location: Germany

Re: On an ESP32: Is using the F()Macro as pointless as using PROGMEM?

Postby ullixesp » Wed Apr 28, 2021 8:06 am

That's what I had thought.

Now I went ahead and removed all of the F()s in my code (bin total is ~1MB on an ESP32) and looked at code size differences with xtensa-esp32-elf-size. Without the F()s I got a reduction in text of 400 bytes, and in data of 28 bytes.

Not that much after all, yet I had thought the F()s do nothing. And not only that, it seems it is actually counterproductive using F()s on an ESP32. I am surprised.

lbernstone
Posts: 828
Joined: Mon Jul 22, 2019 3:20 pm

Re: On an ESP32: Is using the F()Macro as pointless as using PROGMEM?

Postby lbernstone » Wed Apr 28, 2021 1:24 pm

Note that const arrays are stored in the program memory (duh) and then mapped directly from flash into memory as needed. So, no flashstringhelper is needed- just a constant assignment. This allows you to store large files (eg javascripts or images) in your code as uint8_t arrays, and update them as part of your firmware. I often have a separate "bytefiles.h" that I include which defines variables like:

Code: Select all

const char* statusHtml = R"literal(
<html>
...
</html>
)literal";

const char favicon_jpg[] = {
  0xff, 0xd8, 0xff, 0xe0, 0x00, 0x10, 0x4a, 0x46, 0x49, 0x46, 0x00, 0x01,
...
  0xff, 0xd9
};
const int favicon_jpg_len = 4226;

ullixesp
Posts: 83
Joined: Wed Oct 16, 2019 9:34 am
Location: Germany

Re: On an ESP32: Is using the F()Macro as pointless as using PROGMEM?

Postby ullixesp » Fri Apr 30, 2021 7:58 am

Yes, those 2 constructs look very familiar :). In particular the R-stuff is very helpful for HTML code.
Good to hear that it is not only convenient but also good practise. Thanks.

ullixesp
Posts: 83
Joined: Wed Oct 16, 2019 9:34 am
Location: Germany

Re: On an ESP32: Is using the F()Macro as pointless as using PROGMEM?

Postby ullixesp » Tue May 25, 2021 12:10 pm

Are these arguments valid only for the ESP32, or is the use of PROGMEM and F()-Macro equally pointless on ESP8266 ?

Who is online

Users browsing this forum: No registered users and 45 guests