drivers/periph/dac: add dac_play API for async multi sample output#22261
Open
benpicco wants to merge 7 commits into
Open
drivers/periph/dac: add dac_play API for async multi sample output#22261benpicco wants to merge 7 commits into
benpicco wants to merge 7 commits into
Conversation
The `dac_set()` API says > The value is always given as 16-bit value and is internally scaled to the > actual resolution that the DAC unit provides (e.g. 12-bit). We didn't do that scaling before - enable it even though it breaks existing users.
crasbe
reviewed
May 8, 2026
| #include "shell.h" | ||
|
|
||
| #ifndef CONFIG_DAC_LINE | ||
| #define CONFIG_DAC_LINE 0 |
Contributor
There was a problem hiding this comment.
Suggested change
| #define CONFIG_DAC_LINE 0 | |
| # define CONFIG_DAC_LINE 0 |
| #endif | ||
|
|
||
| #ifndef CONIFG_DAC_BUF_SAMPLES | ||
| #define CONIFG_DAC_BUF_SAMPLES 2048 |
Contributor
There was a problem hiding this comment.
Suggested change
| #define CONIFG_DAC_BUF_SAMPLES 2048 | |
| # define CONIFG_DAC_BUF_SAMPLES 2048 |
Comment on lines
+1189
to
+1190
| * @param cb | ||
| * @param ctx |
Contributor
There was a problem hiding this comment.
Would be nice to have documentation for that too.
| * @brief Release a previously acquired DMA channel | ||
| * | ||
| * @param dma DMA channel to release | ||
| * @param ctx |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Contribution description
The
periph_dacAPI only plays single samples. Thedac_ddsdrives does exist, but it's interrupt based so it's prone to distortions when interrupts are disabled even for a brief time.The proper solution is to use DMA to feed the DAC and since the DAC API is CPU specific, the only way to do this is by extending the DAC API with a new
dac_play()function that allows to supply a buffer of samples that will then be played asynchronously.Support for this is indicated by the
periph_dac_playfeature.We can supply a completion callback to supply the next buffer or set it to loop mode where the same sample is played over and over again independent of the CPU (e.g. if we want to produce a constant sine wave).
dac_set()behaves on sam0Previously the 12 bit DAC was not left-adjusted, that means
dac_set(0xfff)would produce the highest amplitude whiledac_set(0x1000)would be clipped to 0.However our API doc says
Fix this by setting the
LEFTADJbit.This however means that existing users will experience a lower amplitude when they supply 12 bit values.
Testing procedure
Try out the different waveforms in
tests/periph/dac_play:(The overshooting for square and sawtooth waves are actually an artifact of the oscilloscope, the signal looks clean on the (more expensive) Tektronix scope)
Issues/PRs references
Declaration of AI-Tools / LLMs usage:
AI-Tools / LLMs that were used are: