你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

【经验分享】STM32 FreeRTOS临界区

[复制链接]
STMCU小助手 发布时间:2022-6-17 14:44
01. 概述
临界段代码也叫做临界区,是指那些必须完整运行,不能被打断的代码段,比如有的外设的初始化需要严格的时序,初始化过程中不能被打断。FreeRTOS在进入临界端代码的时候需要关闭中断,当处理完临界段代码以后再打开中断。FreeRTOS系统本身就有很多的临界段代码。这些代码都加了临界段代码保护,我们在写自己的用户程序的时候有些地方也需要添加临界段代码保护。

FreeRTOS与临界段代码保护有关的函数有4个:

  1. /**
  2. * task. h
  3. *
  4. * Macro to mark the start of a critical code region.  Preemptive context
  5. * switches cannot occur when in a critical region.
  6. *
  7. * NOTE: This may alter the stack (depending on the portable implementation)
  8. * so must be used with care!
  9. *
  10. * \defgroup taskENTER_CRITICAL taskENTER_CRITICAL
  11. * \ingroup SchedulerControl
  12. */
  13. #define taskENTER_CRITICAL()               portENTER_CRITICAL() //任务级
  14. #define taskENTER_CRITICAL_FROM_ISR()      portSET_INTERRUPT_MASK_FROM_ISR() //中断级

  15. /**
  16. * task. h
  17. *
  18. * Macro to mark the end of a critical code region.  Preemptive context
  19. * switches cannot occur when in a critical region.
  20. *
  21. * NOTE: This may alter the stack (depending on the portable implementation)
  22. * so must be used with care!
  23. *
  24. * \defgroup taskEXIT_CRITICAL taskEXIT_CRITICAL
  25. * \ingroup SchedulerControl
  26. */
  27. #define taskEXIT_CRITICAL()                portEXIT_CRITICAL() //任务级
  28. #define taskEXIT_CRITICAL_FROM_ISR( x )    portCLEAR_INTERRUPT_MASK_FROM_ISR( x ) //中断级
复制代码

02. 任务级临界区代码保护
taskENTER_CRITICAL()和taskEXIT_CRITICAL()是任务级的临界区代码保护,一个是进入临界区,一个是退出临界区,这两个函数一定要成对的使用。

  1. /**
  2. * task. h
  3. *
  4. * Macro to mark the start of a critical code region.  Preemptive context
  5. * switches cannot occur when in a critical region.
  6. *
  7. * NOTE: This may alter the stack (depending on the portable implementation)
  8. * so must be used with care!
  9. *
  10. * \defgroup taskENTER_CRITICAL taskENTER_CRITICAL
  11. * \ingroup SchedulerControl
  12. */
  13. #define taskENTER_CRITICAL()               portENTER_CRITICAL() //任务级


  14. /**
  15. * task. h
  16. *
  17. * Macro to mark the end of a critical code region.  Preemptive context
  18. * switches cannot occur when in a critical region.
  19. *
  20. * NOTE: This may alter the stack (depending on the portable implementation)
  21. * so must be used with care!
  22. *
  23. * \defgroup taskEXIT_CRITICAL taskEXIT_CRITICAL
  24. * \ingroup SchedulerControl
  25. */
  26. #define taskEXIT_CRITICAL()                portEXIT_CRITICAL() //任务级
复制代码

portENTER_CRITICAL()和portEXIT_CRITICAL()也是宏定义,在portmacro.h中有定义。
  1.     #define portENTER_CRITICAL()                      vPortEnterCritical()
  2.     #define portEXIT_CRITICAL()                       vPortExitCritical()
复制代码

vPortEnterCritical()和vPortExitCritical()函数实现如下:

  1. void vPortEnterCritical( void )
  2. {
  3.     portDISABLE_INTERRUPTS();
  4.     uxCriticalNesting++;

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

  15. void vPortExitCritical( void )
  16. {
  17.     configASSERT( uxCriticalNesting );
  18.     uxCriticalNesting--;

  19.     if( uxCriticalNesting == 0 )
  20.     {
  21.         portENABLE_INTERRUPTS();
  22.     }
  23. }
复制代码

03.中断级临界区代码保护
taskENTER_CRITICAL_FROM_ISR()和taskEXIT_CRITICAL_FROM_ISR()是中断级别临界区代码保护,是用在中断服务程序中的,而且这个中断的优先级一定要低于预先设置的值。

  1. /**
  2. * task. h
  3. *
  4. * Macro to mark the start of a critical code region.  Preemptive context
  5. * switches cannot occur when in a critical region.
  6. *
  7. * NOTE: This may alter the stack (depending on the portable implementation)
  8. * so must be used with care!
  9. *
  10. * \defgroup taskENTER_CRITICAL taskENTER_CRITICAL
  11. * \ingroup SchedulerControl
  12. */
  13. #define taskENTER_CRITICAL_FROM_ISR()      portSET_INTERRUPT_MASK_FROM_ISR() //中断级

  14. /**
  15. * task. h
  16. *
  17. * Macro to mark the end of a critical code region.  Preemptive context
  18. * switches cannot occur when in a critical region.
  19. *
  20. * NOTE: This may alter the stack (depending on the portable implementation)
  21. * so must be used with care!
  22. *
  23. * \defgroup taskEXIT_CRITICAL taskEXIT_CRITICAL
  24. * \ingroup SchedulerControl
  25. */
  26. #define taskEXIT_CRITICAL_FROM_ISR( x )    portCLEAR_INTERRUPT_MASK_FROM_ISR( x ) //中断级
复制代码

在portmacro.h文件中有如下定义:

  1.     #define portSET_INTERRUPT_MASK_FROM_ISR()         ulPortRaiseBASEPRI()
  2.     #define portCLEAR_INTERRUPT_MASK_FROM_ISR( x )    vPortSetBASEPRI( x )
复制代码

ulPortRaiseBASEPRI()和vPortSetBASEPRI()相关实现

  1.     static portFORCE_INLINE void vPortSetBASEPRI( uint32_t ulBASEPRI )
  2.     {
  3.         __asm
  4.         {
  5.             /* Barrier instructions are not used as this function is only used to
  6.              * lower the BASEPRI value. */
  7. /* *INDENT-OFF* */
  8.         msr basepri, ulBASEPRI
  9. /* *INDENT-ON* */
  10.         }
  11.     }

  12.     static portFORCE_INLINE uint32_t ulPortRaiseBASEPRI( void )
  13.     {
  14.         uint32_t ulReturn, ulNewBASEPRI = configMAX_SYSCALL_INTERRUPT_PRIORITY;

  15.         __asm
  16.         {
  17.             /* Set BASEPRI to the max syscall priority to effect a critical
  18.              * section. */
  19. /* *INDENT-OFF* */
  20.             mrs ulReturn, basepri
  21.             msr basepri, ulNewBASEPRI
  22.             dsb
  23.             isb
  24. /* *INDENT-ON* */
  25.         }

  26.         return ulReturn;
  27.     }
复制代码

中断级临界区代码应用示例

VZH7[`C$VHWTLU)0[DF5F{N.png

收藏 评论0 发布时间:2022-6-17 14:44

举报

0个回答

所属标签

相似分享

官网相关资源

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版