SPI STM32.
We won't go over how the protocol works, since it was covered here; let's go straight to the capabilities of the STM32 SPI module:
Four pins can be used for data transfer:
Miso(Master In / Slave Out data) — depending on the mode, the pin can be configured as an input or output: an input in Master mode and an output in Slave mode
Mosi(Master Out / Slave In data) – the other way round: an output in Master mode and an input in Slave mode
SCK – the clock signal generated by the Master.
NSS – this pin is used to select the slave device in Slave mode when a logic zero is applied to it; this pin can also be used to switch SPI modes (Master/Slave) on a multi-master bus.
So the NSS pin doesn't work quite like the familiar CS on AVR, and in Master mode you need to use a GPIO to select the slave.
In Slave mode, the Chip Select signal can be taken from the NSS pin or set by software; this depends on the SSM bit: if it is cleared, the state of the NSS pin is read; otherwise the state of the SSI bit is what counts. To receive data in this case (with software control), set SSM = 1, SSI = 0.
In Master mode, the pin must be pulled up to the supply or a software pull-up enabled (SSM =1, SSI = 1). With a software pull-up, the pin can be used as an ordinary GPIO.
Another option is possible: SSM = 0, SSOE = 0; the NSS pin is configured as an input with pull-up, and when a low level appears on it, the SPI module thinks a new Master has appeared and becomes a Slave itself; this option is used on a multi-master bus.
This is how one Master is connected to a Slave.

The SPI bus consists of two shift registers connected to each other in series, and on each clock pulse the data in the registers shifts. That is, on each clock pulse one bit leaves the register, the remaining bits shift toward the output, and a free space appears at the input, which is taken by the newly arrived bit.
Below is the block diagram of the SPI module.

The diagram shows that the module consists of two control registers and a status register; let's look at what each bit in the registers does. I'd like to note that most bits can only be changed while SPI is disabled (SPE = 0).
SPI_CR1 control register:
BIDIMODE(Bidirectional data mode enable) — if you write a one to this bit, data will be transferred in both directions over one line (single-wire mode); if zero, over two lines. In bidirectional mode the master uses the MOSI pin and the slave the MISO pin.
BIDIOE(Output enable in bidirectional mode) — this bit is used to configure single-wire mode: 1 — data is only transmitted, 0 — data is only received.
CRCEN(Hardware CRC calculation enable) — writing a one to this bit enables hardware CRC calculation, 0 disables it.
CRCNEXT(CRC transfer next) – if this bit is set, it means the checksum will be transmitted next.
DFF(Data frame format) – this bit determines the frame format: 0 — 8 bits, 1 — 16 bits.
RXONLY(Receive only) – this bit is used in two-wire mode; setting it to one disables transmission and the SPI module only receives; otherwise it both receives and transmits.
SSM(Software slave management) — software control of NSS: when this bit is set, the state of the SSI bit is monitored instead of the level on the NSS input; when cleared, the state of the NSS pin is monitored.
SSI(Internal slave select) — this bit only has effect when SSM is set; its value replaces the state of the NSS pin.
LSBFIRST(Frame format) — determines the frame format: 1 — LSB first, 0 — MSB first.
SPE(SPI enable) — a one in this bit enables the SPI module, 0 disables it.
BR[2:0](Baud rate control) — these bits determine the data transfer speed.

MSTR(Master selection) — determines the operating mode: 1 — Master, 0 — Slave.
CPOL(Clock polarity) — determines the polarity in the idle state: 0 — low level when idle, 1 — high level when idle.
CPHA(Clock phase) – sets the clock phase: 0 — data is sampled on the first clock edge, 1 — data is sampled on the second clock edge.
SPI_CR2 control register :
TXEIE(Tx buffer empty interrupt enable) — a one in this bit enables an interrupt when the transmit buffer is empty.
RXNEIE(RX buffer not empty interrupt enable) — a one in this bit enables an interrupt when the receive buffer is not empty.
ERRIE(Error interrupt enable) — a one in this bit enables an interrupt when an error occurs, signaled by the CRCERR, OVR and MODF bits.
SSOE(SS output enable) — a one in this bit lets the NSS pin be used as an output, and the SPI module controls the NSS pin itself, but according to the following algorithm: when the SPI module is enabled (SPE = 1), a low level appears on NSS and stays until the module is disabled (SPE = 0), which in most cases isn't suitable, so I use a GPIO for this purpose, and in Master mode I set NSS high by software (SSM = 1, SSI = 1).
TXDMAEN(Tx buffer DMA enable) — enables/disables a DMA request when the TXE flag is set (when transmission is complete).
RXDMAEN(Rx buffer DMA enable) — enables/disables a DMA request when the RXNE flag is set (when reception is complete).
SPI_SR status register:
BSY(Busy flag) — the busy flag, used to monitor the state of the shift register; it is set by hardware during data exchange, or more precisely from the moment the data passes from the SPI_DR register into the shift register, and cleared when the shift register empties, i.e. when all the data has gone out on the MOSI line.
The manufacturer advises against checking it during reception and transmission and recommends using the TXE and RXNE flags instead.
OVR(Overrun flag) – set when the buffer overflows, when new data overwrites old data that hasn't been read yet.
MODF(Mode fault) — set if, in Master mode, a low-level signal arrives at the NSS input and the module becomes a Slave. It can be cleared with a special sequence.
CRCERR(CRC error flag) – the checksum error flag.
TXE(Transmit buffer empty) – set when the transmit buffer (the SPI_DR register) is empty, cleared when data is loaded
RXNE(Receive buffer not empty) — set when the receive buffer contains data, cleared when the data is read.
SPI_DR(Data register) – the data register; it actually consists of two buffers, receive and transmit, but we work only with the DR register: to send data we write to DR, and to receive we read it.
SPI_CRCPR(CRC polynomial register) – contains the polynomial for CRC calculation; after reset its value is 0x0007.
SPI_RXCRCR(Rx CRC register) – contains the calculated CRC of the received data.
SPI_TXCRCR(Tx CRC register) – contains the calculated CRC of the transmitted data.
Module initialization from a working project.
I found this way of exchanging data on the st.com forum; there are some things in it that should be changed, at the very least adding a timeout when waiting for flags, just in case. But since everything works for now, I decided to leave it alone.
- full-duplex synchronous transfer using 3 lines
- simplex synchronous transfer using two lines, with the option of a bidirectional data line
- 8- or 16-bit frame format
- operation in Master or Slave mode
- in Master mode, 8 baud rate prescaler values (fPCLK/2 max)
- in Slave mode, the maximum speed is fPCLK/2 max
- hardware and software control of the NSS pin, with dynamic Master/Slave switching
- programmable clock polarity and phase
- programmable bit order (MSB-first or LSB-first)
- receive and transmit flags, with interrupts when they occur
- SPI bus busy flag
- Hardware CRC calculation for reliable data exchange:
- the CRC value can be transmitted as the last byte during transmission
- automatic CRC check for the last received byte - interrupts can be generated on the CRC error, overrun and Master mode fault flags
- 1-byte DMA buffer for transmit and receive: Tx and Rx requests
Four pins can be used for data transfer:
Miso(Master In / Slave Out data) — depending on the mode, the pin can be configured as an input or output: an input in Master mode and an output in Slave mode
Mosi(Master Out / Slave In data) – the other way round: an output in Master mode and an input in Slave mode
SCK – the clock signal generated by the Master.
NSS – this pin is used to select the slave device in Slave mode when a logic zero is applied to it; this pin can also be used to switch SPI modes (Master/Slave) on a multi-master bus.
So the NSS pin doesn't work quite like the familiar CS on AVR, and in Master mode you need to use a GPIO to select the slave.
In Slave mode, the Chip Select signal can be taken from the NSS pin or set by software; this depends on the SSM bit: if it is cleared, the state of the NSS pin is read; otherwise the state of the SSI bit is what counts. To receive data in this case (with software control), set SSM = 1, SSI = 0.
In Master mode, the pin must be pulled up to the supply or a software pull-up enabled (SSM =1, SSI = 1). With a software pull-up, the pin can be used as an ordinary GPIO.
Another option is possible: SSM = 0, SSOE = 0; the NSS pin is configured as an input with pull-up, and when a low level appears on it, the SPI module thinks a new Master has appeared and becomes a Slave itself; this option is used on a multi-master bus.
This is how one Master is connected to a Slave.

