store variable data to spiflash

Started by djash40, February 17, 2016, 04:47:10 PM

djash40

I'm looking for and example to store variables data to the flash memory are so it can survive a reboot.
Is there and example. Simple example for my simple mind... lol ;D

TomWS

Quote from: djash40 on February 17, 2016, 04:47:10 PM
I'm looking for and example to store variables data to the flash memory are so it can survive a reboot.
Is there and example. Simple example for my simple mind... lol ;D
Did you look at the SPIFlash_ReadWrite example in that library?  It shows you how to read and write from the flash. 

Tom

djash40

I assume read is: Serial.println(flash.readDeviceId(), HEX);
And write is: flash.writeByte(input-48, millis()%2 ? 0xaa : 0xbb);

Seems greek to me...

Is there a more beginner , emphasize beginner example?

TomWS

Quote from: djash40 on February 17, 2016, 05:11:19 PM
I assume read is: Serial.println(flash.readDeviceId(), HEX);
And write is: flash.writeByte(input-48, millis()%2 ? 0xaa : 0xbb);

Seems greek to me...

Is there a more beginner , emphasize beginner example?
ADVICE: You need to learn how to read a library's .h file.  If you did with the SPIFlash library, you will find, among other things:

class SPIFlash {
public:
  SPIFlash(uint8_t slaveSelectPin, uint16_t jedecID=0);      // THIS is the constructor so you create an instance with: SPIFlash flash(FLASH_SS, 0xEF30);;
  boolean initialize();                                    // THIS is how you initialize it as in:  flash.initialize();
  void writeBytes(long addr, const void* buf, uint16_t len); // THIS is how you write a block of data to flash:  flash.writeBytes(myAddress, &myBlock, sizeof(myBlock));
  void readBytes(long addr, void* buf, word len);       // THIS is how you read a block of data from flash: flash.readBytes(myAddress, &myBlock, sizeof(myBlock));
  void blockErase4K(long address);                      // and THIS is how you erase a 4K block of memory (the minimum size you can erase with this library) as in: flash.blockErase4K(myAddress);
};


Put it all together and you get:
#define myAddress 0x20000     // some place you want to put data that won't get trashed if you use the WirelessProgramming
struct
{
   char someStuff[128];
   int   someMoreStuff;
   float someFancyStuff;
} myBlock;

   flash.initialize();   // you SHOULD check the results of this before you do anything
   flash.readBytes(myAddress,&myBlock,sizeof(myBlock));  // get the data that had been saved before
   ...
   myBlock.someFancyStuff = 3.14159;
   strcpy(myBlock.someStuff,"I sure hope you're getting this!");
   ...
   flash.blockErase4K(myAddress);   // gotta erase the flash BEFORE you can write to it
   flash.writeBytes(myAddress,&myBlock,sizeof(myBlock));  // save the new data
   ...


Net: if you want to use Arduino libraries you need to learn how to read the 'documentation'.

Tom

Felix

Quote from: djash40 on February 17, 2016, 04:47:10 PM
I'm looking for and example to store variables data to the flash memory are so it can survive a reboot.
Is there and example. Simple example for my simple mind... lol ;D

djash40,
SPIFlash is one way to store data, and more so for larger pieces of data. All that Tom already mentioned is valid for that.

I would just like to add that for small variables and configuration in general, the EEPROM of the atmega328 is also a great place to store data that survives power cycles, and it does not require the extra flash mem chip to be present on the Moteino. An example of how to read data from the terminal and save it in a struct that is then saved to EEPROM is here: https://github.com/LowPowerLab/SwitchMote/blob/master/SwitchMoteConfig.ino