Overwrite files on flash, it has no effect ...

Status
Not open for further replies.

darioconcilio

Well-known member
Hi guys,
I writed a simple project because I'm trying to overwrite a config file with different value, but it seems that it not takes effect.

Synthesizing...

This sketch read MYFILE.CFG, that contains 81 chars, afterthat it waits an input 1, 2, or 3, then it tryes to overwrite content file with new information (new 81 chars)

I'm using Flash memory S25FL256S, and I connect in this way (Attached 16-Lead):
HOLD (FLASH) => VCC (Teensy3.2)
VCC => VCC
CS => PIN 9 (CS)
SO/IO1 => PIN 12 (DIN)
SCK => PIN 14 (CLK)
SI/IO0 => PIN 11 (DOUT)
WP#/IO2 => VCC
VSS => GND

NOTE: I already created MYFILE.CFG (into flash) using CopyFromSD sketch (first time), It works all.

Using this sketch, I found this monitor event message...

[Serial Monitor]

Init FLASH connection: OK
Opening file MYFILE.CFG ...done.
Loading data... done.
000025512500012550000000002100000255000325500000000040000000000005150601570158000

[In this point insert, for ex. char "1"]

Opening file MYFILE.CFG ...done.
Writing file 1 ... done.

[It waits 10 second and try to read file again]

Opening file MYFILE.CFG ...not found!!!


I tryed to use erase method also, but it seems not work.

If I reset Teensy 3.2, I found original content file showed above.

Where I wrong?!?

Code:
#include <SerialFlash.h>
#include <SPI.h>

#define FLASH_CS_PIN    9    //Serial FlashControllo
#define FLASH_MOSI_PIN  11   //Serial FlashLettura
#define FLASH_MISO_PIN  12   //Serial FlashScrittura
#define FLASH_SCK_PIN   14   //Serial FlashClock di comunicazione

#define MESSAGE_LENGTH 81

const char* fileName = "MYFILE.CFG";
const char* set1 = "000000025500012550000000002000000255000325500000000040000000000005150601570158002";
const char* set2 = "025500025500012550000000002000123255000325504600000040000000000005150601570158000";
const char* set3 = "012500025500012550000000002000100255000325512800000040000000000005150601570158001";

SerialFlashFile configFile;

void setup() {
  SPI.setSCK(FLASH_SCK_PIN);
  SPI.setMOSI(FLASH_MOSI_PIN);
  SPI.setMISO(FLASH_MISO_PIN);

  while (!Serial) ;
  delay(100);

  //Init
  initFlashMemory();

  //First reading
  readConfig();
}

void loop() {

  if (Serial.available() > 0) {

    byte inByte = Serial.read();

    writeConfig(inByte);

    delay(10000);

    readConfig();

  }

}

void initFlashMemory() {

  Serial.println("OK");
  Serial.print("Init FLASH connection: ");

  if (!SerialFlash.begin(FLASH_CS_PIN)) {
    while (1) {
      Serial.println("Unable to access the flash memory");
      delay(500);
    }
  }
  Serial.println("OK");

}

void readConfig(void) {

  Serial.print("Opening file ");
  Serial.print(fileName);
  Serial.print(" ...");

  if (SerialFlash.exists(fileName)) {

    configFile = SerialFlash.open(fileName);

    byte data[MESSAGE_LENGTH];

    Serial.println("done.");
    Serial.print("Loading data... ");

    configFile.read(data, MESSAGE_LENGTH);

    configFile.close();

    Serial.println("done.");

    for (byte i = 0; i < MESSAGE_LENGTH; i++) {
      Serial.print((char)data[i]);
    }
    Serial.println();

  }
  else
    Serial.println("not found!!!");

}

