Struct stm32wlxx_hal::dac::Dac

source ·
pub struct Dac { /* private fields */ }
Expand description

Digital to analog converter driver

Implementations§

source§

impl Dac

source

pub fn new(dac: DAC, rcc: &mut RCC) -> Dac

Create a new DAC driver from a DAC peripheral.

This will enable clocks and reset the DAC peripheral.

Example
use stm32wlxx_hal::{dac::Dac, pac};

let mut dp: pac::Peripherals = pac::Peripherals::take().unwrap();

let mut dac = Dac::new(dp.DAC, &mut dp.RCC);
source

pub unsafe fn steal() -> Dac

Steal the DAC peripheral from whatever is currently using it.

This will not initialize the DAC peripheral (unlike new).

Safety
  1. Ensure that the code stealing the DAC has exclusive access to the peripheral. Singleton checks are bypassed with this method.
  2. You are responsible for setting up the DAC correctly.
Example
use stm32wlxx_hal::{
    dac::{Dac, ModeChip},
    pac,
};

let mut dp: pac::Peripherals = pac::Peripherals::take().unwrap();

Dac::enable_clock(&mut dp.RCC);
// safety:
// 1. Nothing else is using the DAC in this code.
// 2. This code performs setup for the DAC.
unsafe { Dac::pulse_reset(&mut dp.RCC) };

// safety:
// 1. Nothing else is using the DAC in this code.
// 2. The DAC has been setup, clocks are enabled and the DAC has been reset.
// 3. We do not have ownership of A10 so we switch to a non-pin mode.
let mut dac = unsafe { Dac::steal() };
dac.set_mode_chip(ModeChip::Norm);
source

pub fn free(self) -> DAC

Free the DAC peripheral from the driver.

Example
use stm32wlxx_hal::{dac::Dac, pac};

let mut dp: pac::Peripherals = pac::Peripherals::take().unwrap();
let dac = dp.DAC;

let mut dac_driver = Dac::new(dac, &mut dp.RCC);
// ... use DAC
let dac = dac_driver.free();
source

pub unsafe fn unmask_irq()

Available on non-crate feature stm32wl5x_cm0p and crate feature rt only.

Unmask the DAC interrupt.

Safety

This can break mask-based critical sections.

Example
unsafe { stm32wlxx_hal::dac::Dac::unmask_irq() };
source

pub fn mask_irq()

Available on non-crate feature stm32wl5x_cm0p and crate feature rt only.

Mask the DAC interrupt.

Example
stm32wlxx_hal::dac::Dac::mask_irq();
source

pub fn enable_clock(rcc: &mut RCC)

Enable the DAC clock.

This is done for you in new

Example
use stm32wlxx_hal::{dac::Dac, pac};

let mut dp: pac::Peripherals = pac::Peripherals::take().unwrap();
Dac::enable_clock(&mut dp.RCC);
source

pub unsafe fn disable_clock(rcc: &mut RCC)

Disable the DAC clock.

Safety
  1. You are responsible for ensuring the DAC is in a state where the clock can be disabled without entering an error state.
  2. You cannot use the DAC while the clock is disabled.
  3. You are responsible for re-enabling the clock before resuming use of the DAC.
  4. You are responsible for setting up anything that may have lost state while the clock was disabled.
source

pub unsafe fn pulse_reset(rcc: &mut RCC)

Reset the DAC.

Safety
  1. The DAC must not be in-use.
  2. You are responsible for setting up the DAC after a reset.
  3. After reset the DAC will be using pin A10, if you do not have ownership of this pin switch the DAC to a non-pin mode with set_mode_chip.
Example

See steal

source

pub fn set_mode_pin(&mut self, a10: Analog<A10>, mode: ModePin)

Set the DAC mode with the A10 output pin.

Panics
  • (debug) DAC channel is enabled
  • (debug) DAC calibration is enabled
Example

Normal mode

use stm32wlxx_hal::{
    dac::{Dac, ModePin},
    gpio::{Analog, PortA},
    pac,
};

let mut dp: pac::Peripherals = pac::Peripherals::take().unwrap();
let mut dac: Dac = Dac::new(dp.DAC, &mut dp.RCC);
let mut gpioa: PortA = PortA::split(dp.GPIOA, &mut dp.RCC);

cortex_m::interrupt::free(|cs| dac.set_mode_pin(Analog::new(gpioa.a10, cs), ModePin::NormBuf));

Sample and hold.

use stm32wlxx_hal::{
    dac::{Dac, ModePin},
    gpio::{Analog, PortA},
    pac,
};

let mut dp: pac::Peripherals = pac::Peripherals::take().unwrap();

// enable the LSI clock for DAC sample and hold mode
dp.RCC.csr.modify(|_, w| w.lsion().on());
while dp.RCC.csr.read().lsirdy().is_not_ready() {}

