-
Notifications
You must be signed in to change notification settings - Fork 51
DAC Improvements - Dual DAC support and DMA #213
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
boondocklabs
wants to merge
6
commits into
stm32-rs:main
Choose a base branch
from
boondocklabs:dual_dac
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
bdb9232
DAC Improvements
boondocklabs 5915276
Use fixed crate without cordic flag for DAC. Fix warnings.
boondocklabs efbe285
Use `bit` methods in place of branches
boondocklabs 2799c65
Add Alignment to SampleDepth so alignment is only applicable to 12b s…
boondocklabs e923fc2
Use DacChannel in enable_dma_double
boondocklabs c3e4576
Add simple DualDac example
boondocklabs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,206 @@ | ||
#![deny(missing_docs)] | ||
|
||
//! Dual DAC | ||
//! | ||
//! This module provides access to the dual DAC channels of the STM32G4 series microcontrollers. | ||
//! It allows for simultaneous access to both DAC channels and supports DMA transfers. | ||
//! | ||
|
||
use core::marker::PhantomData; | ||
|
||
use embedded_hal::delay::DelayNs; | ||
|
||
use crate::dac::{ | ||
format::SampleFormat, trigger::DacTriggerSource, DacCh, DacChannel, Disabled, Enabled, | ||
Instance, M_EXT_PIN, | ||
}; | ||
use crate::dac::{hfsel, DacExt as _, Pins}; | ||
|
||
use crate::dac::format::{self, ToDac as _}; | ||
use crate::dma::mux::DmaMuxResources; | ||
use crate::dma::traits::TargetAddress; | ||
use crate::dma::MemoryToPeripheral; | ||
use crate::rcc::Rcc; | ||
|
||
/// Extension trait for [`Instance`] to create a [`DualDac`] from the given pins and RCC. | ||
pub trait DualDacExt: Instance + Sized { | ||
/// Create a dual DAC instance from the given pins and RCC. | ||
/// | ||
/// DAC calibration is performed before enabling the DAC, so | ||
/// a DelayNs implementation is required. | ||
/// | ||
/// This uses the dual hold registers (DHR12xD, DHR8RD) and | ||
/// can write to both DAC channels simultaneously with DMA support. | ||
fn into_dual<F: SampleFormat, PINS>( | ||
self, | ||
pins: PINS, | ||
rcc: &mut Rcc, | ||
delay: &mut impl DelayNs, | ||
) -> DualDac<Self, F, Disabled> | ||
where | ||
PINS: Pins< | ||
Self, | ||
Output = ( | ||
DacCh<Self, 0, M_EXT_PIN, Disabled>, | ||
DacCh<Self, 1, M_EXT_PIN, Disabled>, | ||
), | ||
>; | ||
} | ||
|
||
/// Dual DAC handle | ||
pub struct DualDac<DAC: Instance, F: SampleFormat, State> { | ||
_ch1: DacCh<DAC, 0, M_EXT_PIN, State>, | ||
_ch2: DacCh<DAC, 1, M_EXT_PIN, State>, | ||
_phantom: PhantomData<F>, | ||
} | ||
|
||
impl<DAC: Instance, F: SampleFormat> DualDac<DAC, F, Disabled> { | ||
/// Enable DMA double mode for the specified channel. | ||
/// This creates a single DMA request for every | ||
/// two external hardware triggers (excluding software triggers). | ||
#[inline(always)] | ||
pub fn enable_dma_double(&mut self, channel: u8, enable: bool) { | ||
let dac = unsafe { &(*DAC::ptr()) }; | ||
if enable { | ||
dac.mcr().modify(|_, w| w.dmadouble(channel).set_bit()); | ||
} else { | ||
dac.mcr().modify(|_, w| w.dmadouble(channel).clear_bit()); | ||
} | ||
usbalbin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
/// Enable DMA for the specified channel | ||
#[inline(always)] | ||
pub fn enable_dma(&mut self, channel: DacChannel, enable: bool) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why does Also, is it ok to have |
||
let dac = unsafe { &(*DAC::ptr()) }; | ||
if enable { | ||
dac.cr().modify(|_, w| w.dmaen(channel as u8).set_bit()); | ||
} else { | ||
dac.cr().modify(|_, w| w.dmaen(channel as u8).clear_bit()); | ||
} | ||
} | ||
|
||
/// Enable trigger for the specified channel and the [`DacTriggerSource`] | ||
/// provided as a generic type parameter. | ||
/// | ||
/// This will cause the DAC to copy the hold register to the output register | ||
/// when the trigger is raised by a timer signal or external interrupt, | ||
/// and issue a new DMA request if DMA is enabled. | ||
#[inline(always)] | ||
pub fn enable_trigger<Source>(&mut self, channel: DacChannel) | ||
where | ||
Source: DacTriggerSource<DAC>, | ||
{ | ||
// Set the TSELx bits to the trigger signal identifier from the DacTriggerSource impl | ||
let dac = unsafe { &(*DAC::ptr()) }; | ||
match channel { | ||
DacChannel::Ch1 => { | ||
unsafe { dac.cr().modify(|_, w| w.tsel1().bits(Source::SIGNAL)) }; | ||
} | ||
DacChannel::Ch2 => { | ||
unsafe { dac.cr().modify(|_, w| w.tsel2().bits(Source::SIGNAL)) }; | ||
} | ||
} | ||
|
||
// Enable the TENx flag in DAC CR | ||
dac.cr().modify(|_, w| w.ten(channel as u8).set_bit()); | ||
} | ||
|
||
/// Enable both DAC channels | ||
#[inline(always)] | ||
pub fn enable(self) -> DualDac<DAC, F, Enabled> { | ||
let dac = unsafe { &(*DAC::ptr()) }; | ||
|
||
dac.cr().modify(|_, w| w.en(0).set_bit().en(1).set_bit()); | ||
DualDac { | ||
_ch1: DacCh::new(), | ||
_ch2: DacCh::new(), | ||
_phantom: PhantomData, | ||
} | ||
} | ||
} | ||
|
||
impl<DAC: Instance, F: SampleFormat> DualDac<DAC, F, Enabled> { | ||
/// Write to both DAC channels simultaneously. | ||
#[inline(always)] | ||
pub fn set_channels(&mut self, ch1: F::Scalar, ch2: F::Scalar) { | ||
let dac = unsafe { &(*DAC::ptr()) }; | ||
|
||
let bits = (ch2.to_dac() as u32) << F::CH2_SHIFT | (ch1.to_dac() as u32) & F::CH1_MASK; | ||
|
||
match F::DEPTH { | ||
format::SampleDepth::Bits12 => match F::ALIGNMENT { | ||
format::Alignment::Left => { | ||
dac.dhr12ld().write(|w| unsafe { w.bits(bits as u32) }); | ||
} | ||
format::Alignment::Right => { | ||
dac.dhr12rd().write(|w| unsafe { w.bits(bits as u32) }); | ||
} | ||
}, | ||
format::SampleDepth::Bits8 => { | ||
// NOTE: 8-bit is always right aligned | ||
dac.dhr8rd().write(|w| unsafe { w.bits(bits as u32) }); | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl<DAC: Instance> DualDacExt for DAC { | ||
fn into_dual<F: SampleFormat, PINS>( | ||
self, | ||
pins: PINS, | ||
rcc: &mut Rcc, | ||
delay: &mut impl DelayNs, | ||
) -> DualDac<DAC, F, Disabled> | ||
where | ||
PINS: Pins< | ||
Self, | ||
Output = ( | ||
DacCh<Self, 0, M_EXT_PIN, Disabled>, | ||
DacCh<Self, 1, M_EXT_PIN, Disabled>, | ||
), | ||
>, | ||
{ | ||
let (ch1, ch2) = self.constrain(pins, rcc); | ||
|
||
let ch1 = ch1.calibrate_buffer(delay); | ||
let ch2 = ch2.calibrate_buffer(delay); | ||
usbalbin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Configure HFSEL | ||
let dac = unsafe { &(*DAC::ptr()) }; | ||
let hfsel = hfsel(rcc); | ||
dac.mcr().modify(|_, w| w.hfsel().variant(hfsel)); | ||
|
||
// Apply the format configuration to both channels | ||
for channel in 0..2 { | ||
match F::SIGNED { | ||
true => { | ||
dac.mcr().modify(|_, w| w.sinformat(channel).set_bit()); | ||
} | ||
false => { | ||
dac.mcr().modify(|_, w| w.sinformat(channel).clear_bit()); | ||
} | ||
}; | ||
} | ||
|
||
DualDac { | ||
_ch1: ch1, | ||
_ch2: ch2, | ||
_phantom: PhantomData, | ||
} | ||
} | ||
} | ||
|
||
unsafe impl<DAC: Instance, F: SampleFormat, State> TargetAddress<MemoryToPeripheral> | ||
for DualDac<DAC, F, State> | ||
{ | ||
type MemSize = u32; | ||
const REQUEST_LINE: Option<u8> = Some(DmaMuxResources::DAC1_CH1 as u8); | ||
|
||
fn address(&self) -> u32 { | ||
let dac = unsafe { &(*DAC::ptr()) }; | ||
match F::SIGNED { | ||
true => dac.dhr12ld() as *const _ as u32, | ||
false => dac.dhr12rd() as *const _ as u32, | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.