timer error

I set it to 1 second here, why does it get stuck every time it reaches one second when I set the timer.

#include "bsp_timer.h"
#include "bsp_debug_log.h"

#include "nrfx_timer.h"

void bsp_timer_callback(nrf_timer_event_t event_type, void * p_context)
{
    if(event_type == NRF_TIMER_EVENT_COMPARE0)
    {
        char * p_msg = p_context;
        BSP_DEBUG_LOG("Timer finished. Context passed to the handler: >%s<\n", p_msg);
    }
}


char bsp_timer_init()
{

    #if defined(__ZEPHYR__)
    IRQ_CONNECT(NRFX_IRQ_NUMBER_GET(NRF_TIMER_INST_GET(10)), IRQ_PRIO_LOWEST,
                NRFX_TIMER_INST_HANDLER_GET(10), 0, 0);
    #endif

    const nrfx_timer_t timer_instance =  NRFX_TIMER_INSTANCE(10);
    nrfx_timer_config_t timer_config = NRFX_TIMER_DEFAULT_CONFIG(1000000);
    timer_config.bit_width = NRF_TIMER_BIT_WIDTH_16;
    timer_config.p_context = "Some context";

    nrfx_err_t err;
    err = nrfx_timer_init(&timer_instance, &timer_config, bsp_timer_callback);

    BSP_ASSERT_LOG_RET(err, "bsp_timer_init");

    uint32_t desired_ticks = nrfx_timer_ms_to_ticks(&timer_instance, 1000);
    // NRFX_LOG_INFO("Time to wait: %lu ms", 1000);
    nrfx_timer_extended_compare(&timer_instance, NRF_TIMER_CC_CHANNEL0, desired_ticks,
                                 NRF_TIMER_SHORT_COMPARE0_STOP_MASK, true);
    nrfx_timer_clear(&timer_instance);


    nrfx_timer_enable(&timer_instance);

    return BSP_ERROR_CODE;
}

Parents
  • Hi,

     

    You need to change the resolution:

    timer_config.bit_width = NRF_TIMER_BIT_WIDTH_32;

     

    Note that you have also set the compare0_stop_mask. If you want a repeated event, change this to NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK.

     

    Also, if you want 1 second timer, you could use the zephyr timer API instead, which is lower power:

    https://docs.zephyrproject.org/latest/kernel/services/timing/timers.html#using-a-timer-expiry-function

      

    Kind regards,

    Håkon

  • Thank you for your answer. I'm having trouble understanding many of the function parameters. Why set them to 32 bits? I looked in the manual and it seems I can customize the timer resolution.

    Is it because Zephyr implements software timers, while I use Nordic, which increases power consumption when using hardware timers?

    Yes. If I use software timers, will some tasks not execute in time? Furthermore, the Zephyr RTOS is quite large.

  • Hi,

     

    LinJunXuan said:
    Thank you for your answer. I'm having trouble understanding many of the function parameters. Why set them to 32 bits? I looked in the manual and it seems I can customize the timer resolution.

    You are setting a resolution of 1 MHz, with a bit width of 16, meaning that you can select a timeout between 1 us and (2^16)-1 us.

    Expand this to either 24 or 32 bits, and you will be able to select 1 second.

    LinJunXuan said:

    Is it because Zephyr implements software timers, while I use Nordic, which increases power consumption when using hardware timers?

    kernel timer uses GRTC, and each subsequent k_timer handler will execute in ISR context. Yes, these will be a small overhead in terms of execution time within the zephyr kernel, but the timing shall be similar to the tolerance of your chosen LFCLK source.

     

    Please also note that you're selecting TIMER10, which is the only timer in the radio power domain:

    https://docs.nordicsemi.com/bundle/ps_nrf54L15/page/overview.html#ariaid-title2

     

    You usually want to use this for the radio (softdevice).

     

    Kind regards,

    Håkon

  • The only timer, then, does wireless refer to 2.4G and BLE, NFC, RADIO, etc.?

    I think I understand what timers mean now. It turns out that resolution refers to the timer's timing duration. I always thought it was timer10's timing frequency, 32MHz, divided by my 1M, and then 1000 was the number of calculations, so 1/32 * 1000 was my timing duration. That's how I understood it before.

    Now it's directly 1 / resolution * count value. Is that correct? Assuming the timing is one second and the resolution is 1M, the calculation value also needs to be 1M. Is that 1 / 1M * 1M, correct?

Reply
  • The only timer, then, does wireless refer to 2.4G and BLE, NFC, RADIO, etc.?

    I think I understand what timers mean now. It turns out that resolution refers to the timer's timing duration. I always thought it was timer10's timing frequency, 32MHz, divided by my 1M, and then 1000 was the number of calculations, so 1/32 * 1000 was my timing duration. That's how I understood it before.

    Now it's directly 1 / resolution * count value. Is that correct? Assuming the timing is one second and the resolution is 1M, the calculation value also needs to be 1M. Is that 1 / 1M * 1M, correct?

Children
Related