Beware that this post is related to an SDK in maintenance mode
More Info: Consider nRF Connect SDK for new designs
This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

BLE Peirpheral UART with Timer Problem

Hello,

        I'm using the "ble_app_uart" example without a problem. I need to add a timer to this example. For this purpose, I copied the content of the "peripheral_timer" example into "ble_app_uart" example. I configure the "sdk_config.h" content accordingly.

       The code is crushing in the following line of the "nrfx_timer_init" function.

     NRFX_ASSERT(p_instance->p_reg != NRF_TIMER0);

int main(void)
{
    bool erase_bonds;

    // Initialize.
    uart_init();
    log_init();
    timers_init();
    buttons_leds_init(&erase_bonds);
    power_management_init();
    ble_stack_init();
    gap_params_init();
    gatt_init();
    services_init();
    advertising_init();
    conn_params_init();
    
    // Start execution.
    printf("\r\nUART started.\r\n");
    NRF_LOG_INFO("Debug logging for UART over RTT started.");
    advertising_start();
    
    
    //Configure TIMER_LED for generating simple light effect - leds on board will invert his state one after the other.
    nrf_drv_timer_config_t timer_cfg = NRF_DRV_TIMER_DEFAULT_CONFIG;
    err_code = nrf_drv_timer_init(&TIMER_LED, &timer_cfg, timer_led_event_handler);
    APP_ERROR_CHECK(err_code);

    time_ticks = nrf_drv_timer_ms_to_ticks(&TIMER_LED, time_ms);

    nrf_drv_timer_extended_compare(
         &TIMER_LED, NRF_TIMER_CC_CHANNEL0, time_ticks, NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK, true);

    nrf_drv_timer_enable(&TIMER_LED);
    

   
    // Enter main loop.
    for (;;)
    {
        NRF_LOG_INFO("%d",i++);
        idle_state_handle();
    }
}

  • Hello,

    The reason for this is probably because you try to use TIMER0 for your TIMER_LED timer. The problem is that TIMER0 is also used by the SoftDevice, so you will get an assert, because the NRF_TIMER0 is already in use. 

    In the top of the part from your peripheral_timer example, change:

    const nrf_drv_timer_t TIMER_LED = NRF_DRV_TIMER_INSTANCE(0);

    to

    const nrf_drv_timer_t TIMER_LED = NRF_DRV_TIMER_INSTANCE(1);

     

    And also, you need to enable TIMER1 in your sdk_config.h file.

    If you open the sdk_config.h file, you will probably find that TIMER0_ENABLED is defined to 1. Try to also change

    #define TIMER1_ENABLED 0

    to

    #define TIMER1_ENABLED 1

     

    Then your project should run with the softdevice using TIMER0, and the application timer using TIMER1.

     

    Best regards,

    Edvin

  • By the way. The way that NRF_DRV_TIMER_INSTANCE() is implemented depends on what SDK you use, but it should be readable. You only need to change this to use TIMER1, and enable TIMER1 in sdk_config.h as I described in the previous post.

     

    Best regards,

    Edvin

Related