比神乐 发表于 2024-11-30 18:03

【AT-START-L021测评】FreeRTOS挂起和恢复任务

代码:
#include "at32l021_board.h"
#include "at32l021_clock.h"
#include "FreeRTOS.h"
#include "task.h"

/** @addtogroup UTILITIES_examples
* @{
*/

/** @addtogroup FreeRTOS_demo
* @{
*/
TaskHandle_t led2_handler;
TaskHandle_t led3_handler;

/* led2 task */
void led2_task_function(void *pvParameters);
/* led3 task */
void led3_task_function(void *pvParameters);

/**
* @briefmain function.
* @paramnone
* @retval none
*/
int main(void)
{
system_clock_config();

/* init led2 and led3 */
at32_led_init(LED2);
at32_led_init(LED3);

/* init usart1 */
uart_print_init(115200);

/* enter critical */
taskENTER_CRITICAL();

/* create led2 task */
if(xTaskCreate((TaskFunction_t )led2_task_function,
               (const char*    )"LED2_task",
               (uint16_t       )512,
               (void*          )NULL,
               (UBaseType_t    )2,
               (TaskHandle_t*)&led2_handler) != pdPASS)
{
    printf("LED2 task could not be created as there was insufficient heap memory remaining.\r\n");
}
else
{
    printf("LED2 task was created successfully.\r\n");
}
/* create led3 task */
if(xTaskCreate((TaskFunction_t )led3_task_function,
               (const char*    )"LED3_task",
               (uint16_t       )512,
               (void*          )NULL,
               (UBaseType_t    )2,
               (TaskHandle_t*)&led3_handler) != pdPASS)
{
    printf("LED3 task could not be created as there was insufficient heap memory remaining.\r\n");
}
else
{
    printf("LED3 task was created successfully.\r\n");
}

/* exit critical */
taskEXIT_CRITICAL();

/* start scheduler */
vTaskStartScheduler();
}

/* led2 task function */
void led2_task_function(void *pvParameters)
{
        static int i=0;
        static int num=0;
while(1)
{
                i++;
                if(i==5)
                {
                        printf("在任务中挂起task3\r\n");
                                                vTaskSuspend(led3_handler);
                }
                else if(i==10)
                {
                        printf("在任务中恢复task3\r\n");
                                                vTaskResume(led3_handler);
                }
    printf("task2_num:%d\r\n",++num);/* 打印数值 */
    vTaskDelay(1000);
}
}

/* led3 task function */
void led3_task_function(void *pvParameters)
{
        static int num=0;
while(1)
{
    printf("task3_num:%d\r\n",++num);/* 打印数值 */
    vTaskDelay(500);
}
}
运行效果图:

页: [1]
查看完整版本: 【AT-START-L021测评】FreeRTOS挂起和恢复任务