init basic examples and setup

This commit is contained in:
2026-05-25 18:16:02 +02:00
commit 8028dfd5fc
28 changed files with 2150 additions and 0 deletions

38
examples/README.md Normal file
View File

@@ -0,0 +1,38 @@
# Examples
Each example is its own Cargo package.
## Packages
- `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
- `embassy-blinky` - minimal Embassy executor and async blink
- `embassy-button` - minimal Embassy polling loop for `PA0`
## Build One Example
```bash
just build-blinky
just build-timer
just build-button
just build-embassy-blinky
just build-embassy-button
```
## Flash One Example
```bash
just flash-blinky
just flash-timer
just flash-button
just flash-embassy-blinky
just flash-embassy-button
```
## Run With Logs
```bash
just run-blinky
just run-embassy-blinky
```

View File

@@ -0,0 +1,14 @@
[package]
name = "blinky-basic"
version = "0.1.0"
edition = "2024"
publish = false
build = "build.rs"
[dependencies]
cortex-m.workspace = true
cortex-m-rt.workspace = true
defmt.workspace = true
defmt-rtt.workspace = true
panic-probe.workspace = true
stm32f1xx-hal = { workspace = true, features = ["medium", "stm32f103"] }

View File

@@ -0,0 +1,9 @@
fn main() {
println!("cargo:rerun-if-changed=../../memory.x");
println!(
"cargo:rustc-link-search={}",
std::path::PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap())
.join("../..")
.display()
);
}

View File

@@ -0,0 +1,37 @@
#![deny(unsafe_code)]
#![no_std]
#![no_main]
use cortex_m_rt::entry;
use defmt::info;
use defmt_rtt as _;
use panic_probe as _;
use stm32f1xx_hal::{pac, prelude::*};
const ON_MS: u16 = 500;
const OFF_MS: u16 = 500;
#[entry]
fn main() -> ! {
let dp = pac::Peripherals::take().unwrap();
let cp = cortex_m::Peripherals::take().unwrap();
let mut rcc = dp.RCC.constrain();
let mut gpioc = dp.GPIOC.split(&mut rcc);
// Splitting the GPIO block gives this function ownership of the LED pin.
let mut led = gpioc.pc13.into_push_pull_output(&mut gpioc.crh);
let mut delay = cp.SYST.delay(&rcc.clocks);
info!("blinky-basic: PC13 LED is assumed active-low");
loop {
led.set_low();
info!("led on");
delay.delay_ms(ON_MS);
led.set_high();
info!("led off");
delay.delay_ms(OFF_MS);
}
}

View File

@@ -0,0 +1,15 @@
[package]
name = "blinky-timer"
version = "0.1.0"
edition = "2024"
publish = false
build = "build.rs"
[dependencies]
cortex-m.workspace = true
cortex-m-rt.workspace = true
defmt.workspace = true
defmt-rtt.workspace = true
nb.workspace = true
panic-probe.workspace = true
stm32f1xx-hal = { workspace = true, features = ["medium", "stm32f103"] }

View File

@@ -0,0 +1,9 @@
fn main() {
println!("cargo:rerun-if-changed=../../memory.x");
println!(
"cargo:rustc-link-search={}",
std::path::PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap())
.join("../..")
.display()
);
}

View File

@@ -0,0 +1,35 @@
#![deny(unsafe_code)]
#![no_std]
#![no_main]
use cortex_m_rt::entry;
use defmt::info;
use defmt_rtt as _;
use nb::block;
use panic_probe as _;
use stm32f1xx_hal::{pac, prelude::*, timer::Timer};
const BLINK_HZ: u32 = 2;
#[entry]
fn main() -> ! {
let dp = pac::Peripherals::take().unwrap();
let cp = cortex_m::Peripherals::take().unwrap();
let mut rcc = dp.RCC.constrain();
let mut gpioc = dp.GPIOC.split(&mut rcc);
let mut led = gpioc.pc13.into_push_pull_output(&mut gpioc.crh);
// A timer gives stable periodic work without burning cycles in a delay loop.
let mut timer = Timer::syst(cp.SYST, &rcc.clocks).counter_hz();
timer.start(BLINK_HZ.Hz()).unwrap();
led.set_high();
info!("blinky-timer: {} Hz toggle loop", BLINK_HZ);
loop {
block!(timer.wait()).unwrap();
led.toggle();
info!("tick");
}
}

View File

@@ -0,0 +1,14 @@
[package]
name = "button-input"
version = "0.1.0"
edition = "2024"
publish = false
build = "build.rs"
[dependencies]
cortex-m.workspace = true
cortex-m-rt.workspace = true
defmt.workspace = true
defmt-rtt.workspace = true
panic-probe.workspace = true
stm32f1xx-hal = { workspace = true, features = ["medium", "stm32f103"] }

View File

@@ -0,0 +1,9 @@
fn main() {
println!("cargo:rerun-if-changed=../../memory.x");
println!(
"cargo:rustc-link-search={}",
std::path::PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap())
.join("../..")
.display()
);
}

View File

@@ -0,0 +1,47 @@
#![deny(unsafe_code)]
#![no_std]
#![no_main]
use cortex_m_rt::entry;
use defmt::info;
use defmt_rtt as _;
use panic_probe as _;
use stm32f1xx_hal::{pac, prelude::*};
const POLL_MS: u8 = 25;
#[entry]
fn main() -> ! {
let dp = pac::Peripherals::take().unwrap();
let cp = cortex_m::Peripherals::take().unwrap();
let mut rcc = dp.RCC.constrain();
let mut gpioa = dp.GPIOA.split(&mut rcc);
let mut gpioc = dp.GPIOC.split(&mut rcc);
let button = gpioa.pa0.into_pull_down_input(&mut gpioa.crl);
let mut led = gpioc.pc13.into_push_pull_output(&mut gpioc.crh);
let mut delay = cp.SYST.delay(&rcc.clocks);
let mut was_pressed = button.is_high();
led.set_high();
info!("button-input: PA0 button to 3V3 with internal pull-down");
loop {
let pressed = button.is_high();
if pressed != was_pressed {
was_pressed = pressed;
if pressed {
led.set_low();
info!("button pressed");
} else {
led.set_high();
info!("button released");
}
}
delay.delay_ms(POLL_MS);
}
}

View File

@@ -0,0 +1,15 @@
[package]
name = "embassy-blinky"
version = "0.1.0"
edition = "2024"
publish = false
build = "build.rs"
[dependencies]
cortex-m-rt.workspace = true
defmt.workspace = true
defmt-rtt.workspace = true
embassy-executor = { workspace = true, features = ["defmt", "executor-thread", "platform-cortex-m"] }
embassy-stm32 = { workspace = true, features = ["defmt", "stm32f103c8", "time-driver-tim2"] }
embassy-time.workspace = true
panic-probe.workspace = true

View File

@@ -0,0 +1,9 @@
fn main() {
println!("cargo:rerun-if-changed=../../memory.x");
println!(
"cargo:rustc-link-search={}",
std::path::PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap())
.join("../..")
.display()
);
}

View File

@@ -0,0 +1,28 @@
#![deny(unsafe_code)]
#![no_std]
#![no_main]
use defmt::info;
use defmt_rtt as _;
use embassy_executor::Spawner;
use embassy_stm32::gpio::{Level, Output, Speed};
use embassy_time::Timer;
use panic_probe as _;
#[embassy_executor::main]
async fn main(_spawner: Spawner) {
let p = embassy_stm32::init(Default::default());
let mut led = Output::new(p.PC13, Level::High, Speed::Low);
info!("embassy-blinky: async blink on PC13");
loop {
led.set_low();
info!("led on");
Timer::after_millis(500).await;
led.set_high();
info!("led off");
Timer::after_millis(500).await;
}
}

View File

@@ -0,0 +1,15 @@
[package]
name = "embassy-button"
version = "0.1.0"
edition = "2024"
publish = false
build = "build.rs"
[dependencies]
cortex-m-rt.workspace = true
defmt.workspace = true
defmt-rtt.workspace = true
embassy-executor = { workspace = true, features = ["defmt", "executor-thread", "platform-cortex-m"] }
embassy-stm32 = { workspace = true, features = ["defmt", "stm32f103c8", "time-driver-tim2"] }
embassy-time.workspace = true
panic-probe.workspace = true

View File

@@ -0,0 +1,9 @@
fn main() {
println!("cargo:rerun-if-changed=../../memory.x");
println!(
"cargo:rustc-link-search={}",
std::path::PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap())
.join("../..")
.display()
);
}

View File

@@ -0,0 +1,38 @@
#![deny(unsafe_code)]
#![no_std]
#![no_main]
use defmt::info;
use defmt_rtt as _;
use embassy_executor::Spawner;
use embassy_stm32::gpio::{Input, Level, Output, Pull, Speed};
use embassy_time::Timer;
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);
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");
loop {
let pressed = button.is_high();
if pressed != was_pressed {
was_pressed = pressed;
if pressed {
led.set_low();
info!("button pressed");
} else {
led.set_high();
info!("button released");
}
}
Timer::after_millis(25).await;
}
}