RUST编写STM32F4嵌入式程序
文章目录Toml 文件跑马灯代码Toml 文件[package]authors = ["Andy"]edition = "2018"readme = "README.md"name = "stm32app"version = "0.1.0"[dependencies]cortex-m = "*"cortex-m-rt = "*"cortex-m-semihosting = "*"panic-hal
·
Toml 文件
[package]
authors = ["Andy"]
edition = "2018"
readme = "README.md"
name = "stm32app"
version = "0.1.0"
[dependencies]
cortex-m = "*"
cortex-m-rt = "*"
cortex-m-semihosting = "*"
panic-halt = "*"
# Uncomment for the panic example.
# panic-itm = "0.4.1"
# Uncomment for the allocator example.
# alloc-cortex-m = "0.3.5"
# Uncomment for the device example.
# Update `memory.x`, set target to `thumbv7em-none-eabihf` in `.cargo/config`,
# and then use `cargo build --examples device` to build it.
[dependencies.stm32f4]
features = ["stm32f407", "rt"]
version = "*"
[profile.release]
# optimize for size ('z' would optimize even more)
opt-level = 's'
# link with link time optimization (lto).
lto = true
# enable debugging in release mode.
debug = true
跑马灯代码
#![no_std]
#![no_main]
extern crate stm32f4;
extern crate panic_halt;
extern crate cortex_m_rt;
use cortex_m_rt::entry;
use stm32f4::stm32f407;
// use `main` as the entry point of this application
#[entry]
fn main() -> ! {
// get handles to the hardware
let peripherals = stm32f407::Peripherals::take().unwrap();
let pf = &peripherals.GPIOF;
let rcc = &peripherals.RCC;
// enable system clock RCC for gpiof
rcc.ahb1enr.write(|w|{
w.gpiofen().set_bit()
});
// config GPIOF pin9 and pin10 for led
{
// 1. mode 01 通用输出
pf.moder.write(|w|{
w.moder9().output()
.moder10().output()
});
// 2. otype 0 推挽输出
pf.otyper.write(|w|{
w.ot9().push_pull()
.ot10().push_pull()
});
// 3. ospeed 10, 50MHz high speed
pf.ospeedr.write(|w|{
w.ospeedr9().high_speed()
.ospeedr10().high_speed()
});
// 4. pup 01, 上拉
pf.pupdr.write(|w|{
w.pupdr9().pull_up()
.pupdr10().pull_up()
});
// 5. idr/odr/bsrr
// by condition to read or set
pf.odr.reset();
}
loop{
pf.odr.write(|w| {
w.odr9().set_bit()
.odr10().clear_bit()
});
cortex_m::asm::delay(168*10000*3);
pf.odr.write(|w| {
w.odr9().clear_bit()
.odr10().set_bit()
});
cortex_m::asm::delay(168*10000*3);
}
}
折腾了一个星期才跑起来,意义重大,记录一下。
具体教程见后续…
更多推荐
所有评论(0)