#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "semphr.h"
#include "os_start.h"
#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);
#define KEYMSG_Q_LENGTH 1 //队列长度
#define RXMSG_Q_LENGTH 4
#define RX_BUF_NUM 30
uint8_t Rx_Buf[RX_BUF_NUM];
uint8_t Rx_HAL_Buff_Temp[2]; //HAL函数接收临时缓存
QueueHandle_t Key_Queue; //按键队列
QueueHandle_t UartRX_Queue; //消息队列
SemaphoreHandle_t CounterSemaphore; //计数型信号量句柄
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);
CounterSemaphore = xSemaphoreCreateCounting(10,0); //最大255个计数,初始值为0
if(CounterSemaphore == NULL)
{
printf("CounterSemaphore Creat Fail!\r\n");
}
//创建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( CounterSemaphore != NULL )
{
if(xSemaphoreGive( CounterSemaphore ) == pdTRUE )
{
}
else
{
printf("CounterSemaphore Send Fail!\r\n");
}
}
KeyValue = 0;
}
}
vTaskDelay(10);
}
}
//key任务函数
void Keyprocess_task(void *pvParameters)
{
BaseType_t ret,semVal;
while(1)
{
if(CounterSemaphore != NULL)
{
ret = xSemaphoreTake(CounterSemaphore,portMAX_DELAY);
if(ret == pdTRUE)
{
semVal = uxSemaphoreGetCount(CounterSemaphore);
printf("counter = %d\r\n",(uint8_t)semVal);
}
}
else
{
vTaskDelay(1000);
}
vTaskDelay(1000);
}
}
FreeRTOS计数型值信号量
最新推荐文章于 2024-01-03 21:12:27 发布