Skip to content

examples/audio_rttl: Fix skipped pauses when playing through PWM. - #4182

Open
Felix-LJY wants to merge 1 commit into
apache:masterfrom
Felix-LJY:master
Open

Felix-LJY wants to merge 1 commit into
apache:masterfrom
Felix-LJY:master

Conversation

@Felix-LJY

Copy link
Copy Markdown
Contributor

Note: Please adhere to Contributing Guidelines.

Summary

examples/audio_rttl plays every tone through a PWM device with PWMIOC_SETCHARACTERISTICS, PWMIOC_START, usleep() and PWMIOC_STOP. A pause (the p note) comes out of the parser with a frequency of 0 and was passed to the driver like any other note.

Several PWM lower halves refuse a frequency of 0 in pwm_start(): RA4, HT32F491x3, GD32VW55x, GD32W51x and TLSR82 return -EINVAL, Ameba returns -ERANGE and LiteX -EPERM. On such a driver play_pwm() printed an error and returned before the usleep(), so every pause lasted nothing, the following notes were played too early and one error line was printed per pause.

The pause is now handled before the PWM device is touched: play_pwm() waits for the duration of the pause and returns. The output is stopped at that point, since every note ends with PWMIOC_STOP. Notes are played as before.

Impact

Only audio_rttl runs that name a PWM device are affected:

  • With a driver that refuses 0 Hz, pauses now keep their duration and nothing is printed. Before, the tune was shorter than written and every pause produced an error line.
  • With a driver that accepts 0 Hz, a pause no longer depends on what the driver makes of such a request: the output simply stays stopped.
  • Tunes without pauses behave as before.

No change to Kconfig, the build system or any driver.

Testing

Build host: Ubuntu 26.04.1 LTS, x86_64 (AMD Ryzen 9 8940HX), GCC 15.2.0, CMake 4.2.3.

Target 1: arm/n32h7, custom board with an N32H760 (bft_v1:nsh, local tree based on NuttX 13.0.1), arm-none-eabi-gcc 15.3.1

A passive buzzer is driven by a timer PWM output, /dev/pwm2. The PWM lower half of this board refuses 0 Hz in pwm_start() with -EINVAL, like the RA4 driver. One tick is 10 ms.

To keep the test silent the tune has pauses only. Two images were built that differ only in examples/audio_rttl/audio_rttl_main.c: one with the file as on apache/master (c621be657), one with this change. audio_rttl_main() was started with the command line below from a test thread whose stderr was redirected to memory and read back over the debug probe, together with the timer control register and the pin level.

audio_rttl -s "T:d=4,o=6,b=2000:p,p,p,p" /dev/pwm2

Before:

Failed to start PWM: 22
Failed to start PWM: 22
Failed to start PWM: 22
Failed to start PWM: 22

The call returned within the tick it was started in.

After:

(no output)

The call returned after 16 ticks (160 ms; a 30 ms usleep() takes 4 ticks on this target). In both cases the timer counter was disabled and the output pin was low afterwards.

Target 2: sim:nsh on apache/nuttx master 58250c3546

No in-tree board configuration builds the example, so it was switched on by hand: CONFIG_PWM=y, CONFIG_AUDIO=y, CONFIG_AUDIOUTILS_RTTTL_C=y, CONFIG_EXAMPLES_AUDIO_SOUND=y. The example builds without warnings and audio_rttl is in the built-in command list.

🤖 Generated with Claude Code

A pause (the p note) is reported with a frequency of 0 and was passed
to the PWM driver like any other note.  Drivers that refuse a 0 Hz
frequency, such as the RA4 one, make PWMIOC_START fail with EINVAL.
play_pwm() then printed an error and returned without waiting, so
every pause was skipped, the following notes were played too early
and one error line was printed per pause.

Wait for the duration of the pause instead, with the output left
stopped as it is after every note.

Tested on a custom N32H760 board with a passive buzzer on a timer PWM
output: a tune made of two pauses returns without an error after the
expected delay and the timer stays stopped.

Assisted-by: Claude Code:glm-5.4
Signed-off-by: JingYue LIAO <felix-liao@my.swjtu.edu.cn>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants