Struct stm32wlxx_hal::dac::Dac
source · pub struct Dac { /* private fields */ }
Expand description
Digital to analog converter driver
Implementations§
source§impl Dac
impl Dac
sourcepub fn new(dac: DAC, rcc: &mut RCC) -> Dac
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);
sourcepub unsafe fn steal() -> Dac
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
- Ensure that the code stealing the DAC has exclusive access to the peripheral. Singleton checks are bypassed with this method.
- 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);
sourcepub fn free(self) -> DAC
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();
sourcepub unsafe fn unmask_irq()
Available on non-crate feature stm32wl5x_cm0p
and crate feature rt
only.
pub unsafe fn unmask_irq()
stm32wl5x_cm0p
and crate feature rt
only.sourcepub fn enable_clock(rcc: &mut RCC)
pub fn enable_clock(rcc: &mut RCC)
sourcepub unsafe fn disable_clock(rcc: &mut RCC)
pub unsafe fn disable_clock(rcc: &mut RCC)
Disable the DAC clock.
Safety
- You are responsible for ensuring the DAC is in a state where the clock can be disabled without entering an error state.
- You cannot use the DAC while the clock is disabled.
- You are responsible for re-enabling the clock before resuming use of the DAC.
- You are responsible for setting up anything that may have lost state while the clock was disabled.
sourcepub unsafe fn pulse_reset(rcc: &mut RCC)
pub unsafe fn pulse_reset(rcc: &mut RCC)
Reset the DAC.
Safety
- The DAC must not be in-use.
- You are responsible for setting up the DAC after a reset.
- 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
sourcepub fn set_mode_pin(&mut self, a10: Analog<A10>, mode: ModePin)
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)
});
sourcepub fn set_mode_chip(&mut self, mode: ModeChip) -> Option<Analog<A10>>
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();
sourcepub fn setup_soft_trigger(&mut self)
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();
sourcepub fn soft_trigger(&mut self, val: u16)
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);
sourcepub fn out(&self) -> u16
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);
sourcepub fn set_sample_cycles(&mut self, cycles: u16)
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
sourcepub fn set_hold_cycles(&mut self, cycles: u16)
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
sourcepub fn set_refresh_cycles(&mut self, cycles: u8)
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