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

nrf9160 I2S setting

I want to use I2S on nrf9160. I found some code on the its user manual and try to run in my project. But when the device is running, it always reboots. What other configuration I need to do?

This is my code. The code can be biult successfully but it will have errors in the device. The picture is the erro information.

void init_i2s(void)
{
  //Enable reception
  NRF_I2S->CONFIG.RXEN = I2S_CONFIG_RXEN_RXEN_Enabled << I2S_CONFIG_RXEN_RXEN_Pos;

  //Enable transmission
  NRF_I2S->CONFIG.TXEN = I2S_CONFIG_TXEN_TXEN_Enabled << I2S_CONFIG_TXEN_TXEN_Pos;

  //Enable MCK generator
  NRF_I2S->CONFIG.MCKEN = I2S_CONFIG_MCKEN_MCKEN_Enabled << I2S_CONFIG_MCKEN_MCKEN_Pos;

  //MCKFREQ = 4MHz
  NRF_I2S->CONFIG.MCKFREQ = I2S_CONFIG_MCKFREQ_MCKFREQ_32MDIV8 << I2S_CONFIG_MCKFREQ_MCKFREQ_Pos;

  //Ratio = 256
  NRF_I2S->CONFIG.RATIO = I2S_CONFIG_RATIO_RATIO_256X << I2S_CONFIG_RATIO_RATIO_Pos;

  //MCKFREQ = 4 MHz and Ratio = 256 gives sample rate = 15.625 ks/s
  //Sample width = 16 bit
  NRF_I2S->CONFIG.SWIDTH = I2S_CONFIG_SWIDTH_SWIDTH_16Bit << I2S_CONFIG_SWIDTH_SWIDTH_Pos;

  //Alignment = Left
  NRF_I2S->CONFIG.ALIGN = I2S_CONFIG_ALIGN_ALIGN_Left << I2S_CONFIG_ALIGN_ALIGN_Pos;

  //Format = I2S
  NRF_I2S->CONFIG.FORMAT = I2S_CONFIG_FORMAT_FORMAT_I2S << I2S_CONFIG_FORMAT_FORMAT_Pos;

  //Use Stereo
  NRF_I2S->CONFIG.CHANNELS = I2S_CONFIG_CHANNELS_CHANNELS_Stereo << I2S_CONFIG_CHANNELS_CHANNELS_Pos;

  //Map IO pins using the PINSEL register
  //MCK to pin 25, SCK to pin 14, LRCK to pin 13, SDOUT to pin 17, SDIN to pin 16
  NRF_I2S->PSEL.MCK = 25;
  NRF_I2S->PSEL.SCK = 14;
  NRF_I2S->PSEL.LRCK = 13;
  NRF_I2S->PSEL.SDOUT = 17;
  NRF_I2S->PSEL.SDIN = 16;

  //Configure TX and RX data pointers
  //nrf9160 line 650-670
  NRF_I2S->TXD.PTR = my_tx_buf;
  NRF_I2S->RXD.PTR = my_rx_buf;
  NRF_I2S->RXTXD.MAXCNT = MY_BUF_SIZE;

  //Enable the I2S module 
  NRF_I2S->ENABLE = 1;

  //Start audio streaming using the START task
  NRF_I2S->TASKS_START = 1;

  //Handle the received and transmitted data when receiving the TXPTRUPD and RXPTRUPD events
  if(NRF_I2S->EVENTS_TXPTRUPD != 0)
  {
   NRF_I2S->TXD.PTR = my_next_tx_buf;
   NRF_I2S->EVENTS_TXPTRUPD = 0;
  }
  if(NRF_I2S->EVENTS_RXPTRUPD != 0)
  {
   NRF_I2S->RXD.PTR = my_next_rx_buf;
   NRF_I2S->EVENTS_RXPTRUPD = 0;
  }
}

Related