Search code examples
arraysarduinorfduino

How do I store an integer array in the RFduino flash?


I am trying to do some R&D and need to see how the temperature on the RFduino changes in a 5 minute time interval in a location not connected to a serial console, measured every second. I would like to use the built in flash for this. (I am storing bytes so that's 300 bytes of info which should fit on a 1K page, correct?) This is the code I have come up with but it only saves a single value to the flash. Or maybe I am only able to retrieve a single value. It currently runs for 10 seconds (while count<10). How do I save and retrieve the entire values array?

// select a flash page that isn't in use (see Memory.h for more info)
#define  MY_FLASH_PAGE  251

// double level of indirection required to get gcc
// to apply the stringizing operator correctly
#define  str(x)   xstr(x)
#define  xstr(x)  #x

byte values[500];
int count = 0;

void setup() {
  Serial.begin(38400);
  Serial.println("All output will appear on the serial monitor.");

  // a flash page is 1K in length, so page 251 starts at address 251 * 1024 = 257024 = 3EC00 hex
  byte *p = (byte*)ADDRESS_OF_PAGE(MY_FLASH_PAGE);
  int rc;

  Serial.println("The data stored in flash page " str(MY_FLASH_PAGE) " contains: ");
  Serial.println(*p);

  Serial.print("Attempting to erase flash page " str(MY_FLASH_PAGE) ": ");
  rc = flashPageErase(PAGE_FROM_ADDRESS(p));
  if (rc == 0)
    Serial.println("Success");
  else if (rc == 1)
    Serial.println("Error - the flash page is reserved");
  else if (rc == 2)
    Serial.println("Error - the flash page is used by the sketch");

  Serial.println("The data stored in flash page " str(MY_FLASH_PAGE) " contains: ");
  Serial.println(*p);


  while (count<10){
    RFduino_ULPDelay(SECONDS(1));
    byte temp = RFduino_temperature(CELSIUS);
    Serial.println (count);
    Serial.println (temp);
    values[count]=temp;
    count++;
  }

  Serial.print("Attempting to write data to flash page " str(MY_FLASH_PAGE) ": ");
  rc = flashWriteBlock(p, &values, sizeof(values));
  if (rc == 0)
    Serial.println("Success");
  else if (rc == 1)
    Serial.println("Error - the flash page is reserved");
  else if (rc == 2)
    Serial.println("Error - the flash page is used by the sketch");

  Serial.println("The data stored in flash page " str(MY_FLASH_PAGE) " contains: ");
  Serial.println(*p);
}

void loop() {

  }

This is my output:

All output will appear on the serial monitor.
The data stored in flash page 251 contains: 
255
Attempting to erase flash page 251: Success
The data stored in flash page 251 contains: 
255
0
33
1
33
2
33
3
33
4
33
5
33
6
33
7
33
8
33
9
33
Attempting to write data to flash page 251: Success
The data stored in flash page 251 contains: 
33

I want it to write and retrieve all 10 values!


Solution

  • You can access the data as if you were accessing an array.

    To test this you can (temporarily) comment out your FLASH-writing piece of code and run this snippet:

    byte *p = (byte*)ADDRESS_OF_PAGE(MY_FLASH_PAGE);      
    
    Serial.println("The data stored in flash page " str(MY_FLASH_PAGE) " contains: ");
    for (count = 0; count < 10; count++)
    {
        Serial.print(count);
        Serial.print(":");    
        Serial.print(p[count]);
        Serial.print("; ");    
    }
    Serial.println();
    

    A word of caution: RFduino_temperature(CELSIUS) returns float value.