STM32 I2S.

The I2S protocol, not to be confused with I2C, is designed for transmitting an audio stream. In my device this protocol is used to transfer the audio stream between a codec and the MCU.
I'd like to note right away that STM32 has no separate I2S module as such; the one it has is implemented on the basis of the SPI module, and they share the same DR[0:15] register.

The STM32 I2S has the following features:
  • full-duplex communication
  • half-duplex communication (receiver or transmitter only)
  • can operate in both master and slave mode
  • an 8-bit programmable prescaler allows setting the audio frequency from 8KHz to 192KHz with high accuracy
  • data transfer in 16, 24, 32-bit format
  • the packet size depends on the data size: 16 bits for 16-bit data and 32 bits for 16-, 24- and 32-bit data per audio channel
  • programmable clock polarity
  • an underrun flag in slave transmit mode, a frame error flag for slave receive and transmit, and an overrun flag on receive in both modes
  • one 16-bit register for receiving and transmitting data for both channels

  • supported protocols:
    – I2S Phillps standard
    – MSB-justified standard (left-justified)
    – LSB-justified standard (right-justified)
    – PCM standard (with short and long frame synchronization on 16-bit channel frame
    or 16-bit data frame extended to 32-bit channel frame)

  • data is always transmitted MSB first
  • DMA with a 16-bit bus width can be used for receiving and transmitting
  • the master clock can be output to clock external audio components at a fixed frequency of 256*Fs
  • a dedicated PLL is used to achieve high clock accuracy
  • can be clocked from an external source via the I2S_CKIN pin


You can see the module structure in the picture below by clicking on it.
STM32 I2S.

I'd also like to draw attention to the dedicated PLL, which is used to obtain the required clock frequency.
STM32 I2S.


Another interesting point worth noting is that the MCU can operate in both Master and Slave mode. The difference is that the Master generates the clock signal, while the Slave simply receives it.

SPI_I2SCFGR configuration register:

I2SMOD(I2S mode selection) — a one in this bit switches the module to I2S mode; otherwise the module works in SPI mode.

I2SE(I2S Enable) — a one in this bit enables the I2S module.

I2SCFG(I2S configuration mode) — this bit field configures the I2S mode.
00: Slave - transmit
01: Slave - receive
10: Master - transmit
11: Master - receive

PCMSYNC(PCM frame synchronization) — a one in this bit enables long frame synchronization.

I2SSTD(I2S standard selection) — this bit field selects one of the protocols.
00: I2S Philips standard.
01: MSB justified standard (left justified)
10: LSB justified standard (right justified)
11: PCM standard

CKPOL(Steady state clock polarity) — this bit sets the clock polarity in the idle state. 0 — low level when idle, 1 — high level when idle.

DATLEN(Data length to be transferred) — this bit field sets the data transfer format.
00: 16-bit data length
01: 24-bit data length
10: 32-bit data length
11: Not allowed

CHLEN(Channel length (number of bits per audio channel)) — this bit sets the packet length for one channel.


SPI_I2SPR prescaler register

MCKOE(Master clock output enable) — a one in this bit outputs the master clock.

ODD(Odd factor for the prescaler) — this bit affects the prescaler. If it is zero, the prescaler is calculated as I2SDIV *2; if it is one, as (I2SDIV * 2)+1.

I2SDIV [7:0](I2S Linear prescaler) — the bit field that sets the prescaler. The values 0 and 1 are forbidden.

Initialization from a working project in which the MCU operates in Slave - receive mode.

void I2S2_Init(void)
{
	/*
			WS  -> PB12
			CLK -> PB13
			SD  -> PB15
	*/
	
	//enable port and SPI clocking
	RCC->AHB1ENR |= RCC_AHB1ENR_GPIOBEN;
	RCC->APB1ENR |= RCC_APB1ENR_SPI2EN;
	
	//enable I2S clocking from the PLL
	RCC->CFGR |= RCC_CFGR_I2SSRC;
	
	//alternate function push pull with pull-down, 50 MHz
	GPIOB->MODER |= GPIO_MODER_MODER12_1 | GPIO_MODER_MODER13_1 | GPIO_MODER_MODER15_1;
	GPIOB->PUPDR |= GPIO_PUPDR_PUPDR12_0 | GPIO_PUPDR_PUPDR13_0 | GPIO_PUPDR_PUPDR15_0;
	GPIOB->OSPEEDR |= GPIO_OSPEEDER_OSPEEDR12 | GPIO_OSPEEDER_OSPEEDR13 | GPIO_OSPEEDER_OSPEEDR15; 
	 
        //assign the alternate function to the pins 
        GPIOB->AFR[1] |= (0x05<<4*4);
	GPIOB->AFR[1] |= (0x05<<5*4);
	GPIOB->AFR[1] |= (0x05<<7*4);
      
        SPI2->I2SCFGR |= SPI_I2SCFGR_I2SMOD;//select I2S mode
	SPI2->I2SCFGR |= SPI_I2SCFGR_I2SCFG_0;//Slave - receive
	SPI2->I2SCFGR &= ~SPI_I2SCFGR_CKPOL;//low level on the CLK pin when idle
	SPI2->I2SCFGR &= ~SPI_I2SCFGR_I2SSTD;//I2S Philips
	SPI2->I2SCFGR |= SPI_I2SCFGR_DATLEN_0;//24-bit data
	SPI2->I2SCFGR |= SPI_I2SCFGR_CHLEN;//32-bit packet length
	//SPI2->CR2 |= SPI_CR2_RXDMAEN;//DMA
        SPI2->I2SCFGR |= SPI_I2SCFGR_I2SE;//enable I2S
}


If you run this code, the module will turn on, but the data in the DR register will be corrupted. To solve this, you need to consult the Errata sheet, which says that in our case I2S must be enabled when the WS line is high.

The I2S peripheral must be enabled when the external master sets the WS line at:
• High level when the I2S protocol is selected.
• Low level when the LSB or MSB-justified mode is selected.


This can be done by enabling an external interrupt on the WS pin.

void WS_EXTI_Init(void)
{
     //select the port and pin for the external interrupt
     SYSCFG->EXTICR [3] |= SYSCFG_EXTICR4_EXTI12_PB;
     //on the rising edge
     EXTI->RTSR |= EXTI_RTSR_TR12;
     //set the mask
     EXTI->IMR |= EXTI_IMR_MR12;
     //enable EXTI5 interrupts
     NVIC_EnableIRQ(EXTI15_10_IRQn);
     //NVIC_SetPriority(EXTI15_10_IRQn,15);
}	


And enable the module inside the interrupt handler.

void EXTI15_10_IRQHandler(void)
{
       SPI2->I2SCFGR |= SPI_I2SCFGR_I2SE;//enable I2S

	EXTI->IMR &= ~EXTI_IMR_MR12;
	EXTI->PR |= EXTI_PR_PR12;//reset interrupt flag
	EXTI->PR |= EXTI_PR_PR12;//reset interrupt flag
}