Skip to content

Commit

Permalink
add blinky
Browse files Browse the repository at this point in the history
  • Loading branch information
Guozhanxin committed May 29, 2022
1 parent f19b02c commit b20820c
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 1 deletion.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,17 @@

- git clone https://github.com/Guozhanxin/rust-stm32l4-pandora.git
- rustup target add thumbv7em-none-eabihf

## blinky
- cd blinky
- cargo build --release
- arm-none-eabi-objcopy target/thumbv7em-none-eabihf/release/blinky -O binary blinky.bin

将 blinky.bin 拖入 ST-Link 虚拟 U 盘完成烧录;LED_R 周期性闪烁。

## uart
- cd uart
- cargo build --release
- arm-none-eabi-objcopy target/thumbv7em-none-eabihf/release/uart -O binary uart.bin

使用工具烧写 uart.bin 文件;打开串口调试助手,连接 ST-Link 虚拟串口;串口调试助手会显示键盘输入的内容。
uart.bin 拖入 ST-Link 虚拟 U 盘完成烧录;打开串口调试助手,连接 ST-Link 虚拟串口;串口调试助手会显示键盘输入的内容。
29 changes: 29 additions & 0 deletions blinky/.cargo/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
[target.thumbv7m-none-eabi]
# uncomment this to make `cargo run` execute programs on QEMU
# runner = "qemu-system-arm -cpu cortex-m3 -machine lm3s6965evb -nographic -semihosting-config enable=on,target=native -kernel"

[target.'cfg(all(target_arch = "arm", target_os = "none"))']
# uncomment ONE of these three option to make `cargo run` start a GDB session
# which option to pick depends on your system
# runner = "arm-none-eabi-gdb -q -x openocd.gdb"
# runner = "gdb-multiarch -q -x openocd.gdb"
# runner = "gdb -q -x openocd.gdb"

rustflags = [
# LLD (shipped with the Rust toolchain) is used as the default linker
"-C", "link-arg=-Tlink.x",

# if you run into problems with LLD switch to the GNU linker by commenting out
# this line
# "-C", "linker=arm-none-eabi-ld",

# if you need to link to pre-compiled C libraries provided by a C toolchain
# use GCC as the linker by commenting out both lines above and then
# uncommenting the three lines below
# "-C", "linker=arm-none-eabi-gcc",
# "-C", "link-arg=-Wl,-Tlink.x",
# "-C", "link-arg=-nostartfiles",
]

[build]
target = "thumbv7em-none-eabihf"
25 changes: 25 additions & 0 deletions blinky/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[package]
name = "blinky"
version = "0.1.0"
edition = "2021"
readme = "README.md"
authors = ["guozhanxin <[email protected]>"]

[dependencies]
cortex-m = "0.7"
cortex-m-rt = "0.6.5"
panic-halt = "0.2.0"
panic-semihosting = "0.6.0"
cortex-m-semihosting = "0.5.0"
stm32l4xx-hal = { version = "0.6.0", features = ["rt", "stm32l4x2"] }
nb = "1.0.0"

[profile.dev]
opt-level = 1
debug = true
lto = false

[profile.release]
opt-level = "s" # optimize for size
debug = true # symbols are nice and they don't increase the size on Flash
lto = true # better optimizations
23 changes: 23 additions & 0 deletions blinky/memory.x
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
MEMORY
{
/* NOTE K = KiBi = 1024 bytes */
/* TODO Adjust these memory regions to match your device memory layout */
FLASH : ORIGIN = 0x8000000, LENGTH = 128K
RAM : ORIGIN = 0x20000000, LENGTH = 32K
}

/* This is where the call stack will be allocated. */
/* The stack is of the full descending type. */
/* You may want to use this variable to locate the call stack and static
variables in different memory regions. Below is shown the default value */
/* _stack_start = ORIGIN(RAM) + LENGTH(RAM); */

/* You can use this symbol to customize the location of the .text section */
/* If omitted the .text section will be placed right after the .vector_table
section */
/* This is required only on microcontrollers that store some configuration right
after the vector table */
/* _stext = ORIGIN(FLASH) + 0x400; */

/* Size of the heap (in bytes) */
/* _heap_size = 1024; */
62 changes: 62 additions & 0 deletions blinky/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//! blinky Sample
//!
//! pe7 is used as pandora LED.
//!
#![deny(unsafe_code)]
#![deny(warnings)]
#![no_main]
#![no_std]

extern crate cortex_m;
#[macro_use(entry, exception)]
extern crate cortex_m_rt as rt;
extern crate nb;
extern crate panic_semihosting;

extern crate stm32l4xx_hal as hal;

use crate::hal::delay::Delay;
use crate::hal::prelude::*;
use crate::rt::ExceptionFrame;

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

let cp = cortex_m::Peripherals::take().unwrap();
let dp = hal::stm32::Peripherals::take().unwrap();

let mut flash = dp.FLASH.constrain();
let mut rcc = dp.RCC.constrain();
let mut pwr = dp.PWR.constrain(&mut rcc.apb1r1);

// clock configuration using the default settings (all clocks run at 8 MHz)
// let clocks = rcc.cfgr.freeze(&mut flash.acr);
// TRY this alternate clock configuration (clocks run at nearly the maximum frequency)
let clocks = rcc
.cfgr
.sysclk(80.mhz())
.pclk1(80.mhz())
.pclk2(80.mhz())
.freeze(&mut flash.acr, &mut pwr);

let mut gpioe = dp.GPIOE.split(&mut rcc.ahb2);
let mut led = gpioe
.pe7
.into_push_pull_output(&mut gpioe.moder, &mut gpioe.otyper);
// Get the delay provider.
let mut timer = Delay::new(cp.SYST, clocks);

loop {
led.set_low().ok();
timer.delay_ms(1000 as u32);

led.set_high().ok();
timer.delay_ms(1000 as u32);
}

}

#[exception]
fn HardFault(ef: &ExceptionFrame) -> ! {
panic!("{:#?}", ef);
}

0 comments on commit b20820c

Please sign in to comment.