如何实现任务多优先级
FreeRTOS中,数字优先级越小,逻辑优先级也越小,空闲任务优先级为0.
List_t pxReadyTasksLists[configMAX_PRIORITIES]是数组,数组下标代表任务优先级,任务创建是根据设置的任务优先级插入到对应下标的列表根节点上,如下。
要支持多优先级,就是再任务切换时让pxCurrentTCB
指向最高优先级的TCB即可,之前时手动再任务1、任务2来回切换,现在问题就是怎么找到优先级最高的就绪任务TCB。有2套方法,软件通用方法和硬件指令方法
软件通用方法和硬件指令方法
通过configUSE_PORT_OPTIMISED_TASK_SELECTION指定使用软件通用方法还是硬件指令方法,代码再task.c
中
#if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 )//使用通用方法
#define taskRECORD_READY_PRIORITY( uxPriority ) \
{ \
if( ( uxPriority ) > uxTopReadyPriority ) \
{ \
uxTopReadyPriority = ( uxPriority ); \
} \
}
#define taskSELECT_HIGHEST_PRIORITY_TASK() \
{ \
\
while( listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxTopReadyPriority ] ) ) ) \
{ \
configASSERT( uxTopReadyPriority ); \
--uxTopReadyPriority; \
} \
\
\
listGET_OWNER_OF_NEXT_ENTRY( pxCurrentTCB, &( pxReadyTasksLists[ uxTopReadyPriority ] ) ); \
}
#define taskRESET_READY_PRIORITY( uxPriority )
#define portRESET_READY_PRIORITY( uxPriority, uxTopReadyPriority )
#else
#define taskRECORD_READY_PRIORITY( uxPriority ) portRECORD_READY_PRIORITY( uxPriority, uxTopReadyPriority )
#define taskSELECT_HIGHEST_PRIORITY_TASK() \
{ \
UBaseType_t uxTopPriority; \
\
\
portGET_HIGHEST_PRIORITY( uxTopPriority, uxTopReadyPriority ); \
configASSERT( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ uxTopPriority ] ) ) > 0 ); \
listGET_OWNER_OF_NEXT_ENTRY( pxCurrentTCB, &( pxReadyTasksLists[ uxTopPriority ] ) ); \
}
#define taskRESET_READY_PRIORITY( uxPriority ) \
{ \
if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ ( uxPriority ) ] ) ) == ( UBaseType_t ) 0 ) \
{ \
portRESET_READY_PRIORITY( ( uxPriority ), ( uxTopReadyPriority ) ); \
} \
}
#endif
下面看着几个port接口
#define portRECORD_READY_PRIORITY( uxPriority, uxReadyPriorities ) \
( uxReadyPriorities ) |= ( 1UL << ( uxPriority ) )
#define portRESET_READY_PRIORITY( uxPriority, uxReadyPriorities ) \
( uxReadyPriorities ) &= ~( 1UL << ( uxPriority ) )
可以看到硬件方式是把uxTopReadyPriority 看作一个位图,每位代表一个优先级,一共32bit,任务就绪是就把对应位置1,反之清0.
所以获得最高就绪优先级的硬件方法如下(利用clz指令,计算一个变量从高位开始第一次出现1的位前面0的个数,上图clz(uxReadyPriorities)=6)
#define portGET_HIGHEST_PRIORITY( uxTopPriority, uxReadyPriorities)\
uxTopPriority = ( 31 - __clz( ( uxReadyPriorities ) ) )
以上就是FreeRTOS实时操作系统的多优先级实现的详细内容,更多关于FreeRTOS多优先级实现的资料请关注编程网其它相关文章!