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.
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:
Set the Maxium Output Speed of the Pin PA8 to Very Fast
Wait for the SPI to be done (see commented line above)
How do i get that to toggle faster? Should i create and interrupt for when the SPI is done and set the latch there?
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With