STM32 SPI Slow Compute

Julian

I'm using a STM32F4 and its SPI to talk to a 74HC595 like in this tutorial. Difference is for starters I'm using the non-DMA version for simplicity. I used STMCubeMX to configure SPI and GPIO

Issue is: I'm not getting the latch PIN, that I set to PA8 to toggle during transmission fast enough.

enter image description here

The code I'm using:

        spiTxBuf[0] = 0b00000010;

        HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_RESET);


        HAL_SPI_Transmit(&hspi1, spiTxBuf, 1, HAL_MAX_DELAY);
//        while(HAL_SPI_GetState(&hspi1) != HAL_SPI_STATE_READY);

        HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_SET);

        HAL_Delay(1);

Things I tried:

  1. Set the Maxium Output Speed of the Pin PA8 to Very Fast enter image description here

  2. Wait for the SPI to be done (see commented line above)

  3. Use DMA for the SPI as in here, that made it actually slower.

How do i get that to toggle faster? Should i create and interrupt for when the SPI is done and set the latch there?

followed Monica to Codidact

How do i get that to toggle faster?

If possible, use the hardware NSS pin

Some STM32 controllers can toggle their NSS pin automatically, with a configurable delay after transmission. Check the Reference Manual, if yours is one of these, reconnect the latch pin of the shifter to the SPIx_NSS pin on the MCU.

Don't use HAL

HAL is quite slow and overcomplicated for anything with tight timing requirements. Don't use it.

Just implement the SPI transmit procedure in the Reference Manual.

SPI->CR1 |= SPI_CR1_SPE; // this is required only once
GPIOA->BSRR = 1 << (8 + 16);
*(volatile uint8_t *)&SPI->DR = 0b00000010;
while((SPI->SR & (SPI_SR_TXE | SPI_SR_BSY)) != SPI_SR_TXE)
    ;
GPIOA->BSRR = 1 << 8;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related