0

I am trying to dynamically allocate memory for a char pointer stored in the flash memory.

I need to read a file from flash memory using LittleFS file system and copy it to a character array which also needs to be stored in the flash memory using PROGMEM. I cannot store it in the RAM because of limited space. Also I cannot hardcode the character array directly in the sketch, because I need the ability to change the file during runtime and have it persist after reboot.

If I don't use PROGMEM for the char pointer, the sketch works fine. But adding PROGMEM causes the ESP8266 to throw an exception and reboot and become an infinite loop. This is a simplified sketch showing what I'm trying to achieve.

#include "LittleFS.h"
char* arr PROGMEM;
void setup() {
  Serial.begin(115200);
  LittleFS.begin();
  
  File file = LittleFS.open("/test.txt","r");
  arr = (char*)malloc(file.size()+sizeof(char));
  int len = file.size()/sizeof(char);
  for(int i = 0; i < len; i++){
    arr[i] = file.read();
  }
  arr[len] = '\0';

  while(1){
    Serial.println(arr);
    delay(1000);
  }
}
void loop(){}

It is for the WifiClientSecure library. The class is BearSSL::X509List. To create an object for this class I need to send a char pointer as argument to the constructor. The char pointer should contain address to the SSL certificate string. I have my SSL certificate in the sketch data folder as "ca.crt".

0

1 Answer 1

1

PROGMEM is processed by the linker at build time. Linker positions the array into flash memory address space. Only constants can use the PROGMEM directive.

malloc allocates heap memory which is an address range in the dynamic RAM. It is possible to write to flash at runtime like the LittleFS library does, but that is not done with malloc.

Process the file as you read. Do it the same way you planed to process the array read from file.


For the WifiClientSecure you can use certificates from LittleFS with CertStoreBearSSL.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for the clarifications. I was not planning to process the array. I just need the contents of the file in a char array to pass it as an argument to another function. And I cannot have the char array in the RAM because of limited space. So the only option is having the char array in the flash memory.
you couldn't pass it to the function from the flash if the function doesn't support reading from flash. what function is it? your or library? if your, modify it to read from file. if from library then see if there is some other variant of the function, which can read from file or Stream.
It is the WifiClientSecure library. The class is BearSSL::X509List. To create an object for this class I need to send a char pointer as argument to the constructor. The char pointer should contain address to the SSL certificate string. I have my SSL certificate in the sketch data folder as "ca.crt".
can't you use the CertStoreBearSSL for this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.