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

Timer 1 has no Different resolution

We're currently running into a lot of bugs using timer1(and 2) especially with the softdevice enabled. Every so often it glitches, and after further inspection of the code, we notice that regardless of NRF_TIMER1->BITMODE = x; the bitmode doesn't change.

we use the following code.

void TIMER1_IRQHandler(){
	NRF_TIMER1->EVENTS_COMPARE[0] = 0;
	NRF_TIMER1->CC[0] += 125; ///add an additional 125, so that it interrupts on the next 125 ticks	
	NRF_TIMER1->CC[0] = 75125;

	if (NRF_TIMER1->CC[0] > 0xffff)
	{
		while(1);
	}
	///it should also roll over after reaching the 4billion mark in 9 hours
	main_interrupt();
}

It will NEVER reach the infinite whileloop. and we initialize it like so

	NRF_TIMER1->POWER = 1;
	/// we want this function to be called every 1 ms
	NRF_TIMER1->MODE = TIMER_MODE_MODE_Timer << TIMER_MODE_MODE_Pos;
	///8 bit timer
	NRF_TIMER1->BITMODE = TIMER_BITMODE_BITMODE_08Bit << TIMER_BITMODE_BITMODE_Pos;
	///16Mhz / 2 ^ 7 = 125000,
	NRF_TIMER1->PRESCALER = 7;
	///so to generate a 1Khz interrupt, we set compare register to 125
	NRF_TIMER1->CC[0] = 125;
	NRF_TIMER1->INTENSET = TIMER_INTENSET_COMPARE0_Enabled << TIMER_INTENSET_COMPARE0_Pos;
	///set as the highest priority
	NVIC_SetPriority(TIMER1_IRQn, 0);
	NVIC_EnableIRQ(TIMER1_IRQn);
	nrf_delay_us(100);

If you notice that we even tried setting the bitmode to 8bit, and it still will count up to 16bit value before rolling over. We NEED a 32bit timer. Our current hardware is the nrf51822 QFAC A1

  • Well for a start TIMER1 and TIMER2 are 8 or 16 bit timers, they don't have a 24 or 32 bit mode. So I rather suspect

    NRF_TIMER1->CC[0]=75125 
    

    is actually setting it to 9589 (75125-65536) and so you're never going to get into your while loop.

    You can test that pretty easily by reading back CC[0] after you set it and see what it is.

    By the way, If you're using the softdevice then this

    NVIC_SetPriority(TIMER1_IRQn, 0);
    

    is using an interrupt priority you can't use. Not something causing your problem, but something which will cause you a problem in the softdevice eventually.

  • .. and a 20 second test with the debugger shows that indeed setting TIMER1->CC[0] to 75125 does indeed only set the lower 16 bits.

  • Thank you for your reply. When the Soft device is activated I use softdevice calls and set priority to 1. I would also like to know where this is documented? Looking at the Timer//Counter documentation in the reference manual It makes it seem vague and that each timer can be 8/24/32/16 bit timers.(as each timer has these resolution)

  • From the nRF51 manual

    "The TIMER's maximum value is configured by changing the bit-width of the timer in the BITMODE register. For details on which bitmodes are supporting which timers see the device product specification."
    

    And then from the nRF51822 product spec

    Instance        Bit-width        Capture/Compare registers
    TIMER0         8/16/24/32         4
    TIMER1         8/16                   4
    TIMER2         8/16                   4
    
  • Sorry this is posting on an old post and I'm aware its probably not applicable for you anymore, but for reference ...

    I have had similar issues with the documentation seeming vague with regards to this. As is kind of mentioned in RK's answer, the Reference Manual gives all the options for the device range, whereas the specifics of the exact chip you have are in the Product Specification document for your exact device.

    In my case for the nrf51822, the reference manual (nRF51_RM_v3.0.pdf) says all the stuff about the timers being 8/16/32 bit. However the Product Specification (nRF51822_PS_v3.1.pdf) then says TIMER1/2 are 8/16 bit only (section 4.2).

Related