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

pstorage_update call MCU reset

My custom board, it connected with App, App sends a command to the board and the board keeps the command in the flash, but when i call the "pstorage_update", it will cause reset.

My code is as follow: is there any wrong? Thanks!

nv_write(0,buf,16);

uint32_t nv_write(uint8_t block_index, uint8_t* buf,uint16_t len) {

uint32_t ret;

nv_status = NV_BUSY;

pstorage_handle_t dest_block_id;

pstorage_block_identifier_get(&g_block_id,block_index, &dest_block_id); pstorage_clear(&dest_block_id,16);

ret = pstorage_update(&dest_block_id,buf,len,0);

return ret;

}

  • Why are you clearing before updating? Wouldn't this be equivalent of clearing and then storing? You should check the return values/error codes of pstorage_block_identifier_get(), pstorage_clear(), and pstorage_update. Not just pstorage_update(). pstorage_clear() is non-blocking, so when it returns it doesn't mean that the block is cleared. You need to use the event notification handler to know if and when the operation is successful. Please examine the documentation. There are also several questions on devzone explaining this.

  • Hi, thanks for your help. But I still failed in update the data into the flash. I check the return values/error codes of pstorage_block_identifier_get(), pstorage_clear(), and pstorage_update, the return value of these functions are "NRF_SUCCESS". when call the "pstorage_update" it returns "NRF_SUCCESS", when then it reset.

  • What SoftDevice and SDK version are you using? Could you edit your question and upload your complete project so I can test it here?

  • Hi, SD version is 8.1, SDK is 10.0,

    my code is below, please help me check it.

    #include <stdint.h>
    #include <string.h>
    #include "nordic_common.h"
    #include "nrf.h"
    #include "nrf_soc.h"
    #include "nrf_adc.h"
    #include "app_error.h"
    #include "ble.h"
    #include "ble_hci.h"
    #include "ble_nus.h"
    #include "ble_srv_common.h"
    #include "softdevice_handler.h"
    #include "app_timer.h"
    #include "app_uart.h"
    #include "app_pwm.h"
    #include "device_manager.h"
    #include "app_util.h"
    #include "pstorage.h"
    #include "app_trace.h"
    #include "bsp.h"
    #include "bsp_btn_ble.h"
    #include "nrf_delay.h"
    #include "user_util.h"
    #include "user_nv.h"
    #include "pstorage_platform.h"
    
    pstorage_handle_t g_block_id;
    
    nv_status_t nv_status = NV_IDEL;
    
    static void flash_cb(pstorage_handle_t * handle,uint8_t op_code,uint32_t result,uint8_t * p_data, uint32_t data_len)
    {    
        switch(op_code)
        {
            case PSTORAGE_STORE_OP_CODE:
            {
                if (result==NRF_SUCCESS)
                {
                    nv_status = NV_IDEL;
                }
            
            }
          
            case PSTORAGE_UPDATE_OP_CODE:
            if (result == NRF_SUCCESS)
            {
                
            }
            break;
            
            case PSTORAGE_LOAD_OP_CODE:
            if (result == NRF_SUCCESS)
            {
     
            }
            break;
            
            case PSTORAGE_CLEAR_OP_CODE:
            if (result == NRF_SUCCESS)
            {
               
            }
            break;
        }
        
    }
    
    
    void nv_init(void)
    {
      uint32_t ret;
      
      pstorage_module_param_t module_param;
      module_param.block_count =2; 
      module_param.block_size = 16;
      
      module_param.cb = flash_cb;
      
      ret = pstorage_init(); //Init
      
      if (ret!=NRF_SUCCESS)
      {
          while(1);
      }
      
      ret = pstorage_register(&module_param, &g_block_id);
    
      if (ret!=NRF_SUCCESS)
      {
          while(1);
      }
    
    }
    
    /*
    Get Block NV Data
    */
    uint32_t nv_read(uint8_t block_index, uint8_t* buf,uint16_t len)
    {
      uint32_t ret;
      
      pstorage_handle_t dest_block_id;
      pstorage_block_identifier_get(&g_block_id,block_index, &dest_block_id); 
      
      ret = pstorage_load(buf,&dest_block_id,len,0);
    
      if (ret!=NRF_SUCCESS)
      {
          while(1);
      }
      
      return ret;
    }
    
    /*
    Write Data To NV
    */
    uint32_t nv_write(uint8_t block_index, uint8_t* buf,uint16_t len)
    {
      uint32_t ret;
      
      nv_status = NV_BUSY;
      
      pstorage_handle_t dest_block_id;
      
      ret = pstorage_block_identifier_get(&g_block_id,block_index, &dest_block_id); 
    
      if (ret!=NRF_SUCCESS)
      {
          return ret;
      }
      
      ret = pstorage_clear(&g_block_id,16);
      
      if (ret!=NRF_SUCCESS)
      {
          return ret;
      }
       
      return ret;
    }
    
  • Can you please edit your question and include your complete project? Zip the ble_app_uart folder and attach it.

Related