void writeConfig(byte setId) {

  Serial.print("Opening file ");
  Serial.print(fileName);
  Serial.print(" ...");

  if (SerialFlash.exists(fileName)) {

    configFile = SerialFlash.open(fileName);

    byte data[MESSAGE_LENGTH];

    Serial.println("done.");

    if (setId == 1) {
      for (byte i = 0; i < MESSAGE_LENGTH; i++)
        data[i] = (byte) set1[i];
    } else if (setId == 2) {
      for (byte i = 0; i < MESSAGE_LENGTH; i++)
        data[i] = (byte) set2[i];
    } else if (setId == 3) {
      for (byte i = 0; i < MESSAGE_LENGTH; i++)
        data[i] = (byte) set3[i];
    }

//    Serial.print("Erasing file ...");
//    configFile.erase();
//    Serial.println("done.");

    Serial.print("Writing file ");
    Serial.print((char)setId);
    Serial.print(" ... ");
    configFile.write(data, MESSAGE_LENGTH);

    configFile.close();

    Serial.println("done.");

  }
  else
    Serial.println("not found!!!");

}
 

Attachments

  • Schermata 2016-09-28 alle 07.42.26.png
    Schermata 2016-09-28 alle 07.42.26.png
    31.6 KB · Views: 76
Last edited:
As documented at https://github.com/PaulStoffregen/SerialFlash
The SerialFlash file system is very limited/simple. You cannot rewrite an existing file (well, you can change 1 bits to 0 bits). You need to remove it, then create it again. Inspect the CopyFromSd example to see how it removes a file if it already exists.

the erase() option works only if the file was originally created with createErasable()
 
Last edited:
Strange...... I'm reading CopyFromSD sketch and I not found createErasable method but create method.

A portion skecth of ufficial CopyFromSD, I read that it uses remove and create methods.

