Example to receive a message through the SPI protocol using an EEPROM memory to made communication and SPI configuration. It is assume there is already some information inside the EEPROM, also it is important to notice that in order to read any info from the eeprrom is necessary to transmit some commands
#include "bsp.h"
SPI_HandleTypeDef SpiHandle; /*Structure to handle SPI*/
GPIO_InitTypeDef GPIO_InitStruct; /*Structure to init GPIOs*/
uint8_t TxBuffer[4];
uint8_t RxBuffer[2];
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 );
/*Read a byte from direccion 0 of eeprom memory,SPI_Receive function needs as argument the structure
to handle SPI,amount of data to receive,buffer where data will be stored,and timeout to receive information*/
HAL_GPIO_WritePin( GPIOD, GPIO_PIN_9, RESET ); /*Start transmition pulling down CS pin state*/
TxBuffer[0] = 0x03; /*Read instruction value for eeprom*/
TxBuffer[1] = 0x00; /*Address value to read*/
TxBuffer[2] = 0x00; /*Address value to read*/
HAL_SPI_Transmit( &SpiHandle, TxBuffer, 3, 500 ); /*Function to transmmit the data*/
HAL_SPI_Receive( &SpiHandle, RxBuffer, 1, 500 ); /*Function to receive the data*/
HAL_GPIO_WritePin( GPIOD, GPIO_PIN_9, SET ); /*Stop transmition pulling down CS pin state*/
/*At this point we have one byte from the eeprom store in variable RxBuffer[0]*/
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 );
}