#include <SPI.h>
const int SSPin = 10;
SPISettings mySet(500000, MSBFIRST, SPI_MODE0);
byte data_arr[32]={12,56,78,67,90,0,78};
void setup() {
Serial.begin(9600);
pinMode(SSPin, OUTPUT);
SPI.begin();
sector_clean(0);// 0...255 sectors (1sector=4k_byte)
// block_clean(0);// 0...15 block (1block=64k_byte)
// chip_clean();
delay(1000);
write_flash(200);
}
void loop() {
SPI.beginTransaction(mySet);
digitalWrite(SSPin, LOW);
SPI.transfer(0x03); // read
SPI.transfer(0);
SPI.transfer(0);
SPI.transfer(200);
for (int i=0; i<32; i++) {
byte data = SPI.transfer(0);
Serial.print(data);
}
Serial.println();
digitalWrite(SSPin, HIGH);
SPI.endTransaction();
delay(1000); // повторяем чтение каждую секунду
}
void write_flash(long addr){
SPI.beginTransaction(mySet);
digitalWrite(SSPin, LOW);
SPI.transfer(0x06);// write on
digitalWrite(SSPin, HIGH);
SPI.endTransaction();
byte addr_2 = (addr >> 16) & 0xFF;
byte addr_1 = (addr >> 8) & 0x00FF;
byte addr_0 = addr & 0x0000FF;
SPI.beginTransaction(mySet);
digitalWrite(SSPin, LOW);
SPI.transfer(0x02);
SPI.transfer(addr_2);
SPI.transfer(addr_1);
SPI.transfer(addr_0);
for (int i=0; i<sizeof(data_arr); i++) {
SPI.transfer(data_arr[i]);
}
digitalWrite(SSPin, HIGH);
SPI.endTransaction();
SPI.beginTransaction(mySet);
digitalWrite(SSPin, LOW);
SPI.transfer(0x04);// write off
digitalWrite(SSPin, HIGH);
SPI.endTransaction();
}
void sector_clean(byte addr){
SPI.beginTransaction(mySet);
digitalWrite(SSPin, LOW);
SPI.transfer(0x06);
digitalWrite(SSPin, HIGH);
SPI.endTransaction();
SPI.beginTransaction(mySet);
digitalWrite(SSPin, LOW);
SPI.transfer(0x20);
SPI.transfer(0);
SPI.transfer(0);
SPI.transfer(addr);
digitalWrite(SSPin, HIGH);
SPI.endTransaction();
// Выставление запрета записи:
SPI.beginTransaction(mySet);
digitalWrite(SSPin, LOW);
SPI.transfer(0x04);
digitalWrite(SSPin, HIGH);
SPI.endTransaction();
}
void block_clean(byte addr){
SPI.beginTransaction(mySet);
digitalWrite(SSPin, LOW);
SPI.transfer(0x06);
digitalWrite(SSPin, HIGH);
SPI.endTransaction();
SPI.beginTransaction(mySet);
digitalWrite(SSPin, LOW);
SPI.transfer(0x52);
SPI.transfer(0);
SPI.transfer(0);
SPI.transfer(addr);
digitalWrite(SSPin, HIGH);
SPI.endTransaction();
SPI.beginTransaction(mySet);
digitalWrite(SSPin, LOW);
SPI.transfer(0x04);
digitalWrite(SSPin, HIGH);
SPI.endTransaction();
}
void chip_clean(){
SPI.beginTransaction(mySet);
digitalWrite(SSPin, LOW);
SPI.transfer(0x06);
digitalWrite(SSPin, HIGH);
SPI.endTransaction();
SPI.beginTransaction(mySet);
digitalWrite(SSPin, LOW);
SPI.transfer(0x60);
digitalWrite(SSPin, HIGH);
SPI.endTransaction();
SPI.beginTransaction(mySet);
digitalWrite(SSPin, LOW);
SPI.transfer(0x04);
digitalWrite(SSPin, HIGH);
SPI.endTransaction();
}