Timer configuration (Interrupt service routine)

Firmware/software/electronics/mechanics
Post Reply
joyner
Beginner
Posts: 8
Joined: Mon Nov 18, 2019 12:37 pm

Timer configuration (Interrupt service routine)

Post by joyner » Tue Jan 14, 2020 3:09 pm

hello,

I need to configure a timer in order to get an interrupt every 1ms. So first I need to know which timer I can use. But I also don't know at which point I should configure the timer and where I can define the TIM2_IRQHandler(void). Does anybody know where I can find information about timer used in crazyflie firmware and how to configure them?

I tried to configure TIM2 in the AppLayer even though I don't know if TIM2 is used by the firmware already.
But after NVIC is enabled "NVIC_EnableIRQ(TIM2_IRQn);" the crazyflie reboots and repeats the starting sequence.

Code: Select all

RCC->APB1ENR |=1;               // Enable Timer 2 Clock
TIM2->PSC=15999;                // PSC=15999 
TIM2->ARR=1000; 		// --> interrupt every 1s
TIM2->CR1=0x4;                  // URS=1 (Update Request Source)
TIM2->DIER |=1;                 // UIE=1 (Update Interrupt Enable)
NVIC_ClearPendingIRQ(TIM2_IRQn);
NVIC_SetPriority(TIM2_IRQn, 10); 
NVIC_EnableIRQ(TIM2_IRQn);		
TIM2->CR1 |=0;              


void TIM2_IRQHandler(void)
	{
	    // Clear Update Interrupt Flag UIF
	    TIM2->SR &= ~(1<<0);
		for (i = 0; i < 100000; i++) {
			motorsSetRatio(MOTOR_M2, 2500);	}
	}   
If anybody has some information about timer configuration I would be very glad to get some information. Thanks!



arnaud
Bitcraze
Posts: 2275
Joined: Tue Feb 06, 2007 12:36 pm

Re: Timer configuration (Interrupt service routine)

Post by arnaud » Wed Jan 15, 2020 9:09 am

Do you actually need a hardware timer?

If not, you can run something at 1 KHz in two ways using the RTOS:
1. Setup a FreeRTOS timer at 1KHz. Timers are running at quite low priority and you are not allowed to keep your function running for long (same as with an interrupt ...), so you might get some jitter. Though it is a simple way to have a function called at regular interval.
2. Create a task with an infinite loop and sleep 1 ms in it. If you set your task priority high, you will not get much jitter.

If you really need to use a timer, I found quickly that TIM6 is not used anywhere in the firmware. The way I checked is just to search TIM6 in the project in github, I found that the only place TIM6 is written is in src/lib/.... and nowhere else, this means that the firmware is not using it.

joyner
Beginner
Posts: 8
Joined: Mon Nov 18, 2019 12:37 pm

Re: Timer configuration (Interrupt service routine)

Post by joyner » Wed Jan 15, 2020 2:41 pm

Thanks Arnaud, maybe I will try to configure TIM6 then. And if it's not working, your Idea with using sleep seems sufficient as well. Thank you! Didn't know that the Microcontroller is that fast ...

Post Reply