Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

STM32 SPI Slow Compute

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?

like image 861
Julian Avatar asked Jun 04 '19 08:06

Julian


1 Answers

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;
like image 65
followed Monica to Codidact Avatar answered Nov 12 '22 00:11

followed Monica to Codidact