Skip to main content

<alp/i2s.h> — I²S / SAI

Raw I²S / SAI bus access. For higher-level audio capture and playback, use <alp/audio.h>.

#include <alp/i2s.h>

Quick example​

alp_i2s_t *i2s = alp_i2s_open(&(alp_i2s_config_t){
.bus_id = ALP_E1M_I2S0,
.sample_rate_hz = 44100,
.word_bits = 16,
.channels = 2,
.format = ALP_I2S_FMT_I2S,
.direction = ALP_I2S_DIR_TX,
.block_frames = 256,
});

alp_i2s_start(i2s);
alp_i2s_write(i2s, pcm_buffer, pcm_bytes, /* timeout_ms */ 100);
alp_i2s_stop(i2s);
alp_i2s_close(i2s);

Start, write and recovery (v0.17.0)​

  • Write size. alp_i2s_write() refuses a bytes larger than the block negotiated at open (block_frames × channels × word_bits / 8) with ALP_ERR_OUT_OF_RANGE instead of truncating.
  • Deferred TX start. Calling alp_i2s_start() on a TX handle before the first write is legal. Some backends (e.g. the Zephyr DesignWare driver) cannot trigger with an empty queue, so the real start is deferred until the first write queues a block; a trigger failure then surfaces from that write, and each later write retries it. A write that returns an error has never left a block queued. Write-then-start, and the RX direction, trigger immediately.
  • Underrun / overrun recovery. A TX underrun or RX overrun is recovered transparently: the next write (or a start) resumes the stream, and stopping a stream in that state still returns ALP_OK. An external codec or amplifier that shut itself down when the bit clock stopped is not re-armed by this layer.
  • Shared bit clock. alp_i2s_start() can return ALP_ERR_BUSY for an RX start refused because TX is live on the same shared bit clock at a different rate — retry once TX stops or reconfigures to the same rate.

Default-initialiser macro​

alp_i2s_config_t cfg = ALP_I2S_CONFIG_DEFAULT(ALP_E1M_I2S0);
cfg.direction = ALP_I2S_DIR_TX;

ALP_I2S_CONFIG_DEFAULT(id) (v0.10) sets bus_id from id plus: sample_rate_hz = 48000, word_bits = 16, channels = 2 (stereo), format = ALP_I2S_FMT_I2S, direction = ALP_I2S_DIR_RX, block_frames = 256.

See the shared macro contract for the C++ compound-literal caveat that applies to every ALP_*_CONFIG_DEFAULT.

See also​

Questions about this page? Email contact@alplab.ai