更多分享内容可访问我的个人博客
本系列教程全部置于stm32专栏。本系列教程全部置于 stm32 专栏。
本例程参考stm32fxxx-hal crate
(如stm32f1xx-hal
)官方例程,并在官方例程的基础上增加了一些注释,修正了一些错误。可以借鉴不同型号的 stm32 例程,毕竟固件库的核是一样的。
#![no_main]
#![no_std]
use cortex_m_rt::entry;
use panic_halt as _;
use stm32f3::stm32f303;
use stm32f303::{interrupt, Interrupt, NVIC};
// since peripherals is in singleton mode, we need to use static variables
// to share peripheral register structs with interrupt handles.
static mut TIMER: Option<stm32f303::TIM7> = None;
static mut GPIOE: Option<stm32f303::GPIOE> = None;
static mut INDEX: u8 = 0;
unsafe fn get_gpioe() -> &'static mut stm32f303::GPIOE {
if let Some(ref mut gpioe) = GPIOE {
&mut *gpioe
} else {
panic!()
}
}
unsafe fn get_timer() -> &'static mut stm32f303::TIM7 {
if let Some(ref mut timer) = TIMER {
&mut *timer
} else {
panic!()
}
}
#[entry]
fn main() -> ! {
let dp = stm32f303::Peripherals::take().unwrap();
let rcc = &dp.RCC;
// enable TIM7 interrupt
unsafe {
NVIC::unmask(Interrupt::TIM7);
}
// init LED
rcc.ahbenr.modify(|_, w| w.iopeen().set_bit());
// we will