如何在 RFduino 闪存中存储整数数组?

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

我正在尝试进行一些研发,需要查看 RFduino 上的温度在未连接到串行控制台的位置以 5 分钟的时间间隔变化,每秒测量一次。我想为此使用内置闪光灯。 (我正在存储字节,所以 300 字节的信息应该适合 1K 页,对吗?)这是我想出的代码,但它只将一个值保存到闪存中。或者,也许我只能检索一个值。它当前运行 10 秒(而 count<10)。如何保存和检索整个值数组?

// 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() {

  }

这是我的输出:

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

我想让它写入和检索所有 10 个值!

您可以像访问数组一样访问数据。

要对此进行测试,您可以(暂时)注释掉您的 FLASH 编写代码和 运行 此代码段:

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();

注意事项:RFduino_temperature(CELSIUS) returns 浮点值。