2026-06-03 03:13:49 +08:00
|
|
|
|
#ifndef DEVICE_APP_SPI_COMMAND_H
|
2026-05-30 00:24:31 +08:00
|
|
|
|
#define DEVICE_APP_SPI_COMMAND_H
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief APP 运行期 SPI 命令从机模块。
|
|
|
|
|
|
*
|
|
|
|
|
|
* 复用 BOOT 的 SpiOta 协议帧格式(SOF+TYPE+LEN+PAYLOAD+CRC):
|
|
|
|
|
|
* - TYPE=0xB0 → 请求进入 Bootloader,回复 ACK 帧
|
|
|
|
|
|
* - TYPE=0x01/0x02/0x03(OTA 命令)→ 回复 WAIT 帧(不在 BOOT 模式)
|
|
|
|
|
|
* - 旧式单字节 0xB0 命令仍兼容
|
|
|
|
|
|
*
|
|
|
|
|
|
* 调试日志采用延迟输出:processCommand() 只写缓冲+置标志,
|
|
|
|
|
|
* poll() 中再 printf,避免在帧处理路径上耗时。
|
2026-06-03 03:13:49 +08:00
|
|
|
|
*
|
|
|
|
|
|
* 发送路径使用 DMA0_CH2 推 s_txBuf → SPI0_DATA,
|
|
|
|
|
|
* 与 BOOT 的 SpiOta 实现保持一致;接收仍用 RBNE 字节中断。
|
2026-05-30 00:24:31 +08:00
|
|
|
|
*/
|
|
|
|
|
|
class AppSpiCommand {
|
|
|
|
|
|
public:
|
|
|
|
|
|
/** @brief 初始化 SPI0 从机命令通道。 */
|
|
|
|
|
|
static void init();
|
|
|
|
|
|
|
|
|
|
|
|
/** @brief 主循环轮询:打印延迟日志。 */
|
|
|
|
|
|
static void poll();
|
|
|
|
|
|
|
|
|
|
|
|
/** @brief SPI0 中断处理入口。 */
|
|
|
|
|
|
static void onInterrupt();
|
|
|
|
|
|
|
|
|
|
|
|
/** @brief CS 边沿中断处理入口。 */
|
|
|
|
|
|
static void onCsExtiInterrupt();
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
static constexpr uint16_t kRxBufSize = 128U;
|
|
|
|
|
|
static constexpr uint16_t kTxBufSize = 64U;
|
|
|
|
|
|
static constexpr uint16_t kLogBufSize = 128U;
|
|
|
|
|
|
|
|
|
|
|
|
static volatile bool s_csActive;
|
|
|
|
|
|
static volatile uint8_t s_rxLen;
|
|
|
|
|
|
static volatile uint8_t s_txLen;
|
|
|
|
|
|
static volatile uint8_t s_txPos;
|
|
|
|
|
|
static volatile bool s_logPending;
|
|
|
|
|
|
static uint8_t s_rxBuf[kRxBufSize];
|
|
|
|
|
|
static uint8_t s_txBuf[kTxBufSize];
|
|
|
|
|
|
static char s_logBuf[kLogBufSize];
|
|
|
|
|
|
|
|
|
|
|
|
static void configureGpio();
|
|
|
|
|
|
static void configureSpi0();
|
2026-06-03 03:13:49 +08:00
|
|
|
|
static void configureDma();
|
2026-05-30 00:24:31 +08:00
|
|
|
|
static void configureExti();
|
|
|
|
|
|
static void onCsAssert();
|
|
|
|
|
|
static void onCsRelease();
|
|
|
|
|
|
static void processCommand();
|
|
|
|
|
|
static void logDeferred(const char *msg);
|
|
|
|
|
|
static void logDeferredFmt(const char *fmt, uint8_t val);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2026-05-30 20:03:31 +08:00
|
|
|
|
#endif /* DEVICE_APP_SPI_COMMAND_H */
|