Skip to content

Instantly share code, notes, and snippets.

@thelinuxpoint
Last active July 20, 2022 19:09
Show Gist options
  • Save thelinuxpoint/942a12a2041166cf19e13e405e58b55f to your computer and use it in GitHub Desktop.
Save thelinuxpoint/942a12a2041166cf19e13e405e58b55f to your computer and use it in GitHub Desktop.
Rust STM32f103c8 external interrupt demo
#![no_std]
#![no_main]
use panic_halt;
use cortex_m::asm::delay;
use cortex_m_rt::entry; // The runtime
use embedded_hal::digital::v2::OutputPin; // the `set_high/low`function
use stm32f1xx_hal::{delay::Delay, pac, prelude::*}; // STM32F1 specific functions
use stm32f1xx_hal::usb::{Peripheral, UsbBus};
use stm32f1xx_hal::pac::{Interrupt};
use stm32f1xx_hal::{stm32};
use stm32f1xx_hal::pac::interrupt;
use stm32f1xx_hal::gpio::*;
use core::panic::PanicInfo;
use core::mem::MaybeUninit;
static mut LED: MaybeUninit<stm32f1xx_hal::gpio::gpioc::PC13<Output<PushPull>>> = MaybeUninit::uninit();
static mut INT_PIN: MaybeUninit<stm32f1xx_hal::gpio::gpioa::PA7<Input<Floating>>> = MaybeUninit::uninit();
#[interrupt]
fn EXTI9_5() {
let led = unsafe { &mut *LED.as_mut_ptr() };
let int_pin = unsafe { &mut *INT_PIN.as_mut_ptr() };
if int_pin.check_interrupt() {
led.toggle();
// if we don't clear this bit, the ISR would trigger indefinitely
int_pin.clear_interrupt_pending_bit();
}
}
#[entry]
fn main() -> ! {
let mut dp = pac::Peripherals::take().unwrap();
let cp = cortex_m::Peripherals::take().unwrap();
let mut rcc = dp.RCC.constrain();
let mut gpioa = dp.GPIOA.split(&mut rcc.apb2);
let mut gpiob = dp.GPIOB.split(&mut rcc.apb2);
let mut gpioc = dp.GPIOC.split(&mut rcc.apb2);
let mut flash = dp.FLASH.constrain();
// let mut led = gpioc.pc13.into_push_pull_output(&mut gpioc.crh);
// let mut led_but = gpiob.pb5.into_push_pull_output(&mut gpiob.crl);
// led_but.set_high().ok();
let mut afio = dp.AFIO.constrain(&mut rcc.apb2);
let led = unsafe { &mut *LED.as_mut_ptr() };
*led = gpioc.pc13.into_push_pull_output(&mut gpioc.crh);
let int_pin = unsafe { &mut *INT_PIN.as_mut_ptr() };
*int_pin = gpioa.pa7.into_floating_input(&mut gpioa.crl);
int_pin.make_interrupt_source(&mut afio);
int_pin.trigger_on_edge(&mut dp.EXTI, Edge::RISING_FALLING);
int_pin.enable_interrupt(&mut dp.EXTI);
let clocks = rcc
.cfgr
.use_hse(8.mhz())
.sysclk(48.mhz())
.pclk1(24.mhz())
.freeze(&mut flash.acr);
unsafe {
pac::NVIC::unmask(pac::Interrupt::EXTI9_5);
}
loop {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment