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

How to use 16Mhz NRF_TIMER1 with softdevice? Source Code to review

Sorry for the silly question, but as it is mandatory to use sd_X function when it is possible to avoid collision, could you tell me if this code is the right way to use the 16MHz Timer 1?

Best regards

/** @brief Function for handling the Timer 1 interrupt.
 */
void TIMER1_IRQHandler(void)
{
    NRF_TIMER1->CC[0] += TIMER_INTERVAL;
  
    // Clear interrupt.
    if ((NRF_TIMER1->EVENTS_COMPARE[0] == 1) && 
        (NRF_TIMER1->INTENSET & TIMER_INTENSET_COMPARE0_Msk))
    {
        NRF_TIMER1->EVENTS_COMPARE[0] = 0;
    }
    
    if(toggle)
      kernel_led_test_on();
    else
      kernel_led_test_off();      
    
    toggle = !toggle;
}

static void timer1_init(void)
{
    uint32_t p_is_running;
    uint32_t err_code;
  
    // Configure timer
    NRF_TIMER1->MODE      = TIMER_MODE_MODE_Timer;
    NRF_TIMER1->BITMODE   = TIMER_BITMODE_BITMODE_16Bit;
    NRF_TIMER1->PRESCALER = 9;

    // Clear the timer
    NRF_TIMER1->TASKS_CLEAR = 1;

    NRF_TIMER1->CC[0] = TIMER_INTERVAL;

    NRF_TIMER1->INTENSET = TIMER_INTENSET_COMPARE0_Enabled << TIMER_INTENSET_COMPARE0_Pos;
    NRF_TIMER1->TASKS_START = 1;

    err_code = sd_nvic_SetPriority(TIMER1_IRQn,APP_IRQ_PRIORITY_LOW);
    APP_ERROR_CHECK(err_code);	
    err_code = sd_nvic_ClearPendingIRQ(TIMER1_IRQn);
    APP_ERROR_CHECK(err_code);	
    err_code = sd_nvic_EnableIRQ(TIMER1_IRQn);  
    APP_ERROR_CHECK(err_code);	
}
Parents
  • This seems to be ok, and I can't see any obvious problems with it, except that you may get some jitter in the CC events, due to the possibility of other interrupts interrupting the read-modify-write on the CC register.

    If you need a constant tick-like interrupt, I'd rather recommend you to use a short between the COMPARE event and the CLEAR task. If you do so, the interrupt will occur with a steady frequency, although the handler may have some jitter do to other interrupts, but this is simply unavoidable when being in a BLE connection.

    Is there a particular reason you're asking? Does it seem to behave unexpectedly in any way?

  • Thank you very much for your reply.

    As it is mandatory to use all softdevice functions like sd_clock_hfclk_request, I was not sure about the direct calls to the timer with NRF_TIMER1.

    Also as softdevice use NRF_TIMER0, can I stop the 16MHz using sd_clock_hfclk_release and start it when I need without disturbing the softdevice?

Reply Children
No Data
Related