如何在不使用下拉电阻浪费电流的情况下使 SPI SCK 线处于空闲状态?

How can I get SPI SCK line to idle low without wasting current with a pulldown resistor?

我正在使用 STM32L475 MCU 并使用 SPI 与 SD 卡通信。

当我配置 SPI SCK GPIO 引脚时,我将其配置为 PushPull 模式下的备用功能 SPI。我将 ClockPolarity 设置为闲置低电平,这会让我认为 SCK 线在不活动时会保持低电平,但事实并非如此。相反,SCK 线空闲高电平,直到 SPI 通信开始。这意味着如果我进行 SPI 写入和后续读取,则会触发额外的时钟脉冲,从而中断我的通信。

我发现能够解决此问题的唯一方法是进一步配置 GPIO 引脚以具有下拉电阻,但在我看来,这在电流消耗方面会很浪费。将引脚配置为漏极开路也无济于事,因为在这种情况下无论如何我都需要添加一个上拉电阻。最后,我尝试写入 SCK 引脚的 GPIO 引脚输出寄存器并将其设置为低电平,但这并没有改变任何东西。

在我看来,我唯一的选择是在推挽引脚上放置一个下拉电阻并不断浪费电流以使 SCK 线保持低电平。我想知道这是否是人们用于 SPI 通信的正常解决方案?我无法想象它是。

CPOL是SPIx_CR1的第1位,控制特定SPI的CLK引脚空闲状态。 所以如果你想在空闲时保持 SCK 引脚处于低状态,尝试:

SPI1->CR1 &= 0xFFFE;

您还可以在STM32CubeMX中配置时钟极性: screenshot.

我没有亲自尝试过,但是根据官方文档,这两种方法应该可以。 Official STM32L47xxx Reference

42.4.6 Communication formats
  Clock phase and polarity controls Four possible timing relationships may be chosen by software, using the CPOL and CPHA bits in the SPIx_CR1 register. The CPOL (clock polarity) bit controls the idle state value of the clock when no data is being transferred. This bit affects both master and slave modes. If CPOL is reset, the SCK pin has a low-level idle state. If CPOL is set, the SCK pin has a high-level idle state.
...
Bit 1 CPOL: Clock polarity
0. CK to 0 when idle
1: CK to 1 when idle
Note: This bit should not be changed when communication is ongoing.
This bit is not used in SPI TI mode except the case when CRC is applied at TI mode.