STM32 SPI HAL library [stm32] - Page 1
Forum section: electronics / general

author: Yizhou
author_rank: Newbie
timestamp: April 22, 2019, 10:15:45 am
content: i was using stm32 spi1 to receive 24 bit data from adc only, i invoke HAL_SPI_Receive(&spi1, (*uint8_t)buffer , 3,，but i get 48 clocks on SCLK Pin which is double of 24. while HAL_SPI_Transmit() works normally.I know that SPI->DR register is 16 bits, but HAL_SPI_Receive() use (* (uint8_t *)hspi->pRxBuffPtr) = *(__IO uint8_t *)&hspi->Instance->DR;is it just fine? i have shutdown the MCU while it is in debug session. need some help..

author: ajb
author_rank: Super Contributor
timestamp: April 22, 2019, 05:39:13 pm
content: My guess is that the SPI peripheral is not being configured correctly, or if this is a master device in 4-wire mode, then it's possible that the writes to the DR register to control the transaction are the wrong width. ST's SPI modules do have a "feature" where a 16-bit access to the data register when the SPI module is in 8-bit mode is assumed to be a a packed write of two bytes. The*(__IO uint8_t *)&hspi->Instance->DR;construct should force an 8-bit access (because the "__IO" macro usually includes the "volatile" qualifier), so make sure that the same construct is being used to write to the data register to trigger the transaction. Examining the disassembly may be faster than sifting through ST's code to confirm if that's the issue or not.

author: newbrain
author_rank: Super Contributor
timestamp: April 23, 2019, 09:49:21 am
content: Quote from: Yizhou on April 22, 2019, 10:15:45 amCan you show us the SPI initialization?Without that code, it is difficult to diagnose what's happening.Which specific STM32 are you using?BTW, a typo: you mean (uint8_t *) not (*uint8_t).@ajb:The HAL, for an SPI in 8 bit mode, will pack bytes in the 16bit DR until only 1 or 0 are left to be transmitted.

author: Yizhou
author_rank: Newbie
timestamp: April 23, 2019, 11:09:05 am
content: Thanks for your replying, i have solved the problem. it is the spi configuration's fault, the spi direction is wrong...i use the CubeMX to initialize all peripherals. i choose Receive Only mode. and the init code is"hspi1.Init.Direction = SPI_DIRECTION_2LINES_RXONLY;"lastly, i know that, _RXONLY means receive data throught MISO and MOSI...But, i still have some question about spi interface .(1) what has stm32 done while 2LINES_RXONLY applied?(2) HAL library defines the direction :#define SPI_DIRECTION_2LINES (0x00000000U)#define SPI_DIRECTION_2LINES_RXONLY SPI_CR1_RXONLY#define SPI_DIRECTION_1LINE SPI_CR1_BIDIMODESPI_DIRECTION_1LINE means BIDIMODE? and there is no need to distinguish 1LINE_RX and 1LINE_TX?

author: newbrain
author_rank: Super Contributor
timestamp: April 23, 2019, 12:43:28 pm
content: Quote from: Yizhou on April 23, 2019, 11:09:05 amThe RXONLY bit in CR1 of SPI simply disables the data output (MOSI in master mode, MISO in slave mode).RXONLY can only be set if there are separate lines for transmit and receive, i.e. if BIDIMODE is not set.When BIDIMODE is set, the same line is used for both input and output (half-duplex) and the BIDIOE (bi-directional output enable) bit controls whether we are transmitting (set) or receiving (reset).If you are using the HAL, the HAL_SPI_Trasmit...() and HAL_SPI_Receive...() functions will take care of controlling BIDIOE, if the SPI has been correctly set up.

author: Yizhou
author_rank: Newbie
timestamp: April 24, 2019, 08:42:36 pm
content: something goes incorrectly....i read the spi part of F429 Reference, and i quite agree with what newbrain 's option.why does the clock is right while i change the direction option to no _RXONLY version? HAL library's relan code is:#define SPI_CR1_RXONLY_Pos (10U)#define SPI_CR1_RXONLY_Msk (0x1UL << SPI_CR1_RXONLY_Pos) /*!< 0x00000400 */#define SPI_CR1_RXONLY SPI_CR1_RXONLY_Mskit set the RXONLY bit to 1 with others set to 0, then how can it double the clocks? still need help....

author: newbrain
author_rank: Super Contributor
timestamp: April 25, 2019, 06:52:09 am
content: I have used many times the SPI on F429 with the HAL and had no issue, but bugs are always a possibility.Can you show the peripheral initialization code?If you are relying on CubeMX to generate the startup code, that would be the MX_SPIn_Init() and the HAL_SPI_MspInit() functions.If 48 clock pulses are emitted, it would seem that a data size of 16 bit was used - the SPIs in F4 support only 8 and 16 bit transfers, other families' SPIs are more flexible.

author: Yizhou
author_rank: Newbie
timestamp: April 25, 2019, 08:46:32 am
content: i used 2 spi interface:Code:/* SPI1 init function */void MX_SPI1_Init(void){hspi1.Instance = SPI1;hspi1.Init.Mode = SPI_MODE_MASTER;hspi1.Init.Direction = SPI_DIRECTION_2LINES_RXONLY;hspi1.Init.DataSize = SPI_DATASIZE_8BIT;hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;hspi1.Init.NSS = SPI_NSS_SOFT;hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;hspi1.Init.TIMode = SPI_TIMODE_DISABLE;hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;hspi1.Init.CRCPolynomial = 10;if (HAL_SPI_Init(&hspi1) != HAL_OK){Error_Handler();}}/* SPI2 init function */void MX_SPI2_Init(void){hspi2.Instance = SPI2;hspi2.Init.Mode = SPI_MODE_MASTER;hspi2.Init.Direction = SPI_DIRECTION_2LINES;hspi2.Init.DataSize = SPI_DATASIZE_8BIT;hspi2.Init.CLKPolarity = SPI_POLARITY_LOW;hspi2.Init.CLKPhase = SPI_PHASE_2EDGE;hspi2.Init.NSS = SPI_NSS_HARD_OUTPUT;hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8;hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB;hspi2.Init.TIMode = SPI_TIMODE_DISABLE;hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;hspi2.Init.CRCPolynomial = 10;if (HAL_SPI_Init(&hspi2) != HAL_OK){Error_Handler();}}The SDI of SPI1 must be used as GPIO.