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

flash write problem

I am trying to implement some write to flash, and started by integrating the relevant functions from the example code I found in this thread.

I added a dummy write function in my main.c like this:

static uint8_t pstorage_wait_flag = 0;
static pstorage_block_t pstorage_wait_handle = 0;

static void dummy_write(void)
{
    pstorage_handle_t       handle;
    pstorage_handle_t		block_0_handle;
    pstorage_module_param_t param;
    uint8_t                 source_data_0[16] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F};
    //uint8_t                 dest_data_0[16];

    param.block_size  = 16;                  
    param.block_count = 10;                  
    param.cb          = example_cb_handler;  

    pstorage_init();
    pstorage_register(&param, &handle);
    pstorage_block_identifier_get(&handle, 0, &block_0_handle);
    pstorage_clear(&block_0_handle, 48);

    pstorage_wait_flag = 1;
    pstorage_wait_handle = block_0_handle.block_id;
    pstorage_store(&block_0_handle, source_data_0, 16, 0); 

    while(pstorage_wait_flag) {
        power_manage();
    }
}

Next to that, I have made the call back function :

static void example_cb_handler(pstorage_handle_t  * handle,
                           uint8_t              op_code,
                           uint32_t             result,
                           uint8_t            * p_data,
                           uint32_t             data_len)
{
    if(handle->block_id == pstorage_wait_handle) { pstorage_wait_flag = 0; } 
}

And I also made sure that in the system event handler, the pstorage system event handler is called:

...
    // Register with the SoftDevice handler module for BLE events.
    err_code = softdevice_sys_evt_handler_set(sys_evt_dispatch);
...

static void sys_evt_dispatch(uint32_t sys_evt)
{
    pstorage_sys_event_handler(sys_evt);

}

When I then call the dummy_write function after the stack initialization, the call back function is never called, and I get stuck in the endless wait loop.

I thought I did everything right, but apparently I missed something. I have read the different threads in the forum about flash write, but do not really found a clue to what my mistake is.

Can anyone help?

Wim

PS: I am using SDK 7.2.0 and soft device 7.3.0

Parents
  • Hi

    I saw nothing wrong with your code. I tried to put your code directly into ble_app_template example in SDK 7.2.0, but experienced the same as you, the example_cb_handler was never called. I saw however that the sys_evt_dispatch handler was called. The CPU goes into a hardfault when trying to call example_cb_handler from pstorage.c, but I do not realize at this point why that happens. However, inserting more code into the example_cb_handler seems to fix the problem. If I define it as e.g.

    static void example_cb_handler(pstorage_handle_t  * handle,
    													 uint8_t              op_code,
    													 uint32_t             result,
    													 uint8_t            * p_data,
    													 uint32_t             data_len)
    {
    		if(handle->block_id == pstorage_wait_handle) 
    		{ 
    			 pstorage_wait_flag = 0; 
    		} 
    		
    		switch(op_code)
    		{
    			case PSTORAGE_LOAD_OP_CODE:
    				 if (result == NRF_SUCCESS)
    				 {
    				 pstorage_wait_flag = 0; 
    				 
    						 SEGGER_RTT_WriteString(0, "pstorage LOAD callback received \n\n");
    						 LEDS_INVERT(BSP_LED_1_MASK);				 
    				 }
    				 else
    				 {
    						 SEGGER_RTT_WriteString(0, "pstorage LOAD ERROR callback received \n\n");
    						 LEDS_INVERT(BSP_LED_2_MASK);	
    				 }
    				 break;
    			case PSTORAGE_STORE_OP_CODE:
    				 if (result == NRF_SUCCESS)
    				 {
    						 SEGGER_RTT_WriteString(0, "pstorage STORE callback received \n\n");
    						 LEDS_INVERT(BSP_LED_1_MASK);	
    				 }
    				 else
    				 {
    						 SEGGER_RTT_WriteString(0, "pstorage STORE ERROR callback received \n\n");
    						 LEDS_INVERT(BSP_LED_2_MASK);	
    				 }
    				 break;				 
    			case PSTORAGE_UPDATE_OP_CODE:
    				 if (result == NRF_SUCCESS)
    				 {
    						 SEGGER_RTT_WriteString(0, "pstorage UPDATE callback received \n\n");
    						 LEDS_INVERT(BSP_LED_1_MASK);	
    				 }
    				 else
    				 {
    						 SEGGER_RTT_WriteString(0, "pstorage UPDATE ERROR callback received \n\n");
    						 LEDS_INVERT(BSP_LED_2_MASK);	
    				 }
    				 break;
    			case PSTORAGE_CLEAR_OP_CODE:
    				 if (result == NRF_SUCCESS)
    				 {
    						 SEGGER_RTT_WriteString(0, "pstorage CLEAR callback received \n\n");
    						 LEDS_INVERT(BSP_LED_1_MASK);	
    				 }
    				 else
    				 {
    						 SEGGER_RTT_WriteString(0, "pstorage CLEAR ERROR callback received \n\n");
    						 LEDS_INVERT(BSP_LED_2_MASK);	
    				 }
    				 break;
    			case PSTORAGE_ERROR_OP_CODE:
    						SEGGER_RTT_WriteString(0, "pstorage ERROR callback received \n\n");
    						LEDS_INVERT(BSP_LED_2_MASK);	
    				 break;				 
    		}	
    }
    

    it seems to fix the problem. I will have to investigate further what causes the hardfault.

    In this case I use Segger RTT to see when each callback is received.

  • Two mistakes in the code.

    1. Variables in dummy_write need to be declared static. With the current implementation where variables only exist locally in the dummy_write function, the flash operation will be pointing to locations in RAM, that actually might be used for something else.

    2. The scheduler apparently needs to be used for softdevice callbacks, i.e. SOFTDEVICE_HANDLER_INIT(NRF_CLOCK_LFCLKSRC_XTAL_20_PPM, true) instead of SOFTDEVICE_HANDLER_INIT(NRF_CLOCK_LFCLKSRC_XTAL_20_PPM, false)

Reply Children
No Data
Related