GD32如何配置中断优先级分组以及中断优先级

[复制链接]
3060|2
 楼主| 聚沃科技 发表于 2024-6-7 13:35 | 显示全部楼层 |阅读模式
使用GD32 MCU的过程中,大家可能会有以下疑问:中断优先级如何配置和使用?
本文将会为大家解析中断优先级分组以及中断优先级的配置使用:
中断优先级分组配置
一个GD32 MCU系统需要大家明确系统中使用的中断优先级分组,避免中断优先级配置越界导致一些不符合预期的中断现象。
中断优先级分组可采用以下函数接口,其中有4bit可用于中断优先级分组,如果全用于抢占优先级,则可以配置0-15的优先级,如果2位用于抢占,2位用于次优先级,则抢占优先级可以配置0-3,此优先级可以配置0-3
  1. C
  2. /*!
  3.     \brief      set the priority group
  4.     \param[in]  nvic_prigroup: the NVIC priority group
  5.       \arg        NVIC_PRIGROUP_PRE0_SUB4:0 bits for pre-emption priority 4 bits for subpriority
  6.       \arg        NVIC_PRIGROUP_PRE1_SUB3:1 bits for pre-emption priority 3 bits for subpriority
  7.       \arg        NVIC_PRIGROUP_PRE2_SUB2:2 bits for pre-emption priority 2 bits for subpriority
  8.       \arg        NVIC_PRIGROUP_PRE3_SUB1:3 bits for pre-emption priority 1 bits for subpriority
  9.       \arg        NVIC_PRIGROUP_PRE4_SUB0:4 bits for pre-emption priority 0 bits for subpriority
  10.     \param[out] none
  11.     \retval     none
  12. */
  13. void nvic_priority_group_set(uint32_t nvic_prigroup)
  14. {
  15.     /* set the priority group value */
  16.     SCB->AIRCR = NVIC_AIRCR_VECTKEY_MASK | nvic_prigroup;
  17. }
注意:如果中断优先级配置为2位抢占和2位此优先级的话,抢占优先级配置为4(二进制为100b),优先级配置越界,实际配置进去的优先级为0,最高优先级,因而明确中断优先级分组非常重要。
中断优先级配置
中断优先级配置采用以下函数。
  1. C
  2. /*!
  3.     \brief      enable NVIC request
  4.     \param[in]  nvic_irq: the NVIC interrupt request, detailed in IRQn_Type
  5.     \param[in]  nvic_irq_pre_priority: the pre-emption priority needed to set
  6.     \param[in]  nvic_irq_sub_priority: the subpriority needed to set
  7.     \param[out] none
  8.     \retval     none
  9. */
  10. void nvic_irq_enable(uint8_t nvic_irq, uint8_t nvic_irq_pre_priority,
  11.                      uint8_t nvic_irq_sub_priority)
  12. {
  13.     uint32_t temp_priority = 0x00U, temp_pre = 0x00U, temp_sub = 0x00U;
  14.     /* use the priority group value to get the temp_pre and the temp_sub */
  15.     if(((SCB->AIRCR) & (uint32_t)0x700U)==NVIC_PRIGROUP_PRE0_SUB4){
  16.         temp_pre=0U;
  17.         temp_sub=0x4U;
  18.     }else if(((SCB->AIRCR) & (uint32_t)0x700U)==NVIC_PRIGROUP_PRE1_SUB3){
  19.         temp_pre=1U;
  20.         temp_sub=0x3U;
  21.     }else if(((SCB->AIRCR) & (uint32_t)0x700U)==NVIC_PRIGROUP_PRE2_SUB2){
  22.         temp_pre=2U;
  23.         temp_sub=0x2U;
  24.     }else if(((SCB->AIRCR) & (uint32_t)0x700U)==NVIC_PRIGROUP_PRE3_SUB1){
  25.         temp_pre=3U;
  26.         temp_sub=0x1U;
  27.     }else if(((SCB->AIRCR) & (uint32_t)0x700U)==NVIC_PRIGROUP_PRE4_SUB0){
  28.         temp_pre=4U;
  29.         temp_sub=0x0U;
  30.     }else{
  31.         nvic_priority_group_set(NVIC_PRIGROUP_PRE2_SUB2);
  32.         temp_pre=2U;
  33.         temp_sub=0x2U;
  34.     }
  35.     /* get the temp_priority to fill the NVIC->IP register */
  36.     temp_priority = (uint32_t)nvic_irq_pre_priority << (0x4U - temp_pre);
  37.     temp_priority |= nvic_irq_sub_priority &(0x0FU >> (0x4U - temp_sub));
  38.     temp_priority = temp_priority << 0x04U;
  39.     NVIC->IP[nvic_irq] = (uint8_t)temp_priority;
  40.     /* enable the selected IRQ */
  41.     NVIC->ISER[nvic_irq >> 0x05U] = (uint32_t)0x01U << (nvic_irq & (uint8_t)0x1FU);
  42. }
nvic_irq为中断号,中断号可以通过gd32f30x.h获取,如下图所示,nvic_irq_pre_priority为抢占优先级配置,nvic_irq_sub_priority为此优先级配置,注意优先级配置要根据优先级分组进行配置,不要越界哦。
图片1.png

本教程由GD32 MCU方案商聚沃科技原创发布,了解更多GD32 MCU教程,关注聚沃科技官网,GD32MCU技术交流群:859440462

FranklinUNK 发表于 2024-6-20 18:51 | 显示全部楼层
数值越小优先级越高
烟雨蒙蒙520 发表于 2024-7-31 22:50 | 显示全部楼层
如果中断优先级配置为2位抢占和2位此优先级的话,抢占优先级配置为4(二进制为100b),优先级配置越界,实际配置进去的优先级为0,最高优先级,因而明确中断优先级分组非常重要。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

170

主题

190

帖子

13

粉丝
快速回复 在线客服 返回列表 返回顶部