作者 @飞洲人飞舟魂转载请注明出处.
实验芯片:stm32f103zet6
实验开发板:正点原子精英板(ALIENTEK ELITE)
参考资料:《The Embedded Rust Book》《 The Rust Programming Language》
实验源代码:如下
#![no_std]
#![no_main]
// pick a panicking behavior
use panic_halt as _; //定义panic为halt
use cortex_m_rt::entry;//cortex-m3内核的程序入口
use stm32f1::stm32f103;//stm32f103系列芯片的总线读写库(Peripheral Access Crate,PAC)
#[entry]
fn main() -> ! {
let peripherals = stm32f103::Peripherals::take().unwrap();
let rcc = peripherals.RCC;
rcc.apb2enr.modify(|_, w| w.iopben().bit(true));//使能PORTB时钟
let gpiob = peripherals.GPIOB;
gpiob.crl.modify(|_, w| w.mode5().bits(0b11));//将GPIOB第5引脚 设为推挽输出
loop {
gpiob.odr.modify(|_, w| w.odr5().bit(false)); //灯亮
my_delay();
gpiob.odr.modify(|_, w| w.odr5().bit(true)); //灯灭
my_delay();
}
}
fn my_delay() {//简陋的延迟函数
let mut count = 2;
while count > 0 {
count = count - 1;
let mut count2 = 120000;
while count2 > 0 {
count2 = count2 - 1;
}
}
}
实验效果:实现了GPIOB口上的小灯闪烁