Skip to content

Commit 67eab6d

Browse files
committed
Make test and examples compile
But calibration hangs
1 parent 109824a commit 67eab6d

File tree

3 files changed

+25
-64
lines changed

3 files changed

+25
-64
lines changed

examples/adc.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,18 @@ fn main() -> ! {
2020
let clocks = rcc.cfgr.freeze(&mut dp.FLASH.constrain().acr);
2121

2222
// set up adc1
23-
let mut adc1 = adc::Adc::adc1(
23+
let mut adc = adc::Adc::new(
2424
dp.ADC1, // The ADC we are going to control
2525
// The following is only needed to make sure the clock signal for the ADC is set up
2626
// correctly.
27-
&mut dp.ADC1_2,
28-
&mut rcc.ahb,
29-
adc::ClockMode::default(),
3027
clocks,
28+
&mut rcc.ahb,
3129
);
3230

3331
// Set up pin PA0 as analog pin.
3432
// This pin is connected to the user button on the stm32f3discovery board.
3533
let mut gpioa = dp.GPIOA.split(&mut rcc.ahb);
36-
let mut adc1_in1_pin = gpioa.pa0.into_analog(&mut gpioa.moder, &mut gpioa.pupdr);
34+
let mut analog_pin = gpioa.pa0.into_analog(&mut gpioa.moder, &mut gpioa.pupdr);
3735

3836
// Be aware that the values in the table below depend on the input of VREF.
3937
// To have a stable VREF input, put a condensator and a volt limiting diode in front of it.
@@ -54,8 +52,8 @@ fn main() -> ! {
5452
").expect("Error using hprintln.");
5553

5654
loop {
57-
let adc1_in1_data: u16 = adc1.read(&mut adc1_in1_pin).expect("Error reading adc1.");
58-
hprintln!("PA0 reads {}", adc1_in1_data).ok();
55+
let adc_data: u16 = adc.read(&mut analog_pin).expect("Error reading adc1.");
56+
hprintln!("PA0 reads {}", adc_data).ok();
5957
asm::delay(2_000_000);
6058
}
6159
}

src/adc.rs

Lines changed: 16 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ pub struct Adc<ADC> {
4444
/// ADC Register
4545
adc: ADC,
4646
clocks: Clocks,
47-
clock_mode: ClockMode,
4847
operation_mode: Option<OperationMode>,
4948
}
5049

@@ -124,39 +123,6 @@ pub enum OperationMode {
124123
OneShot,
125124
}
126125

127-
#[derive(Clone, Copy, PartialEq)]
128-
/// ADC Clock Mode
129-
// TODO: Add Asynchronous mode
130-
#[non_exhaustive]
131-
pub enum ClockMode {
132-
// /// Use Kernel Clock adc_ker_ck_input divided by PRESC. Asynchronous to AHB clock
133-
// Asynchronous,
134-
/// Use AHB clock rcc_hclk3. In this case rcc_hclk must equal sys_d1cpre_ck
135-
SyncDiv1,
136-
/// Use AHB clock rcc_hclk3 divided by 2
137-
SyncDiv2,
138-
/// Use AHB clock rcc_hclk3 divided by 4
139-
SyncDiv4,
140-
}
141-
142-
impl Default for ClockMode {
143-
fn default() -> Self {
144-
ClockMode::SyncDiv2
145-
}
146-
}
147-
148-
// ADC3_2 returns a pointer to a adc1_2 type, so this from is ok for both.
149-
impl From<ClockMode> for CKMODE_A {
150-
fn from(clock_mode: ClockMode) -> Self {
151-
match clock_mode {
152-
//ClockMode::Asynchronous => CKMODE_A::ASYNCHRONOUS,
153-
ClockMode::SyncDiv1 => CKMODE_A::SYNCDIV1,
154-
ClockMode::SyncDiv2 => CKMODE_A::SYNCDIV2,
155-
ClockMode::SyncDiv4 => CKMODE_A::SYNCDIV4,
156-
}
157-
}
158-
}
159-
160126
/// ADC data register alignment
161127
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
162128
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
@@ -311,22 +277,27 @@ where
311277
///
312278
pub fn new(
313279
adc: ADC,
314-
frequency: impl Into<Generic<u32>>,
280+
// TODO: frequency is not a possible input
281+
// Frequency can not be set per ADC peripheral, and therefor
282+
// is no option for Adc::new()
283+
// This has doe be configured with an external function,
284+
// and thus is maye a candidate for a more general RccClocksManangement thingy.
285+
// frequency: impl Into<Generic<u32>>,
286+
clocks: Clocks,
315287
// adc_shared : &mut <ADC as Instance>::SharedInstance,
316288
ahb: &mut AHB,
317-
clocks: Clocks,
318289
) -> Self {
319290
let mut adc = Self {
320291
adc,
321292
clocks,
322-
clock_mode: ClockMode::SyncDiv1,
323293
operation_mode: None,
324294
};
325295

326296
// if !(adc.clocks_welldefined(clocks)) {
327297
// crate::panic!("Clock settings not well defined");
328298
// }
329299

300+
330301
ADC::enable_clock(ahb);
331302
// if !(ADC::enable_clock(ahb)){
332303
// crate::panic!("Clock already enabled with a different setting");
@@ -340,7 +311,7 @@ where
340311
// ADEN bit cannot be set during ADCAL=1
341312
// and 4 ADC clock cycle after the ADCAL
342313
// bit is cleared by hardware
343-
adc.wait_adc_clk_cycles(4);
314+
adc.wait_adc_clk_cycles(4, clocks);
344315
adc.enable();
345316

346317
adc
@@ -440,18 +411,15 @@ where
440411
.modify(|_, w| w.adcaldif().single_ended().adcal().calibration());
441412

442413
while self.adc.cr.read().adcal().is_calibration() {}
414+
defmt::info!("{}", &"hi");
443415
}
444416

445-
fn wait_adc_clk_cycles(&self, cycles: u32) {
446-
// using a match statement here so compilation will fail once asynchronous clk
447-
// mode is implemented (CKMODE[1:0] = 00b). This will force whoever is working
448-
// on it to rethink what needs to be done here :)
449-
let adc_per_cpu_cycles = match self.clock_mode {
450-
ClockMode::SyncDiv1 => 1,
451-
ClockMode::SyncDiv2 => 2,
452-
ClockMode::SyncDiv4 => 4,
453-
};
454-
asm::delay(adc_per_cpu_cycles * cycles);
417+
fn wait_adc_clk_cycles(&self, cycles: u32, clocks: Clocks) {
418+
let frequency = ADC::clock(&clocks);
419+
let cpu_cycles = cycles * clocks.sysclk().0 / frequency.0;
420+
defmt::info!("{}", cpu_cycles);
421+
422+
asm::delay(cpu_cycles);
455423
}
456424

457425
fn advregen_enable(&mut self) {

testsuite/tests/adc.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ mod tests {
3434
#[init]
3535
fn init() -> State {
3636
let mut dp = unwrap!(pac::Peripherals::take());
37-
3837
let mut rcc = dp.RCC.constrain();
3938
let mut flash = dp.FLASH.constrain();
4039
let clocks = rcc.cfgr.freeze(&mut flash.acr);
@@ -48,12 +47,10 @@ mod tests {
4847
};
4948

5049
State {
51-
adc: Some(adc::Adc::adc1(
50+
adc: Some(adc::Adc::new(
5251
dp.ADC1,
53-
&mut dp.ADC1_2,
54-
&mut rcc.ahb,
55-
adc::ClockMode::default(),
5652
clocks,
53+
&mut rcc.ahb,
5754
)),
5855
analog: pair.0,
5956
output: pair.1,
@@ -90,12 +87,10 @@ mod tests {
9087
let adc1 = adc.free();
9188

9289
defmt::debug!("Reconfigure");
93-
let new_adc = adc::Adc::adc1(
90+
let new_adc = adc::Adc::new(
9491
adc1,
95-
&mut state.adc1_2,
96-
&mut state.ahb,
97-
adc::ClockMode::default(),
9892
state.clocks,
93+
&mut state.ahb,
9994
);
10095

10196
defmt::debug!("Replace");

0 commit comments

Comments
 (0)