From be5dd9632c92178e45b1967bbe7dd5ea01b14a72 Mon Sep 17 00:00:00 2001 From: wieerwill Date: Mon, 25 May 2026 18:41:46 +0200 Subject: [PATCH] add new hardware peripheral --- Cargo.lock | 2 ++ README.md | 34 ++++++++++++++++++++++++----- docs/hardware/blue-pill.md | 31 +++++++++++++++++++++----- examples/README.md | 4 ++-- examples/button-input/src/main.rs | 13 ++++++++--- examples/embassy-blinky/Cargo.toml | 1 + examples/embassy-blinky/src/main.rs | 1 + examples/embassy-button/Cargo.toml | 1 + examples/embassy-button/src/main.rs | 10 ++++++--- justfile | 12 ++++++++++ 10 files changed, 91 insertions(+), 18 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8437a37..fb17937 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -296,6 +296,7 @@ dependencies = [ name = "embassy-blinky" version = "0.1.0" dependencies = [ + "cortex-m", "cortex-m-rt", "defmt 1.1.0", "defmt-rtt", @@ -309,6 +310,7 @@ dependencies = [ name = "embassy-button" version = "0.1.0" dependencies = [ + "cortex-m", "cortex-m-rt", "defmt 1.1.0", "defmt-rtt", diff --git a/README.md b/README.md index a4228e7..492a5da 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,29 @@ See [blue-pill.md](docs/hardware/blue-pill.md) for wiring notes. +## Attached Hardware + +- internal LED: `PC13` +- RGB LED: + - `PA5` = red + - `PA6` = green + - `PA7` = blue + - `PA4` = drive low for the LED return path +- 5-way button board: + - `PB12` = GND, drive low + - `PA10` = VCC, drive high + - `PB13` = right + - `PB14` = down + - `PB15` = left + - `PA8` = center + - `PA9` = up +- HMC5883L module: + - `PB4` = VCC, drive high + - `PB5` = GND, drive low + - `PB6` = SCL + - `PB7` = SDA + - `PB8` = DRDY + ## Important Target Note - Blue Pill `STM32F103` is Cortex-M3 and uses `thumbv7m-none-eabi`. @@ -71,14 +94,15 @@ probe-rs chip list | rg STM32F103 - `blinky-basic` - plain `no_std` blink with a simple delay - `blinky-timer` - periodic blink driven by a timer abstraction -- `button-input` - poll a button on `PA0`, mirror state on the LED, log transitions +- `button-input` - poll the center button on `PA8`, mirror state on the LED, log transitions - `embassy-blinky` - minimal async blink with Embassy -- `embassy-button` - minimal Embassy polling loop for a button on `PA0` +- `embassy-button` - minimal Embassy polling loop for the center button on `PA8` -The button examples assume a simple external button: +The button examples assume the 5-way board is wired like this: -- one side to `PA0` -- one side to `3V3` +- `PB12` forced low for ground +- `PA10` forced high for power +- `PA8` used as the center button input - internal pull-down enabled in firmware - common ground still required for the probe diff --git a/docs/hardware/blue-pill.md b/docs/hardware/blue-pill.md index f4206df..a3b7cd0 100644 --- a/docs/hardware/blue-pill.md +++ b/docs/hardware/blue-pill.md @@ -13,12 +13,33 @@ - `set_low()` usually turns it on - `set_high()` usually turns it off -## Button Assumption +## RGB LED -- The repo button examples use `PA0` -- Assumed wiring: - - button between `PA0` and `3V3` - - firmware enables internal pull-down +- `PA5` = red +- `PA6` = green +- `PA7` = blue +- `PA4` = drive low for the LED return path + +## 5-Way Button Board + +- `PB12` = GND, drive low +- `PA10` = VCC, drive high +- `PB13` = right +- `PB14` = down +- `PB15` = left +- `PA8` = center +- `PA9` = up +- Assumption: the button inputs read high when pressed +- The button examples use `PA8` as the default center button +- TODO: flip the input polarity if your module is active-low instead + +## HMC5883L Module + +- `PB4` = VCC, drive high +- `PB5` = GND, drive low +- `PB6` = SCL +- `PB7` = SDA +- `PB8` = DRDY ## Probe Wiring diff --git a/examples/README.md b/examples/README.md index 139ee9c..f1d2edf 100644 --- a/examples/README.md +++ b/examples/README.md @@ -6,9 +6,9 @@ Each example is its own Cargo package. - `blinky-basic` - simple delay, simple LED ownership story - `blinky-timer` - periodic blink with a timer abstraction -- `button-input` - poll `PA0`, mirror state to `PC13`, log transitions +- `button-input` - poll the center button on `PA8`, mirror state to `PC13`, log transitions - `embassy-blinky` - minimal Embassy executor and async blink -- `embassy-button` - minimal Embassy polling loop for `PA0` +- `embassy-button` - minimal Embassy polling loop for the center button on `PA8` ## Build One Example diff --git a/examples/button-input/src/main.rs b/examples/button-input/src/main.rs index 987ee44..5ac5adc 100644 --- a/examples/button-input/src/main.rs +++ b/examples/button-input/src/main.rs @@ -17,18 +17,25 @@ fn main() -> ! { let mut rcc = dp.RCC.constrain(); let mut gpioa = dp.GPIOA.split(&mut rcc); + let mut gpiob = dp.GPIOB.split(&mut rcc); let mut gpioc = dp.GPIOC.split(&mut rcc); - let button = gpioa.pa0.into_pull_down_input(&mut gpioa.crl); + // Assumes the center switch line goes high when pressed. + let button = gpioa.pa8.into_pull_down_input(&mut gpioa.crh); + let mut vcc = gpioa.pa10.into_push_pull_output(&mut gpioa.crh); + let mut gnd = gpiob.pb12.into_push_pull_output(&mut gpiob.crh); let mut led = gpioc.pc13.into_push_pull_output(&mut gpioc.crh); let mut delay = cp.SYST.delay(&rcc.clocks); + + gnd.set_low(); + vcc.set_high(); let mut was_pressed = button.is_high(); led.set_high(); - info!("button-input: PA0 button to 3V3 with internal pull-down"); + info!("button-input: PA8 center button, PA10 high, PB12 low"); loop { - let pressed = button.is_high(); + let pressed = !button.is_high(); if pressed != was_pressed { was_pressed = pressed; diff --git a/examples/embassy-blinky/Cargo.toml b/examples/embassy-blinky/Cargo.toml index c04a020..a12f1ff 100644 --- a/examples/embassy-blinky/Cargo.toml +++ b/examples/embassy-blinky/Cargo.toml @@ -6,6 +6,7 @@ publish = false build = "build.rs" [dependencies] +cortex-m.workspace = true cortex-m-rt.workspace = true defmt.workspace = true defmt-rtt.workspace = true diff --git a/examples/embassy-blinky/src/main.rs b/examples/embassy-blinky/src/main.rs index ac62e60..b7fcaa4 100644 --- a/examples/embassy-blinky/src/main.rs +++ b/examples/embassy-blinky/src/main.rs @@ -4,6 +4,7 @@ use defmt::info; use defmt_rtt as _; +use cortex_m as _; use embassy_executor::Spawner; use embassy_stm32::gpio::{Level, Output, Speed}; use embassy_time::Timer; diff --git a/examples/embassy-button/Cargo.toml b/examples/embassy-button/Cargo.toml index 3b81746..9cfdc3c 100644 --- a/examples/embassy-button/Cargo.toml +++ b/examples/embassy-button/Cargo.toml @@ -6,6 +6,7 @@ publish = false build = "build.rs" [dependencies] +cortex-m.workspace = true cortex-m-rt.workspace = true defmt.workspace = true defmt-rtt.workspace = true diff --git a/examples/embassy-button/src/main.rs b/examples/embassy-button/src/main.rs index 7fbdd80..e3b6dcf 100644 --- a/examples/embassy-button/src/main.rs +++ b/examples/embassy-button/src/main.rs @@ -4,6 +4,7 @@ use defmt::info; use defmt_rtt as _; +use cortex_m as _; use embassy_executor::Spawner; use embassy_stm32::gpio::{Input, Level, Output, Pull, Speed}; use embassy_time::Timer; @@ -12,14 +13,17 @@ use panic_probe as _; #[embassy_executor::main] async fn main(_spawner: Spawner) { let p = embassy_stm32::init(Default::default()); - let button = Input::new(p.PA0, Pull::Down); + // Assumes the center switch line goes high when pressed. + let button = Input::new(p.PA8, Pull::Down); + let _vcc = Output::new(p.PA10, Level::High, Speed::Low); + let _gnd = Output::new(p.PB12, Level::Low, Speed::Low); let mut led = Output::new(p.PC13, Level::High, Speed::Low); let mut was_pressed = button.is_high(); - info!("embassy-button: polling PA0 every 25 ms"); + info!("embassy-button: polling PA8 every 25 ms"); loop { - let pressed = button.is_high(); + let pressed = !button.is_high(); if pressed != was_pressed { was_pressed = pressed; diff --git a/justfile b/justfile index f284d5d..5e02e49 100644 --- a/justfile +++ b/justfile @@ -45,10 +45,22 @@ run-blinky: cargo build -p blinky-basic probe-rs run --chip {{chip}} target/{{target}}/debug/blinky-basic +run-timer: + cargo build -p blinky-timer + probe-rs run --chip {{chip}} target/{{target}}/debug/blinky-timer + +run-button: + cargo build -p button-input + probe-rs run --chip {{chip}} target/{{target}}/debug/button-input + run-embassy-blinky: cargo build -p embassy-blinky probe-rs run --chip {{chip}} target/{{target}}/debug/embassy-blinky +run-embassy-button: + cargo build -p embassy-button + probe-rs run --chip {{chip}} target/{{target}}/debug/embassy-button + fmt: cargo fmt --all