This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Flash Storage Write API has issues

Hello,

I am trying to use the flash API of fstorage in my code. I have called below function at the start of my code. I want to save some integer values every 1 min interval.

Flash reading is working fine, but when writing was tried my whole code gets problem. When write api called debug messaged also doesn’t get print. I have included my c code for flash below. Kindly suggest what should be done.

Thank You.

NRF_FSTORAGE_DEF(nrf_fstorage_t fstorage) =
{
    /* Set a handler for fstorage events. */
    .evt_handler = fstorage_evt_handler,

    /* These below are the boundaries of the flash space assigned to this instance of fstorage.
     * You must set these manually, even at runtime, before nrf_fstorage_init() is called.
     * The function nrf5_flash_end_addr_get() can be used to retrieve the last address on the
     * last page of flash available to write data. */
    .start_addr = FLASH_START_ADDR,         //    0x70ff0
    .end_addr   = FLASH_END_ADDR,          //     0x80000
};


void Flash_init(void)
{
 uint32_t data_1=5;
      ret_code_t rc;

      #ifdef  FLASH_12
      nrf_fstorage_api_t * p_fs_api;

     /* Initialize an fstorage instance using the nrf_fstorage_sd backend.
     * nrf_fstorage_sd uses the SoftDevice to write to flash. This implementation can safely be
     * used whenever there is a SoftDevice, regardless of its status (enabled/disabled). */
      p_fs_api = &nrf_fstorage_sd;



      rc = nrf_fstorage_init(&fstorage, p_fs_api, NULL);
      APP_ERROR_CHECK(rc);

      /* It is possible to set the start and end addresses of an fstorage instance at runtime.
     * They can be set multiple times, should it be needed. The helper function below can
     * be used to determine the last address on the last page of flash memory available to
     * store data. */
//     (void) nrf5_flash_end_addr_get();


//       print_flash_info(&fstorage);

      #ifdef  UART_Flash
      printf("Flash Init=%d\n",rc);
      #endif
//
    
            wait_for_flash_ready(&fstorage);


    #endif

}


void Flash_Operation(void)
{
 ret_code_t rc;
 uint32_t local_var=0, data=5;
 uint8_t  len=0;
  
  /***********************************Flash Read *****************************************/
    #ifdef  FLASH_12
      wait_for_flash_ready(&fstorage);
//    printf("Done.");

      rc = nrf_fstorage_read(&fstorage, FLASH_START_ADDR, &local_var, sizeof(local_var));
       
       APP_ERROR_CHECK(rc);
        wait_for_flash_ready(&fstorage);
                   
      #ifdef  UART_Flash
        printf("local_var=%x , RC=%x \n",local_var,rc); 
        printf("Flash read Done.\n");
      #endif


      #endif
     /***********************************Flash read end *****************************************/

      /***********************************Flash write *****************************************/
          
           rc = nrf_fstorage_erase(&fstorage, FLASH_START_ADDR , 1, NULL);
        
            APP_ERROR_CHECK(rc);
            wait_for_flash_ready(&fstorage);
            rc = nrf_fstorage_write(&fstorage, FLASH_START_ADDR , &data, sizeof(data), NULL);
    
            APP_ERROR_CHECK(rc);
            wait_for_flash_ready(&fstorage);
            
                #ifdef  UART_Flash
                printf("ERR code=%d,Flash write %x\n",rc,data);
                #endif
        /***********************************Flash write end *****************************************/

}

  • Hi,

    Is it so that the call to nrf_fstorage_write() on line 86 in the snippet returns but not the call to wait_for_flash_ready() on line 89? If so, a typical reason for such a behavior would be if this code is running with the same or higher interrupt priority than fstorage. If so the scheduled write would never happen, and there would be a deadlock.

  • Hi Einar,

                Can you please explain more on this I am not clear on what you said "Is it so that the call to nrf_fstorage_write() on line 86 in the snippet returns but not the call to wait_for_flash_ready() on line 89? "

    I have changed the priority of other peripherals but the issue is still there. As you said, fstorage priority higher or same but I couldn't able to find in the sdk_config.h file

  • Hi,

    GeekAD said:
    Can you please explain more on this I am not clear on what you said "Is it so that the call to nrf_fstorage_write() on line 86 in the snippet returns but not the call to wait_for_flash_ready() on line 89? "

    I was just writing down how I understood your description of the behaviour to try to be sure I got it right. To rephrase: Is it so that execution reaches wait_for_flash_ready() after the call to wait_for_flash_ready(), but it never returns? I assume so.

    GeekAD said:
    I have changed the priority of other peripherals but the issue is still there. As you said, fstorage priority higher or same but I couldn't able to find in the sdk_config.h file

    If my initial theory is relevant, then what matters is the priority where you are waiting for the flash operation to finish relative to the fstorage priority, which is essentially the SoftDevice interrupt priority, if you are using a SoftDevice. Can you confirm that you are using a SoftDevice?

    If yes,

    • How is your Flash_Operation() called? where is it called from? Is it from an event handler or similar running (interrupt), or from the main loop?

    If this is not the issue or you do not make progress perhaps you can show more or all of your project so that I get a proper context to understand what is happening?

  • Hi,

    I am calling this function at the beginning of the main. and yes I am using softdevice.

        // Hardware initialization 
        Hardware_INIT();
        #ifdef  FLASH_12
        // Flash initialization 
          Flash_init();
      
        // Flash Read & write function to check if it is actually working
          Flash_Operation();
        #endif
        
         

  • I see. I do not see anything obvious, then. Can you upload code that reproduce this on a DK so that I can get a full understanding and test on my side? Which SDK version are you using?

Related