hardware timer in nrf connect sdk

Hi, can anyone tell me how to configure hardware timers in nrf connect sdk. using nrfx_timer driver? not the kernel timers.

  • There are different ways to use the TIMER peripheral in the nRF Connect SDK:

    Using the Zephyr API

    Take a look at the counter Alarm Sample and set CONFIG_COUNTER_NRF_TIMER=y. It will make the counter driver use Timer peripheral through the NRF HAL

    Using the NRFX API

    Take a look at the Direct Test Mode sample, which uses the Timer peripheral through the nrfx api

    Best regards,

    Simon

  • I implemented this code in the blinky with the example you mentioned using NRFX API 

    const nrfx_timer_t timer_led = NRFX_TIMER_INSTANCE(0);
    
    static void wait_timer_handler(nrf_timer_event_t event_type, void *context)
    {
    	printk("timer expired");
    }
    
    static int timer_init(void)
    {
    	nrfx_err_t err;
    	nrfx_timer_config_t timer_cfg = {
    		.frequency = NRF_TIMER_FREQ_1MHz,
    		.mode = NRF_TIMER_MODE_TIMER,
    		.bit_width = NRF_TIMER_BIT_WIDTH_16,
    	};
    
    	err = nrfx_timer_init(&timer_led, &timer_cfg, wait_timer_handler);
    	if (err != NRFX_SUCCESS) {
    		printk("nrfx_timer_init failed with: %d\n", err);
    		return -EAGAIN;
    	}
    	
    	IRQ_CONNECT(TIMER1_IRQn, 3,
    		    nrfx_timer_1_irq_handler, NULL, 0);
    
    	nrfx_timer_compare(&timer_led,
    		NRF_TIMER_CC_CHANNEL0,
    		nrfx_timer_ms_to_ticks(&timer_led, 1000000),
    		true);
    
    	nrfx_timer_enable(&timer_led);
    	
    	return 0;
    }
    
    
    void main(void)
    {
    	int err;
    	
    	err = timer_init();
    	if (err) {
    		return;
    		printk("timer init failed\n");
    	}
    	
    	printk("timer initialized\n");
    	for(;;)
    	{
    		k_msleep(1000);
    	}
    }

    added all the config options too, as in dtm example and flashed it to the nrf52840 dk board. it just keeps resetting like this 

  • I would highly recommend you to use the Zephyr API (which actually uses the nrfx API under the hood), as it is much less complicated and easier to implement.

    Take a look at the Counter Alarm sample and set CONFIG_COUNTER_NRF_TIMER=y in the prj.conf.

    Please let me know if this sample is not sufficient for you.

    Best regards,

    Simon

  • Hi, 

    If I set CONFIG_COUNTER_NRF_TIMER=y in prj.conf of this sample it gives following error

    error: COUNTER_NRF_TIMER (defined at drivers/counter/Kconfig.nrfx:4) is assigned in a configuration
    file, but is not directly user-configurable (has no prompt). It gets its value indirectly from other
    symbols.

  • Hello,

    Can you try to set CONFIG_COUNTER_TIMERx=y (where x is 1,2,3 or 4) instead. If you look in /ncs/zephyr/drivers/counter/Kconfig.nrfx you can see that these Kconfig options will enable COUNTER_NRF_TIMER.

    Best regards,

    Simon

Related