cancel
Showing results for 
Search instead for 
Did you mean: 

How to configure SPI1 in Nucleo L476RG in SPI_DATASIZE_8BIT mode

David Krcmarik
Associate II

Hello,

please can I ask how to configure SPI1 in Nucleo L476RG? I have configured SPI interface using CubeMX and then I have checked the functionality using osciloscope. When I want to send 1 byte (8 bits) CLK signal generated has 16 pulses instead of 8 pulses. My configuration of SPI is:

static void MX_SPI1_Init(void) {

/* SPI1 parameter configuration*/

hspi1.Instance = SPI1;

hspi1.Init.Mode = SPI_MODE_MASTER;

hspi1.Init.Direction = SPI_DIRECTION_2LINES;

hspi1.Init.DataSize = SPI_DATASIZE_8BIT;

hspi1.Init.CLKPolarity = SPI_POLARITY_HIGH;

hspi1.Init.CLKPhase = SPI_PHASE_2EDGE;

hspi1.Init.NSS = SPI_NSS_SOFT;

hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16;

hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;

hspi1.Init.TIMode = SPI_TIMODE_DISABLE;

hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;

hspi1.Init.CRCPolynomial = 7;

hspi1.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;

hspi1.Init.NSSPMode = SPI_NSS_PULSE_DISABLE;

if (HAL_SPI_Init(&hspi1) != HAL_OK) {

_Error_Handler(__FILE__, __LINE__);

}

}

I have checked also the registers in HAL_SPI_Init and everything seems fine. Basically when I choose some hspi1.Init.DataSize the number of CLK pulses is always doubled. It seems to me it could be some problem in HAL. Please can you help me how to configure SPI in a such way I would be able to use it for hspi1.Init.DataSize = SPI_DATASIZE_8BIT (it means 8 CLK pulses for each byte)? Attached is a screenshot from osciloscope and program.

David Krcmarik

3 REPLIES 3
David Krcmarik
Associate II

I have forget this function definition for code:

int32_t SPI_WriteReadByte(uint8_t data)

{

while (!__HAL_SPI_GET_FLAG(&hspi1, SPI_FLAG_TXE));

hspi1.Instance->DR = data;

while (!__HAL_SPI_GET_FLAG(&hspi1, SPI_FLAG_RXNE));

return (int32_t)hspi1.Instance->DR;

}

Watch the width of the write to DR lest it stuff 16-bits worth of data into the FIFO

*((volatile uint8_t *)&hspi1.Instance->DR) = data;

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
David Krcmarik
Associate II

Thanks this works.