18 Serial Peripheral Interface (SPI)
The Serial Peripheral Interface (SPI) module is a synchronous serial interface, useful for communicating with other peripheral or microcontroller devices. These peripheral devices may be serial EEPROMs, shift registers, display drivers, A/D Converters, etc. The SPI module is compatible with the Motorola® SPI and SIOP interfaces. All devices in the dsPIC33CK512MPT608 family include three SPI modules.
The module supports operation in two Buffer modes. In Standard mode, data are shifted through a single serial buffer. In Enhanced Buffer mode, data are shifted through a FIFO buffer. The FIFO level depends on the configured mode.
Variable length data can be transmitted and received, from 2 to 32 bits.
The module also supports Audio modes. Four different Audio modes are available.
- I2S mode
- Left Justified mode
- Right Justified mode
- PCM/DSP mode
In each of these modes, the serial clock is free-running and audio data are always transferred.
The SPI serial interface consists of four pins:
- SDIx: Serial Data Input
- SDOx: Serial Data Output
- SCKx: Shift Clock Input or Output
The SPI module can be configured to operate using two, three or four pins.
The SPI module has the ability to generate three interrupts, reflecting the events that occur during the data communication. The following types of interrupts can be generated:
- Receive interrupts are signaled by SPIxRXIF. This event occurs
when:
- RX watermark interrupt
- SPIROV =
1
- SPIRBF =
1
- SPIRBE =
1
provided the respective mask bits are enabled in SPIxIMSKL/H.
- Transmit interrupts are signalled by SPIxTXIF. This event occurs
when:
- TX watermark interrupt
- SPITUR =
1
- SPITBF =
1
- SPITBE =
1
provided the respective mask bits are enabled in SPIxIMSKL/H.
- General interrupts are signalled by SPIxGIF. This event occurs
when:
- FRMERR =
1
- SPIBUSY =
1
- SRMT =
1
provided the respective mask bits are enabled in SPIxIMSKL/H.
- FRMERR =
Block diagrams of the module in Standard and Enhanced modes are shown in Figure 18-1 and Figure 18-2.
To set up the SPIx module for the Standard Host mode of operation:
- If using interrupts:
- Clear the interrupt flag bits in the respective IFSx register.
- Set the interrupt enable bits in the respective IECx register.
- Write the SPIxIP bits in the respective IPCx register to set the interrupt priority.
- Write the desired settings to the SPIxCON1L and SPIxCON1H registers
with the MSTEN bit (SPIxCON1L[5]) =
1
. - Clear the SPIROV bit (SPIxSTATL[6]).
- Enable SPIx operation by setting the SPIEN bit (SPIxCON1L[15]).
- Write the data to be transmitted to the SPIxBUFL and SPIxBUFH registers. Transmission (and reception) will start as soon as data are written to the SPIxBUFL and SPIxBUFH registers.
To set up the SPIx module for the Standard Client mode of operation:
- Clear the SPIxBUF registers.
- If using interrupts:
- Clear the SPIxBUFL and SPIxBUFH registers.
- Set the interrupt enable bits in the respective IECx register.
- Write the SPIxIP bits in the respective IPCx register to set the interrupt priority.
- Write the desired settings to the SPIxCON1L, SPIxCON1H and SPIxCON2L
registers with the MSTEN bit (SPIxCON1L[5]) =
0
. - Clear the SMP bit.
- If the CKE bit (SPIxCON1L[8]) is set, then the SSEN bit (SPIxCON1L[7]) must be set to enable the SSx pin.
- Clear the SPIROV bit (SPIxSTATL[6]).
- Enable SPIx operation by setting the SPIEN bit (SPIxCON1L[15]).
To set up the SPIx module for the Enhanced Buffer Host mode of operation:
- If using interrupts:
- Clear the interrupt flag bits in the respective IFSx register.
- Set the interrupt enable bits in the respective IECx register.
- Write the SPIxIP bits in the respective IPCx register.
- Write the desired settings to the SPIxCON1L, SPIxCON1H and SPIxCON2L
registers with MSTEN (SPIxCON1L[5]) =
1
. - Clear the SPIROV bit (SPIxSTATL[6]).
- Select Enhanced Buffer mode by setting the ENHBUF bit (SPIxCON1L[0]).
- Enable SPIx operation by setting the SPIEN bit (SPIxCON1L[15]).
- Write the data to be transmitted to the SPIxBUFL and SPIxBUFH registers. Transmission (and reception) will start as soon as data are written to the SPIxBUFL and SPIxBUFH registers.
To set up the SPIx module for the Enhanced Buffer Client mode of operation:
- Clear the SPIxBUFL and SPIxBUFH registers.
- If using interrupts:
- Clear the interrupt flag bits in the respective IFSx register.
- Set the interrupt enable bits in the respective IECx register.
- Write the SPIxIP bits in the respective IPCx register to set the interrupt priority.
- Write the desired settings to the SPIxCON1L, SPIxCON1H and SPIxCON2L
registers with the MSTEN bit (SPIxCON1L[5]) =
0
. - Clear the SMP bit.
- If the CKE bit is set, then the SSEN bit must be set, thus enabling the SSx pin.
- Clear the SPIROV bit (SPIxSTATL[6]).
- Select Enhanced Buffer mode by setting the ENHBUF bit (SPIxCON1L[0]).
- Enable SPIx operation by setting the SPIEN bit (SPIxCON1L[15]).
To set up the SPIx module for Audio mode:
- Clear the SPIxBUFL and SPIxBUFH registers.
- If using interrupts:
- Clear the interrupt flag bits in the respective IFSx register.
- Set the interrupt enable bits in the respective IECx register.
- Write the SPIxIP bits in the respective IPCx register to set the interrupt priority.
- Write the desired settings to the SPIxCON1L, SPIxCON1H and SPIxCON2L
registers with AUDEN (SPIxCON1H[15]) =
1
. - Clear the SPIROV bit (SPIxSTATL[6]).
- Enable SPIx operation by setting the SPIEN bit (SPIxCON1L[15]).
- Write the data to be transmitted to the SPIxBUFL and SPIxBUFH registers. Transmission (and reception) will start as soon as data are written to the SPIxBUFL and SPIxBUFH registers.