Skip to content

sys/analog_util: add DAC sine wave generation function - #22624

Open
fabian18 wants to merge 19 commits into
RIOT-OS:masterfrom
fabian18:pr/dac_dma/sine
Open

sys/analog_util: add DAC sine wave generation function#22624
fabian18 wants to merge 19 commits into
RIOT-OS:masterfrom
fabian18:pr/dac_dma/sine

Conversation

@fabian18

Copy link
Copy Markdown
Contributor

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:

  • ChatGPT, Gemini for review.

@github-actions github-actions Bot added Platform: ARM Platform: This PR/issue effects ARM-based platforms Area: tests Area: tests and testing framework Area: build system Area: Build system Area: drivers Area: Device drivers Area: cpu Area: CPU/MCU ports Area: sys Area: System labels Aug 26, 2026
@crasbe crasbe added the Type: new feature The issue requests / The PR implemements a new feature for RIOT label Aug 26, 2026
@crasbe crasbe added State: waiting for other PR State: The PR requires another PR to be merged first CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR AI: Helped PR/Issue uses AI sparingly, e.g. code inline assistant, debugging with AI, etc. labels Aug 26, 2026

@crasbe crasbe left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same for the ones below.

@@ -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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)
{

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@benpicco some comments belong to #22261

Comment thread sys/include/analog_util.h Outdated
Comment on lines +102 to +103
* @retval 0 Success
* @retval <0 Negative error code on failure

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 thread sys/include/analog_util.h Outdated
Comment on lines +106 to +107
uint16_t dac_ref_mv,
uint16_t *buf, uint32_t *buf_max);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 thread sys/include/analog_util.h Outdated
* @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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think Doxygen will render this as a single line, perhapy you have to add a dedicated line break.

Haven't checked yet.

Comment thread sys/analog_util/dac_util.c Outdated
Comment on lines +51 to +52
uint16_t dac_ref_mv,
uint16_t *buf, uint32_t *buf_max)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 */

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 */

*
* 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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 😇

@riot-ci

riot-ci commented Aug 26, 2026

Copy link
Copy Markdown

Murdock results

FAILED

5201115 fixup! sys/analog_util: add function to create sine wave with DAC

Success Failures Total Runtime
88 0 10260 01m:13s

Artifacts

Comment thread sys/analog_util/dac_util.c Outdated
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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is only set on sam0, IS_USED(MODULE_CORTEXM_FPU) will cover a few more cores

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

AI: Helped PR/Issue uses AI sparingly, e.g. code inline assistant, debugging with AI, etc. Area: build system Area: Build system Area: cpu Area: CPU/MCU ports Area: drivers Area: Device drivers Area: sys Area: System Area: tests Area: tests and testing framework CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR Platform: ARM Platform: This PR/issue effects ARM-based platforms State: waiting for other PR State: The PR requires another PR to be merged first Type: new feature The issue requests / The PR implemements a new feature for RIOT

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants