Writing to the FLASH

Started by LesB, January 23, 2015, 08:56:11 PM

LesB

I am needing to learn read/write to the flash.
So I checked the sample file, "SPIFlash_ReadWrite.ino" and found this write statement:

flash.writeByte(input-48, millis()%2 ? 0xaa : 0xbb);



I will be adding data sequentially once per second.  Is there a reference that can tell me about the parameters?  Or someone tell me here?

Thanks,
LesB

TomWS

Quote from: LesB on January 23, 2015, 08:56:11 PM
I am needing to learn read/write to the flash.
So I checked the sample file, "SPIFlash_ReadWrite.ino" and found this write statement:

flash.writeByte(input-48, millis()%2 ? 0xaa : 0xbb);



I will be adding data sequentially once per second.  Is there a reference that can tell me about the parameters?  Or someone tell me here?

Thanks,
LesB
One question, will you be using the WirelessProgramming library with your Moteino?

The reason I ask is that the SPI_Flash library is included (or expected) with that Library.  And, if you're using that library, then it's a simple matter of resolving WHERE you want to store your data - the code is easy.  You need to store user data ABOVE the first 32K block because this is used by the WirelessProgramming library.

Assuming you already have this bit of code in your program (it's used in the RFM69 Node example):
SPIFlash flash(FLASH_SS, 0xEF30); //EF30 for 4mbit  Windbond chip (W25X40CL)


Then, to write to flash, you need to do two things, first is understand that, in order to write to flash, you must first erase the block to which you're writing and blocks are a minimum of 4K bytes.  So, assuming you want to log 12K samples, you would:

#define MY_LOG_ADDR 32768
static unsigned long nextLogEntry = 0;

TIRE_INFO_TYPE sample;

void setup(void)
{
    ...
    flash.initialize();   // you're probably doing this already

    // completely erase where the log is going
    int numBlocks = 3 * sizeof(sample);   // calculate how many 4K blocks we have

    for (int i=0; i<numBlocks; i++)
        flash.blockErase4K(MY_LOG_ADDR+ (i * 4096));
   
    nextLogEntry = 0;   // and re-initialize the index

   ...


Then, as you get a sample that you want to save...

    flash.writeBytes(MY_LOG_ADDR+nextLogEntry, &sample, sizeof(sample));
    nextLogEntry += sizeof(sample);


Look at SPIFlash.h in the SPIFlash library to see if you can understand how to read this data.  If not, come back and ask.

Tom

Bruno

Sorry, but I don't understand the coefficient 3 ?

int numBlocks = 3 * sizeof(sample);   // calculate how many 4K blocks we have

TomWS

Quote from: Bruno on March 17, 2015, 07:37:51 AM
Sorry, but I don't understand the coefficient 3 ?

int numBlocks = 3 * sizeof(sample);   // calculate how many 4K blocks we have
LOL, it took me a while to figure out why I used '3' as well.  The 'secret' is in the line above the code:
Quote from: TomWS on January 24, 2015, 09:04:26 AM
So, assuming you want to log 12K samples, you would:
So, if the OP wanted 12K 'samples', the number of blocks used would be 3 (as in 12K/4K) * the size of the sample...

Sorry, I should have commented the code better.

Tom

Bruno

Thank you ! Do you think it is possible to use the library WirelessProgramming to send data from node to gateway (memory flash above the 32K)  ? The idea would be to use CheckForSerialHEX on the node. but there will be a memory address problem?

TomWS

Quote from: Bruno on March 17, 2015, 09:12:50 AM
Thank you ! Do you think it is possible to use the library WirelessProgramming to send data from node to gateway (memory flash above the 32K)  ? The idea would be to use CheckForSerialHEX on the node. but there will be a memory address problem?
I'm not sure I understand your question.  What I think you're asking is if the Wireless Programming library can be used to send the log (which is stored in flash) to the gateway?

If so, no, the library is not oriented that way.  It would require total restructuring to do that.  Besides, sending the log to the gateway is trivial.

If the log is made up of 'n' samples, each sample of size 's', create a structure that holds 'm' samples, where 'm'*'s' < 61 bytes - the max size of a packet, then send 'n'/'m' of these to the gateway...   Each 'm' samples can be read from flash similar to the way you stored them.  When all are transferred, erase the blocks.

Tom