[00:06:49.411,024] <inf> i2s_nrfx: I2S MCK frequency: 256000, actual PCM rate: 8000
hi
-5 (EIO) error during free-run execution when the I2S pins are connected. When the I2S lines are disconnected, I2C register reads work properly. Expected behaviour, code is still missing the required k_mem_slab buffer handling code completely as far as I can tell.
/*
* Copyright 2024–2025
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/kernel.h>
#include <zephyr/device.h>
#include <zephyr/drivers/i2s.h>
#include <zephyr/sys/printk.h>
#include <string.h>
#include <stdbool.h>
#include "codec_header.h"
/* ================= CONFIG ================= */
#define SAMPLE_NO 64
#define CHANNELS 2
#define NUM_BLOCKS 4
#define SAMPLE_RATE 8000
#define WORD_SIZE 16
#define BLOCK_SIZE (SAMPLE_NO * CHANNELS * sizeof(int16_t))
#define TIMEOUT_MS 2000
/* ================= I2S DEVICE ================= */
#define I2S_NODE DT_NODELABEL(i2s20)
static const struct device *dev_i2s = DEVICE_DT_GET(I2S_NODE);
/* ================= MEMORY SLAB ================= */
/* DMA-safe memory pool for I2S */
K_MEM_SLAB_DEFINE(tx_slab, BLOCK_SIZE, NUM_BLOCKS, 4);
/* ================= SINE WAVE ================= */
static const int16_t sine[SAMPLE_NO] = {
3211, 6392, 9511, 12539, 15446, 18204, 20787, 23169,
25329, 27244, 28897, 30272, 31356, 32137, 32609, 32767,
32609, 32137, 31356, 30272, 28897, 27244, 25329, 23169,
20787, 18204, 15446, 12539, 9511, 6392, 3211, 0,
-3212, -6393, -9512,-12540,-15447,-18205,-20788,-23170,
-25330,-27245,-28898,-30273,-31357,-32138,-32610,-32767,
-32610,-32138,-31357,-30273,-28898,-27245,-25330,-23170,
-20788,-18205,-15447,-12540, -9512, -6393, -3212, -1
};
/* ================= BUFFER FILL ================= */
static void fill_buf(int16_t *buf, int shift)
{
for (int i = 0; i < SAMPLE_NO; i++) {
buf[2 * i] = sine[i] >> shift; /* Left */
buf[2 * i + 1] = sine[i] >> shift; /* Right */
}
}
/* ================= MAIN ================= */
int main(void)
{
struct i2s_config i2s_cfg = {0};
int ret;
int idx = 0;
printk("\n=== I2S k_mem_slab TX test ===\n");
/* ---------- Device ready ---------- */
if (!device_is_ready(dev_i2s)) {
printk("I2S device not ready\n");
return 0;
}
/* ---------- Codec init ---------- */
ret = i2c_init_codec();
if (ret) {
printk("Codec init failed (%d)\n", ret);
return 0;
}
/* ---------- I2S CONFIG ---------- */
i2s_cfg.word_size = WORD_SIZE;
i2s_cfg.channels = CHANNELS;
i2s_cfg.format = I2S_FMT_DATA_FORMAT_I2S;
i2s_cfg.frame_clk_freq = SAMPLE_RATE;
i2s_cfg.block_size = BLOCK_SIZE;
i2s_cfg.timeout = TIMEOUT_MS;
i2s_cfg.mem_slab = &tx_slab;
i2s_cfg.options =
I2S_OPT_FRAME_CLK_SLAVE |
I2S_OPT_BIT_CLK_SLAVE;
ret = i2s_configure(dev_i2s, I2S_DIR_TX, &i2s_cfg);
if (ret) {
printk("I2S configure failed (%d)\n", ret);
return 0;
}
/* ---------- Queue initial buffers ---------- */
for (int i = 0; i < NUM_BLOCKS; i++) {
void *mem_block;
ret = k_mem_slab_alloc(&tx_slab, &mem_block, K_FOREVER);
if (ret) {
printk("Slab alloc failed\n");
return 0;
}
fill_buf((int16_t *)mem_block, i);
i2s_write(dev_i2s, mem_block, BLOCK_SIZE);
}
/* ---------- Start I2S ---------- */
ret = i2s_trigger(dev_i2s, I2S_DIR_TX, I2S_TRIGGER_START);
if (ret) {
printk("I2S start failed (%d)\n", ret);
return 0;
}
printk("I2S streaming started\n");
/* ---------- Streaming loop ---------- */
while (1) {
void *mem_block;
ret = k_mem_slab_alloc(&tx_slab, &mem_block, K_FOREVER);
if (ret) {
printk("Slab alloc failed\n");
break;
}
fill_buf((int16_t *)mem_block, idx % 3);
ret = i2s_write(dev_i2s, mem_block, BLOCK_SIZE);
if (ret == 0) {
idx++;
} else if (ret == -EAGAIN) {
k_mem_slab_free(&tx_slab, mem_block);
k_msleep(1);
} else {
printk("I2S write error (%d)\n", ret);
k_mem_slab_free(&tx_slab, mem_block);
break;
}
}
/* ---------- Stop ---------- */
i2s_trigger(dev_i2s, I2S_DIR_TX, I2S_TRIGGER_DROP);
printk("I2S test done\n");
return 0;
} cirrect it please check and solve my problem in i2c and i2s