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.




  • Full-duplex synchronous transfer (on 3 lines: MOSI, MISO, SCK).
  • Simplex synchronous transfer (on 2 lines: MOSI/MISO, SCK).
  • 8 or 16 bit data transfer width.
  • Master or slave mode.
  • Slave select either by hardware or software.
  • Interrupt, CRC and DMA support.


(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




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

(not used in I²S mode)




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

(not used in I²S mode)




Contains computed CRC value of received bytes.

(not used in I²S mode)




Contains computed CRC value of transmitted bytes.

(not used in SPI mode)




(not used in SPI mode)







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

  • Writing to DR writes to TX Buffer.
  • Reading from DR reads from RX Buffer.


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. */


  • stm32/peripherals/spi.txt
  • Last modified: 2022/12/28 08:36
  • by ruan