Update SPIFlash_ReadWrite.ino

This commit is contained in:
Felix Rusu 2020-06-03 16:58:33 -04:00
parent 3ff4c0629b
commit 7543abc897
1 changed files with 40 additions and 21 deletions

View File

@ -9,7 +9,7 @@
// Get the SPIFlash library from here: https://github.com/LowPowerLab/SPIFlash // Get the SPIFlash library from here: https://github.com/LowPowerLab/SPIFlash
// Note: if other SPI devices are present, ensure their CS pins are pulled up or set HIGH // Note: if other SPI devices are present, ensure their CS pins are pulled up or set HIGH
// ********************************************************************************** // **********************************************************************************
// Copyright Felix Rusu, LowPowerLab.com // (C) 2020 Felix Rusu, LowPowerLab.com
// Library and code by Felix Rusu - felix@lowpowerlab.com // Library and code by Felix Rusu - felix@lowpowerlab.com
// ********************************************************************************** // **********************************************************************************
// License // License
@ -37,20 +37,11 @@
// and copyright notices in any redistribution of this code // and copyright notices in any redistribution of this code
// ********************************************************************************** // **********************************************************************************
#include <SPIFlash.h> //get it here: https://github.com/LowPowerLab/SPIFlash #include <SPIFlash.h> //get it here: https://github.com/LowPowerLab/SPIFlash
#include <SPI.h>
#define SERIAL_BAUD 115200 #define SERIAL_BAUD 115200
char input = 0; char input = 0;
long lastPeriod = -1; long lastPeriod = -1;
#ifdef __AVR_ATmega1284P__
#define LED 15 // Moteino MEGAs have LEDs on D15
#define FLASH_SS 23 // and FLASH SS on D23
#else
#define LED 9 // Moteinos have LEDs on D9
#define FLASH_SS 8 // and FLASH SS on D8
#endif
////////////////////////////////////////// //////////////////////////////////////////
// flash(SPI_CS, MANUFACTURER_ID) // flash(SPI_CS, MANUFACTURER_ID)
// SPI_CS - CS pin attached to SPI flash chip (8 in case of Moteino) // SPI_CS - CS pin attached to SPI flash chip (8 in case of Moteino)
@ -58,7 +49,8 @@ long lastPeriod = -1;
// 0xEF30 for windbond 4mbit flash // 0xEF30 for windbond 4mbit flash
// 0xEF40 for windbond 64mbit flash // 0xEF40 for windbond 64mbit flash
////////////////////////////////////////// //////////////////////////////////////////
SPIFlash flash(FLASH_SS, 0xEF30); uint16_t expectedDeviceID=0xEF30;
SPIFlash flash(SS_FLASHMEM, expectedDeviceID);
void setup(){ void setup(){
Serial.begin(SERIAL_BAUD); Serial.begin(SERIAL_BAUD);
@ -67,11 +59,22 @@ void setup(){
if (flash.initialize()) if (flash.initialize())
{ {
Serial.println("Init OK!"); Serial.println("Init OK!");
Blink(LED, 20, 10); Blink(20, 10);
}
else {
Serial.print("Init FAIL, expectedDeviceID(0x");
Serial.print(expectedDeviceID, HEX);
Serial.print(") mismatched the read value: 0x");
Serial.println(flash.readDeviceId(), HEX);
} }
else
Serial.println("Init FAIL!");
Serial.println("\n************************");
Serial.println("Available operations:");
Serial.println("'c' - read flash chip's deviceID 10 times to ensure chip is present");
Serial.println("'d' - dump first 256 bytes on the chip");
Serial.println("'e' - erase entire flash chip");
Serial.println("'i' - read deviceID");
Serial.println("************************\n");
delay(1000); delay(1000);
} }
@ -92,6 +95,22 @@ void loop(){
Serial.println(); Serial.println();
} }
else if (input == 'c') {
Serial.print("Checking chip is present ... ");
uint16_t deviceID=0;
for (uint8_t i=0;i<10;i++) {
uint16_t idNow = flash.readDeviceId();
if (idNow==0 || idNow==0xffff || (i>0 && idNow != deviceID)) {
deviceID=0;
break;
}
deviceID=idNow;
}
if (deviceID!=0) {
Serial.print("OK, deviceID=0x");Serial.println(deviceID, HEX);
}
else Serial.println("FAIL, deviceID is inconsistent or 0x0000/0xffff");
}
else if (input == 'e') else if (input == 'e')
{ {
Serial.print("Erasing Flash chip ... "); Serial.print("Erasing Flash chip ... ");
@ -115,19 +134,19 @@ void loop(){
if ((int)(millis()/500) > lastPeriod) if ((int)(millis()/500) > lastPeriod)
{ {
lastPeriod++; lastPeriod++;
pinMode(LED, OUTPUT); pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED, lastPeriod%2); digitalWrite(LED_BUILTIN, lastPeriod%2);
} }
} }
void Blink(byte PIN, int DELAY_MS, byte loops) void Blink(int DELAY_MS, byte loops)
{ {
pinMode(PIN, OUTPUT); pinMode(LED_BUILTIN, OUTPUT);
while (loops--) while (loops--)
{ {
digitalWrite(PIN,HIGH); digitalWrite(LED_BUILTIN,HIGH);
delay(DELAY_MS); delay(DELAY_MS);
digitalWrite(PIN,LOW); digitalWrite(LED_BUILTIN,LOW);
delay(DELAY_MS); delay(DELAY_MS);
} }
} }