0

I'm using a WinBond 25Q128FVIQ (Flash 128 mbit, 3.3V) in combination with the Marzogh SpiMemory library on my Arduino Nano (also 3.3V).

After using the default example, I found out what was read, did not always fully match what I wrote. So I created a slightly alternative example writing 16 bytes:

#include<SPIMemory.h>

uint32_t address;

#define BAUD_RATE 115200
#define RANDPIN A0

//SPIFlash flash(SS1, &SPI1);       //Use this constructor if using an SPI bus other than the default SPI. Only works with chips with more than one hardware SPI bus
SPIFlash flash;

bool readSerialStr(String &inputStr);

void setup() {
  Serial.begin(BAUD_RATE);
#if defined (ARDUINO_SAMD_ZERO) || (__AVR_ATmega32U4__)
  while (!Serial) ; // Wait for Serial monitor to open
#endif

  flash.begin();
  Serial.println(F("Flash Test"));
  Serial.print(F("Capacity: "));
  Serial.println(flash.getCapacity());

  char writeBuffer[16] = "0123456789ABCDEF";
  char readBuffer[17];

  for (int n = 0; n < 5; n++)
  {
    memset(readBuffer, 17, 0);
    randomSeed(analogRead(RANDPIN));
    address = random(0, flash.getCapacity() - 16);
    bool err = flash.writeAnything(address, writeBuffer, true);
    delay(50);
    Serial.print(F("Write: "));
    Serial.println(err);
    for (int m = 0; m < 10; m++)
    {
      if (flash.readAnything(address, readBuffer, true))
      {
        Serial.print(m);
        Serial.print(F(", read: "));
        Serial.print(readBuffer);
        Serial.print(F(", from address: "));
        Serial.println(address);
      }
      delay(50);
    }
  }
}

void loop() {

}

//Reads a string from Serial
bool readSerialStr(String &inputStr) {
  if (!Serial)
    Serial.begin(115200);
  while (Serial.available()) {
    inputStr = Serial.readStringUntil('\n');
    Serial.println(inputStr);
    return true;
  }
  return false;
} 

Short explanation: 5 times I write a string (hex numbers), and I read this 5 times. The result is: (one example, differs every time):

 Flash Test
Capacity: 8388608
Write: 1
0, read: 0123456789ABCDEF, from address: 6252204
1, read: 0123456789ABCDEF, from address: 6252204
2, read: 0123456789ABCDEF, from address: 6252204
3, read: 0123456789ABCDEF, from address: 6252204
4, read: 0123456789ABCDEF, from address: 6252204
5, read: 0123456789ABCDEF, from address: 6252204
6, read: 0123456789ABCDEF, from address: 6252204
7, read: 0123456789ABCDEF, from address: 6252204
8, read: 0123456789ABCDEF, from address: 6252204
9, read: 0123456789ABCDEF, from address: 6252204
Write: 1
0, read: 0123456789ABCDEF, from address: 5680766
1, read: 0123456789ABCDEF, from address: 5680766
2, read: 0123456789ABCDEF, from address: 5680766
3, read: 0123456789ABCDEF, from address: 5680766
4, read: 0123456789ABCDEF, from address: 5680766
5, read: 0123456789ABCDEF, from address: 5680766
6, read: 0123456789ABCDEF, from address: 5680766
7, read: 0123456789ABCDEF, from address: 5680766
8, read: 0123456789ABCDEF, from address: 5680766
9, read: 0123456789ABCDEF, from address: 5680766
Write: 0
0, read: , from address: 5277398
1, read: , from address: 5277398
2, read: , from address: 5277398
3, read: , from address: 5277398
4, read: , from address: 5277398
5, read: , from address: 5277398
6, read: , from address: 5277398
7, read: , from address: 5277398
8, read: , from address: 5277398
9, read: , from address: 5277398
Write: 0
0, read: , from address: 4941258
1, read: , from address: 4941258
2, read: , from address: 4941258
3, read: , from address: 4941258
4, read: , from address: 4941258
5, read: , from address: 4941258
6, read: , from address: 4941258
7, read: , from address: 4941258
8, read: , from address: 4941258
9, read: , from address: 4941258
Write: 0
0, read: ⸮⸮⸮⸮⸮l⸮څ⸮w⸮r⸮0123456789ABCDEF, from address: 4689153
1, read: ⸮⸮⸮⸮⸮l⸮څ⸮w⸮r⸮0123456789ABCDEF, from address: 4689153
2, read: ⸮⸮⸮⸮⸮l⸮څ⸮w⸮r⸮0123456789ABCDEF, from address: 4689153
3, read: ⸮⸮⸮⸮⸮l⸮څ⸮w⸮r⸮0123456789ABCDEF, from address: 4689153
4, read: ⸮⸮⸮⸮⸮l⸮څ⸮w⸮r⸮0123456789ABCDEF, from address: 4689153
5, read: ⸮⸮⸮⸮⸮l⸮څ⸮w⸮r⸮0123456789ABCDEF, from address: 4689153
6, read: ⸮⸮⸮⸮⸮l⸮څ⸮w⸮r⸮0123456789ABCDEF, from address: 4689153
7, read: ⸮⸮⸮⸮⸮l⸮څ⸮w⸮r⸮0123456789ABCDEF, from address: 4689153
8, read: ⸮⸮⸮⸮⸮l⸮څ⸮w⸮r⸮0123456789ABCDEF, from address: 4689153
9, read: ⸮⸮⸮⸮⸮l⸮څ⸮w⸮r⸮0123456789ABCDEF, from address: 4689153

Conclusion: The write seems to be the problem as the read consequently returns the same value (either garbage or the correct string).

What do I wrong?

Actually, I even don't want the 50 ms delay (and when I remove it, the result is the same).

1

I made a stupid mistake; I accidentally removed the next line at the end of the application which takes care of erasing the sector. If this is omitted, then the next time write actions will fail (or fail sometimes):

flash.eraseSector(address);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.