Compare commits

..

No commits in common. "4c49258506889a1530e02f538139d97706e2dd6c" and "72459052e05af14e260186f159da24545ac27f07" have entirely different histories.

6 changed files with 33 additions and 110 deletions

View File

@ -1,6 +1,6 @@
language: python
python:
- "3.6"
- "2.7"
# Cache PlatformIO packages using Travis CI container-based infrastructure
sudo: false

View File

@ -1,6 +1,6 @@
SPIFlash
========
[![Build Status](https://app.travis-ci.com/LowPowerLab/SPIFlash.svg?branch=master)](https://app.travis-ci.com/LowPowerLab/SPIFlash)
[![Build Status](https://travis-ci.org/LowPowerLab/SPIFlash.svg?branch=master)](https://travis-ci.org/LowPowerLab/SPIFlash)
[![GitHub release](https://img.shields.io/github/release/LowPowerLab/SPIFlash.svg)](https://github.com/LowPowerLab/SPIFlash)
[![GitHub issues](https://img.shields.io/github/issues/LowPowerLab/SPIFlash.svg)](https://github.com/LowPowerLab/SPIFlash/issues)
[![GitHub pull requests](https://img.shields.io/github/issues-pr/LowPowerLab/SPIFlash.svg)](https://github.com/LowPowerLab/SPIFlash/pulls)

View File

@ -38,13 +38,8 @@ uint8_t SPIFlash::UNIQUEID[8];
/// IMPORTANT: NAND FLASH memory requires erase before write, because
/// it can only transition from 1s to 0s and only the erase command can reset all 0s to 1s
/// See http://en.wikipedia.org/wiki/Flash_memory
/// The smallest range that can be erased is a sector (4K, 32K, 64K); there is also a chip erase command
/// IMPORTANT: When flash chip is powered down, aka sleeping, the only command it will respond to is
/// Release Power-down / Device ID (ABh), per section 8.2.19 of the W25X40CL datasheet.
/// This means after using the sleep() function of this library, wake() must be the first
/// function called. If other commands are used, the flash chip will ignore the commands.
/// See http://en.wikipedia.org/wiki/Flash_memory
/// The smallest range that can be erased is a sector (4K, 32K, 64K); there is also a chip erase command
/// Constructor. JedecID is optional but recommended, since this will ensure that the device is present and has a valid response
/// get this from the datasheet of your flash chip
@ -185,14 +180,11 @@ void SPIFlash::command(uint8_t cmd, boolean isWrite){
command(SPIFLASH_WRITEENABLE); // Write Enable
unselect();
}
// wait for any write/erase to complete
//wait for any write/erase to complete
// a time limit cannot really be added here without it being a very large safe limit
// that is because some chips can take several seconds to carry out a chip erase or other similar multi block or entire-chip operations
//
// Note: If the MISO line is high, busy() will return true.
// This can be a problem and cause the code to hang when there is noise/static on MISO data line when:
// 1) There is no flash chip connected
// 2) The flash chip connected is powered down, aka sleeping.
// a recommended alternative to such situations where chip can be or not be present is to add a 10k or similar weak pulldown on the
// open drain MISO input which can read noise/static and hence return a non 0 status byte, causing the while() to hang when a flash chip is not present
if (cmd != SPIFLASH_WAKE) while(busy());
select();
SPI.transfer(cmd);
@ -300,46 +292,11 @@ void SPIFlash::blockErase64K(uint32_t addr) {
unselect();
}
/// found() - checks there is a FLASH chip by checking the deviceID repeatedly - should be a consistent value
uint8_t SPIFlash::found() {
uint16_t deviceID=0;
wakeup(); //if sleep() was previously called, wakeup() is required or it's non responsive
for (uint8_t i=0;i<10;i++) {
uint16_t idNow = readDeviceId();
if (idNow==0 || idNow==0xffff || (i>0 && idNow != deviceID)) {
deviceID=0;
break;
}
deviceID=idNow;
}
if (deviceID==0) { //NO FLASH CHIP FOUND, ABORTING
return false;
}
return true;
}
///regionIsEmpty() - check a random flashmem byte array is all clear and can be written to (ie. it's all 0xff)
uint8_t SPIFlash::regionIsEmpty(uint32_t startAddress, uint8_t length) {
uint8_t flashBuf[length];
readBytes(startAddress, flashBuf, length);
for (uint8_t i=0;i<length;i++) if (flashBuf[i]!=0xff) return false;
return true;
}
/// Put flash memory chip into power down mode
/// WARNING: after this command, only the WAKEUP and DEVICE_ID commands are recognized
/// hence a wakeup() command should be invoked first before further operations
/// If a MCU soft restart is possible with flash chip left in sleep(), then a wakeup() command
/// should always be invoked before any other commands to ensure the flash chip was not left in sleep
void SPIFlash::sleep() {
command(SPIFLASH_SLEEP);
unselect();
}
/// Wake flash memory from power down mode
/// NOTE: this command is required after a sleep() command is used, or no other commands will be recognized
/// If a MCU soft restart is possible with flash chip left in sleep(), then a wakeup() command
/// should always be invoked before any other commands to ensure the flash chip was not left in sleep
void SPIFlash::wakeup() {
command(SPIFLASH_WAKE);
unselect();

View File

@ -105,9 +105,7 @@ public:
void blockErase64K(uint32_t addr);
uint16_t readDeviceId();
uint8_t* readUniqueId();
uint8_t found();
uint8_t regionIsEmpty(uint32_t startAddress, uint8_t length);
void sleep();
void wakeup();
void end();

View File

@ -3,30 +3,27 @@
#include <SPIFlash.h> //https://github.com/lowpowerlab/spiflash
#include <SPI.h>
#define SERIAL_BAUD 115200
#define BLINK_FAST_DELAY 50
#define BLINK_SLOW_DELAY 1000
#define SERIAL_ENABLE
#define BLINK_FAST_DELAY 100
#define BLINK_SLOW_DELAY 500
SPIFlash flash(SS_FLASHMEM, 0xEF30); //EF40 for 16mbit windbond chip
int LEDTIME = 500;
// the setup routine runs once when you press reset:
void setup() {
#ifdef SERIAL_BAUD
Serial.begin(SERIAL_BAUD);
while (!Serial) delay(100); //wait until Serial/monitor is opened
#ifdef SERIAL_ENABLE
Serial.begin(115200);
delay(4000); //wait a bit until SerialMonitor can be opened
#endif
pinMode(LED_BUILTIN, OUTPUT);
//ensure the radio module CS pin is pulled HIGH or it might interfere!
pinMode(SS, OUTPUT); digitalWrite(SS, HIGH);
//ensure FLASH chip is not sleep mode (unresponsive)
flash.wakeup();
if (flash.initialize()) {
Serial.println("SPI Flash Init OK!");
LEDTIME = BLINK_SLOW_DELAY;
} else {
}
else
{
Serial.println("SPI Flash Init FAIL! (is chip soldered?)");
LEDTIME = BLINK_FAST_DELAY;
}

View File

@ -2,17 +2,14 @@
// This sketch is an example of using the SPIFlash library with a Moteino
// that has an onboard SPI Flash chip. This sketch listens to a few serial commands
// Hence type the following commands to interact with the SPI flash memory array:
//'c' - read flash chip's deviceID 10 times to ensure chip is present
//'d' - dump first 256 bytes on the chip
//'e' - erase entire flash chip
//'E' - erase last 4K block in flash chip
//'D' - dump first 256 bytes in the last 4K block
//'i' - read deviceID
//'0'-'9' - write 0xaa/0xbb bytes at addresses 0..9, 520192..520447
// - 'd' dumps the first 256bytes of the flash chip to screen
// - 'e' erases the entire memory chip
// - 'i' print manufacturer/device ID
// - [0-9] writes a random byte to addresses [0-9] (either 0xAA or 0xBB)
// 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
// **********************************************************************************
// (C) 2021 Felix Rusu, LowPowerLab.com
// (C) 2020 Felix Rusu, LowPowerLab.com
// Library and code by Felix Rusu - felix@lowpowerlab.com
// **********************************************************************************
// License
@ -56,17 +53,9 @@ uint16_t expectedDeviceID=0xEF30;
SPIFlash flash(SS_FLASHMEM, expectedDeviceID);
void setup(){
#ifdef SERIAL_BAUD
Serial.begin(SERIAL_BAUD);
while (!Serial) delay(100); //wait until Serial/monitor is opened
#endif
pinMode(LED_BUILTIN, OUTPUT);
Serial.print("Start...");
//ensure the radio module CS pin is pulled HIGH or it might interfere!
pinMode(SS, OUTPUT); digitalWrite(SS, HIGH);
if (flash.initialize())
{
Serial.println("Init OK!");
@ -84,10 +73,7 @@ void setup(){
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("'E' - erase last 4K block in flash chip");
Serial.println("'D' - dump first 256 bytes in the last 4K block");
Serial.println("'i' - read deviceID");
Serial.println("'0'-'9' - write 0xaa/0xbb bytes at addresses 0..9, 520192..520447");
Serial.println("************************\n");
delay(1000);
}
@ -97,26 +83,16 @@ void loop(){
// ie: display first 256 bytes in FLASH, erase chip, write bytes at first 10 positions, etc
if (Serial.available() > 0) {
input = Serial.read();
if (input == 'd') { //d=dump flash area
Serial.println("Flash content (256 bytes, starting address 0):");
if (input == 'd') //d=dump flash area
{
Serial.println("Flash content:");
int counter = 0;
while(counter<256){
Serial.print(flash.readByte(counter), HEX);
counter++;
if (counter%16 == 0) Serial.println(); else Serial.print('.');
}
Serial.println();
}
else if (input == 'D') { //d=dump flash area @ last 4K block
Serial.println("Flash content (256bytes, starting address 520192):");
uint32_t counter = 520192;
while(counter<520192+256){
Serial.print(flash.readByte(counter), HEX);
counter++;
if (counter%16 == 0) Serial.println(); else Serial.print('.');
while(counter<=256){
Serial.print(flash.readByte(counter++), HEX);
Serial.print('.');
}
Serial.println();
}
else if (input == 'c') {
@ -142,13 +118,6 @@ void loop(){
while(flash.busy());
Serial.println("DONE");
}
else if (input == 'E')
{
Serial.print("Erasing last 4K block @520192 ... ");
flash.blockErase4K(520192);
while(flash.busy());
Serial.println("DONE");
}
else if (input == 'i')
{
Serial.print("DeviceID: ");
@ -158,19 +127,21 @@ void loop(){
{
Serial.print("\nWriteByte("); Serial.print(input); Serial.print(")");
flash.writeByte(input-48, millis()%2 ? 0xaa : 0xbb);
flash.writeByte(input-48+520192, millis()%2 ? 0xaa : 0xbb);
}
}
// Periodically blink the onboard LED while listening for serial commands
if ((int)(millis()/500) > lastPeriod) {
if ((int)(millis()/500) > lastPeriod)
{
lastPeriod++;
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, lastPeriod%2);
}
}
void Blink(int DELAY_MS, byte loops)
{
pinMode(LED_BUILTIN, OUTPUT);
while (loops--)
{
digitalWrite(LED_BUILTIN,HIGH);