參考正點原子的freertos的視頻教程,在STM32F407上面的實驗。
#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "led.h"
#include "FreeRTOS.h"
#include "task.h"
#include "string.h"
#include "key.h"
#include "queue.h"
#include "timer.h"
//開始任務
#define StackTskSize 128
#define TskPri 1
void Start_Task( void *pvParameters);
TaskHandle_t StartTsk_Handle;
//控制led亮滅任務
#define StackTsk1Size 50
#define Tsk1Pri 2
void Start_Task1(void *pvParameters);
TaskHandle_t StartTsk1_Handle;
//按鍵處理任務
#define KeyProcessTskSize 80
#define KeyProcessTskPri 3
void KeyProcess_Task(void *pvParameters);
TaskHandle_t KeyProcess_Handle;
//定義隊列相關
QueueHandle_t Key_Queue;
#define Key_Queue_Size (1)
QueueHandle_t Message_Queue;
#define Message_Queue_Size (4)
int main(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);//設置系統中斷優先級分組4
delay_init(168); //初始化延時函數
uart_init(115200); //初始化串口
LED_Init(); //初始化LED端口
KEY_Init();
TIM3_Int_Init(5000-1,16800-1);
//開始任務的任務編號爲0
xTaskCreate( (TaskFunction_t) Start_Task, //任務函數
(char * ) "Start_Task", //任務名
(uint16_t ) StackTskSize, //任務堆棧大小
(void * ) NULL, //任務參數
(UBaseType_t ) TskPri, //任務優先級
(TaskHandle_t *) &StartTsk_Handle );
vTaskStartScheduler(); //開啓任務調度 //先後創建了空閒任務和定時器服務任務
}
void Start_Task( void *pvParameters)
{
taskENTER_CRITICAL();
//創建隊列
Key_Queue=xQueueCreate(Key_Queue_Size, sizeof(u8));
Message_Queue=xQueueCreate(Message_Queue_Size,USART_REC_LEN);
if(Key_Queue==NULL)
{
printf("Key_Queue create fail!!!\r\n");
}
//創建task1
xTaskCreate( (TaskFunction_t) Start_Task1, //任務函數
(char * ) "Start_Task1", //任務名
(uint16_t ) StackTsk1Size, //任務堆棧大小
(void * ) NULL, //任務參數
(UBaseType_t ) Tsk1Pri, //任務優先級
(TaskHandle_t *) &StartTsk1_Handle );
//查詢任務
xTaskCreate( (TaskFunction_t)KeyProcess_Task, //任務函數
(char * ) "KeyProcess_Task", //任務名
(uint16_t ) KeyProcessTskSize, //任務堆棧大小
(void * ) NULL, //任務參數
(UBaseType_t ) KeyProcessTskPri, //任務優先級
(TaskHandle_t *) &KeyProcess_Handle );
vTaskDelete(StartTsk_Handle); //NULL就是刪除當前任務。
taskEXIT_CRITICAL();
}
void Start_Task1(void *pvParameters)
{
u8 key;
BaseType_t err;
while(1)
{
key=KEY_Scan(0);
if((Key_Queue!=NULL)&&(key)) //隊列和按鍵都是有效的
{
//err=xQueueSend(Key_Queue,&key,10); //10時阻塞時間10ms 普通隊列發送
err=xQueueOverwrite(Key_Queue,&key); //覆寫的方式發送
if(err!= pdTRUE)
{
printf("Queue send failed!!\r\n");
}
}
LED0=~LED0;
vTaskDelay(10);
}
}
void KeyProcess_Task(void *pvParameters)
{
BaseType_t err;
u8 key;
while(1)
{
if(Key_Queue!=NULL)
{
err=xQueueReceive(Key_Queue,&key,portMAX_DELAY); //隊列普通接收
if(err==pdTRUE) //成功獲取到消息
{
printf("key=%d\r\n",key);
}
}
else
{
vTaskDelay(10);
}
}
}
#include "sys.h"
#include "usart.h"
#include "FreeRTOS.h"
#include "queue.h"
#include "task.h"
#include "string.h"
//如果使用ucos,則包括下面的頭文件即可.
#if SYSTEM_SUPPORT_OS
#include "FreeRTOS.h" //FreeRTOS使用
#endif
//加入以下代碼,支持printf函數,而不需要選擇use MicroLIB
#if 1
#pragma import(__use_no_semihosting)
//標準庫需要的支持函數
struct __FILE
{
int handle;
};
FILE __stdout;
//定義_sys_exit()以避免使用半主機模式
void _sys_exit(int x)
{
x = x;
}
//重定義fputc函數
int fputc(int ch, FILE *f)
{
while((USART1->SR&0X40)==0);//循環發送,直到發送完畢
USART1->DR = (u8) ch;
return ch;
}
#endif
#if EN_USART1_RX //如果使能了接收
//串口1中斷服務程序
//注意,讀取USARTx->SR能避免莫名其妙的錯誤
u8 USART_RX_BUF[USART_REC_LEN]; //接收緩衝,最大USART_REC_LEN個字節.
//接收狀態
//bit15, 接收完成標誌
//bit14, 接收到0x0d
//bit13~0, 接收到的有效字節數目
u16 USART_RX_STA=0; //接收狀態標記
//初始化IO 串口1
//bound:波特率
void uart_init(u32 bound){
//GPIO端口設置
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE); //使能GPIOA時鐘
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);//使能USART1時鐘
//串口1對應引腳複用映射
GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1); //GPIOA9複用爲USART1
GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1); //GPIOA10複用爲USART1
//USART1端口配置
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10; //GPIOA9與GPIOA10
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//複用功能
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //速度50MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推輓複用輸出
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉
GPIO_Init(GPIOA,&GPIO_InitStructure); //初始化PA9,PA10
//USART1 初始化設置
USART_InitStructure.USART_BaudRate = bound;//波特率設置
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字長爲8位數據格式
USART_InitStructure.USART_StopBits = USART_StopBits_1;//一個停止位
USART_InitStructure.USART_Parity = USART_Parity_No;//無奇偶校驗位
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//無硬件數據流控制
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收發模式
USART_Init(USART1, &USART_InitStructure); //初始化串口1
USART_Cmd(USART1, ENABLE); //使能串口1
//USART_ClearFlag(USART1, USART_FLAG_TC);
#if EN_USART1_RX
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//開啓相關中斷
//Usart1 NVIC 配置
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;//串口1中斷通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=6;//搶佔優先級3
NVIC_InitStructure.NVIC_IRQChannelSubPriority =0; //子優先級3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根據指定的參數初始化VIC寄存器、
#endif
}
extern QueueHandle_t Message_Queue; //消息隊列句柄
void USART1_IRQHandler(void) //串口1中斷服務程序
{
u8 Res;
BaseType_t ERR;
BaseType_t PriorityTaskWoken=pdFALSE;
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //接收中斷(接收到的數據必須是0x0d 0x0a結尾)
{
Res =USART_ReceiveData(USART1);//(USART1->DR); //讀取接收到的數據
if((USART_RX_STA&0x8000)==0)//接收未完成
{
if(USART_RX_STA&0x4000)//接收到了0x0d
{
if(Res!=0x0a)
{
USART_RX_STA=0;//接收錯誤,重新開始
}
else
{
USART_RX_STA|=0x8000; //接收完成了
//向隊列發送數據
if(Message_Queue!=NULL)
{
ERR=xQueueSendFromISR(Message_Queue,USART_RX_BUF, &PriorityTaskWoken);
if(ERR==pdTRUE)
{
USART_RX_STA=0;
memset(USART_RX_BUF,0,USART_REC_LEN);
portYIELD_FROM_ISR(PriorityTaskWoken);
}
else
{
printf("Send Failed!!\r\n");
}
}
}
}
else //還沒收到0X0D
{
if(Res==0x0d)USART_RX_STA|=0x4000;
else
{
USART_RX_BUF[USART_RX_STA&0X3FFF]=Res ;
USART_RX_STA++;
if(USART_RX_STA>(USART_REC_LEN-1))USART_RX_STA=0;//接收數據錯誤,重新開始接收
}
}
}
}
}
#endif