SPH0645 I2S MIC DATA FORMAT

Hi,I have been using nrf5340 audio DK Board to access the I2S .I am using SPH0645 mic with that i need to get mic data .My Mic datasheet specifies that "The Data Format is I2S, 24-bit, 2’s compliment, MSB first. The data precision is 18 bits; unused bits are zeros".Do I need to convert the value ? As it specifies data of 2's complement value in order to get original mic data or it will be internally converted by the nrf5340 controller itself.I am taking data size of 32 bit and in that I am taking 16 bit of each mic data and confirm me regarding data format whether to be Left-justified or Right-justified.I am attaching the Datasheet of I2s mic.

Regards,

Kashyap

Parents Reply Children
  • Sample 1578: -1755
    Sample 1579: -1748
    Sample 1580: -1735
    Sample 1581: -1717
    Sample 1582: -1701
    Sample 1583: -1688
    Sample 1584: -1679
    Sample 1585: -1676
    Sample 1586: -1683
    Sample 1587: -1695
    Sample 1588: -1695
    Sample 1589: -1685
    Sample 1590: -1676
    Sample 1591: -1682
    Sample 1592: -1707
    Sample 1593: -1719
    Sample 1594: -1708
    Sample 1595: -1702
    Sample 1596: -1719
    Sample 1597: -1741
    Sample 1598: -1757
    Sample 1599: -1760
    Sample 1568: -1752
    --- 1568 messages dropped ---
    Sample 1569: -1752
    Sample 1570: -1743
    Sample 1571: -1731
    Sample 1572: -1722
    Sample 1573: -1713
    Sample 1574: -1699
    Sample 1575: -1683
    Sample 1576: -1661
    Sample 1577: -1643
    Sample 1578: -1640
    Sample 1579: -1647
    Sample 1580: -1661
    Sample 1581: -1670
    Sample 1582: -1674
    Sample 1583: -1691
    Sample 1584: -1722
    Sample 1585: -1747
    Sample 1586: -1759
    Sample 1587: -1760
    Sample 1588: -1748
    Sample 1589: -1745
    Sample 1590: -1752
    Sample 1591: -1748
    Sample 1592: -1752
    Sample 1593: -1769
    Sample 1594: -1780
    Sample 1595: -1783
    Sample 1596: -1786
    Sample 1597: -1789
    Sample 1598: -1792
    Sample 1599: -1797
    Sample 1568: -1738
    --- 1568 messages dropped ---
    Sample 1569: -1723
    Sample 1570: -1720
    Sample 1571: -1700
    Sample 1572: -1655
    Sample 1573: -1617
    Sample 1574: -1606
    Sample 1575: -1616
    Sample 1576: -1609
    Sample 1577: -1589
    Sample 1578: -1577
    Sample 1579: -1584
    Sample 1580: -1603
    Sample 1581: -1606
    Sample 1582: -1621
    Sample 1583: -1650
    Sample 1584: -1665
    Sample 1585: -1678
    Sample 1586: -1699
    Sample 1587: -1711
    Sample 1588: -1712
    Sample 1589: -1721
    Sample 1590: -1731
    Sample 1591: -1733
    Sample 1592: -1737
    Sample 1593: -1734
    Sample 1594: -1743
    Sample 1595: -1768
    Sample 1596: -1787
    Sample 1597: -1801
    Sample 1598: -1815
    Sample 1599: -1823
    Sample 1568: -1735
    --- 1568 messages dropped ---
    Sample 1569: -1770
    Sample 1570: -1783
    Sample 1571: -1805
    Sample 1572: -1837
    Sample 1573: -1828
    Sample 1574: -1794
    Sample 1575: -1785
    Sample 1576: -1820
    Sample 1577: -1837
    Sample 1578: -1773
    Sample 1579: -1703
    Sample 1580: -1716
    Sample 1581: -1759
    Sample 1582: -1747
    Sample 1583: -1660
    Sample 1584: -1597
    Sample 1585: -1646
    Sample 1586: -1710
    Sample 1587: -1706
    Sample 1588: -1696
    Sample 1589: -1722
    Sample 1590: -1727
    Sample 1591: -1675
    Sample 1592: -1631
    Sample 1593: -1609
    Sample 1594: -1604
    Sample 1595: -1662
    Sample 1596: -1704
    Sample 1597: -1713
    Sample 1598: -1723
    Sample 1599: -1699
    Sample 1568: -1776
    --- 1568 messages dropped ---
    Sample 1569: -1795
    Sample 1570: -1772
    Sample 1571: -1772
    Sample 1572: -1821
    Sample 1573: -1828
    Sample 1574: -1748
    Sample 1575: -1688
    Sample 1576: -1690
    Sample 1577: -1703
    Sample 1578: -1695
    Sample 1579: -1677
    Sample 1580: -1675
    Sample 1581: -1663
    Sample 1582: -1637
    Sample 1583: -1613
    Sample 1584: -1601
    Sample 1585: -1628
    Sample 1586: -1658
    Sample 1587: -1644
    Sample 1588: -1647
    Sample 1589: -1687
    Sample 1590: -1701
    Sample 1591: -1676
    Sample 1592: -1675
    Sample 1593: -1714
    Sample 1594: -1724
    Sample 1595: -1703
    Sample 1596: -1717
    Sample 1597: -1752
    Sample 1598: -1771
    Sample 1599: -1766
    Sample 1568: -1763
    --- 1568 messages dropped ---
    Sample 1569: -1745
    Sample 1570: -1728
    Sample 1571: -1726
    Sample 1572: -1732
    Sample 1573: -1749
    Sample 1574: -1769
    Sample 1575: -1767

    &pinctrl {
    i2s0_default: i2s0_default {
    group1 {
    psels = <NRF_PSEL(I2S_SCK_M, 0, 26)>,
    <NRF_PSEL(I2S_LRCK_M, 0, 25)>,
    <NRF_PSEL(I2S_SDIN, 0, 7)>,
    <NRF_PSEL(I2S_SDOUT, 0, 6)>;
    };
    };
    i2s0_sleep: i2s0_sleep {
    group1 {
    psels = <NRF_PSEL(I2S_SCK_M, 0, 26)>,
    <NRF_PSEL(I2S_LRCK_M, 0, 25)>,
    <NRF_PSEL(I2S_SDIN, 0, 7)>,
    <NRF_PSEL(I2S_SDOUT, 0, 6)>;
    };
    };
    };
    
    &clock {
    status = "okay";
    hfclkaudio-frequency = <12288000>;
    };
    
    
    i2s_rxtx: &i2s0 {
    status = "okay";
    pinctrl-0 = <&i2s0_default>;
    pinctrl-1 = <&i2s0_sleep>;
    clock-source = "ACLK";
    pinctrl-names = "default","sleep";
    };

     Ok,In that case help me with getting data as I am not able to get mic data correctly.Here I am just reading and logging manually and using that i iam converting to wav file externally to check audio.But using this I am not getting all data,some data is not printing and dont know it is correct data or not

    #include <zephyr/kernel.h>
    #include <zephyr/sys/printk.h>
    #include <zephyr/drivers/i2s.h>
    #include <zephyr/drivers/gpio.h>
    #include <string.h>
    #include <zephyr/drivers/uart.h>
    
    #define I2S_RX_NODE DT_NODELABEL(i2s_rxtx)
    
    #define SAMPLE_FREQUENCY 16000
    #define SAMPLE_BIT_WIDTH 32
    #define BYTES_PER_SAMPLE sizeof(int16_t)
    #define NUMBER_OF_CHANNELS 1
    #define SAMPLES_PER_BLOCK ((SAMPLE_FREQUENCY / 10) * NUMBER_OF_CHANNELS)
    #define INITIAL_BLOCKS 2
    #define TIMEOUT 1000
    
    #define BLOCK_SIZE (BYTES_PER_SAMPLE * SAMPLES_PER_BLOCK)
    #define BLOCK_COUNT (INITIAL_BLOCKS + 2)
    K_MEM_SLAB_DEFINE_STATIC(mem_slab, BLOCK_SIZE, BLOCK_COUNT, 4);
    
    const struct device *const i2s_dev_rx = DEVICE_DT_GET(I2S_RX_NODE);
    struct i2s_config config;
    
    static int16_t echo_block[SAMPLES_PER_BLOCK];
    uint8_t loopCnt = 0;
    
    static void process_block_data(void *mem_block, uint32_t number_of_samples)
    {
    	for (int i = 0; i < number_of_samples; ++i)
    	{
    		int16_t *sample = &((int16_t *)mem_block)[i];
    		echo_block[i] = *sample;
    	  }
    	 
    }
    
    static bool configure_streams(const struct device *i2s_dev_rx, const struct i2s_config *config)
    {
    	int ret = i2s_configure(i2s_dev_rx, I2S_DIR_RX, config);
    	if (ret == 0)
    	{
    		printk("Configure Stream Successful.\n");
    		return true;
    	}
    	printk("Failed to configure streams: %d\n", ret);
    	return false;
    }
    
    static bool trigger_command(const struct device *i2s_dev_rx, enum i2s_trigger_cmd cmd)
    {
    	int ret = i2s_trigger(i2s_dev_rx, I2S_DIR_RX, cmd);
    	if (ret == 0)
    	{
    		printk("Trigger Command Successful.\n");
    		return true;
    	}
    	printk("Failed to trigger command %d: %d\n", cmd, ret);
    	return false;
    }
    
    static void i2sStreamInit(void){
    
    	printk("I2S echo sample\n");
    
    	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;
    
    	if (!configure_streams(i2s_dev_rx, &config))
    	{
    		return 0;
    	}
    
    	if (!trigger_command(i2s_dev_rx, I2S_TRIGGER_START))
    	{
    		return 0;
    	}
    }
    
    
    
    int main(void)
    {
    	i2sStreamInit();
    	printk("Streams started\n");
    
    	while (1)
    	{
    		void *mem_block;
    		uint32_t block_size;
    		int ret;
    
    		ret = i2s_read(i2s_dev_rx, &mem_block, &block_size);
    		if (ret < 0)
    		{
    			printk("Failed to read data: %d\n", ret);
    			break;
    		}
    
    		process_block_data(mem_block, SAMPLES_PER_BLOCK);
    		k_mem_slab_free(&mem_slab, mem_block);
    
    	}
    	printk("Exiting main loop\n");
    	return 0;
    }

  • Hi,I able to solve the issue ,the main thing is by logging data by RTT the buffer size is very low 1024 which is default once I increased size it works this is done by increasing CONFIG_SEGGER_RTT_BUFFER_SIZE_UP,CONFIG_LOG_BUFFER_SIZE


    CONFIG_LOG_MODE_OVERFLOW=n
    CONFIG_SEGGER_RTT_BUFFER_SIZE_UP=1024
    CONFIG_LOG_BUFFER_SIZE=1024

    CONFIG_LOG_PROCESS_THREAD_STACK_SIZE=2048
    But I dont know what size can I give in that.I increased it randomly by keeping 50k my sampling rate is 16k and each sample 16bits .Can you help me what size do I need to define correctly.
  • Great to see that you've gotten further and also shared this solution Kashyap.

    Kashyap23 said:
    But I dont know what size can I give in that.

    I am not sure if I understand the problem, but CONFIG_SEGGER_RTT_BUFFER_SIZE_UP doesn't seem to have a max but several samples increase it from the default 1024 to 4096. CONFIG_LOG_BUFFER_SIZE is often increased to 4096 as well.

    Maybe it seems practical at the moment, though logging every single measurement doesn't seem necessary in a final application to me. So I guess this isn't a big issue either way, no matter what you increase it to for the time being.

    Regards,

    Elfving

Related