I2S Audio Host Mode of Operation
A typical application could be to play PCM data (8 kHz sample frequency, 16-bit data, 32-bit frame size) when interfaced to a codec client device. In this case, the SPI module is initialized to generate BCLK @ 625 kbps. Assuming a 20 MHz peripheral clock, FSPICLK = 20e6, the baud rate would be determined using Equation 19-2.
Solving for the value of SPIxBRG is shown in Equation 19-3.
The Baud Rate is now equal to 625e3. Equation 19-4 shows the resulting calculation.
If the result of Equation 19-4 is rounded to the nearest integer, SPIxBRG is now equal to 15; therefore, the effective Baud Rate is that of Equation 19-5.
The following steps can be used to set up the SPI module for operation in I2S Audio Host mode:
- If using interrupts, disable the SPIx interrupts in the respective IECx register.
- Stop and reset the SPI module by clearing the ON bit (SPIxCON1[15]).
- Reset the SPIx Control Register 1, SPIxCON1.
- Reset the SPIx Baud Rate Register, SPIxBRG.
- Clear the receive buffer.
- Clear the ENHBUF bit (SPIxCON1[0]) if using Standard Buffer mode or set the bit if using Enhanced Buffer mode.
- If using interrupts, perform these additional steps:
- Clear the SPIx interrupt flags/events in the respective IFSx register.
- Write the SPIx interrupt priority and sub-priority bits in the respective IPCx register.
- Set the SPIx interrupt enable bits in the respective IECx register.
- Clear the SPIROV bit (SPIxSTAT[6]).
- Write the desired settings to the SPIxCON1 register. The AUDMOD[1:0] bits
(SPIxCON1[25:24]) must be set to ‘
00
’ for I2S mode and the AUDEN bit (SPIxCON1[31]) must be set to ‘1
’ to enable the audio protocol. - Set the SPIxBRG register to 0x0F (to generate approximately 625 kbps sample rate with SPICLK @ 20 MHz).
- Write the desired settings to the SPIxCON1 register:
- MSTEN (SPIxCON1[5]) =
1
. - CKP (SPIxCON1[6]) =
1
. - MODE[32,16] (SPIxCON1[11:10]) =
0
for 16-bit audio channel data. - Enable SPI operation by setting the ON bit (SPIxCON1[15]).
- MSTEN (SPIxCON1[5]) =
- Transmission (and reception) will start immediately after the ON bit is set.
I2S Host Mode, 625 kbps BCLK, 16-Bit Channel Data, 32-Bit Frame
/* The following code example will initialize the SPI1 Module in I2S Host mode. */
_SPI1TXIP = 4;
SPI1STATbits.SPIROV = 0; // clear the Overflow
SPI1BRG = 0x000F; // to generate 625 kbps sample rate, SPICLK @ 20 MHz
SPI1CON1=0x80000460; // AUDEN=1, I2S mode, stereo mode,
// 16 bits/32 channel transfer, Host mode, CKP = 1
SPI1IMSKbits.SPITBFEN = 1; // SPI1 transmit buffer full generates interrupt event
_SPI1TXIE = 1; // Enable interrupts
SPI1CON1bits.ENHBUF = 1;
SPI1CON1bits.ON = 1;
// from here, the device is ready to receive and transmit data