From 371f3ef41902bb808f9bc505c162efc55594254f Mon Sep 17 00:00:00 2001 From: Matous Hybl Date: Sat, 19 Mar 2022 11:05:00 +0100 Subject: Add ADC support for H7 --- examples/stm32h7/Cargo.toml | 2 +- examples/stm32h7/src/bin/adc.rs | 42 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 examples/stm32h7/src/bin/adc.rs (limited to 'examples') diff --git a/examples/stm32h7/Cargo.toml b/examples/stm32h7/Cargo.toml index 6146b6dc6..64baad994 100644 --- a/examples/stm32h7/Cargo.toml +++ b/examples/stm32h7/Cargo.toml @@ -8,7 +8,7 @@ resolver = "2" [features] [dependencies] -embassy = { version = "0.1.0", path = "../../embassy", features = ["defmt", "defmt-timestamp-uptime"] } +embassy = { version = "0.1.0", path = "../../embassy", features = ["defmt", "defmt-timestamp-uptime", "unstable-traits"] } embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "defmt", "stm32h743bi", "net", "time-driver-any", "exti", "unstable-pac", "unstable-traits"] } embassy-net = { path = "../../embassy-net", default-features = false, features = ["defmt", "tcp", "medium-ethernet", "pool-16"] } diff --git a/examples/stm32h7/src/bin/adc.rs b/examples/stm32h7/src/bin/adc.rs new file mode 100644 index 000000000..b12bca307 --- /dev/null +++ b/examples/stm32h7/src/bin/adc.rs @@ -0,0 +1,42 @@ +#![no_std] +#![no_main] +#![feature(type_alias_impl_trait)] + +use embassy::executor::Spawner; +use embassy::time::{Delay, Duration, Timer}; +use embassy_stm32::adc::{Adc, SampleTime}; +use embassy_stm32::rcc::AdcClockSource; +use embassy_stm32::time::U32Ext; +use embassy_stm32::{Config, Peripherals}; + +use defmt::*; +use defmt_rtt as _; // global logger +use panic_probe as _; + +pub fn config() -> Config { + let mut config = Config::default(); + config.rcc.sys_ck = Some(400.mhz().into()); + config.rcc.hclk = Some(200.mhz().into()); + config.rcc.per_ck = Some(64.mhz().into()); + config.rcc.adc_clock_source = AdcClockSource::PerCk; + config +} + +#[embassy::main(config = "config()")] +async fn main(_spawner: Spawner, mut p: Peripherals) { + info!("Hello World!"); + + let mut adc = Adc::new(p.ADC3, &mut Delay); + + adc.set_sample_time(SampleTime::Cycles32_5); + + let mut vref_channel = adc.enable_vref(); + + loop { + let vref = adc.read_internal(&mut vref_channel); + info!("vref: {}", vref); + let measured = adc.read(&mut p.PC0); + info!("measured: {}", measured); + Timer::after(Duration::from_millis(500)).await; + } +} -- cgit