Cannot read flash memory after reset using FDS

8738.sdk_config.h

Hi, I'm developing an application using internal flash memory.
I based on flash_fds example to implement this feature.
Now I can write and read data from flash.
My basic feature is when device reset It will read data from memory and it will blink led it data I want to read consist in flash memory. In this case I saved "hello !"

Here is my code

#define FILE_ID 0x0001    /* The ID of the file to write the records into. */
#define RECORD_KEY 0x1111 /* A key for the first record. */

// record using for write
fds_record_t record;
fds_record_desc_t record_desc;
fds_flash_record_t flash_record;
fds_find_token_t ftok;

bool flash_flag = false;
int flash_count = 0;
bool m_fds_initialized = false;

static char const m_hello[] = "hello !";

static void wait_for_fds_ready(void) {
  while (!m_fds_initialized) {
    power_manage();
  }
}

static void fds_evt_handler(fds_evt_t const *p_fds_evt) {
  switch (p_fds_evt->id) {
  case FDS_EVT_INIT:
    if (p_fds_evt->result == NRF_SUCCESS) {
      m_fds_initialized = true;
    }
    break;
  case FDS_EVT_WRITE:
    // NRF_LOG_INFO("write")
    flash_flag = true;
    memset(&ftok, 0x00, sizeof(fds_find_token_t));
    break;
  // case FDS_EVT_READ:
  default:
    break;
  }
}

uint8_t dataFromFlash[100];

int main(void) {
  bool erase_bonds;
  log_init();

  fds_register(fds_evt_handler);
  fds_init();
  wait_for_fds_ready();

  fds_record_open(&record_desc, &flash_record);
  sprintf(dataFromFlash, "%s", flash_record.p_data);
  if (strstr(dataFromFlash, m_hello) != NULL) {
    blinkLED(orange);

  } else {
    blinkLED(yellow);
  }
  fds_record_close(&record_desc);

  record.file_id = FILE_ID;
  record.key = RECORD_KEY;

  record.data.p_data = &m_hello;
  record.data.length_words = (sizeof(m_hello) + 3) / 4;
  fds_record_write(&record_desc, &record);

  while (flash_flag) {

    // while (fds_record_find(FILE_ID, RECORD_KEY, &record_desc, &ftok) == NRF_SUCCESS) {
    fds_record_open(&record_desc, &flash_record);
    sprintf(dataFromFlash, "%s", flash_record.p_data);
    NRF_LOG_INFO("%s", dataFromFlash);
    fds_record_close(&record_desc);
    break;
  }
  for (;;) {
  }
}

 
I attched my sdk_config.h

Parents
  • Hi Simonr, I use hard reset by button.
    According to piece of code I attached.
    Step by to step. Assume the first time my nRF have no data in flash memory.
    Step 1:
    From line 52->58:
    - nRF copy data from flash and saved dataFromFlash
    - then compare with m_hello which declared as "hello !" => false (because flash has no any data before was wroten before) => blink led yellow (line 56) 

    Step 2: 
    From line 60 -> 70
    - nRF wrote to flash data a string "hello !" -> I read from flash and show at 72

    Step 3: After reading the flash successfully => I press the DK button to hard reset the device => the program returns to step 1 => so in this step the nRF will flash the orange led (line 53) because the data from the flash has been pre-saved in the previous step

    Do you understand my idea? I mean i wrote data at first time and next times nRF will blink orange LED instead of yellow LED because on first run the nRF has already written data to flash

    I didn't get any error code, just only flash have no data to read.

Reply
  • Hi Simonr, I use hard reset by button.
    According to piece of code I attached.
    Step by to step. Assume the first time my nRF have no data in flash memory.
    Step 1:
    From line 52->58:
    - nRF copy data from flash and saved dataFromFlash
    - then compare with m_hello which declared as "hello !" => false (because flash has no any data before was wroten before) => blink led yellow (line 56) 

    Step 2: 
    From line 60 -> 70
    - nRF wrote to flash data a string "hello !" -> I read from flash and show at 72

    Step 3: After reading the flash successfully => I press the DK button to hard reset the device => the program returns to step 1 => so in this step the nRF will flash the orange led (line 53) because the data from the flash has been pre-saved in the previous step

    Do you understand my idea? I mean i wrote data at first time and next times nRF will blink orange LED instead of yellow LED because on first run the nRF has already written data to flash

    I didn't get any error code, just only flash have no data to read.

Children
No Data
Related