Example to send a message through the SPI protocol. The communication is carried out with an EEPROM memory which is configured at a working frequency of 4 MHz, this memory works with a clock cycle with data collection at rising edge, we can configure this feature by enabling the member SpiHandle. Init.CLKPhase with phase SPI_PHASE_1EDGE for Rising-Edge or SPI_PHASE_2EDGE for Falling-Edge, configuration will be carried out depending on the device with which the communication is made.

#include "bsp.h"

SPI_HandleTypeDef SpiHandle;        /*Structure to handle SPI*/
GPIO_InitTypeDef GPIO_InitStruct;   /*Structure to init GPIOs*/

uint8_t TxBuffer[4];

int main(void)
{
    HAL_Init();           /*Library Initialization*/

    __GPIOD_CLK_ENABLE(); /*Enable port D clock*/
    __GPIOC_CLK_ENABLE(); /*Enable port C clock*/

    GPIO_InitStruct.Pin   = GPIO_PIN_9;                    /*CS pin to start/finish SPI transmition */
    GPIO_InitStruct.Mode  = GPIO_MODE_OUTPUT_PP;           /*Output push-pull Type                  */
    GPIO_InitStruct.Pull  = GPIO_NOPULL;                   /*Pin without pull-up or pull-down       */
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;           /*Pin in low speed                       */
    /*Initialize pins with the previous parameters*/
    HAL_GPIO_Init( GPIOD, &GPIO_InitStruct );


    /*Configuration of SPI in master mode, Full-Duplex communication, Clock polarity in Low,
    clock phase in Rising edge and CLK frecc of 4 MHz*/
    SpiHandle.Instance            = SPI1;                        /*SPI instance according MOSI,MISO,CLK and CS lines*/
    SpiHandle.Init.Mode           = SPI_MODE_MASTER;             /*Master mode selected to transmmit data from master*/
    SpiHandle.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4;  /*Prescaler selected according slave work freccuency*/
    SpiHandle.Init.Direction      = SPI_DIRECTION_2LINES;        /*Number of lines of transmmition (Full-Duplex)*/
    SpiHandle.Init.CLKPhase       = SPI_PHASE_1EDGE;             /*CLK Phase selected in rising edge */
    SpiHandle.Init.CLKPolarity    = SPI_POLARITY_LOW;            /*CLK polarity selected in rising edge, Idle state in 0*/
    SpiHandle.Init.DataSize       = SPI_DATASIZE_8BIT;           /*Zise of data to be transmitted*/
    SpiHandle.Init.FirstBit       = SPI_FIRSTBIT_MSB;            /*Selected the MSB as first bit to be transmitted*/
    SpiHandle.Init.NSS            = SPI_NSS_SOFT;                /*NSS-CS configured by software*/
    SpiHandle.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED; /*Specifies if the CRC calculation is enabled or not*/
    SpiHandle.Init.TIMode         = SPI_TIMODE_DISABLED;         /*TI protocol master mode disabled*/

    /*Ensures slave is disabled pin D9 high*/
    HAL_GPIO_WritePin( GPIOD, GPIO_PIN_9, SET );
    /*Apply configuration to SPI1*/
    HAL_SPI_Init( &SpiHandle );

    /*Enable writing instructions in the eeprom memory by sending a 0x06, SPI_Transmit function needs 
    as argument the structure to handle spi, amount of data to transmit, buffer where data is stored, 
    and timeout to send information*/
    HAL_GPIO_WritePin( GPIOD, GPIO_PIN_9, RESET );      /*Start transmition pulling down CS pin state*/
    TxBuffer[0] = 0x06;                                 /*WREN Instruction value for eeprom*/
    HAL_SPI_Transmit( &SpiHandle, TxBuffer, 1, 500 );   /*Function to transmmit the data */
    HAL_GPIO_WritePin( GPIOD, GPIO_PIN_9, SET );        /*Stop transmition pulling up CS pin state*/

    while (1)
    {

    }
}

msps.c

/*This function will be called inside the HAL_SPI_Init function*/
void HAL_SPI_MspInit( SPI_HandleTypeDef *hspi )
{
    /* pins D8, D6 and D5 in alternate function spi1 for CLK, MOSI and MISO*/
    GPIO_InitTypeDef GPIO_InitStruct;
    __GPIOD_CLK_ENABLE();
    __SPI1_CLK_ENABLE();

    GPIO_InitStruct.Pin = GPIO_PIN_8 | GPIO_PIN_6 | GPIO_PIN_5; /*Function of pin: CLK , MOSI , MISO */ 
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;                     /*Alternate Function mode for pins*/
    GPIO_InitStruct.Pull = GPIO_PULLUP;                         /*Pull-Up activation for selected pins*/
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;               /*High speed selected for pins*/
    GPIO_InitStruct.Alternate = GPIO_AF1_SPI1;                  /*Alternate function for selected pins*/
    HAL_GPIO_Init( GPIOD, &GPIO_InitStruct );
}