更改 运行 中断处理程序的优先级?

Change priority level level of running interrupt handler?

我正在尝试在 cortex-m3 控制器(尤其是 STM32L151)上实现以下伪代码

void SysTick_Handler() {
    do_high_priority_periodic_tasks(); // not to be interrupted
    lower_interrupt_priority();
    do_low_priority_periodic_tasks();  // these may be interrupted
}

换句话说,运行优先级为0的第一部分,然后以某种方式将当前中断优先级降低到15,以便其余部分可以被其他硬件中断抢占。

一个想法是将 do_low_priority_periodic_tasks(); 移动到一个单独的中断处理程序中,并通过 NVIC_SetPendingIRQ() 调用此处理程序,从而在 NVIC->ISPR[] 寄存器中设置一个挂起位。这样,另一个中断将立即跟在 SysTick 之后,除非有任何优先级在 0 到 14 之间的挂起。

#define LOWPRIO_IRQn 55
void IRQ55_Handler() {
    do_low_priority_periodic_tasks();  // these may be interrupted
}

void SysTick_Handler() {
    do_high_priority_periodic_tasks(); // not to be interrupted
    NVIC_SetPendingIRQ(LOWPRIO_IRQ);
}

void main() {
    HAL_Init();
    HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
    HAL_NVIC_SetPriority(LOWPRIO_IRQn, 15, 0);
    HAL_NVIC_EnableIRQ(LOWPRIO_IRQn);
    while(1) {
        /* main loop */
    }
}

我选择了 IRQ 55,因为它没有被我的控制器占用,它将是 STM32L162 上的 AES 中断处理程序,但我对此有点担心。我是否应该选择一个不同的 IRQ,也许是一个未使用的 DMA 通道中断?使用在 Cortex-M3 内核中定义但在 STM32L 系列中没有定义的中断 57-67 是否安全?有没有更好的方法呢?

Is it safe to use interrupts 57-67, which are defined in the Cortex-M3 core, but not on the STM32L series?

没有。您的 NVIC 可能实际上没有实现它们。

但是 PendSV 正是为这个任务而生的:

void SysTick_Handler() {
  do_high_priority_periodic_tasks(); // not to be interrupted
  // Set the PENDSVSET to trigger a PendSV exception
  SCB->ICSR |= SCB_ICSR_PENDSVSET_Msk;
}

void PendSV_Handler() {
    do_low_priority_periodic_tasks();  // these may be interrupted
}

另见关于 PendSV 的answer