cancel
Showing results for 
Search instead for 
Did you mean: 

Timer interrupt Handler

Rahimi.Meysam
Associate II
Posted on April 17, 2015 at 17:51

Hello.

I have Using One of the example Program that port UCOS on STM32F4 Devices.

I have One problem. When I activate Timer in one the tasks, The Timer is Working in a right way(example is on the blinking LED). but i want to add some code in its interrupt service routin. i cant find where todo that?

here is the code that activate timer 2 and its interrupt service routine(that i used).

static void timer_init(void)

{

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

  TIM_TimeBaseInitTypeDef timerInitStructure;

  NVIC_InitTypeDef NVIC_InitStructure;

  /* Enable the TIM2 gloabal Interrupt */

  

  NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;

  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 5;

  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 10;

  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

  NVIC_Init(&NVIC_InitStructure);

  

  

  timerInitStructure.TIM_Prescaler = 40000;

  timerInitStructure.TIM_CounterMode = TIM_CounterMode_Up;

  timerInitStructure.TIM_Period = 5000;

  timerInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;

  timerInitStructure.TIM_RepetitionCounter = 0;

  TIM_TimeBaseInit(TIM2, &timerInitStructure);

  TIM_Cmd(TIM2, ENABLE);

}

Thank you for your answers..

#timer-interupt-handler #ucos-ii
6 REPLIES 6
hbarta2
Associate III
Posted on April 17, 2015 at 20:56

I can think of a couple ways, none of which may be correct.

Does the project have an stm32[something]_it.c file? In it you will find copies of some interrupt handlers. In the .s file you will find the actual vectors so once your interrupt is firing, you need only provide a function similar to what is in the ..._it.c and matches the vector declared in the .s file.

I've been using the examples heavily and have not added any ISRs so hopefully this suggestion is correct.

Another alternative would be to find an example that uses a timer ISR and see how it is set up and used.

HTH,

hank

Rahimi.Meysam
Associate II
Posted on April 17, 2015 at 21:04

Hi HankB and thank you for your suggestions.

The First Way. No . the project does not have that file.

and second way i have searched for examples but i was unable to find it. I will be appreciate if you found something like that inform me.

again thank you for your answer.

Chris1
Senior III
Posted on April 17, 2015 at 22:10

Typically, you will have a startup file that has the vector table with names for the interrupt handler functions, for example: TIM2_IRQHandler.

You can then define a function of your own with that name, for example:

void TIM2_IRQHandler(void)

{

    if (TIM2->SR & TIM_IT_CC2) // rising edge capture

    {

        TIM2->SR &= (uint16_t)~TIM_IT_CC2;  // clear CC interrupt pending bit

 

        TIM2_InputCapture1 = TIM2->CCR1;    // save captured values

        TIM2_InputCapture2 = TIM2->CCR2;

    }

    else // unexpected interrupt !?

    {

    }

}

Note that you will probably want to clear the interrupt pending bits that you are using.

Chris1
Senior III
Posted on April 17, 2015 at 22:32

Example code may be found in STM32F4 DSP and standard peripherals library, among other places:

http://www.st.com/web/en/catalog/tools/PF257901

For example in:

STM32F4xx_DSP_StdPeriph_Lib\Project\STM32F4xx_StdPeriph_Examples\TIM\TIM_TimeBase\

STM32F4xx_DSP_StdPeriph_Lib\Project\STM32F4xx_StdPeriph_Examples\TIM\TIM_InputCapture\

Rahimi.Meysam
Associate II
Posted on April 18, 2015 at 14:35

Hi Chris T.

Your suggestion in the case that we do not use UCOS is working. but when using UCOS This functions is no longer valid(at least i think this is like that).

My problem is that i can not run this interrupt service routines in UCOS.

thank you for your answer.

Chris1
Senior III
Posted on April 20, 2015 at 15:27

I haven't used UCOS, but it may have restrictions on what interrupt levels can be used, especially if you call any UCOS functions in your interrupt handler.

To simplify things, I suggest configuring the NVIC to not use subpriority, for example, prior to your timer init function:

    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4); // use all 4 bits for pre-emption priority

                                                                                            //  (0 bits for subpriority)

    

Then in your timer init function TIM2 channel assignment:

   

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 12;    //   

    NVIC_InitStructure.NVIC_IRQChannelSubPriority               =  0;     // subpriority not used

Higher IRQChannelPreemptionPriority values have lower actual priority.