FreeRTOS-中断实验
本次实验简单验证优先级问题 用定时器3和定时器4优先级分别配置4和5 优先级低于5的时候FreeRTOS中断不能屏蔽而大于等于5的优先级会屏蔽
FreeRTOS开关中断函数为portENABLE_INTERRUPTS ()和portDISABLE_INTERRUPTS(),这两个函数其实是宏定义,在portmacro.h中有定义,
#define portDISABLE_INTERRUPTS() vPortRaiseBASEPRI()
#define portENABLE_INTERRUPTS() vPortSetBASEPRI(0)
time.h
#ifndef _TIME_H_
#define _TIME_H_
#include "stm32f10x.h"
void Init_TIM3(void);
void Init_TIM4(void);
#endif
timc.c
#include "time.h"
#include "usart.h"
void Init_TIM3(void)
{
TIM_TimeBaseInitTypeDef TIME3_InitStructure;
NVIC_InitTypeDef TIM3_NVICStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
TIME3_InitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIME3_InitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIME3_InitStructure.TIM_Period = 10000 - 1;
TIME3_InitStructure.TIM_Prescaler = 3600 - 1;
TIM_TimeBaseInit(TIM3,&TIME3_InitStructure);
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);
TIM3_NVICStructure.NVIC_IRQChannel = TIM3_IRQn;
TIM3_NVICStructure.NVIC_IRQChannelCmd = ENABLE;
TIM3_NVICStructure.NVIC_IRQChannelPreemptionPriority = 4;
TIM3_NVICStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_Init(&TIM3_NVICStructure);
TIM_Cmd(TIM3,ENABLE);
}
void Init_TIM4(void)
{
TIM_TimeBaseInitTypeDef TIME4_InitStructure;
NVIC_InitTypeDef TIM4_NVICStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE);
TIME4_InitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIME4_InitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIME4_InitStructure.TIM_Period = 10000 - 1;
TIME4_InitStructure.TIM_Prescaler = 3600 - 1;
TIM_TimeBaseInit(TIM4,&TIME4_InitStructure);
TIM_ITConfig(TIM4,TIM_IT_Update,ENABLE);
TIM4_NVICStructure.NVIC_IRQChannel = TIM4_IRQn;
TIM4_NVICStructure.NVIC_IRQChannelCmd = ENABLE;
TIM4_NVICStructure.NVIC_IRQChannelPreemptionPriority = 5;
TIM4_NVICStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_Init(&TIM4_NVICStructure);
TIM_Cmd(TIM4,ENABLE);
}
void TIM3_IRQHandler(void)
{
if(TIM_GetITStatus(TIM3,TIM_IT_Update) != RESET)
{
printf("time3 interrupt\r\n");
}
TIM_ClearITPendingBit(TIM3,TIM_IT_Update);
}
void TIM4_IRQHandler(void)
{
if(TIM_GetITStatus(TIM4,TIM_IT_Update) != RESET)
{
printf("time4 interrupt\r\n");
}
TIM_ClearITPendingBit(TIM4,TIM_IT_Update);
}
main.c
#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "led.h"
#include "FreeRTOS.h"
#include "task.h"
#include "time.h"
#define START_TASK_PRIO 1
#define START_STK_SIZE 128
TaskHandle_t StartTask_Handler;
void start_task(void *pvParameters);
#define INTERRUPT_TASK_PRIO 2
#define INTERRUPT_STK_SIZE 50
TaskHandle_t INTERRUPTTask_Handler;
void interrupt_task(void *pvParameters);
int main(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
delay_init();
uart_init(115200);
LED_Init();
Init_TIM3();
Init_TIM4();
xTaskCreate((TaskFunction_t )start_task,
(const char* )"start_task",
(uint16_t )START_STK_SIZE,
(void* )NULL,
(UBaseType_t )START_TASK_PRIO,
(TaskHandle_t* )&StartTask_Handler);
vTaskStartScheduler();
}
void start_task(void *pvParameters)
{
taskENTER_CRITICAL();
xTaskCreate((TaskFunction_t )interrupt_task,
(const char* )"interrupt_task",
(uint16_t )INTERRUPT_STK_SIZE,
(void* )NULL,
(UBaseType_t )INTERRUPT_TASK_PRIO,
(TaskHandle_t* )&INTERRUPTTask_Handler);
vTaskDelete(StartTask_Handler);
taskEXIT_CRITICAL();
}
void interrupt_task(void *pvParameters)
{
char task_num = 0;
while(1)
{
task_num++;
if(task_num == 5)
{
printf(" 关闭中断 \r\n");
portDISABLE_INTERRUPTS();
delay_xms(5000);
printf(" 开启中断 \r\n");
portENABLE_INTERRUPTS();
}
LED0 = ~LED0;
vTaskDelay(1000);
}
}
|