SPI

0. Pin Map

On the STM32F446RET6 Nucleo board, the SPI1 is connected as below

PA4

CS

A2

PA5

MOSI

PWM/MOSI/D11

PA6

MISO

MISO/D12

PA7

SCLK

SCK/D13

1. Configure STM32

First, set the configuration from the Template Projectarrow-up-right.

Set PA4 as GPIO Output.

In the left sidebar, select Connectivity -> SPI1.

Select Mode to "Full-Duplex Master".

Select Hardware NSS Signal to "Disable".

Set Clock Parameters -> Prescaler to 128 or more, to ensure the Baud Rate is slower than 1Mbits/s.

Errata: the hardware-driven NSS on STM32 is a bit mysterious. It seems that we need to deinit the SPI module and re-init to control the CS pin. Also, the CS pin is open-drain, so it requires an external pull-up resistor to function. We will save all the hassle and just use software-controlled CS pin. Detailed discussion of hardware NSS can be found herearrow-up-right.

SPI Mode

Clock Polarity: CPOL

Clock Phase: CPHA

Mode
Clock Polarity
Clock Phase
Output Edge
Data Capture

MODE 0

Low

1 Edge

Falling

Rising

MODE 1

Low

2 Edge

Rising

Falling

MODE 2

High

1 Edge

Rising

Falling

MODE 3

High

2 Edge

Falling

Rising

2. Code

First, add the code from the Template Projectarrow-up-right.

In main.c, add the following code

After saving, upload the code.

3. Result

After connecting an RFID-RC522 SPI module, we can see that we can read the register from the sensor. The SPI signal looks like this:

https://community.st.com/s/question/0D53W00001nAhvYSAS/how-to-use-spi-nss-on-stm32g0arrow-up-right

Last updated