let mut dac: Dac = Dac::new(dp.DAC, &mut dp.RCC);
let mut gpioa: PortA = PortA::split(dp.GPIOA, &mut dp.RCC);

cortex_m::interrupt::free(|cs| {
    dac.set_mode_pin(Analog::new(gpioa.a10, cs), ModePin::SampleHoldBuf)
});
source

pub fn set_mode_chip(&mut self, mode: ModeChip) -> Option<Analog<A10>>

Set the DAC mode to output to on-chip peripherals.

If A10 is currently in-use by the DAC this method returns the A10 pin. This method is the only way to retrieve A10 if the DAC has ownership of the pin.

Panics
  • (debug) DAC channel is enabled
  • (debug) DAC calibration is enabled
Example

Normal mode.

use stm32wlxx_hal::{
    dac::{Dac, ModeChip},
    pac,
};

let mut dp: pac::Peripherals = pac::Peripherals::take().unwrap();
let mut dac: Dac = Dac::new(dp.DAC, &mut dp.RCC);
dac.set_mode_chip(ModeChip::Norm);

Sample and hold mode.

use stm32wlxx_hal::{
    dac::{Dac, ModeChip},
    pac,
};

let mut dp: pac::Peripherals = pac::Peripherals::take().unwrap();

// enable the LSI clock for DAC sample and hold mode
dp.RCC.csr.modify(|_, w| w.lsion().on());
while dp.RCC.csr.read().lsirdy().is_not_ready() {}

let mut dac: Dac = Dac::new(dp.DAC, &mut dp.RCC);
dac.set_mode_chip(ModeChip::SampleHold);

Retrieving the A10 pin.

Example
use stm32wlxx_hal::{
    dac::{Dac, ModeChip, ModePin},
    gpio::{pins, Analog, PortA},
    pac,
};

let mut dp: pac::Peripherals = pac::Peripherals::take().unwrap();
let mut dac: Dac = Dac::new(dp.DAC, &mut dp.RCC);
let mut gpioa: PortA = PortA::split(dp.GPIOA, &mut dp.RCC);

cortex_m::interrupt::free(|cs| dac.set_mode_pin(Analog::new(gpioa.a10, cs), ModePin::NormBuf));
let a10: Analog<pins::A10> = dac.set_mode_chip(ModeChip::Norm).unwrap();
source

pub fn setup_soft_trigger(&mut self)

Setup the DAC for use with a software trigger.

This will enable the DAC.

Example
use stm32wlxx_hal::{dac::Dac, pac};

let mut dp: pac::Peripherals = pac::Peripherals::take().unwrap();
let mut dac: Dac = Dac::new(dp.DAC, &mut dp.RCC);
dac.setup_soft_trigger();
source

pub fn disable(&mut self)

Disable the DAC.

source

pub fn soft_trigger(&mut self, val: u16)

Set the value of the DAC output.

The DAC should be setup for use with a software trigger with setup_soft_trigger before calling this method.

Panics
  • (debug) DAC is not trigger is not set to software
  • (debug) DAC is not enabled
Example
use stm32wlxx_hal::{dac::Dac, pac};

let mut dp: pac::Peripherals = pac::Peripherals::take().unwrap();
let mut dac: Dac = Dac::new(dp.DAC, &mut dp.RCC);

dac.setup_soft_trigger();
dac.soft_trigger(12);
source

pub fn out(&self) -> u16

Get the current DAC output.

Note: Only the lower 12 bits of the return value are used.

Example
use stm32wlxx_hal::{dac::Dac, pac};

let mut dp: pac::Peripherals = pac::Peripherals::take().unwrap();
let mut dac: Dac = Dac::new(dp.DAC, &mut dp.RCC);

dac.setup_soft_trigger();
dac.soft_trigger(1234);
assert_eq!(dac.out(), 1234);
source

pub fn set_sample_cycles(&mut self, cycles: u16)

Set the sample time in number of LSI cycles.

This is valid only in sample and hold mode.

Note: Only the lower 9 bits of the cycles value are used.

Panics
  • (debug) DAC not in sample and hold mode
source

pub fn set_hold_cycles(&mut self, cycles: u16)

Set the hold time in number of LSI cycles.

This is valid only in sample and hold mode.

Note: Only the lower 9 bits of the cycles value are used.

Panics
  • (debug) DAC channel is enabled
  • (debug) DAC calibration is enabled
  • (debug) DAC not in sample and hold mode
source

pub fn set_refresh_cycles(&mut self, cycles: u8)

Set the refresh time in number of LSI cycles.

This is valid only in sample and hold mode.

Panics
  • (debug) DAC channel is enabled
  • (debug) DAC calibration is enabled
  • (debug) DAC not in sample and hold mode

Trait Implementations§

source§

impl Debug for Dac

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl RefUnwindSafe for Dac

§

impl Send for Dac

§

impl !Sync for Dac

§

impl Unpin for Dac

§

impl UnwindSafe for Dac

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.