SPI / I²S 1..6

The SPI interface offers 2 main functions: SPI and I²S (audio protocol).
There are a total of 6 SPI controllers which can be configured and used independently.




Features


Configuration Registers

SPI_CR1 - Configuration register 1

(not used in I²S mode)




MSTR0Slave mode (reset state)
1Master mode
BR000BR ⇒ fPCLK2 / 2 (reset state)100BR ⇒ fPCLK2 / 32
001BR ⇒ fPCLK2 / 4101BR ⇒ fPCLK2 / 64
010BR ⇒ fPCLK2 / 8110BR ⇒ fPCLK2 / 128
011BR ⇒ fPCLK2 / 16111BR ⇒ fPCLK2 / 256
SPE0SPI disabled (reset state)
1SPI enabled
LSBFIRST0MSB transmitted first (reset state)
1LSB transmitted first
SSM0Slave management by hw (reset state)
1Slave management by sw
DFF0Data frame: 8 bit (reset state)
1Data frame: 16 bit

SPI_CR2 - Configuration register 2




SSOE0Slave select disabled in master mode (reset state)
1Slave select enabled in master mode (not for multi master setup)
FRF0Motorolla frame format (reset state)
1Texas Instruments frame format
ERRIE0Error interrupt disabled (reset state)
1Error interrupt enabled
RXNEIE0RX not empty interrupt disabled (reset state)
1RX not empty interrupt enabled
TXEIE0TX empty interrupt disabled (reset state)
1TX empty interrupt enabled

SPI_CRCPR - CRC polynomial register

(not used in I²S mode)




Contains polynomial used to calculate CRC. Reset value: 0x0007.

SPI_RXCRCR - RX CRC register

(not used in I²S mode)




Contains computed CRC value of received bytes.

SPI_TXCRCR - TX CRC register

(not used in I²S mode)




Contains computed CRC value of transmitted bytes.

SPI_I2SCFGR - I²S configuration register

(not used in SPI mode)




SPI_I2SPR - I²S prescaler register

(not used in SPI mode)




Data Register

SPI_DR - Data register




Data received or to be transmitted. Split into 2 buffers:


Programming Example

The code snippet bellow shows how to configure and use the SPI peripheral.

#include "reg_stm32f4xx.h"
 
RCC->AHBENR[0] |= (0x1 << 0u);       /* Enable GPIOA clock */
RCC->APBENR[1] |= (0x1 << 12u);      /* Enable SPI1 clock */
 
/* Configure GPIO pin A.4 to A.7 in alternate function mode. */
GPIOA->MODER &= ~(0xff << 8u);       /* Clear existing mode bits 8 to 15. */
GPIOA->MODER |= (0xaa << 8u);        /* Set pin 4..7 to alternate function mode. */
 
GPIOA->OSPEEDR &= ~(0xff << 8u);     /* Clear existing output speed bits 8 to 15. */
GPIOA->OSPEEDR |= (0xff << 8u);      /* Set pin 4..7 to high speed (100MHz) mode. */
 
GPIOA->AFR[0] &= ~(0xffff << 16u);   /* Clear existing af bits 16 to 31. */
GPIOA->AFR[0] |= (0x5555 << 16u);    /* Set pin 4..7 to AF5 (SPI1). */
 
/* Configure SPI1 in slave mode. */
SPI1->CR1 = 0u;                      /* Reset SPI1 configuration. */
SPI1->CR2 = 0u;                      /* Reset SPI1 configuration. */
 
SPI1->CR1 |= (0x1 << 3u) |           /* Select 11,25MHz as baudrate (45 MHz / 4). */
             (0x1 << 6u);            /* Enable SPI. */