diff options
| author | Matous Hybl <[email protected]> | 2022-03-19 11:05:00 +0100 |
|---|---|---|
| committer | Matous Hybl <[email protected]> | 2022-04-12 22:25:00 +0200 |
| commit | 371f3ef41902bb808f9bc505c162efc55594254f (patch) | |
| tree | b72d98a8651f82758903c81c91020ea55dff9065 /examples/stm32h7/src/bin/adc.rs | |
| parent | ac3986e40ef297b90de19812aebccfe2e7f9ceec (diff) | |
Add ADC support for H7
Diffstat (limited to 'examples/stm32h7/src/bin/adc.rs')
| -rw-r--r-- | examples/stm32h7/src/bin/adc.rs | 42 |
1 files changed, 42 insertions, 0 deletions
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 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | use embassy::executor::Spawner; | ||
| 6 | use embassy::time::{Delay, Duration, Timer}; | ||
| 7 | use embassy_stm32::adc::{Adc, SampleTime}; | ||
| 8 | use embassy_stm32::rcc::AdcClockSource; | ||
| 9 | use embassy_stm32::time::U32Ext; | ||
| 10 | use embassy_stm32::{Config, Peripherals}; | ||
| 11 | |||
| 12 | use defmt::*; | ||
| 13 | use defmt_rtt as _; // global logger | ||
| 14 | use panic_probe as _; | ||
| 15 | |||
| 16 | pub fn config() -> Config { | ||
| 17 | let mut config = Config::default(); | ||
| 18 | config.rcc.sys_ck = Some(400.mhz().into()); | ||
| 19 | config.rcc.hclk = Some(200.mhz().into()); | ||
| 20 | config.rcc.per_ck = Some(64.mhz().into()); | ||
| 21 | config.rcc.adc_clock_source = AdcClockSource::PerCk; | ||
| 22 | config | ||
| 23 | } | ||
| 24 | |||
| 25 | #[embassy::main(config = "config()")] | ||
| 26 | async fn main(_spawner: Spawner, mut p: Peripherals) { | ||
| 27 | info!("Hello World!"); | ||
| 28 | |||
| 29 | let mut adc = Adc::new(p.ADC3, &mut Delay); | ||
| 30 | |||
| 31 | adc.set_sample_time(SampleTime::Cycles32_5); | ||
| 32 | |||
| 33 | let mut vref_channel = adc.enable_vref(); | ||
| 34 | |||
| 35 | loop { | ||
| 36 | let vref = adc.read_internal(&mut vref_channel); | ||
| 37 | info!("vref: {}", vref); | ||
| 38 | let measured = adc.read(&mut p.PC0); | ||
| 39 | info!("measured: {}", measured); | ||
| 40 | Timer::after(Duration::from_millis(500)).await; | ||
| 41 | } | ||
| 42 | } | ||
