2015-09-06 02:09 AM
Hi everyone.
Few days ago I realized that my NVIC priorities don't work. I'm tring to use three different interrupts by timer, exti and SysTick. I guess when EXTI or SysTick have highest priority then TIM with while loop inside, TIM shouldn't block whole program but it does (I guess).void InitializeTimer(){
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
TIM_TimeBaseInitTypeDef timerInitStructure;
timerInitStructure.TIM_Prescaler = 40000;
timerInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
timerInitStructure.TIM_Period = 1000;
timerInitStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM2, &timerInitStructure);
TIM_Cmd(TIM2, ENABLE);
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
}
void EnableTimerInterrupt(){
NVIC_InitTypeDef nvicStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
nvicStructure.NVIC_IRQChannel = TIM2_IRQn;
nvicStructure.NVIC_IRQChannelPreemptionPriority = 2;
nvicStructure.NVIC_IRQChannelSubPriority = 2;
nvicStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&nvicStructure);
}
void EnableExternalInterrupt(){
NVIC_InitTypeDef nvicStructure1;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
nvicStructure1.NVIC_IRQChannel = EXTI0_IRQn;
nvicStructure1.NVIC_IRQChannelPreemptionPriority = 0;
nvicStructure1.NVIC_IRQChannelSubPriority = 0;
nvicStructure1.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&nvicStructure1);
EXTI_InitTypeDef extiStructure;
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource0);
extiStructure.EXTI_Line = EXTI_Line0;
extiStructure.EXTI_Mode = EXTI_Mode_Interrupt;
extiStructure.EXTI_Trigger = EXTI_Trigger_Falling;
extiStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&extiStructure);
}
void EXTI0_IRQHandler(){
if(EXTI_GetITStatus(EXTI_Line0) != RESET)
{
if(GPIO_ReadOutputDataBit(GPIOE, LED4)== 0) GPIO_SetBits(GPIOE,LED4);
else GPIO_ResetBits(GPIOE, LED4);
EXTI_ClearITPendingBit(EXTI_Line0);
}
}
void TIM2_IRQHandler(){
if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
{
if(GPIO_ReadOutputDataBit(GPIOE, LED7)== 0) GPIO_SetBits(GPIOE,LED7);
else GPIO_ResetBits(GPIOE, LED7);
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
}
int i=0;
while(1);
}
void SysTick_Handler(void){
if(GPIO_ReadOutputDataBit(GPIOE, LED10)== 0) GPIO_SetBits(GPIOE,LED10);
else GPIO_ResetBits(GPIOE, LED10);
}
void main(void){
InitGPIOA();
InitLED3();
InitLED10();
InitializeTimer();
EnableTimerInterrupt();
EnableExternalInterrupt();
NVIC_SetPriority(TIM2_IRQn,2);
NVIC_SetPriority(EXTI0_IRQn,1);
NVIC_SetPriority(SysTick_IRQn,0);
if (SysTick_Config(160000)) while (1); //Maksymalnie 16 000 000, ze wzgledu na 24-bit timer
while(1);
}
Thanks in advance for your help and have a nice day.
#while #loop #nvic-tim #exti
2015-09-06 05:27 AM
NVIC_PriorityGroupConfig() has global effect, you configure it ONCE, not on a per interrupt basis. You'd want Group 2 if you expected 2-bits for preemption. I'd perhaps have ordered the code differently, getting the SysTick configured earlier, as I seem to recall it having some priority issue, it's a ''System Handler'' not an ''NVIC IRQ'' in that sense.
2015-09-07 11:28 AM
It works. SysTick should be configured before NVIC, and EXTI as well. Thank You so much. Again.