1.中断简介:
2.中断优先级分组:
注意,中断优先级数值越低,其优先级越高。而在任务优先级数值越高,其优先级越大。
3.中断相关寄存器介绍:
4.系统中断优先级设置:
FreeRTOS管理中断的两个函数:portDISABLE_INTERRUPTS()、portENABLE_INTERRUPTS()。
在设置FreeRTOS的中断时需要注意宏定义configMAX_SYSCALL_INTERRUPT_PRIORITY:
宏定义configMAX_SYSCALL_INTERRUPT_PRIORITY表明FreeRTOS能够管理的最高中断优先级,所有中断优先级数值小于等于configMAX_SYSCALL_INTERRUPT_PRIORITY的中断,都无法被FreeRTOS管理。
将191转为二进制便是1011 1111,又因为Cortex-M只使用高四位来配置中断优先级,则191实际有效位为1011,即为0Xb0。所以在本文中,所有中断优先级数值小于等于11的中断都不受FreeRTOS的管理。
portDISABLE_INTERRUPTS()关中断。
portENABLE_INTERRUPTS()开中断。
4.代码:本文只展示定时器中断相关和main的代码,完整代码可将本文内容和前面的动态创建任务相结合。在定时器中断服务函数来打印输出,并通过任务开关中断。
(1)中断相关代码:
#include "time.h"
#include "usart.h"
void TIME_Init(u16 arr,u16 psc)
{
//¶¨Ò嶨ʱÆ÷ºÍÖжϽṹÌ壺
TIM_TimeBaseInitTypeDef TIM_TimBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
//1.ʹÄܶ¨Ê±Æ÷ʱÖÓ;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3 | RCC_APB1Periph_TIM4,ENABLE);
//2.³õʼ»¯time3£º
TIM_TimBaseStructure.TIM_Period = arr; //ÉèÖÃ×Ô¶¯ÖØ×°ÔؼĴæÆ÷µÄÖµ
TIM_TimBaseStructure.TIM_Prescaler = psc;
TIM_TimBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimBaseStructure.TIM_CounterMode = TIM_CounterMode_Down;
TIM_TimeBaseInit(TIM3,&TIM_TimBaseStructure);
//2.³õʼ»¯time4£º
TIM_TimBaseStructure.TIM_Period = arr; //ÉèÖÃ×Ô¶¯ÖØ×°ÔؼĴæÆ÷µÄÖµ
TIM_TimBaseStructure.TIM_Prescaler = psc;
TIM_TimBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimBaseStructure.TIM_CounterMode = TIM_CounterMode_Down;
TIM_TimeBaseInit(TIM4,&TIM_TimBaseStructure);
TIM_ITConfig(TIM3 ,TIM_IT_Update,ENABLE); //ÔÊÐí¸üÐÂÖжÏ
TIM_ITConfig(TIM4 ,TIM_IT_Update,ENABLE); //ÔÊÐí¸üÐÂÖжÏ
//3.TIME3ÖжÏÅäÖãº
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 12;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_Init(&NVIC_InitStructure);
//4.TIME4ÖжÏÅäÖãº
NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 4;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_Init(&NVIC_InitStructure);
//4.ʹÄܶ¨Ê±Æ÷£º
TIM_Cmd(TIM3,ENABLE);
TIM_Cmd(TIM4,ENABLE);
}
//ÖØдtime3µÄÖжϷþÎñº¯Êý£º
void TIM3_IRQHandler(void)
{
//¼ì²éÊÇ·ñ·¢ÉúÖжϣº
if(TIM_GetITStatus(TIM3,TIM_IT_Update) != RESET)
{
//Çå³ýtimeµÄ¸üÐÂÖжϱê־λ
TIM_ClearITPendingBit(TIM3,TIM_IT_Update);
printf("TIM3ÖжÏÓÅÏȼ¶Îª6ÕýÔÚÔËÐÐ\r\n");
}
}
void TIM4_IRQHandler(void)
{
//¼ì²éÊÇ·ñ·¢ÉúÖжϣº
if(TIM_GetITStatus(TIM4,TIM_IT_Update) != RESET)
{
//Çå³ýtimeµÄ¸üÐÂÖжϱê־λ
TIM_ClearITPendingBit(TIM4,TIM_IT_Update);
printf("TIM4ÖжÏÓÅÏȼ¶Îª4ÕýÔÚÔËÐÐ!!!!!!\r\n");
}
}
(2)main():
#include "stm32f10x.h"
#include "led.h"
#include "delay.h"
#include "FreeRTOS.h"
#include "task.h"
#include "key.h"
#include "sys.h"
#include "usart.h"
#include "time.h"
//¶¨Òåstart_taskµÄÅäÖã¬ÈÎÎñ¾ä±ú£¬ÈÎÎñÓÅÏȼ¶£¬¶ÑÕ»´óС£¬ÈÎÎñÉùÃ÷£º
#define START_TASK_PRIO 1
#define START_TASK_STACK_SIZE 64
TaskHandle_t start_handler;
void start_task(void);
//¶¨ÒåÈÎÎñ1µÄÅäÖã¬ÈÎÎñ¾ä±ú£¬ÈÎÎñÓÅÏȼ¶£¬¶ÑÕ»´óС£¬ÈÎÎñÉùÃ÷£º
#define TASK1_PRIO 2
#define TASK1_STACK_SIZE 64
TaskHandle_t task1_handler;
void task1(void);
int flag = 0;
int main(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);// ÉèÖÃÖжÏÓÅÏȼ¶·Ö×é4,×ÓÓÅÏȼ¶Îª0
LED_Init();
KEY_Init();
delay_init();
usart_init(9600);
TIME_Init(10000-1,7200-1);
xTaskCreate((TaskFunction_t) start_task, //ÈÎÎñº¯Êý
(const char *)"start_task", //ÈÎÎñÃû³Æ
(uint16_t)START_TASK_STACK_SIZE, //ÈÎÎñ¶ÑÕ»´óС
(void *)NULL, //´«µÝ¸øÈÎÎñº¯ÊýµÄ²ÎÊý
(UBaseType_t)START_TASK_PRIO, //ÈÎÎñÓÅÏȼ¶
(TaskHandle_t *)&start_handler); //ÈÎÎñ¾ä±ú
vTaskStartScheduler(); //¿ªÊ¼ÈÎÎñµ÷¶È
}
/*´´½¨¿ªÊ¼ÈÎÎñ£º*/
void start_task(void)
{
// taskENTER_CRITICAL();
/*´´½¨ÈÎÎñ*/
if(flag == 0)
{
xTaskCreate((TaskFunction_t) task1, //ÈÎÎñº¯Êý
(const char *)"task1", //ÈÎÎñÃû³Æ
(uint16_t)TASK1_STACK_SIZE, //ÈÎÎñ¶ÑÕ»´óС
(void *)NULL, //´«µÝ¸øÈÎÎñº¯ÊýµÄ²ÎÊý
(UBaseType_t)TASK1_PRIO, //ÈÎÎñÓÅÏȼ¶
(TaskHandle_t *)&task1_handler); //ÈÎÎñ¾ä±ú
flag = 1;
}
vTaskDelay(500);
vTaskDelete(NULL); //ɾ³ýµ±Ç°ÈÎÎñ
// taskEXIT_CRITICAL();
}
void task1(void)
{
u32 i = 0;
while(1)
{
if((i++) == 5)
{
printf("¹ØÖжϣ¡£¡£¡\r\n");
portDISABLE_INTERRUPTS();
delay_us(5000000);
printf("¿ªÖжϣ¡£¡£¡\r\n");
portENABLE_INTERRUPTS();
delay_us(5000000);
i = 0;
}
vTaskDelay(500);
}
}
5.运行结果:
6.总结:
FreeRTOS通过portDISABLE_INTERRUPTS()和portENABLE_INTERRUPTS()来开关中断。需要注意的是在关闭中断后,不要轻易的调用vTaskDelay(),因为调用vTaskDelay()后会重新开启系统的中断。
此外,若想要通过FreeRTOS来管理中断,则中断的优先级数值一定要大于宏定义configMAX_SYSCALL_INTERRUPT_PRIORITY的取值。