-
Notifications
You must be signed in to change notification settings - Fork 14
/
adc_hal.rs
70 lines (51 loc) · 2.14 KB
/
adc_hal.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//! 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::asm;
use cortex_m_rt::entry;
use daisy_bsp::hal;
use hal::rcc::rec::AdcClkSel;
use hal::rcc::PllConfigStrategy;
use hal::{adc, delay::Delay};
use hal::{pac, prelude::*};
#[entry]
fn main() -> ! {
let cp = cortex_m::Peripherals::take().unwrap();
let dp = pac::Peripherals::take().unwrap();
// - power & clocks -------------------------------------------------------
let pwr = dp.PWR.constrain();
let pwrcfg = pwr.vos0(&dp.SYSCFG).freeze();
let mut ccdr = dp
.RCC
.constrain()
.use_hse(16.MHz()) // external crystal @ 16 MHz
.pll1_strategy(PllConfigStrategy::Iterative) // pll1 drives system clock
.sys_ck(480.MHz()) // system clock @ 480 MHz
.per_ck(4.MHz()) // adc1 clock @ 4 MHz
.freeze(pwrcfg, &dp.SYSCFG);
// switch adc_ker_ck_input multiplexer to per_ck
ccdr.peripheral.kernel_adc_clk_mux(AdcClkSel::PER);
// - adc ------------------------------------------------------------------
let mut delay = Delay::new(cp.SYST, ccdr.clocks);
let mut adc1 =
adc::Adc::adc1(dp.ADC1, &mut delay, ccdr.peripheral.ADC12, &ccdr.clocks).enable();
adc1.set_resolution(adc::Resolution::SIXTEENBIT);
let gpioc = dp.GPIOC.split(ccdr.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 ------------------------------------------------------------
let scale_factor = ccdr.clocks.sys_ck().raw() as f32 / 65_535.;
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 * scale_factor) as u32;
led_user.set_high();
asm::delay(ticks);
led_user.set_low();
asm::delay(ticks);
}
}