Skip to content

Commit

Permalink
Add example: adc_bsp
Browse files Browse the repository at this point in the history
  • Loading branch information
antoinevg committed Feb 3, 2021
1 parent 5908ce8 commit 5694618
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 3 deletions.
29 changes: 28 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
72 changes: 72 additions & 0 deletions examples/adc_bsp.rs
Original file line number Diff line number Diff line change
@@ -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);
}
}
5 changes: 3 additions & 2 deletions examples/adc_hal.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit 5694618

Please sign in to comment.