Skip to main content

i2s-tone

Generate a 440 Hz sine tone and play it out on ALP_E1M_I2S0. The minimal demo for <alp/i2s.h>.

Source: examples/i2s-tone/.

board.yaml

som:
sku: E1M-AEN701

preset: e1m-evk

cores:
m55_hp:
app: ./src
peripherals: [i2s]

diagnostics:
log_level: info

Source (abbreviated)

#include <alp/i2s.h>
#include <alp/e1m_pinout.h>
#include <math.h>

#define SAMPLE_RATE 44100
#define FRAMES 1024

static int16_t pcm[FRAMES * 2]; /* stereo */

int main(void) {
alp_i2s_t *i2s = alp_i2s_open(&(alp_i2s_config_t){
.bus_id = ALP_E1M_I2S0,
.sample_rate_hz = SAMPLE_RATE,
.word_bits = 16,
.channels = 2,
.format = ALP_I2S_FMT_I2S,
.direction = ALP_I2S_DIR_TX,
.block_frames = FRAMES,
});
if (i2s == NULL) return -1;

/* open() only configures + allocates the slab; start the clock. */
if (alp_i2s_start(i2s) != ALP_OK) return -1;

for (size_t i = 0; i < FRAMES; i++) {
double t = (double)i / SAMPLE_RATE;
int16_t s = (int16_t)(sin(2.0 * M_PI * 440.0 * t) * 16384);
pcm[i*2 + 0] = s;
pcm[i*2 + 1] = s;
}

/* alp_i2s_write takes a BYTE count, not a frame count. */
for (int loop = 0; loop < 100; loop++) {
alp_i2s_write(i2s, pcm, sizeof(pcm), UINT32_MAX);
}

alp_i2s_stop(i2s);
alp_i2s_close(i2s);
return 0;
}

See also

Questions about this page? Discuss in Community Forum