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

pstorage wait problem.

Hi.

I'm trying to make NRF52 write 6 bytes of data into flash and read in the DFU Bootloader.

For that purpose I choose to use "pstorage" library instead of other flash related library.

I referenced the code in this thread.

The first problem I noticed is my code is not working correctly in the debug mode.

If I run this code in the debug mode with Keil, main() function is called infinitely and it's unable to control.

The code is executed until it reaches first while loop in the code.

  while(pstorage_wait_flag == 1); 

But at this point the program goes back to starting point of main loop and repeating it.

I don't know what's wrong with it.

The second problem is also weird.

When I executed the code in the normal mode(not debug), the code doesn't loop the main and executed as I expected until it reaches the first while loop.

  while(pstorage_wait_flag == 1); 

At this point, this loop condition is not cleared even if pstorage_wait_flag is cleared to 0 in the pstorage_cb_handler.

I checked pstorage_cb_handler() function is called normally but nrf52 stuck in that while loop.

But, if I add debug printf code in the while loop, it works as I expected.

Like this

while(pstorage_wait_flag == 1); 
{
    SEGGER_RTT_printf(0, "while %d\r\n", pstorage_wait_flag); // problem occurs.
};

But If I remove the SEGGER_RTT_printf() function, then it stuck again.

I don't know what I'm missing in my code.

I use nRF52 (QFAAB0) with SDK 11 and s132.

Thanks for reading. Any advice will be appreciated.

Below is my code.

--- handler() --- 
static void pstorage_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; 
    SEGGER_RTT_printf(0, "handler %d\r\n", pstorage_wait_flag);
    nrf_gpio_pin_toggle(LED_2);
  }  //If we are waiting for this callback, clear the wait flag.
}

--- main() ---
  ble_stack_init();
  err_code = pstorage_init();
  APP_ERROR_CHECK(err_code);

  pstorage_handle_t  handle;
  pstorage_handle_t	 block_0_handle;
  pstorage_module_param_t param;
  uint8_t  source_data_0[6] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06};
  uint8_t  dest_data_0[6];
  uint32_t  queue_count = 0;

  param.block_size  = 16;                   //Select block size for 16 bytes.
  param.block_count = 1;                   //Select 1 blocks.
  param.cb          = pstorage_cb_handler;   //Set the pstorage callback handler

  err_code = pstorage_register(&param, &handle);
  APP_ERROR_CHECK(err_code);

  //get handle to access.
  err_code = pstorage_block_identifier_get(&handle, 0, &block_0_handle);
  APP_ERROR_CHECK(err_code);

  SEGGER_RTT_printf(0, "Clear Wait %d\r\n", pstorage_wait_flag);
  err_code = pstorage_clear(&block_0_handle, 16);
  APP_ERROR_CHECK(err_code);
  pstorage_wait_handle = block_0_handle.block_id;            //Specify which pstorage handle to wait for
  pstorage_wait_flag = 1;                                    //Set the wait flag. Cleared in the example_cb_handler
 
  while(pstorage_wait_flag == 1); 
  /*
  {
    SEGGER_RTT_printf(0, "while %d\r\n", pstorage_wait_flag); // problem occurs.
  };
  */
  SEGGER_RTT_printf(0, "Store Wait %d\r\n", pstorage_wait_flag);
  err_code = pstorage_store(&block_0_handle, source_data_0, 16, 0);
  APP_ERROR_CHECK(err_code);
  pstorage_wait_handle = block_0_handle.block_id;            //Specify which pstorage handle to wait for
  pstorage_wait_flag = 1;                                    //Set the wait flag. Cleared in the example_cb_handler

  while(pstorage_wait_flag == 1);
  /*
  {
    SEGGER_RTT_printf(0, "while %d\r\n", pstorage_wait_flag); // problem occurs.
  };
  */
  err_code = pstorage_load(dest_data_0, &block_0_handle, 16, 0);
  APP_ERROR_CHECK(err_code);

  for(;;){
  }

----fix.

I found the second problem was caused by compiler optimization.

I remove the optimization option O3 and it works.

It seems like compiler removed the loop condition..

But still unsure what's wrong with the first problem.

Related