The SPI bus consists of two shift registers connected to each other in series, and on each clock pulse the data in the registers shifts. That is, on each clock pulse one bit leaves the register, the remaining bits shift toward the output, and a free space appears at the input, which is taken by the newly arrived bit.
Below is the block diagram of the SPI module.

The diagram shows that the module consists of two control registers and a status register; let's look at what each bit in the registers does. I'd like to note that most bits can only be changed while SPI is disabled (SPE = 0).
SPI_CR1 control register:
BIDIMODE(Bidirectional data mode enable) — if you write a one to this bit, data will be transferred in both directions over one line (single-wire mode); if zero, over two lines. In bidirectional mode the master uses the MOSI pin and the slave the MISO pin.
BIDIOE(Output enable in bidirectional mode) — this bit is used to configure single-wire mode: 1 — data is only transmitted, 0 — data is only received.
CRCEN(Hardware CRC calculation enable) — writing a one to this bit enables hardware CRC calculation, 0 disables it.
CRCNEXT(CRC transfer next) – if this bit is set, it means the checksum will be transmitted next.
DFF(Data frame format) – this bit determines the frame format: 0 — 8 bits, 1 — 16 bits.
RXONLY(Receive only) – this bit is used in two-wire mode; setting it to one disables transmission and the SPI module only receives; otherwise it both receives and transmits.
SSM(Software slave management) — software control of NSS: when this bit is set, the state of the SSI bit is monitored instead of the level on the NSS input; when cleared, the state of the NSS pin is monitored.
SSI(Internal slave select) — this bit only has effect when SSM is set; its value replaces the state of the NSS pin.
LSBFIRST(Frame format) — determines the frame format: 1 — LSB first, 0 — MSB first.
SPE(SPI enable) — a one in this bit enables the SPI module, 0 disables it.
BR[2:0](Baud rate control) — these bits determine the data transfer speed.

MSTR(Master selection) — determines the operating mode: 1 — Master, 0 — Slave.
CPOL(Clock polarity) — determines the polarity in the idle state: 0 — low level when idle, 1 — high level when idle.
CPHA(Clock phase) – sets the clock phase: 0 — data is sampled on the first clock edge, 1 — data is sampled on the second clock edge.
SPI_CR2 control register :
TXEIE(Tx buffer empty interrupt enable) — a one in this bit enables an interrupt when the transmit buffer is empty.
RXNEIE(RX buffer not empty interrupt enable) — a one in this bit enables an interrupt when the receive buffer is not empty.
ERRIE(Error interrupt enable) — a one in this bit enables an interrupt when an error occurs, signaled by the CRCERR, OVR and MODF bits.
SSOE(SS output enable) — a one in this bit lets the NSS pin be used as an output, and the SPI module controls the NSS pin itself, but according to the following algorithm: when the SPI module is enabled (SPE = 1), a low level appears on NSS and stays until the module is disabled (SPE = 0), which in most cases isn't suitable, so I use a GPIO for this purpose, and in Master mode I set NSS high by software (SSM = 1, SSI = 1).
TXDMAEN(Tx buffer DMA enable) — enables/disables a DMA request when the TXE flag is set (when transmission is complete).
RXDMAEN(Rx buffer DMA enable) — enables/disables a DMA request when the RXNE flag is set (when reception is complete).
SPI_SR status register:
BSY(Busy flag) — the busy flag, used to monitor the state of the shift register; it is set by hardware during data exchange, or more precisely from the moment the data passes from the SPI_DR register into the shift register, and cleared when the shift register empties, i.e. when all the data has gone out on the MOSI line.
The manufacturer advises against checking it during reception and transmission and recommends using the TXE and RXNE flags instead.
OVR(Overrun flag) – set when the buffer overflows, when new data overwrites old data that hasn't been read yet.
MODF(Mode fault) — set if, in Master mode, a low-level signal arrives at the NSS input and the module becomes a Slave. It can be cleared with a special sequence.
CRCERR(CRC error flag) – the checksum error flag.
TXE(Transmit buffer empty) – set when the transmit buffer (the SPI_DR register) is empty, cleared when data is loaded
RXNE(Receive buffer not empty) — set when the receive buffer contains data, cleared when the data is read.
SPI_DR(Data register) – the data register; it actually consists of two buffers, receive and transmit, but we work only with the DR register: to send data we write to DR, and to receive we read it.
SPI_CRCPR(CRC polynomial register) – contains the polynomial for CRC calculation; after reset its value is 0x0007.
SPI_RXCRCR(Rx CRC register) – contains the calculated CRC of the received data.
SPI_TXCRCR(Tx CRC register) – contains the calculated CRC of the transmitted data.
Module initialization from a working project.
#define CS_LOW GPIOB->BSRR = GPIO_BSRR_BR10;
#define CS_HIGH GPIOB->BSRR = GPIO_BSRR_BS10;
void Spi_Init(void)
{
//enable clocking of port B and alternate functions
RCC->APB2ENR |= RCC_APB2ENR_IOPBEN | RCC_APB2ENR_AFIOEN;
//pins 13(SCK) and 15(MOSI) - alternate function push pull, pin 14(MISO) - Input floating, pin 10(CS) - output, push-pull
GPIOB->CRH &= ~(GPIO_CRH_CNF13_0 | GPIO_CRH_CNF15_0 | GPIO_CRH_CNF10_0);
GPIOB->CRH |= GPIO_CRH_CNF13_1 | GPIO_CRH_CNF15_1;
GPIOB->CRH |= GPIO_CRH_MODE10_0 | GPIO_CRH_MODE13_1 | GPIO_CRH_MODE15_1;
//enable SPI2 clocking
RCC->APB1ENR |= RCC_APB1ENR_SPI2EN;
SPI2->CR1 |= SPI_CR1_BR; //Baud rate = Fpclk/256
SPI2->CR1 &= ~SPI_CR1_CPOL; //Polarity cls signal CPOL = 0;
SPI2->CR1 &= ~SPI_CR1_CPHA; //Phase cls signal CPHA = 0;
SPI2->CR1 |= SPI_CR1_DFF; //16 bit data
SPI2->CR1 &= ~SPI_CR1_LSBFIRST; //MSB will be first
SPI2->CR1 |= SPI_CR1_SSM | SPI_CR1_SSI; //Software slave management & Internal slave select
SPI2->CR1 |= SPI_CR1_MSTR; //Mode Master
SPI2->CR1 |= SPI_CR1_SPE; //Enable SPI2
}
uint16_t Spi_Write_Data(uint16_t data)
{
//wait until the Tx buffer is empty
while(!(SPI2->SR & SPI_SR_TXE));
//activate Chip Select
CS_LOW
//send data
SPI2->DR = data;
//wait for the response
while(!(SPI2->SR & SPI_SR_RXNE));
//read the received data
data = SPI2->DR;
//deactivate Chip Select
CS_HIGH
//return what we read
return data;
}
I found this way of exchanging data on the st.com forum; there are some things in it that should be changed, at the very least adding a timeout when waiting for flags, just in case. But since everything works for now, I decided to leave it alone.