【问题标题】:Can't we use HAL_Delay() in ISR of stm32 F407VG我们不能在 stm32 F407VG 的 ISR 中使用 HAL_Delay()
【发布时间】:2021-09-27 17:37:43
【问题描述】:

我是stm32的新手,我尝试使用stm32F407VG的用户按钮实现中断。 我在中断函数中添加了HAL_Delay()。 当按下按钮时,中断服务程序开始执行,但它永远不会返回到main() 函数。

这是负责中断的代码部分:

void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
    if(GPIO_Pin==GPIO_PIN_0)
    {
        if(prev_val==false)
        {
            HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14, 1);
            prev_val=true;
        }
        else
        {
            HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14, 0);
            prev_val = false;
        }
        HAL_Delay(1000);

    }
}

【问题讨论】:

  • 您是否在 main 或 init 中调用了 HAL_Init();
  • main().

标签: stm32 interrupt-handling stm32f4discovery


【解决方案1】:

我找到了处理方法,我的中断优先级默认为0。而HAL_Delay() 的优先级也为 0。 所以我降低了外部中断的优先级,设置为1。 现在它工作正常

【讨论】:

  • 认为您在优先问题上处于正确的轨道上,我已经向您发布了答案...
  • 你能演示一下代码吗?
【解决方案2】:

注意:如果使用 ST 提供的默认 HAL 设置,调用 HAL_Init() 时 SysTick IRQ 的优先级设置为 15。

因此,您必须在 stm32f7xx_hal_conf.h 文件中或使用 HAL_InitTick(TickPriority) 函数进行更改。

另见用户手册page 31

HAL_Delay(). this function implements a delay (expressed in milliseconds) using the SysTick timer.
Care must be taken when using HAL_Delay() since this function provides an accurate delay (expressed in
milliseconds) based on a variable incremented in SysTick ISR. This means that if HAL_Delay() is called from
a peripheral ISR, then the SysTick interrupt must have highest priority (numerically lower) than the
peripheral interrupt, otherwise the caller ISR is blocked.

【讨论】:

    猜你喜欢
    • 2022-01-04
    • 2020-11-03
    • 2020-12-22
    • 2022-09-22
    • 2021-09-23
    • 2019-09-22
    • 2020-02-26
    • 2021-08-01
    • 1970-01-01
    相关资源
    最近更新 更多