创建几个简单的FreeRTOS任务:
#define START_TASK_PRIO 1
#define START_STK_SIZE 128
TaskHandle_t StartTask_Handler;
void start_task(void *pvParameters);
#define USART1_SEND_TASK_PRIO 2
#define USART1_SEND_STK_SIZE 256
TaskHandle_t USART1_SendTask_Handler;
void USART1_Send_task(void *pvParameters);
#define APP_TASK_PRIO 3
#define APP_STK_SIZE 256
TaskHandle_t APP_Task_Handler;
void APP_task(void *pvParameters);
#define SENSOR_TASK_PRIO 4
#define SENSOR_STK_SIZE 256
TaskHandle_t Sensor_Task_Handler;
void Sensor_task(void *pvParameters);
#define USART1_TX_Q_NUM 5
QueueHandle_t Queue_Usart1_tx;
#define MAX_FRAME_COMM_BUFFER_SIZE USART1_TX_DMA_BUFFER_MAX_SIZE
typedef struct
{
uint8_t* start_addr;
uint16_t len;
}Usart1_Tx_Buffer_Typedef;
int main(void)
{
BaseType_t TaskCreateStatus;
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_DMA_Init();
MX_USART1_UART_Init();
TaskCreateStatus= 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);
if(pdPASS == TaskCreateStatus)
{
//printf(" start_task create success\r\n");
}
vTaskStartScheduler();
while (1)
{
}
}
void start_task(void *pvParameters)
{
BaseType_t TaskCreateStatus;
taskENTER_CRITICAL();
Queue_Usart1_tx=xQueueCreate(USART1_TX_Q_NUM,sizeof(Usart1_Tx_Buffer_Typedef));
TaskCreateStatus= xTaskCreate((TaskFunction_t )USART1_Send_task,
(const char* )"USART1_Send_task",
(uint16_t )USART1_SEND_STK_SIZE,
(void* )NULL,
(UBaseType_t )USART1_SEND_TASK_PRIO,
(TaskHandle_t* )&USART1_SendTask_Handler);
if(pdPASS == TaskCreateStatus)
{
//printf(" LED0Task create success\r\n");
}
TaskCreateStatus= xTaskCreate((TaskFunction_t )APP_task,
(const char* )"APP_task",
(uint16_t )APP_STK_SIZE,
(void* )NULL,
(UBaseType_t )APP_TASK_PRIO,
(TaskHandle_t* )&APP_Task_Handler);
if(pdPASS == TaskCreateStatus)
{
//printf(" LED0Task create success\r\n");
}
TaskCreateStatus= xTaskCreate((TaskFunction_t )Sensor_task,
(const char* )"Sensor_task",
(uint16_t )SENSOR_STK_SIZE,
(void* )NULL,
(UBaseType_t )SENSOR_TASK_PRIO,
(TaskHandle_t* )&Sensor_Task_Handler);
if(pdPASS == TaskCreateStatus)
{
//printf(" LED0Task create success\r\n");
}
vTaskDelete(StartTask_Handler);
taskEXIT_CRITICAL();
}
void USART1_SendData(uint8_t *pData,uint16_t len)
{
Usart1_Tx_Buffer_Typedef Tx_Buff;
Tx_Buff.start_addr = pData;
Tx_Buff.len = len;
xQueueSendToBack(Queue_Usart1_tx,(void *)&Tx_Buff,portMAX_DELAY);
}
void USART1_Send_task(void *pvParameters)
{
Usart1_Tx_Buffer_Typedef Tx_Type;
BaseType_t res = pdTRUE;
while(1)
{
res = xQueueReceive(Queue_Usart1_tx,&Tx_Type,portMAX_DELAY);
if(res == pdTRUE) Usart1_DMASend_NBytes(Tx_Type.start_addr,Tx_Type.len);//通过DMA发送
vTaskDelay(10);
}
}
void APP_task(void *pvParameters)
{
uint8_t testOut[128];
uint32_t index=0;
while(1)
{
sprintf((char*)testOut,"APP_task %u \r\n",index++);
USART1_SendData(testOut,strlen((const char*)testOut));
vTaskDelay(100);
}
}
void Sensor_task(void *pvParameters)
{
uint8_t testOut2[128];
uint32_t index=0;
while(1)
{
sprintf((char*)testOut2,"Sensor_task %u \r\n",index++);
USART1_SendData(testOut2,strlen((const char*)testOut2));
vTaskDelay(100);
}
}
|