Code:
Serial.print(ff.size());
        Serial.println(" bytes");
      }
      // delete the copy on the Flash chip, if different
      Serial.println("  delete file from Flash chip");
      SerialFlash.remove(filename);
    }

    // create the file on the Flash chip and copy data
    if (SerialFlash.create(filename, length)) {
      SerialFlashFile ff = SerialFlash.open(filename);
      if (ff) {
        Serial.print("  copying");
        // copy data loop
        unsigned long count = 0;
        unsigned char dotcount = 9;

How ever I tryed to follow your way and I upgrade my sketch in this approach, I create a first file SETTING.CFG, afterthat I try to remove and create again my file.
If I use create OR createErasable, I can't open file.

Code:
#include <SerialFlash.h>
#include <SPI.h>

#define FLASH_CS_PIN    9    //Serial FlashControllo
#define FLASH_MOSI_PIN  11   //Serial FlashLettura
#define FLASH_MISO_PIN  12   //Serial FlashScrittura
#define FLASH_SCK_PIN   14   //Serial FlashClock di comunicazione

#define MESSAGE_LENGTH 81

const char* fileName = "SETTING.CFG";
const char* set1 = "011100025500012550000000002000000255000325500000000040000000000005150601570158002";
const char* set2 = "022200025500012550000000002000123255000325504600000040000000000005150601570158000";
const char* set3 = "025500025500012550000000002000100255000325512800000040000000000005150601570158001";
const char* set4 = "025525525500012550000000002000100255000325512800000040000000000005150601570158001";

SerialFlashFile configFile;

void setup() {
  SPI.setSCK(FLASH_SCK_PIN);
  SPI.setMOSI(FLASH_MOSI_PIN);
  SPI.setMISO(FLASH_MISO_PIN);

  unsigned long startMillis = millis();
  while (!Serial && (millis() - startMillis < 10000)) ;
  delay(100);

  Serial.println("1.0");

  //Init
  initFlashMemory();

  //FirstFile erasable
  createFirstConfig();

  //First reading
  readConfig();
}

void loop() {

  if (Serial.available() > 0) {

    byte inByte = Serial.read();

    writeConfig(inByte);

    delay(10000);

    readConfig();

  }

}

void initFlashMemory() {

  Serial.print("Init FLASH connection: ");

  if (!SerialFlash.begin(FLASH_CS_PIN)) {
    while (1) {
      Serial.println("Unable to access the flash memory");
      delay(500);
    }
  }
  Serial.println("OK");

}

void createFirstConfig(void) {

  Serial.print("Creating first eraseable file...");
  if (SerialFlash.create(fileName, MESSAGE_LENGTH)) {
  //if (SerialFlash.createErasable(fileName, MESSAGE_LENGTH)) {
    SerialFlashFile ff = SerialFlash.open(fileName);
    if (ff) {
      byte data[MESSAGE_LENGTH];

      for (byte i = 0; i < MESSAGE_LENGTH; i++)
        data[i] = (byte) set4[i];

      ff.write(data, MESSAGE_LENGTH);
      ff.close();
      Serial.println("OK");
    }
    else
      Serial.println("KO: Cannot open erasable file");
  } else
    Serial.println("KO: Cannot create erasable file");
}

void readConfig(void) {

  Serial.print("Opening file ");
  Serial.print(fileName);
  Serial.print(" ...");

  if (SerialFlash.exists(fileName)) {

    configFile = SerialFlash.open(fileName);

    byte data[MESSAGE_LENGTH];

    Serial.println("done.");
    Serial.print("Loading data... ");

    configFile.read(data, MESSAGE_LENGTH);

    configFile.close();

    Serial.println("done.");

    for (byte i = 0; i < MESSAGE_LENGTH; i++) {
      Serial.print((char)data[i]);
    }
    Serial.println();

  }
  else
    Serial.println("not found!!!");

}

void writeConfig(byte setId) {

  Serial.print("Overwriting file ");
  Serial.print(fileName);
  Serial.print(" ...");

  if (SerialFlash.exists(fileName)) {

    SerialFlash.remove(fileName);

    if (SerialFlash.create(fileName, MESSAGE_LENGTH)) {
    //if (SerialFlash.createErasable(fileName, MESSAGE_LENGTH)) {
      configFile = SerialFlash.open(fileName);

      byte data[MESSAGE_LENGTH];

      Serial.println("done.");

      if (setId == 1) {
        for (byte i = 0; i < MESSAGE_LENGTH; i++)
          data[i] = (byte) set1[i];
      } else if (setId == 2) {
        for (byte i = 0; i < MESSAGE_LENGTH; i++)
          data[i] = (byte) set2[i];
      } else if (setId == 3) {
        for (byte i = 0; i < MESSAGE_LENGTH; i++)
          data[i] = (byte) set3[i];
      }

      //    Serial.print("Erasing file ...");
      //    configFile.erase();
      //    Serial.println("done.");

      Serial.print("Writing file ");
      Serial.print((char)setId);
      Serial.print(" ... ");
      configFile.write(data, MESSAGE_LENGTH);

      configFile.close();

      Serial.println("done.");
    }
  }
  else
    Serial.println("not found!!!");

}

Serial Monitor:

1.0
Init FLASH connection: OK
Creating first eraseable file...KO: Cannot open erasable file
Opening file SETTING.CFG ...not found!!!


Have you got any suggest?
 
Strange...... I'm reading CopyFromSD sketch and I not found createErasable method but create method.
Correct. That's why your erase() wouldn't work in your original tests.

I ran your sketch and get
Code:
1.0
Init FLASH connection: OK
Creating first eraseable file...OK
Opening file SETTING.CFG ...done.
Loading data... done.
025525525500012550000000002000100255000325512800000040000000000005150601570158001
works for me

Since you are probably entering ASCII data, you need to change your if's to
if (setId == '1') {

I did a 1 and got
Code:
Overwriting file SETTING.CFG ...done.
Writing file 1 ... done.
Opening file SETTING.CFG ...done.
Loading data... done.
50000000002000100255   ... ? some garbage
so there is still a problem
 
Last edited:
ahh, create only works first time. you need to add the check for existence in first create. if file already exists, remove it first.

if should check if (setId == '1') { (single quote not quote)

and that fixed the garbage problem. you probably need a final else for your setId check to write something useful if user did not type in 1 2 or 3
(note remove() doesn't free the space on the flash, so evenutally your flash will need to have a full erase ...)

Code:
1.0
Init FLASH connection: OK
Creating first eraseable file...OK
Opening file SETTING.CFG ...done.
Loading data... done.
025525525500012550000000002000100255000325512800000040000000000005150601570158001
Overwriting file SETTING.CFG ...done.
Writing file 3 ... done.
Opening file SETTING.CFG ...done.
Loading data... done.
025500025500012550000000002000100255000325512800000040000000000005150601570158001
 
Last edited:
I don't understand where I wrong, can you show me your connections Teensy3.2-Flash?
And then, in WrtiteConfig method is correct SerialFlash.remove(fileName); or I have to use erase?

WP is to HIGH? (VCC)
 
Last edited:
Status
Not open for further replies.
Back
Top