MySensors Library & Examples  2.3.2
SPIFlash.h
Go to the documentation of this file.
1 // Copyright (c) 2013-2015 by Felix Rusu, LowPowerLab.com
2 // SPI Flash memory library for arduino/moteino.
3 // This works with 256byte/page SPI flash memory
4 // For instance a 4MBit (512Kbyte) flash chip will have 2048 pages: 256*2048 = 524288 bytes (512Kbytes)
5 // Minimal modifications should allow chips that have different page size but modifications
6 // DEPENDS ON: Arduino SPI library
7 // > Updated Jan. 5, 2015, TomWS1, modified writeBytes to allow blocks > 256 bytes and handle page misalignment.
8 // > Updated Feb. 26, 2015 TomWS1, added support for SPI Transactions (Arduino 1.5.8 and above)
9 // > Selective merge by Felix after testing in IDE 1.0.6, 1.6.4
10 // > Updated May 19, 2016 D-H-R, added support for SST25/Microchip Flash which does not support Page programming with OPCode 0x02,
11 // > use define MY_SPIFLASH_SST25TYPE for SST25 Type Flash Memory. Added / changed comments to better suit doxygen
12 // > Updated Sep 07, 2018 tekka, sync with https://github.com/LowPowerLab/SPIFlash
13 // **********************************************************************************
14 // License
15 // **********************************************************************************
16 // This program is free software; you can redistribute it
17 // and/or modify it under the terms of the GNU General
18 // Public License as published by the Free Software
19 // Foundation; either version 3 of the License, or
20 // (at your option) any later version.
21 //
22 // This program is distributed in the hope that it will
23 // be useful, but WITHOUT ANY WARRANTY; without even the
24 // implied warranty of MERCHANTABILITY or FITNESS FOR A
25 // PARTICULAR PURPOSE. See the GNU General Public
26 // License for more details.
27 //
28 // You should have received a copy of the GNU General
29 // Public License along with this program.
30 // If not, see <http://www.gnu.org/licenses/>.
31 //
32 // Licence can be viewed at
33 // http://www.gnu.org/licenses/gpl-3.0.txt
34 //
35 // Please maintain this license information along with authorship
36 // and copyright notices in any redistribution of this code
37 
66 
67 #ifndef _SPIFLASH_H_
68 #define _SPIFLASH_H_
69 
70 #if ARDUINO >= 100
71 #include <Arduino.h>
72 #else
73 #include <wiring.h>
74 #include "pins_arduino.h"
75 #endif
76 
77 #include <SPI.h>
78 
79 #ifndef SPIFLASH_WRITEENABLE
80 #define SPIFLASH_WRITEENABLE 0x06
81 #endif
82 
83 #ifndef SPIFLASH_WRITEDISABLE
84 #define SPIFLASH_WRITEDISABLE 0x04
85 #endif
86 
87 #ifndef SPIFLASH_BLOCKERASE_4K
88 #define SPIFLASH_BLOCKERASE_4K 0x20
89 #endif
90 
91 #ifndef SPIFLASH_BLOCKERASE_32K
92 #define SPIFLASH_BLOCKERASE_32K 0x52
93 #endif
94 
95 #ifndef SPIFLASH_BLOCKERASE_64K
96 #define SPIFLASH_BLOCKERASE_64K 0xD8
97 #endif
98 
99 #ifndef SPIFLASH_CHIPERASE
100 #define SPIFLASH_CHIPERASE 0x60
101 #endif
102 
104 #ifndef SPIFLASH_STATUSREAD
105 #define SPIFLASH_STATUSREAD 0x05
106 #endif
107 
108 #ifndef SPIFLASH_STATUSWRITE
109 #define SPIFLASH_STATUSWRITE 0x01
110 #endif
111 
112 #ifndef SPIFLASH_ARRAYREAD
113 #define SPIFLASH_ARRAYREAD 0x0B
114 #endif
115 
116 #ifndef SPIFLASH_ARRAYREADLOWFREQ
117 #define SPIFLASH_ARRAYREADLOWFREQ 0x03
118 #endif
119 
120 #ifndef SPIFLASH_SLEEP
121 #define SPIFLASH_SLEEP 0xB9
122 #endif
123 
124 #ifndef SPIFLASH_WAKE
125 #define SPIFLASH_WAKE 0xAB
126 #endif
127 
128 #ifndef SPIFLASH_BYTEPAGEPROGRAM
129 #define SPIFLASH_BYTEPAGEPROGRAM 0x02
130 #endif
131 
132 #ifndef SPIFLASH_AAIWORDPROGRAM
133 #define SPIFLASH_AAIWORDPROGRAM 0xAD
134 #endif
135 
138 #ifndef SPIFLASH_IDREAD
139 #define SPIFLASH_IDREAD 0x9F
140 #endif
141 
144 #ifndef SPIFLASH_MACREAD
145 #define SPIFLASH_MACREAD 0x4B
146 #endif
147 
156 #ifdef DOXYGEN //needed to tell doxygen not to ignore the define which is actually made somewhere else
157 #define MY_SPIFLASH_SST25TYPE
158 #endif
159 
161 class SPIFlash
162 {
163 public:
164  static uint8_t UNIQUEID[8];
165  explicit SPIFlash(uint8_t slaveSelectPin, uint16_t jedecID=0);
166  bool initialize();
167  void command(uint8_t cmd, bool isWrite=
168  false);
169  uint8_t readStatus();
170  uint8_t readByte(uint32_t addr);
171  void readBytes(uint32_t addr, void* buf, uint16_t len);
172  void writeByte(uint32_t addr, uint8_t byt);
173  void writeBytes(uint32_t addr, const void* buf,
174  uint16_t len);
175  bool busy();
176  void chipErase();
177  void blockErase4K(uint32_t address);
178  void blockErase32K(uint32_t address);
179  void blockErase64K(uint32_t addr);
180  uint16_t readDeviceId();
181  uint8_t* readUniqueId();
182 
183  void sleep();
184  void wakeup();
185  void end();
186 protected:
187  void select();
188  void unselect();
189  uint8_t _slaveSelectPin;
190  uint16_t _jedecID;
191  uint8_t _SPCR;
192  uint8_t _SPSR;
193 #ifdef SPI_HAS_TRANSACTION
194  SPISettings _settings;
195 #endif
196 };
197 
198 #endif
SPIFlash::UNIQUEID
static uint8_t UNIQUEID[8]
Storage for unique identifier.
Definition: SPIFlash.h:164
SPIFlash
Definition: SPIFlash.h:161
SPIFlash::_SPCR
uint8_t _SPCR
SPCR.
Definition: SPIFlash.h:191
SPIFlash::busy
bool busy()
check if the chip is busy erasing/writing
SPIFlash::writeByte
void writeByte(uint32_t addr, uint8_t byt)
Write 1 byte to flash memory.
SPIFlash::end
void end()
end
SPIFlash::wakeup
void wakeup()
Wake device.
SPISettings
Definition: SPIBCM.h:69
SPIFlash::readBytes
void readBytes(uint32_t addr, void *buf, uint16_t len)
read unlimited # of bytes
SPIFlash::readDeviceId
uint16_t readDeviceId()
Get the manufacturer and device ID bytes (as a short word)
SPIFlash::sleep
void sleep()
Put device to sleep.
SPIFlash::blockErase4K
void blockErase4K(uint32_t address)
erase a 4Kbyte block
SPIFlash::_SPSR
uint8_t _SPSR
SPSR.
Definition: SPIFlash.h:192
SPIFlash::initialize
bool initialize()
setup SPI, read device ID etc...
SPIFlash::chipErase
void chipErase()
erase entire flash memory array
SPIFlash::_jedecID
uint16_t _jedecID
JEDEC ID.
Definition: SPIFlash.h:190
SPIFlash::blockErase64K
void blockErase64K(uint32_t addr)
erase a 64Kbyte block
SPIFlash::command
void command(uint8_t cmd, bool isWrite=false)
Send a command to the flash chip, pass TRUE for isWrite when its a write command.
SPIFlash::SPIFlash
SPIFlash(uint8_t slaveSelectPin, uint16_t jedecID=0)
Constructor.
SPIFlash::readStatus
uint8_t readStatus()
return the STATUS register
SPIFlash::unselect
void unselect()
unselect
SPIFlash::readByte
uint8_t readByte(uint32_t addr)
read 1 byte from flash memory
SPIFlash::select
void select()
select
SPIFlash::writeBytes
void writeBytes(uint32_t addr, const void *buf, uint16_t len)
write multiple bytes to flash memory (up to 64K), if define SPIFLASH_SST25TYPE is set AAI Word Progra...
SPIFlash::_slaveSelectPin
uint8_t _slaveSelectPin
Slave select pin.
Definition: SPIFlash.h:189
SPIFlash::readUniqueId
uint8_t * readUniqueId()
Get the 64 bit unique identifier, stores it in UNIQUEID[8].
SPIFlash::blockErase32K
void blockErase32K(uint32_t address)
erase a 32Kbyte block