2020-03-12 11:38 AM
Hello,
I'm using HAL_SPI_Transmit function to transfer two bytes over SPI, NSS pin controlled by SPI module. I noticed that NSS pin is changing state after each transmitted byte. I need that NSS goes low before transmission is started and goes back to high only when both bytes transmitted.
Is that possible?
2020-03-12 02:11 PM
Which STM32?
What you describe sounds like NSS Pulse mode, see description of SPIx_CR2.NSSP and the NSS pulse mode subchapter in SPI chapter in RM. Check by reading back the SPI registers, if this bit is set.
I don't Cube, but Cube is open source so you can easily find yourself, how is this bit set.
JW
2020-03-13 03:09 AM
waclawek.jan, thanks for prompt response.
I was searching over several forums, seems i'm not alone facing this problem.
Here is screenshot from my scope using NSS pin controlled by hardware:
Blue curve represents SPI clock, yellow is NSS pin. In my case i'm working with flash chip, to read/write data i need that NSS remains low until both bytes transmitted.
I also tried to use GPIO as output for chip select controlled by software, in this case everything works, but software controlled pin is very slow, even sys.clock is 48Mhz. Here is screenshot:
Is there any suggestions what else to try?
Thank you in advance.
2020-03-13 05:23 AM
Is there any suggestions what else to try?
2020-03-13 06:02 AM
Thanks for response, berendi.
MCU is STM32F030C8T6
Do you mean that both delays shown in second screenshot (before and after transmission) caused by HAL function?
2020-03-13 07:03 AM
Try yourself - set GPIO pin low by writing into BSRR, then write byte into SPI_DR, wait until SPI_RXNE gets set, read SPI_DR, repeat once more, then set GPIO pin high again.
Observe on oscilloscope the result.
JW
2020-03-13 07:11 AM
2020-03-14 04:06 AM
Thanks for suggestions.
I tried to use own SPI send/receive function and CS pin controlled by software. Result surprised me, sending 2 bytes took only 6us. Using HAL_SPI function it took 24us.
Thanks a lot, my problem solved.
2020-03-14 05:16 AM
No surprises here, just look how overcomplicated the HAL functions are compared to your straightforward one.
HAL tries to account for lots of possible uses of a peripheral in one function, your code does exactly what is necessary.
2020-03-14 11:03 AM
> HAL tries to account for lots of possible uses of a peripheral
Isn't it ironic that it tries to do everything, but in real life can do nothing. Not even the UART!