Hi.
I need to store a series of array of integers to flash mem. As far as I know the functions avaliale for these are:
readByte, readBytes, writeByte, writeBytes
Very basic, so I guess I need to program a way to store integer.
Correct me please please in these pseudocode:
for each element of int array // ex. {1600,500,1800,300,10,2500}
read int
split into 2 byte
save byte1
save byte2
end for
Maybe you can point me to an already created example for this.
Thanks
Regards
Javier
Quote from: javiercuellar on May 08, 2016, 07:01:38 PM
Hi.
I need to store a series of array of integers to flash mem. As far as I know the functions avaliale for these are:
readByte, readBytes, writeByte, writeBytes
Very basic, so I guess I need to program a way to store integer.
Correct me please please in these pseudocode:
for each element of int array // ex. {1600,500,1800,300,10,2500}
read int
split into 2 byte
save byte1
save byte2
end for
Maybe you can point me to an already created example for this.
Thanks
Regards
Javier
You don't need to split the multi-byte value. If you use writeBytes with the address of the variable (cast as a byte pointer) and the sizeof the object, you can store as many bytes as the size of your object, including an entire array of integers.
Tom
UPDATED: to include array of integer comment.
Excelente Tom!!! Thanks.
Just as example for anyone else.
#include <SPIFlash.h> //get it here: https://github.com/LowPowerLab/SPIFlash
unsigned int data[] = {8950,4400,700,1550,700,1600,650,450,600,500};
unsigned int data1[100]={};
char input = 0;
#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
SPIFlash flash(FLASH_SS, 0xEF30);
void setup() {
Serial.begin(9600);
Serial.print("Start...");
if (flash.initialize())
Serial.println("Init OK!");
else
Serial.println("Init FAIL!");
delay(1000);
Serial.print("DataSize: ");
Serial.println(sizeof(data));
Serial.println("Input:");
Serial.println("d: to dump all values from 0 to 4100 bytes");
Serial.println("e: to erase the whole flash");
Serial.println("b: to erase 1 block (4096 bytes:");
Serial.println("1: to save values into flash");
Serial.println("2: to read values from flash and dump them to serial");
}
void loop() {
if (Serial.available() > 0) {
input = Serial.read();
int counter = 0;
switch (input){
case 'd': // dump a little more than 4Kb
Serial.println("Flash content:");
while(counter<=4100){
Serial.print(counter);
Serial.print(": ");
Serial.println(flash.readByte(counter++), DEC);
}
break;
case 'e': //erase whole data
Serial.print("Erasing Flash chip ... ");
flash.chipErase();
while(flash.busy());
Serial.println("DONE");
break;
case 'b': // erase 1 block (4K)
Serial.println("Erasing 4K from position 1");
flash.blockErase4K(1);
break;
case '1': // Save to flash, from position 1 the int array in data
Serial.println("Saving...");
flash.writeBytes(1, &data,sizeof(data));
Serial.println("Done");
break;
case '2': // Reads from position 1, into array data1,
Serial.println("Reading....");
flash.readBytes(1, data1, sizeof(data));
for (int i=0; i<sizeof(data1);i++)
{
Serial.print(i);
Serial.print(": ");
Serial.println(data1[i]);
}
Serial.println("Done");
break;
}
}
}