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

Write to Flash during App timer_handler

I am using SDK15.0 and added the functionality of nrf_storage into the ble_app_uart in order to store the results of scan (which are kept during run time in an array in RAM).

I want to write the array to flash every 24 hours.

The command nrf_fstorage_write is working OK if it is in the main or in the uart_event_handle

But it doesn't work (Error) if it is inside the App timer handler (which is counting for 24h)

I cannot put a flag in the timer_handler and poll the flag in the main (as it was suggested before) since the polling is power consuming.

How can I do it?

  • Have you considered using the app scheduler library? You would “schedule” a call to nrf_fstorage_write in your app timer handler and it would be executed in main. 

  • I must keep my solution low power.

    Will scheduler running at main enable low power (ie sleep current between advertisement) till 24h timer reached?

    How does it work correctly if it is inside the uart_event?

    Will I be able to operate the flash storage inside the gpiote event (assuming I will use external low power RTC)?

    One more option:

    Is it possible to use the array in the RAM after soft reset  (nvic_systemreset()) and put the flash storage as part of the initialization? i.e. every 24 hours, the chip will reset, and store into the flash the content of the RAM array. In the RAM I can use a counter that will be used as a counter to write to different portion of the flash each day

    Will it work?

  • Hello,

     

    Avi said:
    Is it possible to use the array in the RAM after soft reset  (nvic_systemreset()) and put the flash storage as part of the initialization? i.e. every 24 hours, the chip will reset, and store into the flash the content of the RAM array.

     I doubt that a reset every 24 hours will cost less power consumption than polling a flag before sd_app_evt_wait(), or the similar sleep function that you use in your main loop.

    As I see it, you have 3 options:

    1: Use a scheduler, as  suggested.

    2: Set a flag in the timer interrupt, and poll it in your main loop.

    3: Use PPI to link the timer interrupt with a custom event handler (using the EGU), and write to flash in this interrupt.

    General question: 

    But it doesn't work (Error) if it is inside the App timer handler (which is counting for 24h)

     What sort of error do you encounter? Is it one of the function calls that return an err_code != 0, or are you missing a callback, or what error is it?

    Out of curiosity, what timer do you use? App_timer? RTC? TIMER? And what interrupt priority does your timer have?

    Best regards,

    Edvin

  • Hi,

    I will probably use the flag option, I check the current increment and it goes up to 20-30uA instead of 1uA (sleep). 

    Regarding the timer, I used App_timer and for testing I set it to 3 min long. I don't know which error it is, the code is stuck during the printf statement.

    The priority of the App_TImer is 7. I tried to raise it to 3 and it didn't help.

    Avi 

  • Hi,

     

    Avi said:
    I will probably use the flag option, I check the current increment and it goes up to 20-30uA instead of 1uA (sleep). 

     Is the only difference the flag poll between 20-30µA and 1µA? Do you run the app_timer in the 1µA scenario?

    You can leave the app_timer priority at 7. What I wanted to check was whether the issue was that you called the fstorage calls from a higher priority than what fstorage was running on, blocking the execution.

    Can you please specify what sort of error you encountered when you called the fstorage directly from the app_timer?

    BR,

    Edvin

Related