2023-01-03 11:49 AM
Hello,
I am using bare metal STM32G441 SPI (no ST HAL or LL). It has been a very tiresome dilemma with the chip select. If I use the hardware NSS (Motorola mode) then I get a pulse after each byte. If I use software NSS then the last bit gets crippled because waiting for the busy flag or the TXE fires before the last clock is done.
So how does one properly transfer multiple bytes with SPI? At the moment I am polling for the last clcok pulse which is ugly.
while((SPI1->SR & (bit7|bit2|bit1)) != (bit1|bit2));
while((GPIOA->IDR & bit5) == bit5);
GPIOA->BSRR = bit4;
2023-01-03 12:03 PM
SPI's BSY is known to be unusable. Try using RXNE instead.
JW
2023-01-04 12:30 AM
Hello,
it has the exact same effect.
SPI1->DR = cn;
while((SPI1->SR & bit0) == 0);
dummy = SPI1->DR;
GPIOA->BSRR = bit4;
It still goes hi in the middle of the last clock. The peripheral is unusable like this. Do I really have to poll while the clock pin is high to work around? This is a major fault of the CPU. How does one solve this the nice way?
Edit: changing CPOL and CPHA to 1 seems to work. But there is no long pause after the last bit.
Thanks you for that link! I did not know that but it is good to know.
2023-01-04 04:08 AM
I conducted the experiment which is described in the linked article maybe a decade ago. Indeed, the second part of the waveform there clearly indicates that RXNE is not good enough with CPOL=0/CPHA=0... I need to update the text to say so explicitly.
Thanks,
JW
PS.
> while((SPI1->SR & bit0) == 0);
Do you consider using a generic "bit0" to be better than the SPI_SR_RXNE symbol provided by the CMSIS-mandated header?
2023-01-04 04:23 AM
Reviewing the old files I realized, that the problem is not BSY as such, but both RXNE and BSY behaviour with CPHA = 0. Using BSY with CPHA=1 apparently works as expected.
I need to revisit this more deeply...
JW
2023-01-04 07:09 AM
Regarding the bit0: I usually use a mix of bitx, binary, hex and dec values. Whatever suits me best in each individual situation. I suppose its just a peculiarity of myself.
2023-01-04 07:09 AM
Thanks for the infos!