Skip to content

Commit a9832b9

Browse files
panic on wrong settings instead of returning None obj
1 parent 3258be6 commit a9832b9

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

examples/adc.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,7 @@ fn main() -> ! {
2727
&mut rcc.ahb,
2828
adc::CKMODE::default(),
2929
clocks,
30-
// If everything is set up correctly, we'll get `Some(adc1)`. to access it, we need to `unwrap`
31-
// it. If there was an error, we'll get `None` and this unwrap will `panic!`.
32-
)
33-
.unwrap();
30+
);
3431

3532
// Set up pin PA0 as analog pin.
3633
// This pin is connected to the user button on the stm32f3discovery board.

src/adc.rs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,8 @@ adc_pins!(ADC4,
270270
gpiod::PD12<Analog> => 9,
271271
gpiod::PD13<Analog> => 10,
272272
gpiod::PD14<Analog> => 11,
273-
gpiob::PD8<Analog> => 12,
274-
gpiob::PD9<Analog> => 13,
273+
gpiod::PD8<Analog> => 12,
274+
gpiod::PD9<Analog> => 13,
275275
);
276276

277277
// Abstract implementation of ADC functionality
@@ -287,24 +287,30 @@ macro_rules! adc_hal {
287287
/// Init a new ADC
288288
///
289289
/// Enables the clock, performs a calibration and enables the ADC
290+
///
291+
/// # Panics
292+
/// If one of the following occurs:
293+
/// * the clocksetting is not well defined.
294+
/// * the clock was already enabled with a different setting
295+
///
290296
pub fn $adcx(
291297
rb: $ADC,
292298
adc_common : &mut $ADC_COMMON,
293299
ahb: &mut AHB,
294300
ckmode: CKMODE,
295301
clocks: Clocks,
296-
) -> Option<Self> {
302+
) -> Self {
297303
let mut this_adc = Self {
298304
rb,
299305
clocks,
300306
ckmode,
301307
operation_mode: None,
302308
};
303309
if !(this_adc.clocks_welldefined(clocks)) {
304-
return None;
310+
panic!("Clock settings not well defined");
305311
}
306312
if !(this_adc.enable_clock(ahb, adc_common)){
307-
return None;
313+
panic!("Clock already enabled with a different setting");
308314
}
309315
this_adc.set_align(Align::default());
310316
this_adc.calibrate();
@@ -313,13 +319,19 @@ macro_rules! adc_hal {
313319
// bit is cleared by hardware
314320
this_adc.wait_adc_clk_cycles(4);
315321
this_adc.enable();
316-
Some(this_adc)
322+
323+
this_adc
317324
}
318325

319326
/// Software can use CKMODE::SYNCDIV1 only if
320327
/// hclk and sysclk are the same. (see reference manual 15.3.3)
321328
fn clocks_welldefined(&self, clocks: Clocks) -> bool {
322-
!(self.ckmode == CKMODE::SYNCDIV1 && !(clocks.hclk().0 == clocks.sysclk().0))
329+
if (self.ckmode == CKMODE::SYNCDIV1)
330+
{
331+
clocks.hclk().0 == clocks.sysclk().0
332+
} else {
333+
true
334+
}
323335
}
324336

325337
/// sets up adc in one shot mode for a single channel

0 commit comments

Comments
 (0)