#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "semphr.h"
SemaphoreHandle_t BinarySemaphore; //二值信号量句柄
#define START_TASK_PRIO 1
#define START_STK_SIZE 128
TaskHandle_t StartTask_Handler;
void start_task(void *pvParameters);
#define KeyScan_TASK_PRIO 2
#define KeyScan_STK_SIZE 50
TaskHandle_t KeyScanTask_Handler;
void KeyScan_task(void *pvParameters);
#define KEYPROCESS_TASK_PRIO 3
#define KEYPROCESS_STK_SIZE 50
TaskHandle_t KeyprocessTask_Handler;
void Keyprocess_task(void *pvParameters);
void os_start(void)
{
User_SysTick_ReConfig(configTICK_RATE_HZ);
HAL_UART_Receive_IT(&huart6,Rx_HAL_Buff_Temp,1);
//创建开始任务
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(); //进入临界区
//创建队列
Key_Queue = xQueueCreate(KEYMSG_Q_LENGTH,1);
UartRX_Queue = xQueueCreate(RXMSG_Q_LENGTH,1);
BinarySemaphore = xSemaphoreCreateBinary();
//创建LED1任务
xTaskCreate((TaskFunction_t )KeyScan_task,
(const char* )"led1_task",
(uint16_t )KeyScan_STK_SIZE,
(void* )NULL,
(UBaseType_t )KeyScan_TASK_PRIO,
(TaskHandle_t* )&KeyScanTask_Handler);
//创建key任务
xTaskCreate((TaskFunction_t )Keyprocess_task,
(const char* )"keyprocess_task",
(uint16_t )KEYPROCESS_STK_SIZE,
(void* )NULL,
(UBaseType_t )KEYPROCESS_TASK_PRIO,
(TaskHandle_t* )&KeyprocessTask_Handler);
vTaskDelete(StartTask_Handler); //删除开始任务
taskEXIT_CRITICAL(); //退出临界区
}
//KeyScan任务函数
void KeyScan_task(void *pvParameters)
{
while(1)
{
//按键检测
if(KeyScan((uint8_t *)&KeyValue) == 0)
{
if(KeyValue == 3)
{
if( BinarySemaphore != NULL )
{
if(xSemaphoreGive( BinarySemaphore ) == pdTRUE )
{
}
else
{
printf("BinarySemaphore Send Fail!\r\n");
}
}
KeyValue = 0;
}
}
vTaskDelay(10);
}
}
//key任务函数
void Keyprocess_task(void *pvParameters)
{
BaseType_t ret;
uint8_t counter = 0;
while(1)
{
if(BinarySemaphore != NULL)
{
ret = xSemaphoreTake(BinarySemaphore,portMAX_DELAY);
if(ret == pdTRUE)
{
counter ++;
printf("counter = %d\r\n",counter);
}
}
else
{
vTaskDelay(1000);
}
}
}
//发生溢出中断或DMA中断时
void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart)
{
huart->ErrorCode = HAL_UART_ERROR_NONE;
HAL_UART_Receive_IT(&huart6,Rx_HAL_Buff_Temp,1);
}
//接收到1字节数据,回调函数如下
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
BaseType_t ret;
BaseType_t pxHigherPriorityTaskWoken;
if(huart->Instance==USART6)
{
//加上自己的代码
if(BinarySemaphore != NULL)
{
ret = xSemaphoreGiveFromISR(BinarySemaphore,&pxHigherPriorityTaskWoken);
if(ret == pdTRUE)
{
}
else
{
printf("Semaphore Send ISR Fail!\r\n");
}
portYIELD_FROM_ISR(pxHigherPriorityTaskWoken);
}
HAL_UART_Receive_IT(&huart6,Rx_HAL_Buff_Temp,1);
}
}
FreeRTOS二值信号量
最新推荐文章于 2024-07-09 14:47:23 发布