feat(audio): I2S3 audio amplifier driver — Issue #143 #173

Merged
sl-jetson merged 1 commits from sl-firmware/issue-143-audio-amp into main 2026-03-02 10:36:36 -05:00
Collaborator

Summary

Closes #143.

Adds I2S3 audio output driver for MAX98357A / PCM5102A class-D amplifiers.

Changes

New files

  • include/audio.h — public API: audio_init, audio_mute, audio_set_volume, audio_play_tone, audio_write_pcm, audio_tick, audio_is_playing; AudioTone enum (STARTUP/ARM/DISARM/FAULT/BEEP_SHORT/BEEP_LONG)
  • src/audio.c — I2S3 driver: PLLI2S N=192/R=2 → 96 MHz → FS≈22058 Hz (<0.04% error), GPIO PC10/PA15/PB5 AF6, mute=PC5, DMA1_Stream7_Ch0 circular, ping-pong ISR callbacks, square-wave tone generator, tone sequencer, 4096-sample SPSC PCM FIFO
  • test/test_audio.py — 45 tests, all passing

Modified files

  • include/config.h — AUDIO_BCLK/LRCK/DOUT/MUTE pin defines; AUDIO_SAMPLE_RATE=22050, AUDIO_BUF_HALF=441, AUDIO_VOLUME_DEFAULT=80
  • include/jlink.h — JLINK_CMD_AUDIO=0x08 (PCM audio chunk from Jetson)
  • src/jlink.c#include "audio.h"; JLINK_MAX_PAYLOAD 64→252 (126 int16 samples/frame); AUDIO dispatch case calls audio_write_pcm()
  • src/main.caudio_init() + STARTUP tone on boot; ARM tone after arming; FAULT tone on tilt-fault edge; audio_tick(now) in main loop

Hardware notes

  • SPI3 repurposed as I2S3 (blackbox flash unused on balance bot)
  • Pins: PC10=BCLK, PA15=LRCLK, PB5=DIN, PC5=MUTE (all available on LQFP64)
  • No MCLK needed (MAX98357A/PCM5102A derive clocks from BCLK+LRCLK)
  • Double-buffered DMA: 441 samples/half = 20 ms latency, glitch-free

Test results

45 passed in 0.04s
## Summary Closes #143. Adds I2S3 audio output driver for MAX98357A / PCM5102A class-D amplifiers. ## Changes ### New files - **`include/audio.h`** — public API: `audio_init`, `audio_mute`, `audio_set_volume`, `audio_play_tone`, `audio_write_pcm`, `audio_tick`, `audio_is_playing`; `AudioTone` enum (STARTUP/ARM/DISARM/FAULT/BEEP_SHORT/BEEP_LONG) - **`src/audio.c`** — I2S3 driver: PLLI2S N=192/R=2 → 96 MHz → FS≈22058 Hz (<0.04% error), GPIO PC10/PA15/PB5 AF6, mute=PC5, DMA1_Stream7_Ch0 circular, ping-pong ISR callbacks, square-wave tone generator, tone sequencer, 4096-sample SPSC PCM FIFO - **`test/test_audio.py`** — 45 tests, all passing ### Modified files - **`include/config.h`** — AUDIO_BCLK/LRCK/DOUT/MUTE pin defines; AUDIO_SAMPLE_RATE=22050, AUDIO_BUF_HALF=441, AUDIO_VOLUME_DEFAULT=80 - **`include/jlink.h`** — JLINK_CMD_AUDIO=0x08 (PCM audio chunk from Jetson) - **`src/jlink.c`** — `#include "audio.h"`; JLINK_MAX_PAYLOAD 64→252 (126 int16 samples/frame); AUDIO dispatch case calls `audio_write_pcm()` - **`src/main.c`** — `audio_init()` + STARTUP tone on boot; ARM tone after arming; FAULT tone on tilt-fault edge; `audio_tick(now)` in main loop ## Hardware notes - SPI3 repurposed as I2S3 (blackbox flash unused on balance bot) - Pins: PC10=BCLK, PA15=LRCLK, PB5=DIN, PC5=MUTE (all available on LQFP64) - No MCLK needed (MAX98357A/PCM5102A derive clocks from BCLK+LRCLK) - Double-buffered DMA: 441 samples/half = 20 ms latency, glitch-free ## Test results ``` 45 passed in 0.04s ```
sl-webui added 1 commit 2026-03-02 10:31:53 -05:00
Add I2S3/DMA audio output driver for MAX98357A/PCM5102A class-D amps:

- audio_init(): PLLI2S N=192/R=2 → 96 MHz → FS≈22058 Hz (<0.04% error),
  GPIO PC10/PA15/PB5 (AF6), PC5 mute, DMA1_Stream7_Ch0 circular,
  HAL_I2S_Transmit_DMA ping-pong, 441-sample half-buffers (20 ms each)
- Square-wave tone generator (ISR-safe, integer volume scaling 0-100)
- Tone sequencer: STARTUP/ARM/DISARM/FAULT/BEEP sequences via audio_tick()
- PCM FIFO (4096 samples, SPSC ring): receives Jetson audio via JLink
- JLink protocol: JLINK_CMD_AUDIO = 0x08, JLINK_MAX_PAYLOAD 64→252 bytes
  (supports 126 int16 samples/frame = 5.7 ms @22050 Hz)
- main.c: audio_init(), STARTUP tone on boot, ARM/FAULT tones, audio_tick()
- config.h: AUDIO_BCLK/LRCK/DOUT/MUTE pin defines + PLLI2S constants
- test_audio.py: 45 tests, all passing

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
sl-webui force-pushed sl-firmware/issue-143-audio-amp from 4827d3cd55 to c3ada4a156 2026-03-02 10:34:42 -05:00 Compare
sl-jetson merged commit f2b24e29d8 into main 2026-03-02 10:36:36 -05:00
Sign in to join this conversation.
No Reviewers
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: seb/saltylab-firmware#173
No description provided.