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

nRF51 pstorage during ble_radio_active_evt

I've been banging my head against this for a couple days now, and need some help. First the layout.

In our setup we have an accelerometer with a movement interrupt pin (high when movement detected) tied to a sense high input, and a stationary interrupt pin (high when still) tied to another input on an nRF51822.

[wake up code] When the movement pin goes high the nRF wakes up, initializes pstorage, and loads a counter value into RAM. It then sets up advertising setting a radio notification callback, begins advertising, and goes into power management with sd_app_evt_wait().

[advertise check code] When the radio wakes up to advertise, the radio notification callback is called which increments the RAM counter and checks the stationary signal from the accelerometer. If it gets the signal that the accelerometer is not moving I try to save the RAM counter back to flash using pstorage before entering deep sleep with sd_power_system_off().

I'm using a callback to clear a flag and verify clear, load, and store pstorage operations complete before advancing, and the logic works great if I call it from the [wake up] code; however when the same logic is called from the [advertise check code] the callback handler is never called and it just loops forever.

static void save_counter() {
    uint32_t err_code;

    pstorage_wait_flag = 1;
    err_code = pstorage_clear(&persistent_storage_handle, 4);
    APP_ERROR_CHECK(err_code);  // NRF_SUCCESS
    while(pstorage_wait_flag) {
        // commented out sd_app_evt_wait just in case it was part of the problem
        // sd_app_evt_wait();  // never exits this block, pstorage cb never fires
    };

    pstorage_wait_flag = 1;
    err_code = pstorage_block_identifier_get(&persistent_storage_handle, 0, &persistent_counter);
    APP_ERROR_CHECK(err_code);
    err_code = pstorage_store(&persistent_counter, (uint8_t*)&ram_counter, 4, 0);
    APP_ERROR_CHECK(err_code);
    while(pstorage_wait_flag) {
        // sd_app_evt_wait()
    };
}

It seems like the sys_evt_dispatch function where the pstorage_sys_event_handler is called from is no longer registered, or some clock isn't running. Any help would be greatly appreciated.

Thanks!

  • Hi Jeremy,

    Stay in a radio notification call back to wait for the pstorage call back may be not a very a good idea. Radio notification and flash notification share the same interrupt.So flash notification won't be able to trigger when you are still in the radio notification call back. Same applied for any same priority level interrupts.

    What you should do is to set a flag and then wait for the operation to complete in a sleep in the main loop, or use app scheduler.

  • Thanks! Knowing what was going on it was a lot easier to solve. Moved the sd_power_system_off() call to the pstorage callback to fire when an PSTORAGE_UPDATE_OP_CODE was detected, and just returned from the radio notification interrupt after starting the update, and everything seems to be working great.

Related