Writing password to flash memory on Due

dear friend,

my program is running & not retrieving password from flash, please help

i am using due

deuflash.ino (1.67 KB)

if i use

configuration.pass = "chnewpass";

instead of

chnewpass[0]='1';
chnewpass[1]='2';
chnewpass[2]='3';
chnewpass[3]='4';
configuration.pass = (chnewpass);

it is working but i need to pass it through a variable as it is received from keypad.

thanks in advance

You didn't use DueFlashStorageStructureExample.ino properly.

In this modified version, the password is retrieved:

#include <DueFlashStorage.h>
DueFlashStorage dueFlashStorage;

char chnewpass[5];

struct Configuration {

  char* pass;

};

Configuration configuration;

void setup() {

  Serial.begin(115200);
  
  chnewpass[0] = '1';
  chnewpass[1] = '2';
  chnewpass[2] = '3';
  chnewpass[3] = '4';
  chnewpass[4] = '5';

  configuration.pass = (chnewpass);
  /* Flash is erased every time new code is uploaded. Write the default configuration to flash if first time */
  // running for the first time?
  uint8_t codeRunningForTheFirstTime = dueFlashStorage.read(0); // flash bytes will be 255 at first run
  Serial.print("codeRunningForTheFirstTime: ");
  if (codeRunningForTheFirstTime) {
    Serial.println("yes");
    /* OK first time running, set defaults */

    // write configuration struct to flash at adress 4
    byte b2[sizeof(Configuration)]; // create byte array to store the struct
    memcpy(b2, &configuration, sizeof(Configuration)); // copy the struct to the byte array
    dueFlashStorage.write(4, b2, sizeof(Configuration)); // write byte array to flash

    // write 0 to address 0 to indicate that it is not the first time running anymore
    dueFlashStorage.write(0, 0);
  }
  else {
    Serial.println("no");
  }
}


void loop() {
  byte* b = dueFlashStorage.readAddress(4); // byte array which is read from flash at adress 4
  Configuration configurationFromFlash; // create a temporary struct
  memcpy(&configurationFromFlash, b, sizeof(Configuration)); // copy byte array to temporary struct
  Serial.print(" pass: ");
  
  for (int i = 0; i < 5; i++) {
    Serial.print(configurationFromFlash.pass[i]);
  }

  Serial.println();
  delay(1000);
}

dear friend,

the below code should be run only once so i placed it inside "if (codeRunningForTheFirstTime)" loop

then this not working

chnewpass[0] = '1';
chnewpass[1] = '2';
chnewpass[2] = '3';
chnewpass[3] = '4';
chnewpass[4] = '5';

configuration.pass = (chnewpass);

why ?

i cross checked this "Serial.print(configurationFromFlash.pass*);"*
is just using "configuration.pass = (chnewpass);" and printing

if i use

configuration.pass = "chnewpass";

inside "if (codeRunningForTheFirstTime)" loop

it is working

but ............

if i place the below code inside "if (codeRunningForTheFirstTime)" loop

then this not working

chnewpass[0] = '1';
chnewpass[1] = '2';
chnewpass[2] = '3';
chnewpass[3] = '4';
chnewpass[4] = '5';

configuration.pass = (chnewpass);

why ?

thanks for your effort Mr. ard_newbie

new update

after changing my code to the below --

the password is changing to 1234 instead of 4321 after power off /reset

it means password is not stored in flash memory

#include <DueFlashStorage.h>
DueFlashStorage dueFlashStorage;
String newpass , flashpass;

 char chnewpass[5];

struct Configuration {
  
  char* pass;
  
};


Configuration configuration;

void setup() {
 chnewpass[0]='1';
      chnewpass[1]='2';
      chnewpass[2]='3';
      chnewpass[3]='4';
      chnewpass[4]='\0';
      
 Serial.begin(115200);
  delay(500);
   
    
  /* Flash is erased every time new code is uploaded. Write the default configuration to flash if first time */
  // running for the first time?
  uint8_t codeRunningForTheFirstTime = dueFlashStorage.read(10); // flash bytes will be 255 at first run
  Serial.print("codeRunningForTheFirstTime: ");
  if (codeRunningForTheFirstTime) {
    Serial.println("yes");




    chnewpass[0]='4';
      chnewpass[1]='3';
      chnewpass[2]='2';
      chnewpass[3]='1';
      chnewpass[4]='\0';
      
configuration.pass = (chnewpass);
   delay(500);
    //OK first time running, set defaults 
   
      for(int i = 0; i < 5; i++)
{delay(500);
  Serial.print("chnewpass byte array: " );
Serial.print(i);
Serial.print( " = ");
  Serial.println (chnewpass[i]);}
  
        for(int i = 0; i < 5; i++)
{delay(500);
  Serial.print("configuration.pass: " );
Serial.print(i);
Serial.print( " = ");
  Serial.println (configuration.pass[i]);}
  
    // write configuration struct to flash at adress 4
    byte b2[sizeof(Configuration)]; // create byte array to store the struct
    memcpy(b2, &configuration, sizeof(Configuration)); // copy the struct to the byte array
    delay(500);
    dueFlashStorage.write(4, b2, sizeof(Configuration)); // write byte array to flash
    delay(500);
    // write 0 to address 0 to indicate that it is not the first time running anymore
   dueFlashStorage.write(10, 0); 
  }
  else {
     delay(500);
    Serial.println("no");
  }
}


void loop() {
  
 byte* b = dueFlashStorage.readAddress(4); // byte array which is read from flash at adress 4
  Configuration configurationFromFlash; // create a temporary struct
  memcpy(&configurationFromFlash, b, sizeof(Configuration)); // copy byte array to temporary struct

Serial.print(" configurationFromFlash.pass:");
Serial.println(configurationFromFlash.pass);
Serial.print(" pass:");


  flashpass = configurationFromFlash.pass;
Serial.println(flashpass);
//Serial.println(flashpass);

  Serial.println();
  delay(10000);}