free rtos 任务切换的理解

rtos 系统运行的几个要素:本质上,系统的运行依靠sys tick/PendSV 中断 驱动 任务管理程序完成任务切换执行不同任务程序。除sys tick/PendSV 中断 以外的中断程序执行。以及任务任务运行过程引发的任务阻塞,挂起,恢复。
所以占有cpu的优先级:高优先级中断>sys tick/PendSV 中断>高优先级的任务>低优先级任务,cpu在各个代码跳来跳去。

如图:

 

引起任务阻塞的api:
Tasks can enter the Blocked state to wait for two different types of event:
1. Temporal (time-related) events—the event being either a delay period expiring, or an 
absolute time being reached. For example, a task may enter the Blocked state to wait 
for 10 milliseconds to pass.
2. Synchronization events—where the events originate from another task or interrupt. For
example, a task may enter the Blocked state to wait for data to arrive on a queue. 
Synchronization events cover a broad range of event types.
FreeRTOS queues, binary semaphores, counting semaphores, mutexes, recursive mutexes, 
event groups and direct to task notifications can all be used to create synchronization events.
All these features are covered in future chapters of this book.

引起任务挂起的api
vTaskSuspend()/vTaskSuspendall()

任务的让出(ruunnig-->block)
1.vTaskDelay()/vTaskDelayUntil()-->prvAddCurrentTaskToDelayedList
2.等待某事件,将tcb放到EventList
 xQueueReceive->vTaskPlaceOnEventList
  xQueueSemaphoreTake->vTaskPlaceOnEventList
3.(ruunnig-->suspend)vTaskSuspend()
任务的唤醒(block-->ready)
1.xTaskIncrementTick -> prvAddTaskToReadyList( pxTCB );
2.xQueueGenericSend->xTaskRemoveFromEventList
3.(suspend-->ready)vTaskResume()


------------------------------------------------------------------------------------------
#define taskENTER_CRITICAL()        portENTER_CRITICAL()
--->
#define portENTER_CRITICAL()        vPortEnterCritical()
--->
void vPortEnterCritical( void )
{
    portDISABLE_INTERRUPTS();-->msr basepri, configMAX_SYSCALL_INTERRUPT_PRIORITY //设置中断屏蔽寄存器 屏蔽优先级小于configMAX_SYSCALL_INTERRUPT_PRIORITY 的中断,此时os无法完成调度
    uxCriticalNesting++;

    /* This is not the interrupt safe version of the enter critical function so
    assert() if it is being called from an interrupt context.  Only API
    functions that end in "FromISR" can be used in an interrupt.  Only assert if
    the critical nesting count is 1 to protect against recursive calls if the
    assert function also uses a critical section. */
    if( uxCriticalNesting == 1 )
    {
        configASSERT( ( portNVIC_INT_CTRL_REG & portVECTACTIVE_MASK ) == 0 );
    }
}

taskEXIT_CRITICAL
        退出临界段,调用taskEXIT_CRITICAL  portEXIT_CRITICAL  vPortExitCritical 本质都是调用 __set_BASEPRI( 0 )清除中断屏蔽。和...ENTER_CRITICAL成对嵌套使用。

------------------------------------------------------------------------------------------
void vTaskSuspendAll( void )
--->
void vTaskSuspendAll( void )
{
    /* A critical section is not required as the variable is of type
    BaseType_t.  Please read Richard Barry's reply in the following link to a
    post in the FreeRTOS support forum before reporting this as a bug! -
    http://goo.gl/wu4acr */
    ++uxSchedulerSuspended;
}
将uxSchedulerSuspended标志位加一,禁止上下文切换;但是SysTick继续运行,当前任务和中断还可以继续运行。
BaseType_t xTaskResumeAll( void )
       会将所有挂起状态的列表中的任务加入到就绪的列表 list 当中。

------------------------------------------------------------------------------------------

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值