‎2023-03-04 02:09 AM
STM32G031 SPI in 3-wire half duplex mode. When reading data (BIDIMODE=1, BIDIOE=0), how can I tell when I have clocked in a complete u8/u16?
Neither RXNE or FRLVL appear to get updated until I stop the transaction.
I have it working by using a delay in the code:-
CLEAR_BIT(SPI2->CR1, SPI_CR1_BIDIOE); // change to Bidirectional input mode
delay_loop(40);
SET_BIT(SPI2->CR1, SPI_CR1_BIDIOE); // change back to Bidirectional output mode
result = SPI2->DR;
but this seems a nasty cludge
I've RTFM and googled it, seems lots of people have the same problem. The STM32 in 3 wire receive mode just clocks away til you tell it to stop. Surely there's a better way?
Any suggestions gratefully received, thank you
‎2023-03-04 03:36 AM
why not use: HAL_SPI_Receive_IT(SPI_HandleTypeDef *hspi, uint8_t *pData, 1 ) , receive 1x then INT .
?
‎2023-03-04 05:31 AM
Thanks Ascha, but I think the interrupt is generated by the RXNE event flag, so same problem (RXNE isn't generated while in input mode). I'm not using HAL, but if I get completely stuck on this I'll write a test project in HAL, just to double-check this.
‎2023-03-04 07:21 AM
well, i never looked for RXNE event flag - :)
but i have SPI receive slave mode -> circular DMA running,
with 12 Mbit rate, using HAL . no problems (except cache management on H743 was some try and error, but this is not related to using HAL lib or not. )
‎2023-03-04 07:24 AM
So far, to have a real world working 3 wire SPI mode, I use 4 wire SPI mode and disable MOSI when reading data in. Most of my other attempt failed.
‎2023-03-06 09:17 AM
Thanks Ascha. I believe it works differently in slave mode, and I'm not using the DMA
‎2023-03-06 09:19 AM
Mmm yes, I've seen lots of posts to this effect, that's what is worrying me. My board is wired for 3-wire (only MOSI pin connected) and MISO pin is used elsewhere, so that's not really an option. Thanks though
‎2023-03-06 12:19 PM
In some cases, bitbanging the SPI may be a viable option.
JW
‎2023-03-06 01:32 PM
On STM32L471, we have used SPI configured with direction SPI_DIRECTION_2LINES but configured the MISO pin as a regular GPIO (not set as Alternate, e.g., GPIO_AF5_SPI1) as we have no need to read from the connected SPI device.
RXNE operates as expected.
(This may be the same as S.Ma mentioned.)
Edit: This was acting as Master to write to the slave. You say you are reading from the device using MOSI?
‎2023-03-06 10:34 PM
Thanks JW, yes we have tried that, it works, but slow (we're running sysclk at 2MHz to save power). I wanted to use the SPI hardware and do it "properly". Just seems really weird that RXNE doesn't work in bidirectional rx mode. I would love someone from ST to confirm this