任务创建与删除
任务的创建有两个函数
二者的区别
- xTaskCreate是使用动态分配内存
系统自动释放内存, 适用于资源充足的系统, - xTaskCreateStatic是静态分配内容
要求用户在编译时分配内存, 任务释放需要用户手动管理, 适用于内存受限系统,
任务详细列表
void vTaskList( char *pcWriteBuffer );
功能:
显示任务详细信息
参数:
@pcWriteBuffer 保存任务状态信息表的存储区<span class="bd-box"><h-char class="bd bd-beg"><h-inner>,</h-inner></h-char></span>须足够大
pcWriteBuffe表中信息:
Name <span class="bd-box"><h-char class="bd bd-beg"><h-inner>:</h-inner></h-char></span>创建任务的时候给任务分配的名字
State <span class="bd-box"><h-char class="bd bd-beg"><h-inner>:</h-inner></h-char></span>任务的状态信息<span class="bd-box"><h-char class="bd bd-beg"><h-inner>,</h-inner></h-char></span>X<span class="bd-box"><h-char class="bd bd-beg"><h-inner>:</h-inner></h-char></span>运行态<span class="bd-box"><h-char class="bd bd-beg"><h-inner>,</h-inner></h-char></span>B<span class="bd-box"><h-char class="bd bd-beg"><h-inner>:</h-inner></h-char></span>阻塞态<span class="bd-box"><h-char class="bd bd-beg"><h-inner>,</h-inner></h-char></span>R<span class="bd-box"><h-char class="bd bd-beg"><h-inner>:</h-inner></h-char></span>就绪态<span class="bd-box"><h-char class="bd bd-beg"><h-inner>,</h-inner></h-char></span>S<span class="bd-box"><h-char class="bd bd-beg"><h-inner>:</h-inner></h-char></span>挂起态<span class="bd-box"><h-char class="bd bd-beg"><h-inner>,</h-inner></h-char></span>D<span class="bd-box"><h-char class="bd bd-beg"><h-inner>:</h-inner></h-char></span>删除态
Priority <span class="bd-box"><h-char class="bd bd-beg"><h-inner>:</h-inner></h-char></span>任务优先级
Stack <span class="bd-box"><h-char class="bd bd-beg"><h-inner>:</h-inner></h-char></span>任务堆栈的<span class="bd-box"><h-char class="bd bd-end"><h-inner>“</h-inner></h-char></span>高水位线<span class="bd-box"><h-char class="bd bd-beg"><h-inner>”</h-inner></h-char><h-char class="bd bd-beg"><h-inner>,</h-inner></h-char></span>就是堆栈历史最小剩余值
Num <span class="bd-box"><h-char class="bd bd-beg"><h-inner>:</h-inner></h-char></span>任务编号<span class="bd-box"><h-char class="bd bd-beg"><h-inner>,</h-inner></h-char></span>这个编号是唯一的<span class="bd-box"><h-char class="bd bd-beg"><h-inner>;</h-inner></h-char></span>当多个任务使用同一个任务名的时候<span class="bd-box"><h-char class="bd bd-beg"><h-inner>;</h-inner></h-char></span>可以通过编号来区分
注意:
在使用vTaskList前需要在FreeRTOSConfig.h文件中打开如下宏
#define configUSE_TRACE_FACILITY 1
#define configGENERATE_RUN_TIME_STATS 1
#define configUSE_STATS_FORMATTING_FUNCTIONS 1
任务的删除
void vTaskDelete( TaskHandle_t xTaskToDelete );
功能:
删除任务
参数:
@xTaskToDelete 任务句柄
任务退出<span class="bd-box"><h-char class="bd bd-beg"><h-inner>:</h-inner></h-char></span>vTaskDelete(NULL) 在任务函数中调用<span class="bd-box"><h-char class="bd bd-beg"><h-inner>,</h-inner></h-char></span>表示退出当前任务
删除任务<span class="bd-box"><h-char class="bd bd-beg"><h-inner>:</h-inner></h-char></span>vTaskDelete(xHandle)
任务的挂起与恢复
void vTaskSuspend( TaskHandle_t xTaskToSuspend );
参数<span class="bd-box"><h-char class="bd bd-beg"><h-inner>:</h-inner></h-char></span>
xTaskToSuspend 被挂起的任务句柄<span class="bd-box"><h-char class="bd bd-beg"><h-inner>。</h-inner></h-char></span>传递空句柄将导致调用任务被暂停<span class="bd-box"><h-char class="bd bd-beg"><h-inner>。</h-inner></h-char></span>
void vTaskResume( TaskHandle_t xTaskToResume );
参数<span class="bd-box"><h-char class="bd bd-beg"><h-inner>:</h-inner></h-char></span>
xTaskToResume 要恢复的任务句柄<span class="bd-box"><h-char class="bd bd-beg"><h-inner>。</h-inner></h-char></span>
任务调度分析
freeRTOS的调度模式有两种
抢占式调度
时间片调度由滴答定时器决定