No bclk, LRCK using i2s peripheral on RW612 zephyr As a student intern i'm currently working on the i2s example in zephyr. i modified to code so i could output a sound using an audio amplifier and a speaker. after configuring the pins in MCUXpresso config tools, i have managed to get a signal on MCLK on J1.7 after adding
IO_MUX_SetPinMux(IO_MUX_MCLK); in to the main. But adding
IO_MUX_SetPinMux(IO_MUX_FC1_I2S); doesn't show anything on J1.15, 13 and 9. Bellow is my code:
/*
* Copyright (c) 2020 Antmicro <www.antmicro.com>
*
* SPDX-License-Identifier: Apache-2.0
*/
#include
#include
#include
#include
#include
#include "fsl_io_mux.h"
#define AUDIO_SAMPLE_FREQ (44100)
#define AUDIO_SAMPLES_PER_CH_PER_FRAME (128)
#define AUDIO_NUM_CHANNELS (2)
#define AUDIO_SAMPLES_PER_FRAME \
(AUDIO_SAMPLES_PER_CH_PER_FRAME * AUDIO_NUM_CHANNELS)
#define AUDIO_SAMPLE_BYTES (3)
#define AUDIO_SAMPLE_BIT_WIDTH (24)
#define AUDIO_FRAME_BUF_BYTES (AUDIO_SAMPLES_PER_FRAME * AUDIO_SAMPLE_BYTES)
#define I2S_PLAY_BUF_COUNT (500)
#define I2S_DEV_NODE_TX DT_ALIAS(i2s_node0)
static const struct device *host_i2s_tx_dev;
static struct k_mem_slab i2s_tx_mem_slab;
static char tx_buffer[AUDIO_FRAME_BUF_BYTES * I2S_PLAY_BUF_COUNT];
static struct i2s_config i2s_tx_cfg;
static int ret;
static void init(void)
{
printk("INIT ENTERED\n");
//configure tx device
host_i2s_tx_dev = DEVICE_DT_GET_OR_NULL(I2S_DEV_NODE_TX);
if (host_i2s_tx_dev == NULL) {
printk("I2S TX device not found\n");
exit(-1);
}
if (!device_is_ready(host_i2s_tx_dev)) {
printk("I2S TX device not ready\n");
exit(-1);
}
printk("I2S TX device ready\n");
k_mem_slab_init(&i2s_tx_mem_slab, tx_buffer, AUDIO_FRAME_BUF_BYTES,
I2S_PLAY_BUF_COUNT);
// configure i2s for audio playback
i2s_tx_cfg.word_size = AUDIO_SAMPLE_BIT_WIDTH;
i2s_tx_cfg.channels = AUDIO_NUM_CHANNELS;
i2s_tx_cfg.format = I2S_FMT_DATA_FORMAT_I2S;
i2s_tx_cfg.options = I2S_OPT_BIT_CLK_CONTROLLER |
I2S_OPT_FRAME_CLK_CONTROLLER;
i2s_tx_cfg.frame_clk_freq = AUDIO_SAMPLE_FREQ;
i2s_tx_cfg.block_size = AUDIO_FRAME_BUF_BYTES;
i2s_tx_cfg.mem_slab = &i2s_tx_mem_slab;
i2s_tx_cfg.timeout = -1;
ret = i2s_configure(host_i2s_tx_dev, I2S_DIR_TX, &i2s_tx_cfg);
if (ret != 0) {
printk("i2s_configure failed with %d error\n", ret);
exit(-1);
}
}
int main(void)
{
printk("MAIN START\n");
IO_MUX_SetPinMux(IO_MUX_FC1_I2S);
IO_MUX_SetPinMux(IO_MUX_MCLK);
init();
printk("INIT FINISHED\n");
void *tx_mem_block;
uint32_t phase = 0;
const uint32_t tone_freq = 1000;
const int32_t amplitude = 0x200000; /* moderate 24-bit volume */
bool started = false;
while (true) {
ret = k_mem_slab_alloc(&i2s_tx_mem_slab,
&tx_mem_block,
K_FOREVER);
printk("BUFFER ALLOCATED\n");
if (ret != 0) {
printk("TX buffer allocation failed\n");
continue;
}
uint8_t *buf = tx_mem_block;
for (int i = 0; i < AUDIO_SAMPLES_PER_CH_PER_FRAME; i++) {
int32_t sample;
/* Generate 1 kHz square-wave tone */
if (phase < (AUDIO_SAMPLE_FREQ / 2)) {
sample = amplitude;
} else {
sample = -amplitude;
}
phase += tone_freq;
if (phase >= AUDIO_SAMPLE_FREQ) {
phase -= AUDIO_SAMPLE_FREQ;
}
/* Left channel - 24 bit */
*buf++ = sample & 0xFF;
*buf++ = (sample >> 8) & 0xFF;
*buf++ = (sample >> 16) & 0xFF;
/* Right channel - same signal */
*buf++ = sample & 0xFF;
*buf++ = (sample >> 8) & 0xFF;
*buf++ = (sample >> 16) & 0xFF;
}
printk("BEFORE I2S_WRITE\n");
ret = i2s_write(host_i2s_tx_dev,
tx_mem_block,
AUDIO_FRAME_BUF_BYTES);
printk("AFTER I2S_WRITE %d\n", ret);
if (ret != 0) {
printk("i2s_write failed: %d\n", ret);
k_mem_slab_free(&i2s_tx_mem_slab, tx_mem_block);
continue;
}
/* Start TX after the first buffer has been queued */
if (!started) {
printk("BEFORE WRITE\n");
ret = i2s_trigger(host_i2s_tx_dev,
I2S_DIR_TX,
I2S_TRIGGER_START);
printk("AFTER WRITE %d\n", ret);
if (ret != 0) {
printk("I2S start failed: %d\n", ret);
return ret;
}
printk("I2S TX started\n");
started = true;
}
}
return 0;
}
I've added comments to find out what part of the code is being executed. my serial monitor is showing that the printing stops after a few cycles. I suspect that the buffer gets filled after a few cycles makes the program stop. IS there anyone that can assist me in making this project work? Thank you in advance
Re: No bclk, LRCK using i2s peripheral on RW612 zephyr To have a common ground for your project development, please help me providing the following:
Zephyr version (please clarify if downstream or upstream repository).
Base I2S Zephyr example.
Please confirm if you are using FRDM-RW612.
Set Kconfigs (in prj.conf file)
Share the overlay being used (if any).
Regarding your applied pin mux settings "IO_MUX_SetPinMux(IO_MUX_FC1_I2S);" this should be defined at device tree/overlay level in an I2S node assigned to a flexcomm instance. Could you please clarify if you have defined the I2S node in your device tree?
Also, could you please clarify if the shared oscilloscope capture is from the MCLK output pin? Re: No bclk, LRCK using i2s peripheral on RW612 zephyr First off all thank you reponse, - version 4.4.1 upstream repository - example zephyr/sample s/drivers/i2s/output Main modifications compared to the original i2s/output example: 1. Removed RX path The original project was designed for RX/TX operation. RX-related code was removed because the goal is audio playback only (TX). 2. Changed audio format to 24-bit word_size = 24 to match the intended 24-bit audio output format. 3. Changed waveform generation Replaced the sine wave with a 1 kHz square wave. 4. Changed buffer handling Instead of sending a finite number of buffers, audio data is generated continuously inside the while loop. Goal: provide continuous audio output. 5. Added RW612-specific pin mux configuration Added: IO_MUX_SetPinMux(IO_MUX_FC1_I2S); IO_MUX_SetPinMux(IO_MUX_MCLK); - I'm making use of the FRDM-RW612 - prj.conf: CONFIG_LOG=y CONFIG_I2S=y CONFIG_I2S_LITEX_RX_BLOCK_COUNT=500 CONFIG_I2S_LITEX_DATA_BIG_ENDIAN=n CONFIG_ETH_LITEX_LITEETH=n CONFIG_PRINTK=y CONFIG_CONSOLE=y CONFIG_UART_CONSOLE=y - app.overlay / { aliases { i2s-node0 = &flexcomm1; }; }; &flexcomm1 { compatible = "nxp,lpc-i2s"; pinctrl-0 = <&pinmux_flexcomm1_i2s>; pinctrl-names = "default"; status = "okay"; }; The oscilloscope picture is from the MCLK output pin. Re: No bclk, LRCK using i2s peripheral on RW612 zephyr I have tested the i2s_output example vanilla and it generates properly the I2S signals.
Could you please confirm that the measurements of I2S_BCLK, I2S_DATA & I2S_WS are being taken from the following header connections of the FRDM board?
I2S_DATA - J1.2
I2S_WS - J1.4
I2S_BCLK - J2.12
Also, I noticed that you are setting the following LITEX configurations:
CONFIG_I2S_LITEX_RX_BLOCK_COUNT=500
CONFIG_I2S_LITEX_DATA_BIG_ENDIAN=n
CONFIG_ETH_LITEX_LITEETH=n
These configurations are meant to use along Litex-enabled hardware from another vendor, while these might not be applying to your project build, please replace them with Flexcomm specific configurations as below adjusted to your app requirements:
CONFIG_I2S_MCUX_FLEXCOMM_TX_BLOCK_COUNT
CONFIG_I2S_MCUX_FLEXCOMM_DMA_TX_BLOCKS
Additionally, please remove the "IO_MUX_SetPinMux(IO_MUX_FC1_I2S);" call, since this is being configured from the device tree description.
Please let me know if this information helps. Re: No bclk, LRCK using i2s peripheral on RW612 zephyr
Thank you, I was really able to benefit from your replies.
Following your suggestions, I made the following changes:
Added: CONFIG_I2S_MCUX_FLEXCOMM_TX_BLOCK_COUNT=4 removed the LITEX configurations. Removed the "IO_MUX_SetPinMux(IO_MUX_FC1_I2S)
I did not add:
CONFIG_I2S_MCUX_FLEXCOMM_DMA_TX_BLOCKS
because I could not find this Kconfig option anywhere in my Zephyr tree. I searched through the driver sources and only found references to CONFIG_I2S_MCUX_FLEXCOMM_TX_BLOCK_COUNT = 4
Regarding the signal measurements, I initially measured on:
J1.15,13 & 7 due to seeing them configured on the pinmux to flexcomm1
I repeated the measurements on the pins you specified and obtained the following results:
I2S_DATA (J1.2): No observable signal
I2S_WS (J1.4): ~51 kHz square wave
I2S_BCLK (J2.12): ~2.46 MHz square wave
So at the moment I can observe MCLK, BCLK and WS signals, but I do not observe activity on the DATA pin.
Could you please advise whether the absence of a DATA signal suggests an issue with the transmitted audio samples, I2S configuration, or if there is another aspect I should investigate next?
Re: No bclk, LRCK using i2s peripheral on RW612 zephyr Hi @Zakintern, glad to know you made progress.
From your last log, it seems that the writings are performed per the defined configuration you have set "CONFIG_I2S_MCUX_FLEXCOMM_TX_BLOCK_COUNT = 4", which defines the I2S TX queue to 4 blocks, please try by adjusting the number of blocks as per your application requirements.
As a side note, the configuration "CONFIG_I2S_MCUX_FLEXCOMM_DMA_TX_BLOCKS" has been added to Zephyr main branch, it might be available to be used on Zephyr v4.5.0 release.
Please let me know if this helps. Re: No bclk, LRCK using i2s peripheral on RW612 zephyr After troubling shooting I've found out that the The first several i2s_write() calls return successfully but the application eventually blocks inside:
printk("BEFORE I2S_WRITE\n");
ret = i2s_write(host_i2s_tx_dev,
tx_mem_block,
AUDIO_FRAME_BUF_BYTES);
printk("AFTER I2S_WRITE %d\n", ret);
It looks like the buffers are being queued but it eventually fills up. The hardware may not be consuming the queued audio data?
Re: No bclk, LRCK using i2s peripheral on RW612 zephyr Hi Roman, Thanks for point this out. I've made sure to download the 4.5 version of zephyr which includes
CONFIG_I2S_MCUX_FLEXCOMM_TX_BLOCK_COUNT=8
CONFIG_I2S_MCUX_FLEXCOMM_DMA_TX_BLOCKS=8 These are also my correct configuration. My serial monitor shows that I2S right happens successfully, but the the log eventually stops without outputting any data on the data pin GPIO8 J1.
It looks like the buffer eventually fills up and noting gets freed up by the hardware.
Re: No bclk, LRCK using i2s peripheral on RW612 zephyr Hi @Zakintern, thanks for providing more details.
Could you please clarify if you have tried the i2s_output example no modifications done (vanilla)?
To follow up on this regard, I'd suggest to first test the base example and then implement your modifications accordingly, since the base example works properly and can serve as reference to know the required API flow to output the data through the hardware.
I tested the example and I'm able to see data in the mentioned pin (header J1 terminal 2 from the FRDM board), please make sure to do the measurement in this pin, as GPIO8 is the I2S_WS terminal; GPIO9 is the I2S_DATA terminal. Re: No bclk, LRCK using i2s peripheral on RW612 zephyr The base example does work now, I've managed to get it to work after changing CONFIG_I2S_MCUX_FLEXCOMM_TX_BLOCK_COUNT from 8 to 16 in the prj.conf. Thank you for your help, I will use this as a base and implement my modifications.
View full article