sys/analog_util: add DAC sine wave generation function - #22624
Open
fabian18 wants to merge 19 commits into
Open
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.
…nce" This reverts commit 5765ebb.
fabian18
requested review from
MichelRottleuthner,
MrKevinWeiss,
aabadie,
benpicco,
dylad,
keestux and
leandrolanzieri
as code owners
August 26, 2026 11:28
crasbe
reviewed
Aug 26, 2026
crasbe
left a comment
Contributor
There was a problem hiding this comment.
Just a quick initial look.
Comment on lines
+1197
to
+1198
| * @param dma[in] DMA channel to release | ||
| * @param ctx[in] DMA complete callback context |
Contributor
There was a problem hiding this comment.
Suggested change
| * @param dma[in] DMA channel to release | |
| * @param ctx[in] DMA complete callback context | |
| * @param[in] dma DMA channel to release | |
| * @param[in] ctx DMA complete callback context |
| @@ -1179,9 +1186,18 @@ void dma_release_channel(dma_t dma); | |||
| * @param dma DMA channel reference | |||
| * @param trigger Trigger to use for this DMA channel | |||
| * @param prio Channel priority | |||
| * @param irq Whether to enable the interrupt handler for this channel | |||
| * @param cb Callback to call when DMA transfer is done, may be NULL | |||
| * @param ctx Callback context | |||
Contributor
There was a problem hiding this comment.
Suggested change
| * @param ctx Callback context | |
| * @param ctx Callback context, may be NULL |
| } | ||
| } | ||
|
|
||
| static void play_function(uint32_t freq, uint32_t secs, sample_gen_t fun) { |
Contributor
There was a problem hiding this comment.
Suggested change
| static void play_function(uint32_t freq, uint32_t secs, sample_gen_t fun) { | |
| static void play_function(uint32_t freq, uint32_t secs, sample_gen_t fun) | |
| { |
Contributor
Author
Comment on lines
+102
to
+103
| * @retval 0 Success | ||
| * @retval <0 Negative error code on failure |
Contributor
There was a problem hiding this comment.
Suggested change
| * @retval 0 Success | |
| * @retval <0 Negative error code on failure | |
| * @retval 0 Success | |
| * @retval <0 Negative error code on failure |
Care to list which error codes can be returned?
Comment on lines
+106
to
+107
| uint16_t dac_ref_mv, | ||
| uint16_t *buf, uint32_t *buf_max); |
Contributor
There was a problem hiding this comment.
Suggested change
| uint16_t dac_ref_mv, | |
| uint16_t *buf, uint32_t *buf_max); | |
| uint16_t dac_ref_mv, uint16_t *buf, uint32_t *buf_max); |
| * @param[in] dac_ref_mv Reference voltage of the DAC in millivolts | ||
| * @param[out] buf Buffer to store the generated samples | ||
| * @param[in,out] buf_max On input: maximum number of samples the buffer can hold | ||
| * On output: actual number of samples generated |
Contributor
There was a problem hiding this comment.
I think Doxygen will render this as a single line, perhapy you have to add a dedicated line break.
Haven't checked yet.
Comment on lines
+51
to
+52
| uint16_t dac_ref_mv, | ||
| uint16_t *buf, uint32_t *buf_max) |
Contributor
There was a problem hiding this comment.
Suggested change
| uint16_t dac_ref_mv, | |
| uint16_t *buf, uint32_t *buf_max) | |
| uint16_t dac_ref_mv, uint16_t *buf, uint32_t *buf_max) |
Comment on lines
+39
to
+40
| /* simple function to fill buffer the size of one period */ | ||
| /* Sample period may get trimmed to align with wave period */ |
Contributor
There was a problem hiding this comment.
Suggested change
| /* simple function to fill buffer the size of one period */ | |
| /* Sample period may get trimmed to align with wave period */ | |
| /* Simple function to fill buffer the size of one period. | |
| * Sample period may get trimmed to align with wave period */ |
crasbe
reviewed
Aug 26, 2026
| * | ||
| * Generates Sine, Square, Triangle and Sawtooth waves using a DAC. | ||
| * Connect a speaker or headphones to the DAC output pins of your board, | ||
| * you should be able to hear the generated sounds. |
Contributor
There was a problem hiding this comment.
Suggested change
| * you should be able to hear the generated sounds. | |
| * you should be able to hear the generated sounds. | |
| * Remember that a microcontroller has a very limited out put drive capability, | |
| * use an amplifier for larger speakers to avoid damagine the microcontroller! |
Maybe worth adding 😇
benpicco
reviewed
Aug 27, 2026
| uint16_t dac_max_val = dac_util_map(center_mv + amplitude_mv, 0, dac_ref_mv); | ||
| uint16_t dac_amplitude = dac_max_val - dac_center; | ||
|
|
||
| #if __FPU_PRESENT |
Contributor
There was a problem hiding this comment.
This is only set on sam0, IS_USED(MODULE_CORTEXM_FPU) will cover a few more cores
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
With DAC and DMA implementation, it is possible to create a controlled sine wave with frequency and amplitude.
This PR adds a dedicated function to generate the samples for a sine wave.
Testing procedure
Issues/PRs references
Depends on #22261
Declaration of AI-Tools / LLMs usage:
AI-Tools / LLMs that were used are: