STM32 SPI 慢速计算 [英] STM32 SPI Slow Compute

查看:34
本文介绍了STM32 SPI 慢速计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 STM32F4 及其 SPI 与本教程中的 74HC595 通信.区别在于初学者我使用非 DMA 版本为简单起见.我用STMCubeMX配置SPI和GPIO

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

问题是:我没有获得闩锁 PIN,我将其设置为 PA8,以便在传输过程中足够快地切换.

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

我正在使用的代码:

        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);

我尝试过的事情:

  1. 将引脚 PA8 的最大输出速度设置为非常快

  1. Set the Maxium Output Speed of the Pin PA8 to Very Fast

等待 SPI 完成(参见上面的注释行)

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

如何让它更快地切换?我应该在 SPI 完成时创建和中断并在那里设置锁存器吗?

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?

如果可能,请使用硬件 NSS 引脚

一些 STM32 控制器可以自动切换其NSS 引脚,并在传输后具有可配置的延迟.查看参考手册,如果您是其中之一,请将移位器的锁存引脚重新连接到 MCU 上的 SPIx_NSS 引脚.

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.

不要使用 HAL

HAL 对于时序要求严格的任何事情都非常缓慢且过于复杂.不要使用它.

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

只需执行参考手册中的 SPI 传输程序即可.

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;

这篇关于STM32 SPI 慢速计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