MySensors Library & Examples  2.3.2-62-ge298769
SPIFlash_ReadWrite.ino
1 // **********************************************************************************
2 // This sketch is an example of using the SPIFlash library with a Moteino
3 // that has an onboard SPI Flash chip. This sketch listens to a few serial commands
4 // Hence type the following commands to interact with the SPI flash memory array:
5 // - 'd' dumps the first 256bytes of the flash chip to screen
6 // - 'e' erases the entire memory chip
7 // - 'i' print manufacturer/device ID
8 // - [0-9] writes a random byte to addresses [0-9] (either 0xAA or 0xBB)
9 // Get the SPIFlash library from here: https://github.com/LowPowerLab/SPIFlash
10 // **********************************************************************************
11 // Copyright Felix Rusu, LowPowerLab.com
12 // Library and code by Felix Rusu - [email protected]
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 // **********************************************************************************
38 
39 
40 #include <SPIFlash.h> //get it here: https://github.com/LowPowerLab/SPIFlash
41 #include <SPI.h>
42 
43 #define SERIAL_BAUD 115200
44 char input = 0;
45 long lastPeriod = -1;
46 
47 #ifdef __AVR_ATmega1284P__
48 #define LED 15 // Moteino MEGAs have LEDs on D15
49 #define FLASH_SS 23 // and FLASH SS on D23
50 #else
51 #define LED 9 // Moteinos have LEDs on D9
52 #define FLASH_SS 8 // and FLASH SS on D8
53 #endif
54 
56 // flash(SPI_CS, MANUFACTURER_ID)
57 // SPI_CS - CS pin attached to SPI flash chip (8 in case of Moteino)
58 // MANUFACTURER_ID - OPTIONAL, 0x1F44 for adesto(ex atmel) 4mbit flash
59 // 0xEF30 for windbond 4mbit flash
61 SPIFlash flash(FLASH_SS, 0xEF30);
62 
63 void setup()
64 {
65  Serial.begin(SERIAL_BAUD);
66  Serial.print("Start...");
67 
68  if (flash.initialize()) {
69  Serial.println("Init OK!");
70  Blink(LED, 20, 10);
71  } else {
72  Serial.println("Init FAIL!");
73  }
74 
75  delay(1000);
76 }
77 
78 void loop()
79 {
80  // Handle serial input (to allow basic DEBUGGING of FLASH chip)
81  // ie: display first 256 bytes in FLASH, erase chip, write bytes at first 10 positions, etc
82  if (Serial.available() > 0) {
83  input = Serial.read();
84  if (input == 'd') { //d=dump flash area
85  Serial.println("Flash content:");
86  int counter = 0;
87 
88  while(counter<=256) {
89  Serial.print(flash.readByte(counter++), HEX);
90  Serial.print('.');
91  }
92 
93  Serial.println();
94  } else if (input == 'e') {
95  Serial.print("Erasing Flash chip ... ");
96  flash.chipErase();
97  while(flash.busy());
98  Serial.println("DONE");
99  } else if (input == 'i') {
100  Serial.print("DeviceID: ");
101  Serial.println(flash.readDeviceId(), HEX);
102  } else if (input >= 48 && input <= 57) { //0-9
103  Serial.print("\nWriteByte(");
104  Serial.print(input);
105  Serial.print(")");
106  flash.writeByte(input-48, (millis()%2) ? 0xaa : 0xbb);
107  }
108  }
109 
110  // Periodically blink the onboard LED while listening for serial commands
111  if ((int)(millis()/500) > lastPeriod) {
112  lastPeriod++;
113  pinMode(LED, OUTPUT);
114  digitalWrite(LED, lastPeriod%2);
115  }
116 }
117 
118 void Blink(byte PIN, int DELAY_MS, byte loops)
119 {
120  pinMode(PIN, OUTPUT);
121  while (loops--) {
122  digitalWrite(PIN,HIGH);
123  delay(DELAY_MS);
124  digitalWrite(PIN,LOW);
125  delay(DELAY_MS);
126  }
127 }
SPIFlash
Definition: SPIFlash.h:161
HIGH
#define HIGH
Definition: bcm2835.h:572
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.
loop
void loop()
Main loop.
Definition: SPIFlash_ReadWrite.ino:78
LOW
#define LOW
Definition: bcm2835.h:574
SPIFlash.h
SPIFlash provides access to a SPI Flash IC for OTA update or storing data.
SPIFlash::readDeviceId
uint16_t readDeviceId()
Get the manufacturer and device ID bytes (as a short word)
SPIFlash::initialize
bool initialize()
setup SPI, read device ID etc...
SPIFlash::chipErase
void chipErase()
erase entire flash memory array
setup
void setup()
Called after node initialises but before main loop.
Definition: SPIFlash_ReadWrite.ino:63
SPIFlash::readByte
uint8_t readByte(uint32_t addr)
read 1 byte from flash memory