I checked the AT45DB321D datasheet and it says it operates at either 66 MHz oder 33 MHz. Both of these speeds are higher than what an Arduino Nano supports (my google fu fails me here, but according to the Arduino SPI doc it's probably on the order of 4 MHz).
According to the datasheet
Data can be clocked in from the input pin (SI) into either buffer 1 or buffer 2. To load data into the standard DataFlash buffer (528 bytes), a 1-byte opcode, 84H for buffer 1 or 87H for buffer 2, must be clocked into the device, followed by three address bytes comprised of 14 don’t care bits and 10 buffer address bits (BFA9-BFA0). The 10 buffer address bits specify the first byte in the buffer to be written. [...] Data will continue to be loaded into the buffer until a low-to-high transition is detected on the CS pin.
Which means to write a single byte I would need
- 1 opcode byte
- 3 address bytes
- 1 data byte
totaling 5 bytes or 40 bits which at 4 MHz yields 10 µs.
My code, as below, yields roughly 48 µs per byte (outputs a duration of ~12380 µs for transfering 256 bytes). I'm using the DataFlash library.
#include <SPI.h>
#include "DataFlash.h"
static const int csPin = 10;
static const int resetPin = 8;
static const int wpPin = 7;
DataFlash dataflash;
void setup()
{
Serial.begin(115200);
SPI.begin();
dataflash.setup(csPin, resetPin, wpPin);
dataflash.begin();
long start = micros();
dataflash.bufferWrite(0, 0);
for(int i = 0; i <= 255; i++) {
SPI.transfer(i);
}
long afterBuffer = micros();
Serial.print("Writing 256 values to buffer: ");
Serial.println(afterBuffer - start);
}
void loop()
{
// nothing
}
It's very likely I'm missing something, but especially if I want to transfer larger chunks of data at once I'd like to speed up that process a bit. Creating a 256 byte buffer and writing it all at once
// outside time measurement
uint8_t buffer256[256];
for(int i = 0; i <= 255; i++) {
buffer256[i] = i;
}
// inside time measurement
SPI.transfer(buffer256, 256);
takes roughly the same time.
tl;dr: Is everything working correctly/what am I doing wrong? How can I speed up writing to a flash chip using an Arduino Nano?