无法在PE0 STM32上接收中断

Cannot receive interrupt on PE0 STM32

我试图在我的 STM32F407G-DISC1 上接收简单的中断,但我似乎无法配置 EXTI0 中断通道以从 PE0 接收,相反它似乎只在 PA0 发生变化时触发。

当我将 3.3V 引脚短接到 PA0 时,它打印 "Interrupt Received",但是当我将 3.3V 短接到 PE0 时,没有任何反应。

是否缺少某些配置调用?

构建脚本和其他配置文件基于 the cortex-m quickstart,稍作修改。如果这些是相关的,我也可以 post。

这是我的代码:

# Cargo.toml
[package]
name = "embedded-interrupt"
version = "0.1.0"
edition = "2018"

[dependencies]
cortex-m = "0.6.0"
cortex-m-rt = "0.6.8"
cortex-m-semihosting = "0.3.3"
panic-semihosting = "0.5"
hal = {package = "stm32f4xx-hal", features = ["stm32f407", "rt"], version = "0.5" }
// main.rs
#![no_std]
#![no_main]

extern crate panic_semihosting;

use cortex_m_rt::entry;
use cortex_m::interrupt::{Mutex, free};
use cortex_m_semihosting::hprintln;
use hal::prelude::*;
use hal::stm32::{interrupt, Interrupt, EXTI};
use hal::gpio::{ExtiPin, Edge, gpioe, Input, PullDown};

use core::cell::RefCell;

static PE0: Mutex<RefCell<Option<gpioe::PE0<Input<PullDown>>>>> = Mutex::new(RefCell::new(None));
static EXT_INTER: Mutex<RefCell<Option<EXTI>>> = Mutex::new(RefCell::new(None));

#[entry]
fn start() -> ! {

    let p = hal::stm32::Peripherals::take().unwrap();
    let c = cortex_m::Peripherals::take().unwrap();

    let gpioe = p.GPIOE.split();
    let mut syscfg = p.SYSCFG;
    let mut exti = p.EXTI;
    let mut nvic = c.NVIC;

    nvic.enable(Interrupt::EXTI0);

    let mut pe0 = gpioe.pe0.into_pull_down_input();
    pe0.make_interrupt_source(&mut syscfg);
    pe0.enable_interrupt(&mut exti);
    pe0.trigger_on_edge(&mut exti, Edge::FALLING);

    free(|cs| {
        let cell = PE0.borrow(&cs);
        *cell.borrow_mut() = Some(pe0);

        let cell = EXT_INTER.borrow(&cs);
        *cell.borrow_mut() = Some(exti);
    });

    loop{}
}

#[interrupt]
fn EXTI0() {
    hprintln!("Interrupt received").unwrap();

    // clear the interrupt
    free(|cs| {
        let pe0 = PE0.borrow(&cs);
        let exti = EXT_INTER.borrow(&cs);

        if let (Some(pe0), Some(mut exti)) = (pe0.borrow_mut().as_mut(), exti.borrow_mut().as_mut()) {
            pe0.clear_interrupt_pending_bit(&mut exti);
        }
    });

}

显然,必须启用系统配置控制器时钟才能更改 syscfg 寄存器。添加 rcc.apb2enr.modify(|_, w| w.syscfgen().enabled()); 使其工作。完整来源:

#![no_std]
#![no_main]

extern crate panic_semihosting;

use cortex_m::interrupt::{free, Mutex};
use cortex_m_rt::entry;
use cortex_m_semihosting::hprintln;
use hal::gpio::{gpioe, Edge, ExtiPin, Input, PullDown};
use hal::prelude::*;
use hal::stm32::{interrupt, Interrupt, EXTI};

use core::cell::RefCell;

static PE0: Mutex<RefCell<Option<gpioe::PE0<Input<PullDown>>>>> = Mutex::new(RefCell::new(None));
static EXT_INTER: Mutex<RefCell<Option<EXTI>>> = Mutex::new(RefCell::new(None));

#[entry]
fn start() -> ! {
    let p = hal::stm32::Peripherals::take().unwrap();
    let c = cortex_m::Peripherals::take().unwrap();

    let gpioe = p.GPIOE.split();
    let rcc = p.RCC;
    let mut syscfg = p.SYSCFG;
    let mut exti = p.EXTI;
    let mut nvic = c.NVIC;

    rcc.apb2enr.modify(|_, w| w.syscfgen().enabled()); // important

    nvic.enable(Interrupt::EXTI0);

    let mut pe0 = gpioe.pe0.into_pull_down_input();
    pe0.make_interrupt_source(&mut syscfg);
    pe0.enable_interrupt(&mut exti);
    pe0.trigger_on_edge(&mut exti, Edge::FALLING);

    free(|cs| {
        let cell = PE0.borrow(&cs);
        *cell.borrow_mut() = Some(pe0);

        let cell = EXT_INTER.borrow(&cs);
        *cell.borrow_mut() = Some(exti);
    });

    loop {}
}

#[interrupt]
fn EXTI0() {
    hprintln!("Interrupt received").unwrap();

    // clear the interrupt
    free(|cs| {
        let pe0 = PE0.borrow(&cs);
        let exti = EXT_INTER.borrow(&cs);

        if let (Some(pe0), Some(mut exti)) = (pe0.borrow_mut().as_mut(), exti.borrow_mut().as_mut())
        {
            pe0.clear_interrupt_pending_bit(&mut exti);
        }
    });
}