There is an external FLASH memory (W25Q80DV) that I want to use with the Arduino. This memory has 4096 pages of 256 bytes each.
I want to make multiple writes per page to use memory evenly and erase less.
Test code:
#include <SPI.h>
#define readStatusReg_1 0x05
#define readArrayReg 0x0B
#define writeEnableReg 0x06
#define pageProgramStatusReg 0x02
#define FLASH_SELECT_CHIP 33
// # Select / Unselect chip
void selectChip(bool state) {
switch (state) {
case true:
digitalWrite(FLASH_SELECT_CHIP, HIGH);
digitalWrite(FLASH_SELECT_CHIP, LOW);
break;
case false:
digitalWrite(FLASH_SELECT_CHIP, HIGH);
break;
}
}
// # Wait flash ready state
void notBusy() {
uint16_t whileCounter;
selectChip(true);
SPI.transfer(readStatusReg_1);
while((SPI.transfer(0) & 1) && whileCounter < 45000) { whileCounter++; }
selectChip(false);
}
// # Send command and address
void sendCommandAndAddress(const byte command, const unsigned long address) {
SPI.transfer(command);
SPI.transfer((address >> 16) & 0xFF);
SPI.transfer((address >> 8) & 0xFF);
SPI.transfer(address & 0xFF);
}
// # Write enable
void writeEnable() {
notBusy();
selectChip(true);
SPI.transfer(writeEnableReg);
selectChip(false);
}
// # Write FLASH page
bool writeFlashData(const unsigned long address, byte * data) {
writeEnable();
selectChip(true);
sendCommandAndAddress(pageProgramStatusReg, address);
uint16_t len = sizeof(data);
for(uint16_t i = 0; i < len; i++) {
SPI.transfer(*data++);
}
selectChip(false);
notBusy();
return true;
}
void setup() {
// SPI
pinMode(FLASH_SELECT_CHIP, OUTPUT);
digitalWrite(FLASH_SELECT_CHIP, HIGH);
SPI.begin();
SPI.setDataMode(0);
SPI.setBitOrder(MSBFIRST);
// Write FLASH
#define TESTADDRESS 0x1000
byte msgData[] = "Hi all!";
writeFlashData(TESTADDRESS, msgData);
}
void loop() {}
Here, the address #define TESTADDRESS 0x1000 (4096) is specified for writing data.
I'm not clear if this is the page address or the byte address from which I will start recording?
I would like to actually split one page into multiple blocks and write data for them separately.