From 56946182f849f6b7cefdf8be2f66044d81aae067 Mon Sep 17 00:00:00 2001 From: Antoine van Gelder Date: Wed, 3 Feb 2021 14:40:02 +0200 Subject: [PATCH] Add example: adc_bsp --- Cargo.lock | 29 +++++++++++++++++- Cargo.toml | 8 +++++ examples/adc_bsp.rs | 72 +++++++++++++++++++++++++++++++++++++++++++++ examples/adc_hal.rs | 5 ++-- 4 files changed, 111 insertions(+), 3 deletions(-) create mode 100644 examples/adc_bsp.rs diff --git a/Cargo.lock b/Cargo.lock index 63a9edc..9dd3bfc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -124,10 +124,20 @@ dependencies = [ "num-traits", "panic-semihosting", "static-alloc", - "stm32h7xx-hal", + "stm32h7xx-hal 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", + "stm32h7xx-hal 0.8.0 (git+https://github.com/stm32-rs/stm32h7xx-hal.git?rev=0bfeeca4ce120c1b7c6d140a7da73a4372b874d8)", "without-alloc", ] +[[package]] +name = "embedded-dma" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "46c8c02e4347a0267ca60813c952017f4c5948c232474c6010a381a337f1bda4" +dependencies = [ + "stable_deref_trait", +] + [[package]] name = "embedded-hal" version = "0.2.4" @@ -370,6 +380,23 @@ dependencies = [ "void", ] +[[package]] +name = "stm32h7xx-hal" +version = "0.8.0" +source = "git+https://github.com/stm32-rs/stm32h7xx-hal.git?rev=0bfeeca4ce120c1b7c6d140a7da73a4372b874d8#0bfeeca4ce120c1b7c6d140a7da73a4372b874d8" +dependencies = [ + "bare-metal 1.0.0", + "cast", + "cortex-m", + "cortex-m-rt", + "embedded-dma", + "embedded-hal", + "nb 1.0.0", + "paste", + "stm32h7", + "void", +] + [[package]] name = "syn" version = "1.0.48" diff --git a/Cargo.toml b/Cargo.toml index ed75038..ee4336d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -138,3 +138,11 @@ required-features = ["seed", "uses_num"] [[example]] name = "serial" required-features = ["seed"] + +[[example]] +name = "adc_hal" +required-features = ["seed"] + +[[example]] +name = "adc_bsp" +required-features = ["seed"] diff --git a/examples/adc_bsp.rs b/examples/adc_bsp.rs new file mode 100644 index 0000000..e11ec77 --- /dev/null +++ b/examples/adc_bsp.rs @@ -0,0 +1,72 @@ +//! Example of how to directly access the ADC peripheral on the Daisy +//! Seed when using the Board Support Crate. + +#![no_main] +#![no_std] + +use panic_semihosting as _; +use cortex_m_rt::entry; + +use daisy_bsp as daisy; + +use cortex_m::asm; + +use daisy_bsp::hal; +use hal::rcc::PllConfigStrategy; +use hal::rcc::rec::AdcClkSel; +use hal::{adc, delay::Delay}; + +use hal::hal as embedded_hal; +use embedded_hal::digital::v2::OutputPin; + +use hal::{pac, prelude::*}; + + +#[entry] +fn main() -> ! { + // - board setup ---------------------------------------------------------- + + let mut board = daisy::Board::take().unwrap(); + + // - clocks --------------------------------------------------------------- + + // switch adc_ker_ck_input multiplexer to per_ck + board.peripheral.kernel_adc_clk_mux(AdcClkSel::PER); + + // - adc ------------------------------------------------------------------ + + let cp = unsafe { cortex_m::Peripherals::steal() }; + let dp = unsafe { pac::Peripherals::steal() }; + + let mut delay = Delay::new(cp.SYST, board.clocks); + let mut adc1 = adc::Adc::adc1( + dp.ADC1, + &mut delay, + board.peripheral.ADC12, + &board.clocks, + ).enable(); + adc1.set_resolution(adc::Resolution::SIXTEENBIT); + + let gpioc = dp.GPIOC.split(board.peripheral.GPIOC); + let mut adc1_channel_4 = gpioc.pc4.into_analog(); // pot 1 + let mut adc1_channel_10 = gpioc.pc0.into_analog(); // pot 2 + + // - led ------------------------------------------------------------------ + + let mut led_user = gpioc.pc7.into_push_pull_output(); + + // - main loop ------------------------------------------------------------ + + loop { + let pot_1: u32 = adc1.read(&mut adc1_channel_4).unwrap(); + let pot_2: u32 = adc1.read(&mut adc1_channel_10).unwrap(); + + let ticks = (pot_1 as f32 * (480_000_000. / 65_535.)) as u32; + + led_user.set_high().unwrap(); + asm::delay(ticks); + + led_user.set_low().unwrap(); + asm::delay(ticks); + } +} diff --git a/examples/adc_hal.rs b/examples/adc_hal.rs index c1e3c60..b4e0279 100644 --- a/examples/adc_hal.rs +++ b/examples/adc_hal.rs @@ -1,8 +1,10 @@ +//! Example of how to access the ADC peripheral directly via +//! stm32h7xx-hal without using the board support crate. + #![no_main] #![no_std] use panic_semihosting as _; -use cortex_m_semihosting::hprintln; use cortex_m_rt::entry; use cortex_m::asm; @@ -61,7 +63,6 @@ fn main() -> ! { loop { let pot_1: u32 = adc1.read(&mut adc1_channel_4).unwrap(); let pot_2: u32 = adc1.read(&mut adc1_channel_10).unwrap(); - //hprintln!("{}\t{}", pot_1, pot_2); let ticks = (pot_1 as f32 * (480_000_000. / 65_535.)) as u32;