I2S Master Clock always runs at same speed as Bit Clock

Hi,

I have used the I2S echo sample project as a starting point and try to configure it to sample data from an I2S microphone running on the nRF5340.

However, I cannot get the I2S Master Clock to run at a multiple of the Bit Clock. There doesn't seem to be any configuration related to master clock setting in i2s_config.

I have verified that the Bit Clock and LR Clock runs at the expected frequencies using a logic analyser and observe that the Master Clock runs the same as Bit Clock...

Can someone please point me in the right direction for what I am missing?

Kind regards,

Steinar Thon

Here is my code to setup and start the I2S interface:

#include <zephyr/drivers/i2s.h>

#define SAMPLE_FREQUENCY    (48000)
#define SAMPLES_PER_BLOCK   (96)
#define SAMPLE_BIT_WIDTH    (32)
#define BYTES_PER_SAMPLE    (4)
#define NUMBER_OF_CHANNELS  (2)

#define TIMEOUT             (1000)

#define I2S_RX_NODE  DT_NODELABEL(i2s_rxtx)

#define BLOCK_COUNT (16)
#define BLOCK_SIZE  (BYTES_PER_SAMPLE * SAMPLES_PER_BLOCK * NUMBER_OF_CHANNELS)
K_MEM_SLAB_DEFINE_STATIC(mem_slab, BLOCK_SIZE, BLOCK_COUNT, BLOCK_COUNT);

static const struct device *const i2s_dev_rx = DEVICE_DT_GET(I2S_RX_NODE);

static bool i2s_init(void)
{
	struct i2s_config config;

	if (!device_is_ready(i2s_dev_rx))
	{
		printk("%s is not ready\n", i2s_dev_rx->name);
		return 0;
	}
	
	config.word_size = SAMPLE_BIT_WIDTH;
	config.channels = NUMBER_OF_CHANNELS;
	config.format = I2S_FMT_DATA_FORMAT_I2S;
	config.options = I2S_OPT_BIT_CLK_MASTER | I2S_OPT_FRAME_CLK_MASTER;
	config.frame_clk_freq = SAMPLE_FREQUENCY;
	config.mem_slab = &mem_slab;
	config.block_size = BLOCK_SIZE;
	config.timeout = TIMEOUT;

	int ret = i2s_configure(i2s_dev_rx, I2S_DIR_RX, &config);

	if (ret < 0)
	{
		printk("Failed to configure RX stream: %d\n", ret);
		return false;
	}

	ret = i2s_trigger(i2s_dev_rx, I2S_DIR_RX, I2S_TRIGGER_START);
		
	if (ret < 0)
	{
		printk("Failed to trigger command I2S_TRIGGER_START on RX: %d\n", ret);
		return false;
	}
	return true;
}

Here is the overlay to configure the I2S interface and pinout:

&pinctrl {

	i2s0_default_alt: i2s0_default_alt {
		group1 {
			// I2S Mic 
			psels = <NRF_PSEL(I2S_SCK_M, 0, 27)>,
				<NRF_PSEL(I2S_MCK, 1, 10)>,
				<NRF_PSEL(I2S_LRCK_M, 1, 3)>,
				<NRF_PSEL(I2S_SDIN, 1, 2)>;
		};
	};
};

&clock {
	hfclkaudio-frequency = <12288000>;
};

i2s_rxtx: &i2s0 {
	status = "okay";
	pinctrl-0 = <&i2s0_default_alt>;
	pinctrl-names = "default";
	clock-source = "ACLK";
};

  • Hello Steinar,

    Sorry for the late replies. Karl got back, but then had to leave again. 

    I looked into this some more, and discussed with some of the authors of our I2S drivers. Unfortunately, there is no out of the box support to directly set the I2S Master Clock frequency in Zephyr, but it is possible to do so by either using the nrfx_i2s driver directly, or by modifying the nrfx_i2s driver sligtly. 

    The way that the frequency is chosen is through the find_suitable_clock() function in i2s_nrfx.c (ncs\zephyr\drivers\i2s\n2s_nrfx.c).

    If you comment out the line starting with "{  64, ..."

    from here:

    static void find_suitable_clock(const struct i2s_nrfx_drv_cfg *drv_cfg,
    				nrfx_i2s_config_t *config,
    				const struct i2s_config *i2s_cfg)
    {
    	static const struct {
    		uint16_t        ratio_val;
    		nrf_i2s_ratio_t ratio_enum;
    	} ratios[] = {
    		{  32, NRF_I2S_RATIO_32X },
    		{  48, NRF_I2S_RATIO_48X },
    		//{  64, NRF_I2S_RATIO_64X },
    		{  96, NRF_I2S_RATIO_96X },
    		{ 128, NRF_I2S_RATIO_128X },
    		{ 192, NRF_I2S_RATIO_192X },
    		{ 256, NRF_I2S_RATIO_256X },
    		{ 384, NRF_I2S_RATIO_384X },
    		{ 512, NRF_I2S_RATIO_512X }

    This function, which finds the first suitable frequency, will not find the 64 one, and it will move on to the 128 (the 96 will not match). Then you will see that the MCK that the frequency will double.

    Alternatively, you can use the nrfx driver directly, instead of the Zephyr API. If so, you can change the nrfx_i2s_config_t.mck_setup to the MCK frequency you want to use, and make sure to also adjust nrfx_i2s_config_t.ratio, to get the correct bit-clock frequency. Do this before calling nrfx_i2s_init(). It should be pretty straight forward.

    Best regards,

    Edvin

Related