FreeRTOS 学习笔记2——task
FreeRTOS task manage study Note
FreeRTOS task manage study Note
create
task priority
时间,tick
task 状态
task block api
task idle hook function
task Priority set and get
task delete
Scheduling Algorithms
例程
create
task priority
时间,tick
task 状态
task block api
task idle hook function
task Priority set and get
task delete
Scheduling Algorithms
例程
create
BaseType_t xTaskCreate( TaskFunction_t pvTaskCode, /* task 函数 */
const char * const pcName, /* task 字符名,用于debug*/
uint16_t usStackDepth, /* task堆栈大小 */
void *pvParameters, /* task 参数,可以为NULL */
UBaseType_t uxPriority, /* task 任务优先级 */
TaskHandle_t *pxCreatedTask ); /* task句柄(可以用于调整task优先级,删除task等操作),可以为NULL */
task priority
- API: vTaskPrioritySet()
- 任务调度方法有两种:
Generic Method
, 当configUSE_PORT_OPTIMISED_TASK_SELECTION 设为0,或者不定义此项时。freeRTOS 不限制 configMAX_PRIORITIES 的大小,但要尽可能小。Architecture Optimized Method
,当configUSE_PORT_OPTIMISED_TASK_SELECTION 设为1时,configMAX_PRIORITIES的大小限制在32,但尽可能小。
时间,tick
configTICK_RATE_HZ
设置tick产生的频率,当设为100时,tick间隔10ms,当设为1000时,tick间隔1mspdMS_TO_TICKS()
宏将ms转换为当前对应的tick数,当tick rate变化时,时间不变。
task 状态
task block api
- vTaskDelay(), vTaskDelay( pdMS_TO_TICKS( 100 ) ) 延时100ms
- xTaskGetTickCount() 获取当前计数值
- vTaskDelayUntil()
task idle hook function
vApplicationIdleHook( void );
task Priority set and get
void vTaskPrioritySet( TaskHandle_t pxTask, UBaseType_t uxNewPriority );
UBaseType_t uxTaskPriorityGet( TaskHandle_t pxTask );
task delete
void vTaskDelete( TaskHandle_t pxTaskToDelete );
Scheduling Algorithms
configUSE_PREEMPTION
抢占策略
configUSE_TIME_SLICING
时间片策略
configUSE_TICKLESS_IDLE
空闲时降低tick频率,用于低功耗用途。
例程
xTaskCreate( vTask_A, "taskA", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY+1, NULL);
xTaskCreate( vTask_B, "taskB", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY+1, NULL);
void vTask_A( void *pvParameters )
{
int16_t sSendNum = 1;
for (;;) {
vTaskDelay( 2000/portTICK_PERIOD_MS);
printf("Sending Num:%d\r\n", sSendNum);
xQueueSend( xMsgQueue, (void *)&sSendNum, portMAX_DELAY);
sSendNum++;
}
}
void vTask_B( void *pvParameters )
{
int16_t sReceiveNum = 0;
for (;;) {
if ( xQueueReceive( xMsgQueue, &sReceiveNum, portMAX_DELAY)) {
printf("ReceiveNum:%d\r\n", sReceiveNum);
}
}
}