SPIMemory library question

Hello,

Im trying to use the SPIMemory version 3.4.0 on Arduino IDE 1.8.7. Ive never used SPI flash before and was curious and tried using this library to write a byte from ADC to flash. But seems it isnt writing to flash chip.Im using Adafruit Metro M0 Express(SAMD21) Heres basic code:

#include <SPIMemory.h>
//SPI flash setup,Adafruit Metro M0 Express flash chip uses cs = 39
const int csPin = 39;
SPIFlash flash(csPin);

byte tempByte;
byte ubyte;
//uses 32bit addresses for flash
uint32_t address = 0;
int analogPin = A3; 
                 
int val = 0; 
void setup(){
Serial.begin(115200);
flash.begin();
}

void loop(){
val = analogRead(analogPin);  // read the input pin
//split val
tempByte = (val >> 8) & 0xFF;

       
        ubyte =  flash.writeByte(address, tempByte);
        //increment address
        address++;
        Serial.println("ubyte =");
        Serial.println(ubyte);
        //get second half of val
        tempByte = val & 0xFF;
//write second half of val
 flash.writeByte(address, tempByte);
address++;
 

}

And I know code isnt perfect,I just wanted to see if I could use library and at least.

You should check the return value of the flash.begin() call. If it's not true, your chip isn't supported or badly wired. BTW, you failed to provide links to the hardware and a wiring diagram.

pylon:
You should check the return value of the flash.begin() call. If it's not true, your chip isn't supported or badly wired. BTW, you failed to provide links to the hardware and a wiring diagram.

Apologies.

Adafruit Metro M0 Express explanation of SPI flash:(about mid-page)

Adafruit Metro M0 Express schematic:

SPIMemory library:

What result did flash.begin() return for you?

I'm still missing the wiring diagram (of your setup) and a link to the used SPI flash chip.

flash.begin() returns 0. And what do you mean about my wiring, I havent wired anything. I have a adafruit Metro M0 Express board that has a SPI Flash chip on it already(Cypress SFL116K chip,if im not mistaken) this chip comes with the board, and wanted to test the SPI Flash Chip while using SPIMemory library. From my understanding, no other hardware is needed to write/read to it....unless im wrong, if so please inform me.

Link for SPI chip:
https://www.cypress.com/file/196886/download

flash.begin() returns 0.

That means the library cannot identify the flash.

And what do you mean about my wiring, I havent wired anything. I have a adafruit Metro M0 Express board that has a SPI Flash chip on it already(Cypress SFL116K chip,if im not mistaken) this chip comes with the board, and wanted to test the SPI Flash Chip while using SPIMemory library.

You didn't mention before that you're trying to access the onboard flash chip.

You have to modify the constructor call to use the correct SPI bus:

SPIFlash flash(csPin, SPI1);

Apologies. But I tried:

const int csPin = 39;

// I got csPin from here: https://learn.adafruit.com/adafruit-metro-m0-express-designed-for-circuitpython/pinouts

Says cs = 39.

SPIFlash flash(csPin, SPI1);

And get compile error: no matching function for call to 'SPIFlash::SPIFlash(const int&, SPIClass&)'

But putting &SPI1 compiles fine.But sadly still doesnt seem to be able to run basic examples from SPIMemory library, says it supports my chip...but no idea what im doing wrong.

But putting &SPI1 compiles fine.

Correct, my fault, please excuse.

says it supports my chip.

What says that? The example code?

but no idea what im doing wrong.

Did you select the Metro M0 Express as your board type while compiling?

In the read-me (just scroll down) and it says it supports my chip.

And yes Adafruit Metro M0 express is choosen when compiling....If anything I could just use SPI library and go about it this way if I cant get this library working. Datasheet says normal read/write is (50Mhz,MODE 0,MSB) from what ive read for my SPI Flashchip from here:

https://www.cypress.com/file/196886/download

If you select SPI1, flash.begin() still returns 0?

Please post your current code and fix the codes indentation (the IDE offers help for that, Tools->Auto Format).

Heres code, and note line 101 is where I change cs(chip select) to pin 39, and in lines 118 & 119 I ask to know status of flash.begin().Still returns 0.

This is just the test flash example from SPIMemory examples,slightly altered to know status of flash.begin(),and to set CS(chip select) to correct pin.Really nothing too crazy too report. But I tried posting code and it exceeded limit for arduino.cc so I couldnt just post entire example code…

TestFlash1.ino (25.7 KB)

geryuu123:
Heres code, and note line 101 is where I change cs(chip select) to pin 39, and in lines 118 & 119 I ask to know status of flash.begin().Still returns 0.

This is just the test flash example from SPIMemory examples,slightly altered to know status of flash.begin(),and to set CS(chip select) to correct pin.Really nothing too crazy too report. But I tried posting code and it exceeded limit for arduino.cc so I couldnt just post entire example code....

PS.Note I tried setting int,uint32_t, uint16_t,etc cs = 39, same result.

Change these lines

  flash.begin();
  Serial.println("flash.begin()=");
  Serial.println(flash.begin());

to

  int flash_res = flash.begin();
  Serial.print("flash.begin()=");
  Serial.println(flash_res);

Remove the comment from the line

//#define RUNDIAGNOSTIC

in SPIMemory.h of the library.

Post the output of the serial monitor.