My Firmware

My Firmware

Embedded development

© 2020

8-bit SPI transfers on STM32F0

Recently I’ve been trying to develop a library for a NRF24l01+ radio using a STM32F0 MCU. The radio uses 8-bit SPI transfers to communicate with the processor.

The SPI peripheral is configured using the following snippet. The most important thing apart from configuring the slave select, is setting the FRXTH bit. This bit causes the RX interrupt to be invoked when there is a byte available in the RX buffer. Otherwise the interrupt would only be invoked when there are at least two bytes available in the buffer.

SPI1_CR1 &= ~SPI_CR1_SPE;
SPI1_CR1 = SPI_CR1_MSTR | SPI_CR1_MSBFIRST | SPI_CR1_SSM | SPI_CR1_SSI | (SPI_CR1_BR_FPCLK_DIV_4 << 3);
SPI1_CR2 = SPI_CR2_DS_8BIT | SPI_CR2_FRXTH;

SPI1_CR1 |= SPI_CR1_SPE;

To send the data, the data register needs to be accessed as a single byte, meaning being cast to a pointer of uint8_t type. This is fortunately implemented in libopencm3 using the SPI1_DR8 register. To simplify transfers a spi_xfer8 was implemented using the following code:

uint8_t spi_xfer8(uint8_t data) {
    SPI1_DR8 = data;
    while ((SPI1_SR & SPI_SR_RXNE) == 0);
    return SPI1_DR8;
}

This is the base of implementation used to communicate with the radio. In the future, there might be another blogpost explaining the configuration of the radio, but it first needs to be developed and tested.