I'm trying to play around with the MKR MEM Shield flash memory using SPI communication with an Arduino Uno. Some commands I use work and some don't and I can't figure out why. The chip in use is W25Q16JV.
Commands that just require to send an instruction and then receive data work fine, like this example that reads from Status Register 1:
#include <SPI.h>
byte data = 0;
const int CS = 10;
const byte status_register_1 = 0x05;
#define SPIcomm SPISettings(4000000, MSBFIRST, SPI_MODE0)
void setup() {
pinMode(CS, OUTPUT);
Serial.begin(115200);
SPI.begin();
}
void loop() {
SPI.beginTransaction(SPIcomm);
digitalWrite(CS, LOW);
SPI.transfer(status_register_1);
data = SPI.transfer(0);
digitalWrite(CS, HIGH);
SPI.endTransaction();
}
This code returns the correct values of the Status Register.
However, commands that require me to send an instruction followed by an address (or dummy bytes), and then receive data don't seem to work. Like this example to read the device ID:
#include <SPI.h>
byte data = 0;
const int CS = 10;
const byte device_id = 0xAB;
#define SPIcomm SPISettings(4000000, MSBFIRST, SPI_MODE0)
void setup() {
pinMode(CS, OUTPUT);
Serial.begin(115200);
SPI.begin();
}
void loop() {
SPI.beginTransaction(SPIcomm);
digitalWrite(CS, LOW);
SPI.transfer(device_id); //Instruction
SPI.transfer(0); // Dummy Byte
SPI.transfer(0); // Dummy Byte
SPI.transfer(0); // Dummy Byte
data = SPI.transfer(0); // Data in from memory
digitalWrite(CS, HIGH);
SPI.endTransaction();
}
This code simply returns the value zero instead of the device id value.
I've attached the clocking figures of those instructions, if anyone has any insight on why this doesn't work I'd appreciate it.