Skip to content

Commit 1810b4b

Browse files
committed
Fix long delay during ADC initialization
As explained in issue #206, the code here is doing the calculation completely wrong. This leads to a multi-second startup delay instead of just a few clock cycles. Fix this by using the correct calculation; currently only ADC clock configurations synchronous to the AHB are supported which makes this quite easy: We just need to delay the given number of cycles * the selected ADC clock prescaler (1, 2, or 4). To make this code future proof against an implementation of asynchronous ADC clock configurations, use a `match` statement here which will fail to compile once the `ASYNCHRONOUS` variant is added to the `CkMode` struct - thus forcing the implementor to take another look at this. Fixes: #206
1 parent 231c959 commit 1810b4b

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/adc.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -399,8 +399,15 @@ macro_rules! adc_hal {
399399
}
400400

401401
fn wait_adc_clk_cycles(&self, cycles: u32) {
402-
let adc_clk_cycle = self.clocks.hclk().0 / (self.ckmode as u32);
403-
asm::delay(adc_clk_cycle * cycles);
402+
// using a match statement here so compilation will fail once asynchronous clk
403+
// mode is implemented (CKMODE[1:0] = 00b). This will force whoever is working
404+
// on it to rethink what needs to be done here :)
405+
let adc_per_cpu_cycles = match self.ckmode {
406+
CkMode::SYNCDIV1 => 1,
407+
CkMode::SYNCDIV2 => 2,
408+
CkMode::SYNCDIV4 => 4,
409+
};
410+
asm::delay(adc_per_cpu_cycles * cycles);
404411
}
405412

406413
fn advregen_enable(&mut self){

0 commit comments

Comments
 (0)