Skip to content

Commit b0072df

Browse files
authored
adc: add new_no_init method
1 parent 9d922db commit b0072df

File tree

1 file changed

+54
-5
lines changed

1 file changed

+54
-5
lines changed

hal/src/adc.rs

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,42 @@ impl Adc {
428428
adc
429429
}
430430

431+
/// Create a new ADC driver from an ADC peripheral without initialization.
432+
///
433+
/// This is a slightly safer version of [`steal`](Self::steal).
434+
///
435+
/// # Safety
436+
///
437+
/// 1. Reset the ADC peripheral if determinism is required.
438+
/// 2. Enable the ADC peripheral clock before using the ADC.
439+
/// 3. Select the clock source if a non-default clock is required.
440+
///
441+
/// # Example
442+
///
443+
/// ```no_run
444+
/// use stm32wl_hal::{
445+
/// adc::{self, Adc},
446+
/// pac,
447+
/// };
448+
///
449+
/// let mut dp: pac::Peripherals = pac::Peripherals::take().unwrap();
450+
///
451+
/// // safety: nothing is using the peripheral
452+
/// unsafe { Adc::pulse_reset(&mut dp.RCC) };
453+
///
454+
/// Adc::enable_clock(&mut dp.RCC);
455+
///
456+
/// // safety: ADC peripheral has been reset and clocks are enabled
457+
/// let mut adc: Adc = unsafe { Adc::new_no_init(dp.ADC) };
458+
///
459+
/// // select the ADC clock, optional
460+
/// adc.set_clock_source(adc::Clk::PClkDiv4, &mut dp.RCC);
461+
/// ```
462+
#[inline]
463+
pub const unsafe fn new_no_init(adc: pac::ADC) -> Self {
464+
Self { adc }
465+
}
466+
431467
/// Free the ADC peripheral from the driver.
432468
///
433469
/// # Example
@@ -456,16 +492,29 @@ impl Adc {
456492
///
457493
/// 1. Ensure that the code stealing the ADC has exclusive access to the
458494
/// peripheral. Singleton checks are bypassed with this method.
459-
/// 2. You are responsible for setting up the ADC correctly.
495+
/// 2. Reset the ADC peripheral if determinism is required.
496+
/// 3. Enable the ADC peripheral clock before using the ADC.
497+
/// 4. Select the clock source if a non-default clock is required.
460498
///
461499
/// # Example
462500
///
463-
/// ```
464-
/// use stm32wl_hal::adc::Adc;
501+
/// ```no_run
502+
/// use stm32wl_hal::{
503+
/// adc::{self, Adc},
504+
/// pac,
505+
/// };
506+
///
507+
/// let mut dp: pac::Peripherals = pac::Peripherals::take().unwrap();
508+
/// let _: pac::ADC = dp.ADC;
509+
///
510+
/// unsafe { Adc::pulse_reset(&mut dp.RCC) };
465511
///
466-
/// // ... setup happens here
512+
/// Adc::enable_clock(&mut dp.RCC);
467513
///
468-
/// let adc = unsafe { Adc::steal() };
514+
/// let mut adc: Adc = unsafe { Adc::steal() };
515+
///
516+
/// // select the ADC clock, optional
517+
/// adc.set_clock_source(adc::Clk::PClkDiv4, &mut dp.RCC);
469518
/// ```
470519
///
471520
/// [`new`]: Adc::new

0 commit comments

Comments
 (0)