# SPI：GPIO线路上检测到片选信号毛刺，导致从设备被误选中

- **ID:** `embedded/spi-cs-glitch-on-gpio`
- **领域:** embedded
- **类别:** hardware_error
- **验证级别:** ai_generated
- **修复率:** 90%

## 根因

片选GPIO线由软件驱动或驱动强度不足，在GPIO切换时产生短暂低电平脉冲（毛刺），导致SPI从设备被误选中或取消选中。

## 解决方案

1. ```
   Enable the SPI peripheral's hardware NSS (automatic chip select) if available. Example on STM32: hspi.Init.NSS = SPI_NSS_HARD_OUTPUT; then configure the CS pin as SPI_NSS alternate function.
   ```
2. ```
   Add a 10kΩ pull-up resistor on the CS line to hold it high during GPIO transitions, and set the GPIO output to open-drain with a weak drive.
   ```

## 无效尝试

- **** — Adding a delay before toggling CS does not eliminate the glitch; the glitch occurs during the transition itself. (70% 失败率)
- **** — Changing the GPIO output speed to high increases the glitch amplitude due to faster slew rate, making the problem worse. (60% 失败率)
