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

Timer 1 and BLE (softdevice) interfering

Hi,

To decrease the load on the system I would like to periodically send some BT data (and do some SD card writing).

Since Timer 0 is used by the soft device, I am using Timer 1 as follows:

const nrf_drv_timer_t timer = NRF_DRV_TIMER_INSTANCE(1);

static void initBLE() {
    uint32_t error;
    nrf_clock_lf_cfg_t clock_lf_cfg = {
        .source = NRF_CLOCK_LF_SRC_RC,
        .rc_ctiv = 4,
        .rc_temp_ctiv = 1
    };

    SOFTDEVICE_HANDLER_INIT(&clock_lf_cfg, NULL);

    ble_enable_params_t ble_enable_params;
    error = softdevice_enable_get_default_config(CENTRAL_LINK_COUNT, PERIPHERAL_LINK_COUNT, &ble_enable_params);
    _error("softdevice_enable_get_default_config", error);
    
    #if (NRF_SD_BLE_API_VERSION == 3)
    ble_enable_params.gatt_enable_params.att_mtu = NRF_BLE_MAX_MTU_SIZE;
    #endif
    error = softdevice_enable(&ble_enable_params);
    _error("softdevice_enable", error);

    error = softdevice_ble_evt_handler_set(onBLE);
    _error("softdevice_ble_evt_handler_set", error);
}

void onTimer(nrf_timer_event_t event_type, void *p_context) {
    ...
}

int main(void) {
    ...
    nrf_drv_timer_config_t timer_cfg = NRF_DRV_TIMER_DEFAULT_CONFIG;
    timer_cfg.frequency = NRF_TIMER_FREQ_31250Hz;
    timer_cfg.bit_width = NRF_TIMER_BIT_WIDTH_16;
    error = nrf_drv_timer_init(&timer, &timer_cfg, onTimer);
    uint32_t ticks = nrf_drv_timer_ms_to_ticks(&timer, 100);
    nrf_drv_timer_extended_compare(&timer, NRF_TIMER_CC_CHANNEL0, ticks, NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK, true);
    nrf_drv_timer_enable(&timer);
    _error("nrf_drv_timer_init", error);
    ...
    initBLE();
    ...
}

There is no specific error thrown, but the following things happen:

  1. (Usually) the timer is executed perfectly fine at an interval of 100 ms for a short period (~1 - 3 minutes)
  2. After some time onTimer gets executed very often (+10 x per second) and suddenly softdevice functions which get called all the time are returning errors

This of course interferes with the rest of my code and some other things may stop working properly as well (SPI, UART), but I haven't noticed.

When I disable the timer (comment out "nrf_drv_timer_enable"), all problems described are gone.

What could be the problem?

Oh, and I'm not using a 32.768 kHz crystal, but am using the 16 Mhz derived clock.

  • FormerMember
    0 FormerMember

    When TIMER1 starts to behave strange, could you use nrf_drv_timer_capture() to capture the timer value?

  • I just saw I didn't close this ticket, although the issue has been resolved. However, I'm not sure what the exact cause was, I believe it was because I did "very" time consuming actions, such as writing/read to and from an SD card. This wouldn't be completed before the next timer callback, leading to strange behavior.

    I solved it by only setting flags in the onTimer callback, such as "_action_write = true". In the main() loop I checked whether this flag was set, performed the action there, and cleared the flag. Now I'm also sure I'm not trying to write to the same file at the same time (which would happen if the onTimer callbacks overlapped in duration time).

Related