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

nrf_drv_timer with TIMER2 and s310 on nRF5144

I wrote this code found in an example and it works fine:

#include "nrf_drv_config.h"
#include "nrf_drv_timer.h"

//Before I activated TIMER0, TIMER1 and TIMER2 in nrf_drv_config.h
const nrf_drv_timer_t TIMER_LED = NRF_DRV_TIMER_INSTANCE(0);

/**
* @brief Handler for timer events.
*/
void timer_led_event_handler(nrf_timer_event_t event_type, void* p_context)
{
    switch(event_type)
    {
        case NRF_TIMER_EVENT_COMPARE0:
            LED_redLed_toggle();
            break;        
        default:
            //Do nothing.
            break;
    }    
}

int main(void)
{
	LED_init();	

	uint32_t time_ms = 3000;  //Time(in miliseconds) between consecutive compare events.
    uint32_t time_ticks;
    uint32_t err_code = NRF_SUCCESS;
	
	err_code = nrf_drv_timer_init(&TIMER_LED, NULL, 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);

	//ANT_init();
	
	//BLE_init();
	
	// Main Loop
	while(1)
	{	
        PWR_sleep();
	}
}

When I active the softdevice, removing comments at these lines:

	//ANT_init();
	
	//BLE_init();

The code doesn't run. I found that TIMER0 is used by softdevice, so I tried to use TIMER1 or TIMER2 changing this line:

const nrf_drv_timer_t TIMER_LED = NRF_DRV_TIMER_INSTANCE(0);

into:

const nrf_drv_timer_t TIMER_LED = NRF_DRV_TIMER_INSTANCE(1);

and then also into:

const nrf_drv_timer_t TIMER_LED = NRF_DRV_TIMER_INSTANCE(2);

In both cases the interrupt will be continuous, and not after the setted time.

What does could be the issue?

I found on this forum a similar question, but I tried to reduce the timer frequency and I get that I can set the timer untill 2s, but over 2s the timer restart to interrupt continuously. The settings I modified are:

#define TIMER2_ENABLED 1

#if (TIMER2_ENABLED == 1)
#define TIMER2_CONFIG_FREQUENCY    NRF_TIMER_FREQ_31250Hz
#define TIMER2_CONFIG_MODE         TIMER_MODE_MODE_Timer
#define TIMER2_CONFIG_BIT_WIDTH    TIMER_BITMODE_BITMODE_32Bit
#define TIMER2_CONFIG_IRQ_PRIORITY APP_IRQ_PRIORITY_LOW

#define TIMER2_INSTANCE_INDEX      (TIMER1_ENABLED+TIMER0_ENABLED)

How can I use the timer to have interrupts every 5 minuts ?

It seems like the TIMER_BITMODE_BITMODE_32Bit doens't set correclty the register behaviour as a 32bit register, but remains at 16bit so, setting timer times bigger than 2seconds make the interrupts arriving continuously.

I need a timer which can interrupt every 5 minuts or more to wakup the application which I put in sleep with sd_app_wait(); (PWR_sleep(); in my code).

How can I reach this?

Thanks.

Related