-
Notifications
You must be signed in to change notification settings - Fork 5
Description
Background
Not all static const
data actually ends up in flash for some Arduino targets.
The Arduino PROGMEM keyword is required to:
Keep constant data in flash (program) memory only, instead of copying it to SRAM when the program starts.
PROGMEM
is useful only when working with AVR boards (UNO R3, Leonardo etc.). Newer boards (Due, MKR WiFi 1010, GIGA R1 WiFi etc.) automatically use the program space when a variable is declared as a const. However, for retro compatibility,PROGMEM
can still be used with newer boards. This implementation currently lives here.
Current State
The wolfSSL library already has a feature for helping to control flash: FLASH_QUALIFIER, for example this Te[4][256]
declaration that is problematic in getting the AES CTR example to compile and run on the 2K RAM Arduino Uno.
Proposed Change
There's a #if defined(__AVR__)
in the wolfssl/wolfcrypt/settings.h
:
Here, we could add a FLASH_QUALIFIER
like this:
#if defined(__AVR__)
#include <avr/pgmspace.h>
#define FLASH_QUALIFIER PROGMEM
... etc
Complexity Introduced
However, for the AVR targets, it is much more of an issue that just the FLASH_QUALIFIER
. Setting this to PROGMEM
alone will not work.
See the additional sample code in the Arduino Docs:
const PROGMEM uint16_t charSet[] = { 65000, 32796, 16843, 10, 11234};
...
for (byte k = 0; k < 5; k++) {
displayInt = pgm_read_word_near(charSet + k);
Serial.println(displayInt);
}
Note the special function pgm_read_word_near()
needed to read from flash.
In our case, the elements of the Te
array CANNOT be accessed via Te[i][j]
when stored in flash with PROGMEM
.
Instead, conditional logic or a macro would need to be used in every instance of FLASH_QUALIFIER
. Clearly not a desirable solution.
Perhaps there are more graceful ways to handle this?
Additional Info
- Moving wolfSSL Arduino examples: Move Arduino examples, add GitHub workflow wolfssl-examples#499
- Fix ARduino print, AVR WOLFSSL_USER_IO: Fix Arduino progmem print, AVR WOLFSSL_USER_IO wolfssl#8668
- Stackoverflow: How to transplant pgm_read_byte inside Arduino to other systems?
- Arduino forum: PROGMEM and arrays