2015-06-02 20:02:10 +01:00
// Copyright (c) 2013-2015 by Felix Rusu, LowPowerLab.com
// SPI Flash memory library for arduino/moteino.
// This works with 256byte/page SPI flash memory
// For instance a 4MBit (512Kbyte) flash chip will have 2048 pages: 256*2048 = 524288 bytes (512Kbytes)
// Minimal modifications should allow chips that have different page size but modifications
// DEPENDS ON: Arduino SPI library
// > Updated Jan. 5, 2015, TomWS1, modified writeBytes to allow blocks > 256 bytes and handle page misalignment.
// > Updated Feb. 26, 2015 TomWS1, added support for SPI Transactions (Arduino 1.5.8 and above)
// > Selective merge by Felix after testing in IDE 1.0.6, 1.6.4
// **********************************************************************************
// License
// **********************************************************************************
// This program is free software; you can redistribute it
// and/or modify it under the terms of the GNU General
// Public License as published by the Free Software
// Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will
// be useful, but WITHOUT ANY WARRANTY; without even the
// implied warranty of MERCHANTABILITY or FITNESS FOR A
// PARTICULAR PURPOSE. See the GNU General Public
// License for more details.
//
// You should have received a copy of the GNU General
// Public License along with this program.
// If not, see <http://www.gnu.org/licenses/>.
//
// Licence can be viewed at
// http://www.gnu.org/licenses/gpl-3.0.txt
//
// Please maintain this license information along with authorship
// and copyright notices in any redistribution of this code
2013-04-04 23:04:29 +01:00
# include <SPIFlash.h>
2015-06-02 20:02:10 +01:00
uint8_t SPIFlash : : UNIQUEID [ 8 ] ;
2014-05-29 19:32:12 +01:00
2013-07-14 04:07:08 +01:00
/// 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
2021-06-08 04:07:28 +01:00
/// 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.
2013-07-14 04:07:08 +01:00
2013-04-04 23:04:29 +01:00
/// 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
/// Example for Atmel-Adesto 4Mbit AT25DF041A: 0x1F44 (page 27: http://www.adestotech.com/sites/default/files/datasheets/doc3668.pdf)
2013-04-16 20:44:56 +01:00
/// Example for Winbond 4Mbit W25X40CL: 0xEF30 (page 14: http://www.winbond.com/NR/rdonlyres/6E25084C-0BFE-4B25-903D-AE10221A0929/0/W25X40CL.pdf)
2013-04-04 23:04:29 +01:00
SPIFlash : : SPIFlash ( uint8_t slaveSelectPin , uint16_t jedecID ) {
_slaveSelectPin = slaveSelectPin ;
_jedecID = jedecID ;
}
/// Select the flash chip
void SPIFlash : : select ( ) {
2014-07-29 22:10:06 +01:00
//save current SPI settings
2015-06-02 20:02:10 +01:00
# ifndef SPI_HAS_TRANSACTION
noInterrupts ( ) ;
# endif
2018-04-04 15:36:23 +01:00
# if defined (SPCR) && defined (SPSR)
2014-07-29 22:10:06 +01:00
_SPCR = SPCR ;
_SPSR = SPSR ;
2018-04-04 15:36:23 +01:00
# endif
2015-06-02 20:02:10 +01:00
# ifdef SPI_HAS_TRANSACTION
SPI . beginTransaction ( _settings ) ;
# else
// set FLASH SPI settings
2014-07-29 22:10:06 +01:00
SPI . setDataMode ( SPI_MODE0 ) ;
SPI . setBitOrder ( MSBFIRST ) ;
SPI . setClockDivider ( SPI_CLOCK_DIV4 ) ; //decided to slow down from DIV2 after SPI stalling in some instances, especially visible on mega1284p when RFM69 and FLASH chip both present
2015-06-02 20:02:10 +01:00
# endif
2013-04-04 23:04:29 +01:00
digitalWrite ( _slaveSelectPin , LOW ) ;
}
/// UNselect the flash chip
void SPIFlash : : unselect ( ) {
digitalWrite ( _slaveSelectPin , HIGH ) ;
2014-07-29 22:10:06 +01:00
//restore SPI settings to what they were before talking to the FLASH chip
2015-06-02 20:02:10 +01:00
# ifdef SPI_HAS_TRANSACTION
SPI . endTransaction ( ) ;
# else
interrupts ( ) ;
# endif
2018-04-04 15:36:23 +01:00
# if defined (SPCR) && defined (SPSR)
2014-07-29 22:10:06 +01:00
SPCR = _SPCR ;
SPSR = _SPSR ;
2018-04-04 15:36:23 +01:00
# endif
2013-04-04 23:04:29 +01:00
}
/// setup SPI, read device ID etc...
boolean SPIFlash : : initialize ( )
{
2018-04-04 15:36:23 +01:00
# if defined (SPCR) && defined (SPSR)
2014-07-29 22:10:06 +01:00
_SPCR = SPCR ;
_SPSR = SPSR ;
2018-04-04 15:36:23 +01:00
# endif
2013-04-04 23:04:29 +01:00
pinMode ( _slaveSelectPin , OUTPUT ) ;
2018-05-23 17:35:20 +01:00
SPI . begin ( ) ;
2015-06-02 20:02:10 +01:00
# ifdef SPI_HAS_TRANSACTION
_settings = SPISettings ( 4000000 , MSBFIRST , SPI_MODE0 ) ;
# endif
2013-04-04 23:04:29 +01:00
unselect ( ) ;
2014-07-29 22:10:06 +01:00
wakeup ( ) ;
2013-04-04 23:04:29 +01:00
if ( _jedecID = = 0 | | readDeviceId ( ) = = _jedecID ) {
command ( SPIFLASH_STATUSWRITE , true ) ; // Write Status Register
SPI . transfer ( 0 ) ; // Global Unprotect
unselect ( ) ;
return true ;
}
return false ;
}
/// Get the manufacturer and device ID bytes (as a short word)
2015-06-02 20:02:10 +01:00
uint16_t SPIFlash : : readDeviceId ( )
2013-04-04 23:04:29 +01:00
{
2013-08-02 15:23:56 +01:00
# if defined(__AVR_ATmega32U4__) // Arduino Leonardo, MoteinoLeo
command ( SPIFLASH_IDREAD ) ; // Read JEDEC ID
# else
2013-08-02 05:02:46 +01:00
select ( ) ;
SPI . transfer ( SPIFLASH_IDREAD ) ;
2013-08-02 15:23:56 +01:00
# endif
2015-06-02 20:02:10 +01:00
uint16_t jedecid = SPI . transfer ( 0 ) < < 8 ;
2013-04-04 23:04:29 +01:00
jedecid | = SPI . transfer ( 0 ) ;
unselect ( ) ;
return jedecid ;
}
2014-05-29 19:32:12 +01:00
/// Get the 64 bit unique identifier, stores it in UNIQUEID[8]. Only needs to be called once, ie after initialize
/// Returns the byte pointer to the UNIQUEID byte array
/// Read UNIQUEID like this:
2015-06-02 20:02:10 +01:00
/// flash.readUniqueId(); for (uint8_t i=0;i<8;i++) { Serial.print(flash.UNIQUEID[i], HEX); Serial.print(' '); }
2014-05-29 19:32:12 +01:00
/// or like this:
2015-06-02 20:02:10 +01:00
/// flash.readUniqueId(); uint8_t* MAC = flash.readUniqueId(); for (uint8_t i=0;i<8;i++) { Serial.print(MAC[i], HEX); Serial.print(' '); }
uint8_t * SPIFlash : : readUniqueId ( )
2014-05-29 19:32:12 +01:00
{
command ( SPIFLASH_MACREAD ) ;
SPI . transfer ( 0 ) ;
SPI . transfer ( 0 ) ;
SPI . transfer ( 0 ) ;
SPI . transfer ( 0 ) ;
2015-06-02 20:02:10 +01:00
for ( uint8_t i = 0 ; i < 8 ; i + + )
2014-05-29 19:32:12 +01:00
UNIQUEID [ i ] = SPI . transfer ( 0 ) ;
unselect ( ) ;
return UNIQUEID ;
}
2013-04-04 23:04:29 +01:00
/// read 1 byte from flash memory
2015-06-02 20:02:10 +01:00
uint8_t SPIFlash : : readByte ( uint32_t addr ) {
2013-04-04 23:04:29 +01:00
command ( SPIFLASH_ARRAYREADLOWFREQ ) ;
SPI . transfer ( addr > > 16 ) ;
SPI . transfer ( addr > > 8 ) ;
SPI . transfer ( addr ) ;
2015-06-02 20:02:10 +01:00
uint8_t result = SPI . transfer ( 0 ) ;
2013-04-04 23:04:29 +01:00
unselect ( ) ;
return result ;
}
/// read unlimited # of bytes
2015-06-02 20:02:10 +01:00
void SPIFlash : : readBytes ( uint32_t addr , void * buf , uint16_t len ) {
2013-04-04 23:04:29 +01:00
command ( SPIFLASH_ARRAYREAD ) ;
SPI . transfer ( addr > > 16 ) ;
SPI . transfer ( addr > > 8 ) ;
SPI . transfer ( addr ) ;
SPI . transfer ( 0 ) ; //"dont care"
2015-06-02 20:02:10 +01:00
for ( uint16_t i = 0 ; i < len ; + + i )
( ( uint8_t * ) buf ) [ i ] = SPI . transfer ( 0 ) ;
2013-04-04 23:04:29 +01:00
unselect ( ) ;
}
/// Send a command to the flash chip, pass TRUE for isWrite when its a write command
2015-06-02 20:02:10 +01:00
void SPIFlash : : command ( uint8_t cmd , boolean isWrite ) {
2013-05-30 22:47:40 +01:00
# if defined(__AVR_ATmega32U4__) // Arduino Leonardo, MoteinoLeo
DDRB | = B00000001 ; // Make sure the SS pin (PB0 - used by RFM12B on MoteinoLeo R1) is set as output HIGH!
PORTB | = B00000001 ;
# endif
2013-04-04 23:04:29 +01:00
if ( isWrite )
{
command ( SPIFLASH_WRITEENABLE ) ; // Write Enable
unselect ( ) ;
}
2021-06-08 04:07:28 +01:00
// wait for any write/erase to complete
2015-01-09 01:44:12 +00:00
// 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
2021-06-08 04:07:28 +01:00
//
// 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.
2017-08-04 16:44:22 +01:00
if ( cmd ! = SPIFLASH_WAKE ) while ( busy ( ) ) ;
2013-04-04 23:04:29 +01:00
select ( ) ;
SPI . transfer ( cmd ) ;
}
/// check if the chip is busy erasing/writing
boolean SPIFlash : : busy ( )
{
/*
select ( ) ;
SPI . transfer ( SPIFLASH_STATUSREAD ) ;
2015-06-02 20:02:10 +01:00
uint8_t status = SPI . transfer ( 0 ) ;
2013-04-04 23:04:29 +01:00
unselect ( ) ;
return status & 1 ;
*/
return readStatus ( ) & 1 ;
}
/// return the STATUS register
2015-06-02 20:02:10 +01:00
uint8_t SPIFlash : : readStatus ( )
2013-04-04 23:04:29 +01:00
{
select ( ) ;
SPI . transfer ( SPIFLASH_STATUSREAD ) ;
2015-06-02 20:02:10 +01:00
uint8_t status = SPI . transfer ( 0 ) ;
2013-04-04 23:04:29 +01:00
unselect ( ) ;
return status ;
}
/// Write 1 byte to flash memory
/// WARNING: you can only write to previously erased memory locations (see datasheet)
/// use the block erase commands to first clear memory (write 0xFFs)
2015-06-02 20:02:10 +01:00
void SPIFlash : : writeByte ( uint32_t addr , uint8_t byt ) {
2013-04-04 23:04:29 +01:00
command ( SPIFLASH_BYTEPAGEPROGRAM , true ) ; // Byte/Page Program
SPI . transfer ( addr > > 16 ) ;
SPI . transfer ( addr > > 8 ) ;
SPI . transfer ( addr ) ;
SPI . transfer ( byt ) ;
unselect ( ) ;
}
2015-02-06 14:58:42 +00:00
/// write multiple bytes to flash memory (up to 64K)
2013-04-04 23:04:29 +01:00
/// WARNING: you can only write to previously erased memory locations (see datasheet)
/// use the block erase commands to first clear memory (write 0xFFs)
2015-02-06 14:58:42 +00:00
/// This version handles both page alignment and data blocks larger than 256 bytes.
///
2015-06-02 20:02:10 +01:00
void SPIFlash : : writeBytes ( uint32_t addr , const void * buf , uint16_t len ) {
2015-02-09 03:13:47 +00:00
uint16_t n ;
uint16_t maxBytes = 256 - ( addr % 256 ) ; // force the first set of bytes to stay within the first page
uint16_t offset = 0 ;
2015-02-06 14:58:42 +00:00
while ( len > 0 )
{
n = ( len < = maxBytes ) ? len : maxBytes ;
command ( SPIFLASH_BYTEPAGEPROGRAM , true ) ; // Byte/Page Program
SPI . transfer ( addr > > 16 ) ;
SPI . transfer ( addr > > 8 ) ;
SPI . transfer ( addr ) ;
2015-02-09 03:13:47 +00:00
2015-02-06 14:58:42 +00:00
for ( uint16_t i = 0 ; i < n ; i + + )
2015-06-02 20:02:10 +01:00
SPI . transfer ( ( ( uint8_t * ) buf ) [ offset + i ] ) ;
2015-02-06 14:58:42 +00:00
unselect ( ) ;
addr + = n ; // adjust the addresses and remaining bytes by what we've just transferred.
2015-02-09 03:13:47 +00:00
offset + = n ;
2015-02-06 14:58:42 +00:00
len - = n ;
maxBytes = 256 ; // now we can do up to 256 bytes per loop
}
2013-04-04 23:04:29 +01:00
}
/// erase entire flash memory array
/// may take several seconds depending on size, but is non blocking
/// so you may wait for this to complete using busy() or continue doing
/// other things and later check if the chip is done with busy()
/// note that any command will first wait for chip to become available using busy()
/// so no need to do that twice
void SPIFlash : : chipErase ( ) {
command ( SPIFLASH_CHIPERASE , true ) ;
unselect ( ) ;
}
/// erase a 4Kbyte block
2015-06-02 20:02:10 +01:00
void SPIFlash : : blockErase4K ( uint32_t addr ) {
2013-04-04 23:04:29 +01:00
command ( SPIFLASH_BLOCKERASE_4K , true ) ; // Block Erase
SPI . transfer ( addr > > 16 ) ;
SPI . transfer ( addr > > 8 ) ;
SPI . transfer ( addr ) ;
unselect ( ) ;
}
/// erase a 32Kbyte block
2015-06-02 20:02:10 +01:00
void SPIFlash : : blockErase32K ( uint32_t addr ) {
2013-04-04 23:04:29 +01:00
command ( SPIFLASH_BLOCKERASE_32K , true ) ; // Block Erase
SPI . transfer ( addr > > 16 ) ;
SPI . transfer ( addr > > 8 ) ;
SPI . transfer ( addr ) ;
unselect ( ) ;
}
2015-11-10 15:35:25 +00:00
/// erase a 64Kbyte block
void SPIFlash : : blockErase64K ( uint32_t addr ) {
command ( SPIFLASH_BLOCKERASE_64K , true ) ; // Block Erase
SPI . transfer ( addr > > 16 ) ;
SPI . transfer ( addr > > 8 ) ;
SPI . transfer ( addr ) ;
unselect ( ) ;
}
2021-07-20 19:10:28 +01:00
/// 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 ;
}
2021-06-07 21:44:39 +01:00
/// Put flash memory chip into power down mode
2021-07-20 19:10:28 +01:00
/// WARNING: after this command, only the WAKEUP and DEVICE_ID commands are recognized
2021-06-07 21:44:39 +01:00
/// 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
2013-07-14 04:07:08 +01:00
void SPIFlash : : sleep ( ) {
2014-07-29 22:10:06 +01:00
command ( SPIFLASH_SLEEP ) ;
2013-07-14 04:07:08 +01:00
unselect ( ) ;
}
2021-06-07 21:44:39 +01:00
/// 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
2013-07-14 04:07:08 +01:00
void SPIFlash : : wakeup ( ) {
2014-07-29 22:10:06 +01:00
command ( SPIFLASH_WAKE ) ;
2013-07-14 04:07:08 +01:00
unselect ( ) ;
}
2013-04-04 23:04:29 +01:00
/// cleanup
void SPIFlash : : end ( ) {
SPI . end ( ) ;
2021-07-20 19:10:28 +01:00
}