Arduino Core for STM32  1.0
HarwareSPIExample
  1. Basic example of using HardwareSPI class
    #include "HardwareSPI.h"
    #define SPI_CS_PIN PA4
    //At first, enable SPI1 and its NVIC in .ioc file settings.
    extern SPI_HandleTypeDef hspi1;
    HardwareSPI Spi1(hspi1); //creating instance of HardwareSPI for SPI1.
    uint8_t dataOut[8] = {26,32,212,150,45,111,0,1};
    uint8_t dataIn[8] = {0};
    //This method have to be called in main function in main.c file.
    void app(void){
    Spi1.begin();
    //Infinite loop
    while(1){
    //Write CS pin to LOW.
    digitalWrite(SPI_CS_PIN, LOW);
    //Set some settings - this don't have to be called when using HardwareSPI class.
    Spi1.beginTransaction(settings);
    //Transfer (write and receive simultaneously) 8 bytes.
    Spi1.transferBytes(dataOut, dataIn, 8);
    //Transfer (write and receive simultaneously) one byte.
    uint8_t byte1 = Spi1.transfer(11);
    //Transfer (write and receive simultaneously) one byte.
    uint8_t byte2 = Spi1.transfer(12);
    //This method don't have to be called when using HardwareSPI class.
    //Spi1.endTransaction();
    //Write CS pin to HIGH.
    digitalWrite(SPI_CS_PIN, HIGH);
    //Delay one second.
    delay(1000);
    }
    }
  2. Basic example of using HardwareSPI_O optimized class for speed
    #include "HardwareSPI.h"
    #define SPI_CS_PIN PA4
    //At first, enable SPI1 and its NVIC in .ioc file settings.
    extern SPI_HandleTypeDef hspi1;
    HardwareSPI_O Spi1(hspi1); //creating optimized instance of HardwareSPI_O for SPI1.
    uint8_t dataOut[8] = {26,32,212,150,45,111,0,1};
    uint8_t dataIn[8] = {0};
    //This method have to be called in main function in main.c file.
    void app(void){
    Spi1.begin();
    //Infinite loop
    while(1){
    //Write CS pin to LOW.
    digitalWrite(SPI_CS_PIN, LOW);
    //Set some settings and enable periphery - this have to be called when using HardwareSPI_O class.
    Spi1.beginTransaction(settings);
    //Transfer (write and receive simultaneously) 8 bytes.
    Spi1.transferBytes(dataOut, dataIn, 8);
    //Transfer (write and receive simultaneously) one byte.
    uint8_t byte1 = Spi1.transfer(11);
    //Transfer (write and receive simultaneously) one byte.
    uint8_t byte2 = Spi1.transfer(12);
    //This method have to be called when using HardwareSPI_O class to disable periphery.
    Spi1.endTransaction();
    //Write CS pin to HIGH.
    digitalWrite(SPI_CS_PIN, HIGH);
    //Delay one second.
    delay(1000);
    }
    }
Note
Those examples works only with c++, but STM32CubeIDE does not allow you to change main.c to main.cpp, so you have to create own c++ file, and include it in main.h. In your cpp file create function, that will be called in main() function. In this case, function name is app() and is called in main() function between tags USER CODE BEGIN 2 and USER CODE END 2.
See also
Hardware SPI documentation
HardwareSPI
HardwareSPI_O
HardwareSPI.h
HardwareSPI_O
This class is for handling SPI periphery, but is more optimized for speed same as arduino SPI class.
Definition: HardwareSPI.h:1133
delay
#define delay(millis)
Pauses the program for the amount of time (in milliseconds) specified as parameter.
Definition: wirish.h:860
LOW
#define LOW
LOW pin state, same as 0.
Definition: wirish.h:1002
HardwareSPI
This class is for handling SPI periphery.
Definition: HardwareSPI.h:220
HardwareSPI.h
This file contains class with methods to handle SPI periphery.
digitalWrite
void digitalWrite(GPIO_TypeDef *GPIOx, uint16_t PPin, bool val)
Writes new state to pin or multiple pins.
Definition: wirish.h:626
HIGH
#define HIGH
HIGH pin state, same as 1.
Definition: wirish.h:997