Hi,
I would like to know what is wrong with my code, which is presened below. Each time the timer is triggered I would like that the program writes four bytes into the flash. As you can see I used the pstorage methods for writing into the flash. When the timer is triggered the code is executed up to the while(pstorage_wait_flag) statement. After that the program is blocked and it is not possible to connect on nrf51 chip any more. At the same time, the timer is immediately stopped. It should be noted that the obtained result is the same with or wihout power_manage() function in the while loop. In the case of pstorage_test_store_and_update(void) function which is called in main function it works fine and code goes through while(pstorage_wait_flag) statement without problem. Based on this fact I assume that the problem lie in the fact that the pstorage methods is called in interrupt routine. On the other hand, I came across the following thread devzone.nordicsemi.com/.../ where this can be possible to make. If I am honest I am a little bit confused and I will appricate for any advice. The code written in timer:
static void experimental_timer_handler(void *p_context)
{
uint8_t err_code;
uint8_t dest_data_0[4];
uint32_t err_codee;
uint8_t source_data[4] = {0x41, 0x00, 0x00, 0x00};
pstorage_wait_handle = block_0_handle.block_id; //Specify which pstorage handle to wait for
pstorage_wait_flag = 1;
err_codee=pstorage_update(&block_0_handle, source_data,4,0); //Write to flash, only one block is allowed for each pstorage_store command
if(err_codee != NRF_SUCCESS)
{
}
while(pstorage_wait_flag)
{
power_manage();
}
pstorage_load(dest_data_0, &block_0_handle, 4, 0);
printf("value %d\n", dest_data_0[0]);
}
and initial function for flash which is called in the main function:
static void pstorage_test_store_and_update(void)
{
pstorage_handle_t handle;
pstorage_module_param_t param;
uint8_t source_data_0[4] = {0x05, 0x00, 0x00, 0x00};
uint32_t retval;
uint8_t err_code;
param.block_size = HISTORY_MASS_MAX_BUFFERED + 4; // HISTORY_MASS_MAX_BUFFERED = 200
param.block_count = 1;
param.cb = example_cb_handler;
retval = pstorage_register(¶m, &handle);
if (retval != NRF_SUCCESS)
{
}
//Get block identifiers
pstorage_block_identifier_get(&handle, 0, &block_0_handle);
#ifdef FLAG_FLASH
pstorage_wait_handle = block_0_handle.block_id; //Specify which pstorage handle to wait for
pstorage_wait_flag = 1;
retval = pstorage_clear(&block_0_handle, HISTORY_MASS_MAX_BUFFERED + 4);
if(retval != NRF_SUCCESS)
{
}
while(pstorage_wait_flag)
{
power_manage();
}
pstorage_wait_handle = block_0_handle.block_id;
pstorage_wait_flag = 1;
pstorage_update(&block_0_handle, source_data_0, 4, 0);
if(retval != NRF_SUCCESS)
{
}
while(pstorage_wait_flag)
{
power_manage();
}
#endif
}
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; } //If we are waiting for this callback, clear the wait flag.
}
Thanks for help in advance.
Samo