From f56c70c2c6a30f4a72f36094fa7a7c6f84006930 Mon Sep 17 00:00:00 2001 From: Ernesto Cruz Date: Tue, 27 Apr 2021 01:26:40 -0500 Subject: [PATCH 1/2] porting the crate to the knurling app template https://github.com/knurling-rs/app-template --- .cargo/config | 8 --- .cargo/config.toml | 22 +++++++ Cargo.toml | 86 +++++++++++++++++++++------ examples/button_read.rs | 1 - examples/gpio_hal_blinky.rs | 2 - examples/i2c_hal_ssd1306alphabeter.rs | 4 +- examples/i2c_hal_ssd1306helloworld.rs | 4 +- examples/leds.rs | 2 - examples/serial_echo.rs | 2 - openocd.cfg | 6 -- openocd.gdb | 27 --------- openocd_program.sh | 8 --- src/bin/bitfield.rs | 13 ++++ src/bin/format.rs | 29 +++++++++ src/bin/hello.rs | 11 ++++ src/bin/levels.rs | 15 +++++ src/bin/overflow.rs | 25 ++++++++ src/bin/panic.rs | 11 ++++ src/led.rs | 2 + src/lib.rs | 41 +++++++++---- testsuite/Cargo.toml | 35 +++++++++++ testsuite/tests/test.rs | 21 +++++++ tools/capture_example_bloat.sh | 13 ---- 23 files changed, 285 insertions(+), 103 deletions(-) delete mode 100644 .cargo/config create mode 100644 .cargo/config.toml delete mode 100644 openocd.cfg delete mode 100644 openocd.gdb delete mode 100755 openocd_program.sh create mode 100644 src/bin/bitfield.rs create mode 100644 src/bin/format.rs create mode 100644 src/bin/hello.rs create mode 100644 src/bin/levels.rs create mode 100644 src/bin/overflow.rs create mode 100644 src/bin/panic.rs create mode 100644 testsuite/Cargo.toml create mode 100644 testsuite/tests/test.rs delete mode 100755 tools/capture_example_bloat.sh diff --git a/.cargo/config b/.cargo/config deleted file mode 100644 index 241a7ad..0000000 --- a/.cargo/config +++ /dev/null @@ -1,8 +0,0 @@ -[target.thumbv7em-none-eabihf] -runner = "arm-none-eabi-gdb -x openocd.gdb" -rustflags = [ - "-C", "link-arg=-Tlink.x", -] - -[build] -target = "thumbv7em-none-eabihf" diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 0000000..9c81ecd --- /dev/null +++ b/.cargo/config.toml @@ -0,0 +1,22 @@ +[target.'cfg(all(target_arch = "arm", target_os = "none"))'] +runner = "probe-run --chip STM32F429ZITx" +rustflags = [ + "-C", "linker=flip-link", + "-C", "link-arg=-Tlink.x", + "-C", "link-arg=-Tdefmt.x", + # This is needed if your flash or ram addresses are not aligned to 0x10000 in memory.x + # See https://github.com/rust-embedded/cortex-m-quickstart/pull/95 + "-C", "link-arg=--nmagic", +] + +[build] +# (`thumbv6m-*` is compatible with all ARM Cortex-M chips but using the right +# target improves performance) +# target = "thumbv6m-none-eabi" # Cortex-M0 and Cortex-M0+ +# target = "thumbv7m-none-eabi" # Cortex-M3 +# target = "thumbv7em-none-eabi" # Cortex-M4 and Cortex-M7 (no FPU) +target = "thumbv7em-none-eabihf" # Cortex-M4F and Cortex-M7F (with FPU) + +[alias] +rb = "run --bin" +rrb = "run --release --bin" diff --git a/Cargo.toml b/Cargo.toml index c1fa7a2..0fc4be9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,29 +21,79 @@ repository = "https://github.com/stm32-rs/stm32f429i-disc" version = "0.3.0" readme = "README.md" -[dependencies] -cortex-m = "0.6.2" -cortex-m-rt = "0.6.12" +[workspace] +members = ["testsuite"] -[dependencies.embedded-hal] -features = ["unproven"] -version = "0.2.3" +[dependencies] +cortex-m = "0.7.1" +cortex-m-rt = "0.6.13" +defmt = "0.2.0" +defmt-rtt = "0.2.0" +nb = "1.0.0" +panic-probe = { version = "0.2.0", features = ["print-defmt"] } +ssd1306 = "0.5.2" [dependencies.stm32f4xx-hal] -default-features = false -features = ["rt", "stm32f429"] -version = "0.8.0" +version = "0.9" +features = ["rt", "stm32f429"] + +[features] +# set logging levels here +default = [ + "defmt-default", + # "dependency-a/defmt-trace", +] -[dev-dependencies] -ssd1306 = "0.4" -nb = "1.0" -panic-halt = "0.2.0" -l3gd20 = "0.2.0" +# do NOT modify these features +defmt-default = [] +defmt-trace = [] +defmt-debug = [] +defmt-info = [] +defmt-warn = [] +defmt-error = [] +# cargo build/run [profile.dev] -debug = true +codegen-units = 1 +debug = 2 +debug-assertions = true # <- +incremental = false +opt-level = 3 # <- +overflow-checks = true # <- +# cargo test +[profile.test] +codegen-units = 1 +debug = 2 +debug-assertions = true # <- +incremental = false +opt-level = 3 # <- +overflow-checks = true # <- + +# cargo build/run --release [profile.release] -debug = true -lto = true -opt-level = "s" +codegen-units = 1 +debug = 2 +debug-assertions = false # <- +incremental = false +lto = 'fat' +opt-level = 3 # <- +overflow-checks = false # <- + +# cargo test --release +[profile.bench] +codegen-units = 1 +debug = 2 +debug-assertions = false # <- +incremental = false +lto = 'fat' +opt-level = 3 # <- +overflow-checks = false # <- + +# uncomment this to switch from the crates.io version of defmt to its git version +# check app-template's README for instructions +# [patch.crates-io] +# defmt = { git = "https://github.com/knurling-rs/defmt", rev = "use defmt version reported by `probe-run --version`" } +# defmt-rtt = { git = "https://github.com/knurling-rs/defmt", rev = "use defmt version reported by `probe-run --version`" } +# defmt-test = { git = "https://github.com/knurling-rs/defmt", rev = "use defmt version reported by `probe-run --version`" } +# panic-probe = { git = "https://github.com/knurling-rs/defmt", rev = "use defmt version reported by `probe-run --version`" } \ No newline at end of file diff --git a/examples/button_read.rs b/examples/button_read.rs index 6cf69dd..a0fe479 100644 --- a/examples/button_read.rs +++ b/examples/button_read.rs @@ -1,7 +1,6 @@ #![no_main] #![no_std] -use panic_halt as _; use stm32f429i_disc as board; diff --git a/examples/gpio_hal_blinky.rs b/examples/gpio_hal_blinky.rs index 7ccfe9d..4efe1ce 100644 --- a/examples/gpio_hal_blinky.rs +++ b/examples/gpio_hal_blinky.rs @@ -1,8 +1,6 @@ #![no_main] #![no_std] -use panic_halt as _; - use stm32f429i_disc as board; use cortex_m_rt::entry; diff --git a/examples/i2c_hal_ssd1306alphabeter.rs b/examples/i2c_hal_ssd1306alphabeter.rs index 318638a..5014ead 100644 --- a/examples/i2c_hal_ssd1306alphabeter.rs +++ b/examples/i2c_hal_ssd1306alphabeter.rs @@ -1,8 +1,6 @@ #![no_main] #![no_std] -use panic_halt as _; - use stm32f429i_disc as board; use ssd1306::displayrotation::DisplayRotation; @@ -45,7 +43,7 @@ fn main() -> ! { // Set up the SSD1306 display at I2C address 0x3c let interface = I2CDIBuilder::new().init(i2c); - let mut disp: TerminalMode<_> = Builder::new().connect(interface).into(); + let mut disp: TerminalMode<_,_> = Builder::new().connect(interface).into(); // Set display rotation to 180 degrees let _ = disp.set_rotation(DisplayRotation::Rotate180); diff --git a/examples/i2c_hal_ssd1306helloworld.rs b/examples/i2c_hal_ssd1306helloworld.rs index ee746aa..d1adf4d 100644 --- a/examples/i2c_hal_ssd1306helloworld.rs +++ b/examples/i2c_hal_ssd1306helloworld.rs @@ -1,8 +1,6 @@ #![no_main] #![no_std] -use panic_halt as _; - use stm32f429i_disc as board; use ssd1306::displayrotation::DisplayRotation; @@ -45,7 +43,7 @@ fn main() -> ! { // Set up the SSD1306 display at I2C address 0x3c let interface = I2CDIBuilder::new().init(i2c); - let mut disp: TerminalMode<_> = Builder::new().connect(interface).into(); + let mut disp: TerminalMode<_,_> = Builder::new().connect(interface).into(); // Set display rotation to 180 degrees let _ = disp.set_rotation(DisplayRotation::Rotate180); diff --git a/examples/leds.rs b/examples/leds.rs index 90ef996..1bf0afa 100644 --- a/examples/leds.rs +++ b/examples/leds.rs @@ -1,8 +1,6 @@ #![no_main] #![no_std] -use panic_halt as _; - use stm32f429i_disc as board; use cortex_m::peripheral::Peripherals; diff --git a/examples/serial_echo.rs b/examples/serial_echo.rs index 6374b62..9d4e2ff 100644 --- a/examples/serial_echo.rs +++ b/examples/serial_echo.rs @@ -1,8 +1,6 @@ #![no_main] #![no_std] -use panic_halt as _; - use stm32f429i_disc as board; use nb::block; diff --git a/openocd.cfg b/openocd.cfg deleted file mode 100644 index 85b66e8..0000000 --- a/openocd.cfg +++ /dev/null @@ -1,6 +0,0 @@ - -# Sample OpenOCD configuration for the stm32f429i-disc development board - -source [find interface/stlink.cfg] - -source [find target/stm32f4x.cfg] \ No newline at end of file diff --git a/openocd.gdb b/openocd.gdb deleted file mode 100644 index 58f5d8e..0000000 --- a/openocd.gdb +++ /dev/null @@ -1,27 +0,0 @@ -target extended-remote :3333 - -# print demangled symbols -set print asm-demangle on - -# detect unhandled exceptions, hard faults and panics -# break DefaultHandler -# break UserHardFault -break rust_begin_unwind - -# *try* to stop at the user entry point (it might be gone due to inlining) -break main - -# # send captured ITM to the file itm.fifo -# # (the microcontroller SWO pin must be connected to the programmer SWO pin) -# # 8000000 must match the core clock frequency -monitor tpiu config internal itm.txt uart off 180000000 - -# # OR: make the microcontroller SWO pin output compatible with UART (8N1) -# # 2000000 is the frequency of the SWO pin -# monitor tpiu config external uart off 180000000 2000000 - -# # enable ITM port 0 -monitor itm port 0 on - -load -continue \ No newline at end of file diff --git a/openocd_program.sh b/openocd_program.sh deleted file mode 100755 index c710cfb..0000000 --- a/openocd_program.sh +++ /dev/null @@ -1,8 +0,0 @@ -#!/bin/sh -if (( $# != 1 )); then - echo "Usage:" - echo "$0 " - exit 1 -fi - -openocd -f openocd.cfg -c "init" -c "targets" -c "reset halt" -c "program $1 verify reset exit" diff --git a/src/bin/bitfield.rs b/src/bin/bitfield.rs new file mode 100644 index 0000000..8fa4909 --- /dev/null +++ b/src/bin/bitfield.rs @@ -0,0 +1,13 @@ +#![no_main] +#![no_std] + +use stm32f429i_disc as _; // global logger + panicking-behavior + memory layout + +#[cortex_m_rt::entry] +fn main() -> ! { + // value of the FREQUENCY register (nRF52840 device; RADIO peripheral) + let frequency: u32 = 276; + defmt::debug!("FREQUENCY: {0=0..7}, MAP: {0=8..9}", frequency); + + stm32f429i_disc::exit() +} diff --git a/src/bin/format.rs b/src/bin/format.rs new file mode 100644 index 0000000..0689f69 --- /dev/null +++ b/src/bin/format.rs @@ -0,0 +1,29 @@ +#![no_main] +#![no_std] + +use defmt::Format; +use stm32f429i_disc as _; // global logger + panicking-behavior + memory layout // <- derive attribute + +#[derive(Format)] +struct S1 { + x: u8, + y: T, +} + +#[derive(Format)] +struct S2 { + z: u8, +} + +#[cortex_m_rt::entry] +fn main() -> ! { + let s = S1 { + x: 42, + y: S2 { z: 43 }, + }; + defmt::info!("s={:?}", s); + let x = 42; + defmt::info!("x={=u8}", x); + + stm32f429i_disc::exit() +} diff --git a/src/bin/hello.rs b/src/bin/hello.rs new file mode 100644 index 0000000..7928db3 --- /dev/null +++ b/src/bin/hello.rs @@ -0,0 +1,11 @@ +#![no_main] +#![no_std] + +use stm32f429i_disc as _; // global logger + panicking-behavior + memory layout + +#[cortex_m_rt::entry] +fn main() -> ! { + defmt::info!("Hello, world!"); + + stm32f429i_disc::exit() +} diff --git a/src/bin/levels.rs b/src/bin/levels.rs new file mode 100644 index 0000000..0614272 --- /dev/null +++ b/src/bin/levels.rs @@ -0,0 +1,15 @@ +#![no_main] +#![no_std] + +use stm32f429i_disc as _; // global logger + panicking-behavior + memory layout + +#[cortex_m_rt::entry] +fn main() -> ! { + defmt::info!("info"); + defmt::trace!("trace"); + defmt::warn!("warn"); + defmt::debug!("debug"); + defmt::error!("error"); + + stm32f429i_disc::exit() +} diff --git a/src/bin/overflow.rs b/src/bin/overflow.rs new file mode 100644 index 0000000..6d15518 --- /dev/null +++ b/src/bin/overflow.rs @@ -0,0 +1,25 @@ +#![no_main] +#![no_std] + +use stm32f429i_disc as _; // global logger + panicking-behavior + memory layout + +#[cortex_m_rt::entry] +fn main() -> ! { + ack(10, 10); + stm32f429i_disc::exit() +} + +fn ack(m: u32, n: u32) -> u32 { + defmt::info!("ack(m={=u32}, n={=u32})", m, n); + let mut big = [2; 512]; + if m == 0 { + n + 1 + } else { + big[100] += 1; + if n == 0 { + ack(m - 1, 1) + } else { + ack(m - 1, ack(m, n - 1)) + } + } +} diff --git a/src/bin/panic.rs b/src/bin/panic.rs new file mode 100644 index 0000000..ce1b1fe --- /dev/null +++ b/src/bin/panic.rs @@ -0,0 +1,11 @@ +#![no_main] +#![no_std] + +use stm32f429i_disc as _; // global logger + panicking-behavior + memory layout + +#[cortex_m_rt::entry] +fn main() -> ! { + defmt::info!("main"); + + defmt::panic!() +} diff --git a/src/led.rs b/src/led.rs index fbb6203..c5fffe7 100644 --- a/src/led.rs +++ b/src/led.rs @@ -1,5 +1,7 @@ //! On-board user LEDs +use stm32f4xx_hal as hal; // memory layout + use hal::prelude::*; use hal::gpio::gpiog::{self, PG, PG13, PG14}; diff --git a/src/lib.rs b/src/lib.rs index 5849bc7..6af26c3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,14 +1,35 @@ #![no_std] -#![allow(non_camel_case_types)] -pub extern crate stm32f4xx_hal as hal; +use core::sync::atomic::{AtomicUsize, Ordering}; -extern crate cortex_m; -extern crate cortex_m_rt; - -pub use crate::hal::stm32::interrupt::*; -pub use crate::hal::stm32::*; -pub use crate::hal::*; -pub use cortex_m::*; -pub use cortex_m_rt::*; pub mod led; +pub use cortex_m_rt::*; +pub use cortex_m::*; +pub use crate::hal::*; +pub use crate::hal::stm32::*; +pub use crate::hal::stm32::interrupt::*; +pub use defmt_rtt as _; // global logger +pub use panic_probe as _; +pub use stm32f4xx_hal as hal; // memory layout + +// same panicking *behavior* as `panic-probe` but doesn't print a panic message +// this prevents the panic message being printed *twice* when `defmt::panic` is invoked +#[defmt::panic_handler] +fn panic() -> ! { + cortex_m::asm::udf() +} + +static COUNT: AtomicUsize = AtomicUsize::new(0); +defmt::timestamp!("{=usize}", { + // NOTE(no-CAS) `timestamps` runs with interrupts disabled + let n = COUNT.load(Ordering::Relaxed); + COUNT.store(n + 1, Ordering::Relaxed); + n +}); + +/// Terminates the application and makes `probe-run` exit with exit-code = 0 +pub fn exit() -> ! { + loop { + cortex_m::asm::bkpt(); + } +} diff --git a/testsuite/Cargo.toml b/testsuite/Cargo.toml new file mode 100644 index 0000000..0fd027b --- /dev/null +++ b/testsuite/Cargo.toml @@ -0,0 +1,35 @@ +[package] +authors = ["Ernesto Cruz "] +name = "testsuite" +publish = false +edition = "2018" +version = "0.1.0" + +[[test]] +name = "test" +harness = false + +[dependencies] +stm32f429i-disc = { path = ".." } +cortex-m = "0.7.1" +cortex-m-rt = "0.6.12" +defmt = "0.2.0" +defmt-rtt = "0.2.0" +defmt-test = "0.2.0" +panic-probe = { version = "0.2.0", features = ["print-defmt"] } + +[features] +# set logging levels here +default = [ + # in tests, enable all logs + "defmt-trace", + # "dependency-a/defmt-trace", +] + +# do NOT modify these features +defmt-default = [] +defmt-trace = [] +defmt-debug = [] +defmt-info = [] +defmt-warn = [] +defmt-error = [] diff --git a/testsuite/tests/test.rs b/testsuite/tests/test.rs new file mode 100644 index 0000000..472bd56 --- /dev/null +++ b/testsuite/tests/test.rs @@ -0,0 +1,21 @@ +#![no_std] +#![no_main] + +use stm32f429i_disc as _; // memory layout + panic handler + +// See https://crates.io/crates/defmt-test/0.1.0 for more documentation (e.g. about the 'state' +// feature) +#[defmt_test::tests] +mod tests { + use defmt::{assert, assert_eq}; + + #[test] + fn assert_true() { + assert!(true) + } + + #[test] + fn assert_eq() { + assert_eq!(42, 42, "TODO: write actual tests") + } +} diff --git a/tools/capture_example_bloat.sh b/tools/capture_example_bloat.sh deleted file mode 100755 index 7a7a6c4..0000000 --- a/tools/capture_example_bloat.sh +++ /dev/null @@ -1,13 +0,0 @@ -#!/bin/bash - -filename="bloat_log_"`date -Iminutes`".txt" - -for i in `find examples -name "*.rs"`; do - name=$(echo $i | sed -e "s,examples/,,g" -e "s,\.rs,,g") - echo "Processing example $name" - echo >>$filename - echo "Bloat for example $name" >>$filename - cargo bloat -n40 --release --example $name >>$filename -done - -echo "Captures bloat for all examples into $filename" From dfb60ed83df87bf782d92523692074990e83042f Mon Sep 17 00:00:00 2001 From: Ernesto Cruz Date: Tue, 27 Apr 2021 23:56:20 -0500 Subject: [PATCH 2/2] removing warnings --- examples/i2c_hal_ssd1306alphabeter.rs | 2 +- examples/i2c_hal_ssd1306helloworld.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/i2c_hal_ssd1306alphabeter.rs b/examples/i2c_hal_ssd1306alphabeter.rs index 5014ead..081391e 100644 --- a/examples/i2c_hal_ssd1306alphabeter.rs +++ b/examples/i2c_hal_ssd1306alphabeter.rs @@ -39,7 +39,7 @@ fn main() -> ! { .set_open_drain(); // Setup I2C1 using the above defined pins at 400kHz bitrate (fast mode) - let i2c = I2c::i2c1(p.I2C1, (scl, sda), 400.khz(), clocks); + let i2c = I2c::new(p.I2C1, (scl, sda), 400.khz(), clocks); // Set up the SSD1306 display at I2C address 0x3c let interface = I2CDIBuilder::new().init(i2c); diff --git a/examples/i2c_hal_ssd1306helloworld.rs b/examples/i2c_hal_ssd1306helloworld.rs index d1adf4d..88033bb 100644 --- a/examples/i2c_hal_ssd1306helloworld.rs +++ b/examples/i2c_hal_ssd1306helloworld.rs @@ -39,7 +39,7 @@ fn main() -> ! { .set_open_drain(); // Setup I2C1 using the above defined pins at 400kHz bitrate (fast mode) - let i2c = I2c::i2c1(p.I2C1, (scl, sda), 400.khz(), clocks); + let i2c = I2c::new(p.I2C1, (scl, sda), 400.khz(), clocks); // Set up the SSD1306 display at I2C address 0x3c let interface = I2CDIBuilder::new().init(i2c);