Below shows SCLK, MOSI and CS(\SYNC) lines for my SPI from a logic analyzer:

enter image description here

I use a Nucelo board as master to control this DAC(slave) via SPI. I didnt receive the DAC yet and trying to set the SPI.

And here below is an example code for the above timing diagrams:

  while (1)
        SPIoutputBuffer[1] = (char)(value >> 8);
        SPIoutputBuffer[2] = (char)(value & 0x00FF);

        HAL_SPI_Transmit(&hspi3, (uint8_t *)SPIoutputBuffer, 3, 1);


    /* USER CODE BEGIN 3 */

From the logic analyzer, I can measure that the SPI transmission begins 5us after CS goes low and CS goes high 15us after SPI transmission ends.

The DAC's required timing diagrams given as follows:

enter image description here

My questions are:

1-) I use HAL_GPIO_WritePin(CS low) and next line HAL_SPI_Transmit and next line HAL_GPIO_WritePin(CS high). But what could be the reason that there is so much delay between CS high/low and the SPI transmission in my case? How could those delays be reduced?

2-) In my case the SPI transmission begins 5us after CS goes low and CS goes high 15us after SPI transmission ends. Does that satisfythe timing requirements in the datasheet?


1 Answer 1

  1. Either HAL code is slow or your MCU is running slow. Either way, you can use the debugger to trace where the code spends time betwewn setting a pin low and startin of SPI transmission.

  2. Minimum times are given in nanoseconds and there are no maximum time stated. Therefore, it does not matter if the time is 5 microseconds, as long as it exceeds the minimums given.

  • \$\begingroup\$ Increasing the MCU clock from 52MHz to 72MHz reduced the delays proportionally 5us became 3us and 15 us became around 10us. But after the MCU max clock the HAL is probably causing the rest. \$\endgroup\$
    – GNZ
    Commented Apr 1, 2022 at 11:44
  • 1
    \$\begingroup\$ STM32 HAL is tremendously slow. If you want any kind of performance, don't use HAL. This can be done in only a few clock cycles, if done right. \$\endgroup\$
    – Klas-Kenny
    Commented Apr 1, 2022 at 12:47
  • \$\begingroup\$ @Klas-Kenny Which one do you think biggest cause of delay in my case? HAL_GPIO_WritePin or HAL_SPI_Transmit ? \$\endgroup\$
    – GNZ
    Commented Apr 1, 2022 at 13:27
  • \$\begingroup\$ @GNZ I'd guess the SPI routine is the worst. HAL_GPIO_WritePin isn't that terrible, at least not in the HAL library I'm currently looking at. \$\endgroup\$
    – Klas-Kenny
    Commented Apr 1, 2022 at 13:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.