start tutorial
This commit is contained in:
22
tutorial/08-final-combined/README.md
Normal file
22
tutorial/08-final-combined/README.md
Normal file
@@ -0,0 +1,22 @@
|
||||
# 08 - Final Combined (4 min)
|
||||
|
||||
## Goal
|
||||
|
||||
Blink + Button + ADC in einem Programm kombinieren.
|
||||
|
||||
## Behavior
|
||||
|
||||
1. ADC steuert Blink-Intervall.
|
||||
2. Button toggelt Betriebsmodus:
|
||||
- `Mode::Blinking`
|
||||
- `Mode::ForcedOff`
|
||||
|
||||
## Run
|
||||
|
||||
- `bash scripts/run-step.sh 08 task`
|
||||
|
||||
## Done when
|
||||
|
||||
1. LED-Verhalten ändert sich per Button.
|
||||
2. ADC beeinflusst Blinkgeschwindigkeit im Blink-Modus.
|
||||
3. Logs zeigen Mode, ADC und Delay.
|
||||
12
tutorial/08-final-combined/solution/.cargo/config.toml
Normal file
12
tutorial/08-final-combined/solution/.cargo/config.toml
Normal file
@@ -0,0 +1,12 @@
|
||||
[target.thumbv7m-none-eabi]
|
||||
runner = "probe-rs run --chip STM32F103C8"
|
||||
rustflags = [
|
||||
"-C", "link-arg=-Tlink.x",
|
||||
"-C", "link-arg=-Tdefmt.x",
|
||||
]
|
||||
|
||||
[build]
|
||||
target = "thumbv7m-none-eabi"
|
||||
|
||||
[env]
|
||||
DEFMT_LOG = "info"
|
||||
22
tutorial/08-final-combined/solution/Cargo.toml
Normal file
22
tutorial/08-final-combined/solution/Cargo.toml
Normal file
@@ -0,0 +1,22 @@
|
||||
[package]
|
||||
name = "step08_final_combined_solution"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
cortex-m = "0.7.7"
|
||||
cortex-m-rt = "0.7.5"
|
||||
defmt = "0.3.10"
|
||||
defmt-rtt = "0.4.2"
|
||||
panic-probe = { version = "0.3.2", features = ["print-defmt"] }
|
||||
nb = "1.1.0"
|
||||
|
||||
[dependencies.stm32f1xx-hal]
|
||||
version = "0.11.0"
|
||||
features = ["rt", "stm32f103", "medium"]
|
||||
|
||||
[profile.release]
|
||||
codegen-units = 1
|
||||
debug = 2
|
||||
lto = true
|
||||
opt-level = "z"
|
||||
5
tutorial/08-final-combined/solution/memory.x
Normal file
5
tutorial/08-final-combined/solution/memory.x
Normal file
@@ -0,0 +1,5 @@
|
||||
MEMORY
|
||||
{
|
||||
FLASH : ORIGIN = 0x08000000, LENGTH = 64K
|
||||
RAM : ORIGIN = 0x20000000, LENGTH = 20K
|
||||
}
|
||||
4
tutorial/08-final-combined/solution/rust-toolchain.toml
Normal file
4
tutorial/08-final-combined/solution/rust-toolchain.toml
Normal file
@@ -0,0 +1,4 @@
|
||||
[toolchain]
|
||||
channel = "stable"
|
||||
components = ["rustfmt"]
|
||||
targets = ["thumbv7m-none-eabi"]
|
||||
83
tutorial/08-final-combined/solution/src/main.rs
Normal file
83
tutorial/08-final-combined/solution/src/main.rs
Normal file
@@ -0,0 +1,83 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
use cortex_m_rt::entry;
|
||||
use defmt::info;
|
||||
use nb::block;
|
||||
use stm32f1xx_hal::{adc::Adc, pac, prelude::*, timer::Timer};
|
||||
use {defmt_rtt as _, panic_probe as _};
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq)]
|
||||
enum Mode {
|
||||
Blinking,
|
||||
ForcedOff,
|
||||
}
|
||||
|
||||
fn interval_from_adc(raw: u16) -> u32 {
|
||||
100 + (u32::from(raw) * 800 / 4095)
|
||||
}
|
||||
|
||||
#[entry]
|
||||
fn main() -> ! {
|
||||
let cp = cortex_m::Peripherals::take().unwrap();
|
||||
let dp = pac::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 mut led = gpioc.pc13.into_push_pull_output(&mut gpioc.crh);
|
||||
let button = gpioa.pa0.into_pull_up_input(&mut gpioa.crl);
|
||||
|
||||
let mut adc = Adc::new(dp.ADC1, &mut rcc);
|
||||
let mut analog_pin = gpioa.pa1.into_analog(&mut gpioa.crl);
|
||||
|
||||
let mut tick = Timer::syst(cp.SYST, &rcc.clocks).counter_hz();
|
||||
tick.start(100.Hz()).unwrap();
|
||||
|
||||
let mut mode = Mode::Blinking;
|
||||
let mut last_pressed = false;
|
||||
let mut led_on = false;
|
||||
let mut elapsed_ms: u32 = 0;
|
||||
|
||||
loop {
|
||||
let pressed = button.is_low();
|
||||
if pressed && !last_pressed {
|
||||
mode = if mode == Mode::Blinking {
|
||||
info!("mode -> ForcedOff");
|
||||
Mode::ForcedOff
|
||||
} else {
|
||||
info!("mode -> Blinking");
|
||||
Mode::Blinking
|
||||
};
|
||||
}
|
||||
last_pressed = pressed;
|
||||
|
||||
let raw: u16 = adc.read(&mut analog_pin).unwrap();
|
||||
let interval_ms = interval_from_adc(raw);
|
||||
|
||||
match mode {
|
||||
Mode::Blinking => {
|
||||
elapsed_ms += 10;
|
||||
if elapsed_ms >= interval_ms {
|
||||
elapsed_ms = 0;
|
||||
led_on = !led_on;
|
||||
if led_on {
|
||||
led.set_low();
|
||||
} else {
|
||||
led.set_high();
|
||||
}
|
||||
info!("mode=Blinking adc={} interval_ms={} led_on={}", raw, interval_ms, led_on);
|
||||
}
|
||||
}
|
||||
Mode::ForcedOff => {
|
||||
led_on = false;
|
||||
led.set_high();
|
||||
elapsed_ms = 0;
|
||||
info!("mode=ForcedOff adc={} interval_ms={}", raw, interval_ms);
|
||||
}
|
||||
}
|
||||
|
||||
block!(tick.wait()).unwrap();
|
||||
}
|
||||
}
|
||||
12
tutorial/08-final-combined/task/.cargo/config.toml
Normal file
12
tutorial/08-final-combined/task/.cargo/config.toml
Normal file
@@ -0,0 +1,12 @@
|
||||
[target.thumbv7m-none-eabi]
|
||||
runner = "probe-rs run --chip STM32F103C8"
|
||||
rustflags = [
|
||||
"-C", "link-arg=-Tlink.x",
|
||||
"-C", "link-arg=-Tdefmt.x",
|
||||
]
|
||||
|
||||
[build]
|
||||
target = "thumbv7m-none-eabi"
|
||||
|
||||
[env]
|
||||
DEFMT_LOG = "info"
|
||||
22
tutorial/08-final-combined/task/Cargo.toml
Normal file
22
tutorial/08-final-combined/task/Cargo.toml
Normal file
@@ -0,0 +1,22 @@
|
||||
[package]
|
||||
name = "step08_final_combined_task"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
cortex-m = "0.7.7"
|
||||
cortex-m-rt = "0.7.5"
|
||||
defmt = "0.3.10"
|
||||
defmt-rtt = "0.4.2"
|
||||
panic-probe = { version = "0.3.2", features = ["print-defmt"] }
|
||||
nb = "1.1.0"
|
||||
|
||||
[dependencies.stm32f1xx-hal]
|
||||
version = "0.11.0"
|
||||
features = ["rt", "stm32f103", "medium"]
|
||||
|
||||
[profile.release]
|
||||
codegen-units = 1
|
||||
debug = 2
|
||||
lto = true
|
||||
opt-level = "z"
|
||||
5
tutorial/08-final-combined/task/memory.x
Normal file
5
tutorial/08-final-combined/task/memory.x
Normal file
@@ -0,0 +1,5 @@
|
||||
MEMORY
|
||||
{
|
||||
FLASH : ORIGIN = 0x08000000, LENGTH = 64K
|
||||
RAM : ORIGIN = 0x20000000, LENGTH = 20K
|
||||
}
|
||||
4
tutorial/08-final-combined/task/rust-toolchain.toml
Normal file
4
tutorial/08-final-combined/task/rust-toolchain.toml
Normal file
@@ -0,0 +1,4 @@
|
||||
[toolchain]
|
||||
channel = "stable"
|
||||
components = ["rustfmt"]
|
||||
targets = ["thumbv7m-none-eabi"]
|
||||
85
tutorial/08-final-combined/task/src/main.rs
Normal file
85
tutorial/08-final-combined/task/src/main.rs
Normal file
@@ -0,0 +1,85 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
use cortex_m_rt::entry;
|
||||
use defmt::info;
|
||||
use nb::block;
|
||||
use stm32f1xx_hal::{adc::Adc, pac, prelude::*, timer::Timer};
|
||||
use {defmt_rtt as _, panic_probe as _};
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq)]
|
||||
enum Mode {
|
||||
Blinking,
|
||||
ForcedOff,
|
||||
}
|
||||
|
||||
fn interval_from_adc(raw: u16) -> u32 {
|
||||
// Map 0..4095 -> 100..900 ms
|
||||
100 + (u32::from(raw) * 800 / 4095)
|
||||
}
|
||||
|
||||
#[entry]
|
||||
fn main() -> ! {
|
||||
let cp = cortex_m::Peripherals::take().unwrap();
|
||||
let dp = pac::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 mut led = gpioc.pc13.into_push_pull_output(&mut gpioc.crh);
|
||||
let button = gpioa.pa0.into_pull_up_input(&mut gpioa.crl);
|
||||
|
||||
let mut adc = Adc::new(dp.ADC1, &mut rcc);
|
||||
let mut analog_pin = gpioa.pa1.into_analog(&mut gpioa.crl);
|
||||
|
||||
// Fast base tick: all app timing derives from this.
|
||||
let mut tick = Timer::syst(cp.SYST, &rcc.clocks).counter_hz();
|
||||
tick.start(100.Hz()).unwrap(); // 10 ms
|
||||
|
||||
let mut mode = Mode::Blinking;
|
||||
let mut last_pressed = false;
|
||||
let mut led_on = false;
|
||||
let mut elapsed_ms: u32 = 0;
|
||||
|
||||
loop {
|
||||
let pressed = button.is_low();
|
||||
if pressed && !last_pressed {
|
||||
mode = if mode == Mode::Blinking {
|
||||
info!("mode -> ForcedOff");
|
||||
Mode::ForcedOff
|
||||
} else {
|
||||
info!("mode -> Blinking");
|
||||
Mode::Blinking
|
||||
};
|
||||
}
|
||||
last_pressed = pressed;
|
||||
|
||||
let raw: u16 = adc.read(&mut analog_pin).unwrap();
|
||||
let interval_ms = interval_from_adc(raw);
|
||||
|
||||
match mode {
|
||||
Mode::Blinking => {
|
||||
elapsed_ms += 10;
|
||||
if elapsed_ms >= interval_ms {
|
||||
elapsed_ms = 0;
|
||||
led_on = !led_on;
|
||||
if led_on {
|
||||
led.set_low(); // active-low
|
||||
} else {
|
||||
led.set_high();
|
||||
}
|
||||
info!("mode=Blinking adc={} interval_ms={} led_on={}", raw, interval_ms, led_on);
|
||||
}
|
||||
}
|
||||
Mode::ForcedOff => {
|
||||
led_on = false;
|
||||
led.set_high();
|
||||
elapsed_ms = 0;
|
||||
info!("mode=ForcedOff adc={} interval_ms={}", raw, interval_ms);
|
||||
}
|
||||
}
|
||||
|
||||
block!(tick.wait()).unwrap();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user