Data corruption when writing into external flash in P1

Hello,
I am using the flashee-eeprom library to write and read data, to and from the flash respectively. I am writing 128KB of data and reading it after the 128KB is filled. However, about 10 bytes(or less) of 128 KB are getting corrupted.

The flash write is disabled while flash read is active. The flash write is enabled once the data is completely read from flash. I have no issues with writing alphabets into the flash and reading them. I am experiencing data corruption with numbers. Even number chars(‘3’) are getting corrupted.

Here is my code:

 #include "flashee-eeprom.h"

using namespace Flashee;

 #define FLASHSIZE               128000  //bytes
FlashDevice * flash;

Timer perMilliSec     ( 1, &milliSecCallBack );
Timer perSecs       ( 1000, &secsCallBack );

bool recordFlag = true;

void milliSecCallBack()
{
    if(recordFlag) {
      static long flashAddr = 0;

      char alpha = 'a';
      flash->write(alpha, flashAddr);
      flashAddr += 1;  //storing 1 byte

      alpha = 'b';
      flash->write(alpha, flashAddr);
      flashAddr += 1;  
      
      alpha = '3';
      flash->write(alpha, flashAddr);
      flashAddr += 1;  

      alpha = '2';
      flash->write(alpha, flashAddr);
      flashAddr += 1;  

      alpha = '1';
      flash->write(alpha, flashAddr);
      flashAddr += 1; 

      alpha = '8';
      flash->write(alpha, flashAddr);
      flashAddr += 1;  
      

      if(flashAddr > FLASHSIZE){
        flashAddr = 0;
        recordFlag = false;
      }
    }
}

void secsCallBack()
{
    static int reentrantFlag = 1; //flag to wait till the flash is read
    if( !recordFlag && reentrantFlag ) {

      reentrantFlag = 0;

      static long flashAddr = 0;
      char readAlpha;

      //for loop
      static long flashAddress = 0;
      for(flashAddress; flashAddress<=FLASHSIZE; flashAddress=flashAddress+6){
        for(int a=0; a<6; a++){
            readAlpha = 'z';
            flash->read(readAlpha, flashAddress+a);
            Serial.print(readAlpha);
        }
        Serial.println("");
      }

      if(flashAddress > FLASHSIZE ){
        Serial.println("---");


        flashAddress = 0;   //reset address counter
        reentrantFlag = 1;
        recordFlag = true;  //start capturing again
      }

    }
}


void setup()
{
    flash = Devices::createDefaultStore();

    //start timer callbacks
    perMilliSec.start();
    perSecs.start();
}


void loop()
{
}

###Explanation:
Code is nothing fancy.

  1. Defined a flash size of 128 KB.
  2. Pattern ab3218 which is 6 bytes is being written into the flash in 1 ms callback timer
  3. The flash is being read in 1 second timer callback.
  4. Pattern is printed to serial. The number of lines on serial are 128000/6 = 21333.33, 21334 lines

###Results:
Tested with the 2 patterns

  1. abcdef = No data corruption
  2. ab3218 = data corruption
    These are the corrupted patterns from three 128 KB data sets
    �b3218
    a�3218
    ab32�8
    �b3218

###Observations:

  1. What I observed was the data is getting corrupted when I deal with numbers.
  2. After sometime the P1 ends in long blinking cyan with intermittent flashing yellow/orange. It never recovers unless I reset.

I am compiling and flashing this piece of code using particle cli. Any ideas on this?

Thank you
Dheeraj

I didn’t catch this from your description but are you writing/reading from another device(2 devices)?If so the same thing happened to me once, try a common ground if you can…

Best of luck, Jack

This code is running on a single device(P1). I am writing into the external flash and reading again from the same extenal flash.

Ok…