start tutorial

This commit is contained in:
2026-03-08 19:41:38 +01:00
commit a48ba2963d
81 changed files with 1738 additions and 0 deletions

View File

@@ -0,0 +1,20 @@
# 07 - Analog Readout (6 min)
## Goal
ADC auf PA1 lesen und Werte über RTT streamen.
## Run
- `bash scripts/run-step.sh 07 task`
## Tasks
1. Verdrahtung prüfen: Analogsignal an PA1.
2. Werte ausgeben und in drei Bereiche klassifizieren.
3. Potentiometer/Sensor bewegen und Änderung beobachten.
## Done when
1. Rohwert ändert sich mit Eingangsspannung.
2. Klassifizierung (`low/medium/high`) reagiert sinnvoll.

View 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"

View File

@@ -0,0 +1,22 @@
[package]
name = "step07_analog_readout_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"

View File

@@ -0,0 +1,5 @@
MEMORY
{
FLASH : ORIGIN = 0x08000000, LENGTH = 64K
RAM : ORIGIN = 0x20000000, LENGTH = 20K
}

View File

@@ -0,0 +1,4 @@
[toolchain]
channel = "stable"
components = ["rustfmt"]
targets = ["thumbv7m-none-eabi"]

View File

@@ -0,0 +1,37 @@
#![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 _};
fn classify(raw: u16) -> &'static str {
match raw {
0..=1365 => "low",
1366..=2730 => "medium",
_ => "high",
}
}
#[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 adc = Adc::new(dp.ADC1, &mut rcc);
let mut analog_pin = gpioa.pa1.into_analog(&mut gpioa.crl);
let mut timer = Timer::syst(cp.SYST, &rcc.clocks).counter_hz();
timer.start(10.Hz()).unwrap();
loop {
let raw: u16 = adc.read(&mut analog_pin).unwrap();
info!("adc raw={} level={}", raw, classify(raw));
block!(timer.wait()).unwrap();
}
}

View 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"

View File

@@ -0,0 +1,22 @@
[package]
name = "step07_analog_readout_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"

View File

@@ -0,0 +1,5 @@
MEMORY
{
FLASH : ORIGIN = 0x08000000, LENGTH = 64K
RAM : ORIGIN = 0x20000000, LENGTH = 20K
}

View File

@@ -0,0 +1,4 @@
[toolchain]
channel = "stable"
components = ["rustfmt"]
targets = ["thumbv7m-none-eabi"]

View File

@@ -0,0 +1,39 @@
#![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 _};
fn classify(raw: u16) -> &'static str {
// TODO: tune thresholds for your sensor range.
match raw {
0..=1365 => "low",
1366..=2730 => "medium",
_ => "high",
}
}
#[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 adc = Adc::new(dp.ADC1, &mut rcc);
let mut analog_pin = gpioa.pa1.into_analog(&mut gpioa.crl);
let mut timer = Timer::syst(cp.SYST, &rcc.clocks).counter_hz();
timer.start(10.Hz()).unwrap();
loop {
// NOTE: read() returns a Result for one-shot conversions.
let raw: u16 = adc.read(&mut analog_pin).unwrap();
info!("adc raw={} level={}", raw, classify(raw));
block!(timer.wait()).unwrap();
}
}