This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Adafruit I2S MEMS Microphone Breakout with the NRF9160 DK

Hello,

I am working on a project where I can have Voice over IP on the NRF9160 DK. I am now working with a I2S microphone (https://www.adafruit.com/product/3421) at the moment I have trouble programming I2S. I read the I2S documentation on the zephyr website but I have still trouble understanding it and there is also no example where I can look at. I don't know how to setup the struct device (https://docs.zephyrproject.org/2.0.0/reference/peripherals/i2s.html?highlight=i2s#) that you use in the functions to setup i2s and read/write the buffer. I also don't know which pin i have to connect to the microphone, because the pin names are not the same or can i just connect  to any pin and write in the code to use that pin? If sombody has an I2S example I would really appreciate that. I also need to find a speaker that I can use any suggestions?

Thank you in advance

Parents
  • Hi Nahom,

    You can configure any of the available GPIO pins for the nrf9160.

    So you could use the command https://developer.nordicsemi.com/nRF_Connect_SDK/doc/latest/zephyr/reference/peripherals/i2s.html#_CPPv413i2s_configureP6device7i2s_dirP10i2s_config and in the .overlay file select which pins it should get. 

    See the device tree chapter in the NCS tutorials.

  • Hi Martin,

    I am having trouble with the i2s_configure function this is my code and overlay file. I call the recordaudio function in a different file and then I something goes wrong in i2s_init funtion  at i2s_configure. The code stops after the i2s_configure  function.

    Thank you in advance

    #include <zephyr.h>
    #include <device.h>
    #include <drivers/i2s.h>
    #include <sys/printk.h>
    #include <stdbool.h>
    
    #include "microphone.h"
    #include "button.h"
    #include "udp.h"
    
    #define  MICROPHONE_SAMPLE_FREQ (8000)
    #define  MICROPHONE_SAMPLE_PER_FRAME (1)
    #define  MICROPHONE_NUM_CHANNELS (1)
    #define  MICROPHONE_SAMPLE_BIT_WIDTH (24)
    #define  MICROPHONE_SAMPLE_BYTES (2)
    #define  MICROPHONE_BLOCK_SIZE (4)
    #define  DEVICE_NAME  "I2S_0" 
    
    
    struct k_mem_slab mic_mem_slab;
    struct device *i2s_device;
    
    K_MEM_SLAB_DEFINE(mic_mem_slab,MICROPHONE_BLOCK_SIZE,MICROPHONE_SAMPLE_FREQ,4);
    
    void i2s_init()
    {
      struct i2s_config i2s_cfg;
      int config;
      
      i2s_device=device_get_binding(DEVICE_NAME);
      
      i2s_cfg.word_size=MICROPHONE_SAMPLE_BIT_WIDTH;
      i2s_cfg.channels=MICROPHONE_NUM_CHANNELS;
      i2s_cfg.format=I2S_FMT_DATA_FORMAT_I2S;
      i2s_cfg.options=I2S_OPT_FRAME_CLK_MASTER |I2S_OPT_BIT_CLK_MASTER;//?
      i2s_cfg.frame_clk_freq=MICROPHONE_SAMPLE_FREQ;
      i2s_cfg.mem_slab=&mic_mem_slab;
      i2s_cfg.block_size=MICROPHONE_BLOCK_SIZE*MICROPHONE_SAMPLE_FREQ;
      i2s_cfg.timeout=K_FOREVER;
      
      config=i2s_configure(i2s_device,I2S_DIR_RX,&i2s_cfg);
      if(config!=0){
          printk("i2s_configure failed with %d error", config);
      }
     
    
    }
    
    void recordaudio(void)
    {
      int ret;
      int trigger;
      void *buf;  //uint32_t
      size_t size;
      //configure i2s and start
      i2s_init();
      printk("Starting recording...");
      
      trigger=i2s_trigger(i2s_device,I2S_DIR_RX,I2S_TRIGGER_START); 
      if(trigger!=0){
        printk("TRIGGER_START failed with %d error", trigger);
      }
      
      while(startrecording==true){
        ret=i2s_read(i2s_device,&buf,&size);
        if(ret!=0){
          printk("i2s_read failed with %d error", ret);
        }
      }
        i2s_trigger(i2s_device,I2S_DIR_RX,I2S_TRIGGER_STOP);
        if(trigger!=0){
          printk("TRIGGER_STOP failed with %d error", trigger);
        }
    
        i2s_trigger(i2s_device,I2S_DIR_RX,I2S_TRIGGER_DROP);
        if(trigger!=0){
          printk("TRIGGER_DROP failed with %d error", trigger);
        }
      printk("Exiting program...");
    }
    
    
    

    &i2s0 {
    	    status = "okay";
            sdout-pin=<8>;
    	    lrck-pin=<9>;
            sck-pin=<10>;
            sdin-pin=<11>;
            mck-pin=<12>;
    };
    

Reply
  • Hi Martin,

    I am having trouble with the i2s_configure function this is my code and overlay file. I call the recordaudio function in a different file and then I something goes wrong in i2s_init funtion  at i2s_configure. The code stops after the i2s_configure  function.

    Thank you in advance

    #include <zephyr.h>
    #include <device.h>
    #include <drivers/i2s.h>
    #include <sys/printk.h>
    #include <stdbool.h>
    
    #include "microphone.h"
    #include "button.h"
    #include "udp.h"
    
    #define  MICROPHONE_SAMPLE_FREQ (8000)
    #define  MICROPHONE_SAMPLE_PER_FRAME (1)
    #define  MICROPHONE_NUM_CHANNELS (1)
    #define  MICROPHONE_SAMPLE_BIT_WIDTH (24)
    #define  MICROPHONE_SAMPLE_BYTES (2)
    #define  MICROPHONE_BLOCK_SIZE (4)
    #define  DEVICE_NAME  "I2S_0" 
    
    
    struct k_mem_slab mic_mem_slab;
    struct device *i2s_device;
    
    K_MEM_SLAB_DEFINE(mic_mem_slab,MICROPHONE_BLOCK_SIZE,MICROPHONE_SAMPLE_FREQ,4);
    
    void i2s_init()
    {
      struct i2s_config i2s_cfg;
      int config;
      
      i2s_device=device_get_binding(DEVICE_NAME);
      
      i2s_cfg.word_size=MICROPHONE_SAMPLE_BIT_WIDTH;
      i2s_cfg.channels=MICROPHONE_NUM_CHANNELS;
      i2s_cfg.format=I2S_FMT_DATA_FORMAT_I2S;
      i2s_cfg.options=I2S_OPT_FRAME_CLK_MASTER |I2S_OPT_BIT_CLK_MASTER;//?
      i2s_cfg.frame_clk_freq=MICROPHONE_SAMPLE_FREQ;
      i2s_cfg.mem_slab=&mic_mem_slab;
      i2s_cfg.block_size=MICROPHONE_BLOCK_SIZE*MICROPHONE_SAMPLE_FREQ;
      i2s_cfg.timeout=K_FOREVER;
      
      config=i2s_configure(i2s_device,I2S_DIR_RX,&i2s_cfg);
      if(config!=0){
          printk("i2s_configure failed with %d error", config);
      }
     
    
    }
    
    void recordaudio(void)
    {
      int ret;
      int trigger;
      void *buf;  //uint32_t
      size_t size;
      //configure i2s and start
      i2s_init();
      printk("Starting recording...");
      
      trigger=i2s_trigger(i2s_device,I2S_DIR_RX,I2S_TRIGGER_START); 
      if(trigger!=0){
        printk("TRIGGER_START failed with %d error", trigger);
      }
      
      while(startrecording==true){
        ret=i2s_read(i2s_device,&buf,&size);
        if(ret!=0){
          printk("i2s_read failed with %d error", ret);
        }
      }
        i2s_trigger(i2s_device,I2S_DIR_RX,I2S_TRIGGER_STOP);
        if(trigger!=0){
          printk("TRIGGER_STOP failed with %d error", trigger);
        }
    
        i2s_trigger(i2s_device,I2S_DIR_RX,I2S_TRIGGER_DROP);
        if(trigger!=0){
          printk("TRIGGER_DROP failed with %d error", trigger);
        }
      printk("Exiting program...");
    }
    
    
    

    &i2s0 {
    	    status = "okay";
            sdout-pin=<8>;
    	    lrck-pin=<9>;
            sck-pin=<10>;
            sdin-pin=<11>;
            mck-pin=<12>;
    };
    

Children
No Data
Related