|
FreeRTOS学习笔记:通过任务参数传入需要打印输出的文本
[复制链接]
- #include "FreeRTOS.h"
- #include "task.h"
- #include "queue.h"
- #include "croutine.h"
- #include "bsp.h"
- /*
- **********************************************************************************************************
- 函数声明
- **********************************************************************************************************
- */
- static void AppTaskCreate (void);
- extern void vSetupTimerTest( void );
- /*
- **********************************************************************************************************
- 变量声明
- **********************************************************************************************************
- */
- xTaskHandle xHandleTask3;
- int8_t pcWriteBuffer[500];
- /*
- *********************************************************************************************************
- * 函 数 名: main
- * 功能说明: 标准c程序入口。
- * 形 参:无
- * 返 回 值: 无
- *********************************************************************************************************
- */
- static const char *pcTextForTask1 ="Task 1 is running\r\n";
- static const char *pcTextForTask2 ="Task 2 is running\t\n";
- int main(void)
- {
- /* 硬件初始化 */
- bsp_Init();
-
- /* 创建任务 */
- AppTaskCreate();
-
- vSetupTimerTest();
-
- /* 启动调度,开始执行任务 */
- vTaskStartScheduler();
- /*
- If all is well we will never reach here as the scheduler will now be
- running. If we do reach here then it is likely that there was insufficient
- heap available for the idle task to be created.
- */
- while (1);
- }
- /*
- *********************************************************************************************************
- * 函 数 名: vTask1
- * 功能说明: LED闪烁
- * 形 参:pvParameters 是在创建该任务时传递的形参
- * 返 回 值: 无
- *********************************************************************************************************
- */
- void vTask1( void *pvParameters )
- {
- portTickType xDelay, xNextTime;
-
- //char *pcTaskName = (char*)pvParameters;
-
- xNextTime = xTaskGetTickCount () + ( portTickType ) 1000;
-
- while(1)
- {
- bsp_LedToggle(LED1);
- //printf("\r\n%s",pcTaskName);
- printf("\r\n%s",(char*)(pvParameters));
- /* 用vTaskDelay实现vTaskDelayUntil() */
- xDelay = xNextTime - xTaskGetTickCount ();
- xNextTime += ( portTickType ) 1000;
- if( xDelay <= ( portTickType ) 1000 )
- {
- vTaskDelay( xDelay );
- }
- }
- }
- /*
- *********************************************************************************************************
- * 函 数 名: vTask2
- * 功能说明: LED闪烁
- * 形 参:pvParameters 是在创建该任务时传递的形参
- * 返 回 值: 无
- *********************************************************************************************************
- */
- void vTask2( void *pvParameters )
- {
- portTickType xLastWakeTime;
- const portTickType xFrequency = 100;
- // Initialise the xLastWakeTime variable with the current time.
- xLastWakeTime = xTaskGetTickCount();
- while(1)
- {
-
- bsp_LedToggle(LED2); // Wait for the next cycle.
- vTaskDelayUntil( &xLastWakeTime, xFrequency );
- }
- }
- /*
- *********************************************************************************************************
- * 函 数 名: vTask3
- * 功能说明: LED闪烁
- * 形 参:pvParameters 是在创建该任务时传递的形参
- * 返 回 值: 无
- *********************************************************************************************************
- */
- void vTask3( void *pvParameters )
- {
- while(1)
- {
- /* 挂起自己,可以写NULL或者xHandleTask3句柄,都可以的 */
- vTaskSuspend( NULL );
- bsp_LedToggle(LED3);
- }
- }
- /*
- *********************************************************************************************************
- * 函 数 名: vTask4
- * 功能说明: LED闪烁
- * 形 参:pvParameters 是在创建该任务时传递的形参
- * 返 回 值: 无
- *********************************************************************************************************
- */
- void vTask4( void *pvParameters )
- {
- portTickType xLastWakeTime;
- const portTickType xFrequency = 1000;
- // Initialise the xLastWakeTime variable with the current time.
- xLastWakeTime = xTaskGetTickCount();
- while(1)
- {
- vTaskResume(xHandleTask3);
- vTaskList((char *)&pcWriteBuffer);
- printf("%s\r\n", pcWriteBuffer);
- vTaskGetRunTimeStats(( char *)&pcWriteBuffer);
- printf("%s\r\n", pcWriteBuffer);
- vTaskDelayUntil( &xLastWakeTime, xFrequency );
- }
- }
- /*
- *********************************************************************************************************
- * 函 数 名: AppTaskCreate
- * 功能说明: 创建应用任务
- * 形 参:无
- * 返 回 值: 无
- *********************************************************************************************************
- */
- static void AppTaskCreate (void)
- {
- /* Create one task. */
- xTaskCreate( vTask1, /* Pointer to the function that implements the task. */
- "Task 1", /* Text name for the task. This is to facilitate debugging only. */
- 500, /* Stack depth in words. */
- (void*)pcTextForTask1, /*通过任务参数传入需要打印输出的文本. */
- 1, /* This task will run at priority 1. */
- NULL ); /* We are not using the task handle. */
- /* Create one task. */
- xTaskCreate( vTask2, /* Pointer to the function that implements the task. */
- "Task 2", /* Text name for the task. This is to facilitate debugging only. */
- 500, /* Stack depth in words. */
- NULL, /* We are not using the task parameter. */
- 2, /* This task will run at priority 2. */
- NULL ); /* We are not using the task handle. */
-
- /* Create one task. */
- xTaskCreate( vTask3, /* Pointer to the function that implements the task. */
- "Task 3", /* Text name for the task. This is to facilitate debugging only. */
- 500, /* Stack depth in words. */
- NULL, /* We are not using the task parameter. */
- 3, /* This task will run at priority 2. */
- &xHandleTask3 );
-
- /* Create one task. */
- xTaskCreate( vTask4, /* Pointer to the function that implements the task. */
- "Task 4", /* Text name for the task. This is to facilitate debugging only. */
- 500, /* Stack depth in words. */
- NULL, /* We are not using the task parameter. */
- 4, /* This task will run at priority 2. */
- NULL ); /* We are not using the task handle. */
- }
复制代码
|
|