Skip to content

Commit 297866e

Browse files
committed
Add adc tests
1 parent ba8fb54 commit 297866e

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed

testsuite/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ name = "testsuite"
44
publish = false
55
version = "0.0.0"
66

7+
[[test]]
8+
name = "adc"
9+
harness = false
10+
711
[[test]]
812
name = "uart"
913
harness = false

testsuite/tests/adc.rs

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
#![no_std]
2+
#![no_main]
3+
4+
// TODO: Get pa9 and pa10 because these also implement spi and uart
5+
use defmt_rtt as _;
6+
use panic_probe as _;
7+
8+
use stm32f3xx_hal as hal;
9+
10+
use hal::adc;
11+
use hal::gpio::{Analog, Output, PushPull};
12+
use hal::pac;
13+
use hal::prelude::*;
14+
use hal::serial::{Rx, Serial, Tx};
15+
use hal::{
16+
gpio::{
17+
gpioa::{PA10, PA2, PA3, PA9},
18+
gpiob::{PB10, PB11},
19+
gpioc::{PC0, PC1},
20+
},
21+
pac::ADC1_2,
22+
rcc::{Clocks, Rcc, AHB},
23+
};
24+
25+
use core::array::IntoIter;
26+
27+
use hal::serial::Error;
28+
29+
struct State {
30+
adc: Option<adc::Adc<pac::ADC1>>,
31+
analog: PC0<Analog>,
32+
output: PC1<Output<PushPull>>,
33+
ahb: AHB,
34+
flash: hal::flash::Parts,
35+
clocks: Clocks,
36+
adc1_2: ADC1_2,
37+
}
38+
39+
const TEST_MSG: [u8; 8] = [0xD, 0xE, 0xA, 0xD, 0xB, 0xE, 0xE, 0xF];
40+
41+
#[defmt_test::tests]
42+
mod tests {
43+
use super::*;
44+
use defmt::{self, assert, assert_eq, unwrap};
45+
46+
#[init]
47+
fn init() -> State {
48+
let mut dp = unwrap!(pac::Peripherals::take());
49+
50+
let mut rcc = dp.RCC.constrain();
51+
let mut flash = dp.FLASH.constrain();
52+
let clocks = rcc.cfgr.freeze(&mut flash.acr);
53+
let mut gpioc = dp.GPIOC.split(&mut rcc.ahb);
54+
55+
State {
56+
adc: Some(adc::Adc::adc1(
57+
dp.ADC1, // The ADC we are going to control
58+
// The following is only needed to make sure the clock signal for the ADC is set up
59+
// correctly.
60+
&mut dp.ADC1_2,
61+
&mut rcc.ahb,
62+
adc::CkMode::default(),
63+
clocks,
64+
)),
65+
analog: gpioc.pc0.into_analog(&mut gpioc.moder, &mut gpioc.pupdr),
66+
output: gpioc
67+
.pc1
68+
.into_push_pull_output(&mut gpioc.moder, &mut gpioc.otyper),
69+
ahb: rcc.ahb,
70+
flash,
71+
clocks,
72+
adc1_2: dp.ADC1_2,
73+
}
74+
}
75+
76+
#[test]
77+
fn measure_pin_high_low(state: &mut State) {
78+
let mut adc = defmt::unwrap!(state.adc.take());
79+
for _ in 1..10 {
80+
defmt::unwrap!(state.output.set_high());
81+
let adc_level: u16 = defmt::unwrap!(adc.read(&mut state.analog).ok());
82+
defmt::info!("{}", adc_level);
83+
defmt::unwrap!(state.output.set_low());
84+
// Vref is 3V so output should reach the maximum.
85+
defmt::assert!(adc_level >= 4070 && adc_level <= 4100);
86+
let adc_level: u16 = defmt::unwrap!(adc.read(&mut state.analog).ok());
87+
defmt::info!("{}", adc_level);
88+
defmt::assert_eq!(adc_level, 0);
89+
}
90+
91+
// put adc back in place
92+
state.adc.replace(adc);
93+
}
94+
95+
// #[test]
96+
// fn free_and_reconfigure(state: &mut State) {
97+
// let mut adc = defmt::unwrap!(state.adc.take());
98+
// defmt::debug!("Free");
99+
// let adc1 = adc.free();
100+
101+
// // FIXME: This is not working (stuck on this function)
102+
// defmt::debug!("Reconfigure");
103+
// let new_adc = adc::Adc::adc1(
104+
// adc1,
105+
// &mut state.adc1_2,
106+
// &mut state.ahb,
107+
// adc::CkMode::default(),
108+
// state.clocks,
109+
// );
110+
111+
// defmt::debug!("Replace");
112+
// // put adc back in place
113+
// state.adc.replace(new_adc);
114+
// }
115+
}

0 commit comments

Comments
 (0)