STM32F4xx barebones SPI (HAL initialisation)
depending on the implementation of the SPI  probably you need to cast properly

*(volatile uint8_t *)&amp;SPIx -&gt; DR = data;

Then waiting for both flags after the send is pointless. Using the magic numbers is just ridiculous.

void spi_write(uint8_t data) 
{
    while (!(hspi2.Instance-&gt;SR &amp; SPI_SR_TXE)));  // check  the CMSIS definition for you uC
    *(volatile uint8_t *)&amp;hspi2.Instance-&gt;DR = data;
}


The last check is needed only if you are going to disable the SPI or you manually change the state of the CS pin and you have to wait until the last was transmitted.

Another errors - SR register is 32 bit not 16.
What is the point of using HAL for initialization if you want to use it the "bare regiser" way?