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,

    Thank you for your patience with this.

    From the nRF5340 I2S documentation we have that:

    LRCK = MCK / CONFIG.RATIO

    and

    The serial clock (SCK), often referred to as the serial bit clock, pulses once for each data bit being transferred on the serial data lines SDIN and SDOUT.

    When operating in Master mode, the SCK is generated from the MCK, and the frequency of SCK is then given as:
    SCK = 2 * LRCK * CONFIG.SWIDTH 


    I do not however see that this should produce a SCK equal to the MCK in your case. Could you possibly share the oscilloscope traces so that I could have a look?

    I also notice that you are not setting the mck_setup explicitly - could you try this, and see if it then achieves the mck frequency you were intending?

    Best regards,
    Karl

  • Hi Karl,

    Thank you for the help so far.

    I've attached the oscilloscope traces showing master clock (MCLK) and serial clock (SCLK) being equal.

    Here is a screenshot showing all I2S signals:

    As can be seen, the LRCLK is close to the expected sample frequency of 48kHz.

    Furthermore, using the equation for calculating SCK:   SCK = 2 * LRCK * CONFIG.SWIDTH

    SCK = 2 * 48 kHz * 32 = 3.072 MHz       which is close to the measured SCLK = 3.012 MHz from the trace

    I tried to find a setting for mck_setup that you referred or anything related to master clock, but I cannot find where in i2s_config this is?

    Or is the master clock configured in the overlay somhow?

    Best regards,

    Steinar

  • Hello Steinar,

    Sorry for the late reply. Karl is out of office, so I will try to look into this ticket. 

    Is it possible to share a minimized application replicate the issue that you are seeing? We are a bit short staffed due to easter holidays, so if I could use this to get a flying start, that would be really helpful.

    Best regards,

    Edvin

  • Hi Edvin,

    Thank you for the reply.

    In addition to the code already submitted above in the original ticket, you can add the following for a minimized application to replicate the issue I am seeing:

    #include <zephyr/kernel.h>
    
    static uint8_t mem_block[BLOCK_SIZE];
    
    int main(void)
    {
    	i2s_init();
    
    	while (1)
    	{
    		uint32_t block_size;
    
    		i2s_buf_read(i2s_dev_rx, &mem_block, &block_size);
    	}
    }

    All that is needed in the prj.conf is: 

    CONFIG_I2S=y

    The application will print the following to the terminal when started:
    *** Booting nRF Connect SDK v2.5.0 ***
    [00:00:00.411,102] <inf> i2s_nrfx: I2S MCK frequency: 3072000, actual PCM rate: 48000
    Kind regards,
    Steinar
Related