I have wired a Macronix MX35LF1GE4AB Flash memory into an arduino Uno using Logic Level Shifters. What I try to do with it is to dump its contents.
In order to do so, I am implementing the following sketch:
#include <SPI.h>
#define LASTBLOCK 1023
#define LASTPAGE 63
#define LASTBYTE 2111
#define PAGE_READ 13h
#define PAGE_READ_CACHE_SEQUENCIAL 03h
#define READ_FROM_CACHE 3Bh
#define PAGE_READ_CACHE_END 3Fh
#define BUFFER_LEN 2111
int page_to_read = 1;
int block_to_read = 1;
// We store each page to Microcontroiller's ram
byte buffer[BUFFER_LEN];
void setup() {
SPI.beginTransaction(SPISettings(14000000, MSBFIRST, SPI_MODE0));
// Serial.begin(9600);
}
void loop() {
// I need to reset the counts before reading any block
if(page_to_read > LASTPAGE){
page_to_read = 1;
block_to_read ++;
}
if(block_to_read > LASTBLOCK){
return ;
}
if(page_to_read == LASTPAGE){
SPI.transfer(PAGE_READ_CACHE_END);
} else if (page_to_read == 1) {
SPI.transfer(PAGE_READ);
SPI.transfer(PAGE_READ_CACHE_SEQUENCIAL);
}
SPI.transfer(READ_FROM_CACHE);
page_to_read++;
}
Now what I want is to populate the buffer byte buffer[BUFFER_LEN] with data, so I can send them over a serial port.
How I can achieve that?
SPI.transfer(buffer, BUFFER_LEN);-- arduino.cc/en/Reference/SPITransferSPI.begin()in your setup (unless you are running it as a slave) because it help to setup the proper IO states of MOSI, MISO and SS, etc..SPI.beginTransaction()should be called before theSPI.transfer(), andSPI.endTransaction()to be called after you done with the data transfer. This allows other tasks to be able to use the SPI bus.