FreeRTOS 和 RT-Thread 功能差别对比

临界区

FreeRTOS:

非中断的临界区,根据优先级关中断,不会把所有中断都关了;使用了一个变量记录进临界区的次数,保证嵌套不出问题

复制代码
void vPortEnterCritical( void ) // 进入临界区
{
    portDISABLE_INTERRUPTS();
    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 );
    }
}

#define portDISABLE_INTERRUPTS()                            \
{                                                           \
    __set_BASEPRI( configMAX_SYSCALL_INTERRUPT_PRIORITY );  \
    __DSB();                                                \
    __ISB();                                                \
}
/*-----------------------------------------------------------*/

void vPortExitCritical( void ) // 退出临界区
{
    configASSERT( uxCriticalNesting );
    uxCriticalNesting--;
    if( uxCriticalNesting == 0 )
    {
        portENABLE_INTERRUPTS();
    }
}
复制代码

中断的临界区,先读取中断状态(保证嵌套不会有问题),再根据优先级关中断;最后根据之前读取的中断状态恢复中断

uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR(); // 进入临界区

#define portSET_INTERRUPT_MASK_FROM_ISR()        __get_BASEPRI(); portDISABLE_INTERRUPTS()

portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus ); // 退出临界区

 

RT-Thread:关闭所有中断

注:这里的关中断属于总中断,如果在关中断期间来了中断,比如外部中断,开中断后还是能立马响应,不会造成中断丢失。

 

软件定时器

FreeRTOS:定时器函数在 task 中被调用,居于任务调度的定时器

RT-Thread:定时器函数既可以配置为在 task 中被调用,也可以配置为在 tick 中断中被调用

 

posted @   流水灯  阅读(2250)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
点击右上角即可分享
微信分享提示