ymodem.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #ifndef YMODEM_H
  2. #define YMODEM_H
  3. #include <stdint.h>
  4. #include "stm32h7xx_hal.h"
  5. /* 接收结果码 */
  6. typedef enum {
  7. YM_ERR_NONE = 0, /* 会话正常完成 */
  8. YM_ERR_NO_START, /* 探测窗口内无任何 YMODEM 起始(放行跳 APP 判定) */
  9. YM_ERR_TIMEOUT, /* 半途超时 */
  10. YM_ERR_ABORT, /* 对端 CAN 取消 */
  11. YM_ERR_RETRIES, /* 重传次数耗尽 */
  12. YM_ERR_OVERSIZE, /* 超出暂存区容量 */
  13. YM_ERR_WRITE, /* 写存储回调失败 */
  14. YM_ERR_TOOLARGE, /* 文件名块 size 字段超限 */
  15. } ym_err_t;
  16. typedef struct {
  17. uint8_t blk;
  18. uint8_t blk_inv;
  19. uint16_t size; /* 128/1024 */
  20. uint16_t crc;
  21. uint8_t data[1024];
  22. } ym_frame_t;
  23. typedef struct {
  24. ym_err_t result;
  25. uint32_t image_size; /* 文件名块声明的大小, 0=未解析到 */
  26. uint8_t size_known;
  27. uint32_t recv_bytes; /* 实际落盘字节(含末块填充) */
  28. uint32_t blocks_done; /* 已接受的数据块数(绝对序号); 失败时=死在哪一块 */
  29. } ym_result_t;
  30. void ymodem_init(UART_HandleTypeDef *huart);
  31. void ymodem_flush(void);
  32. /*
  33. * 阻塞接收一个 YMODEM 文件流。
  34. * idle_ms: 无进展超时(ms)——自上一帧到达起计时, 持续传输不会触发。
  35. * 探测阶段传短值(如 2000ms 无人发起→NO_START); 升级会话传 30000。
  36. * on_start_cb: 收到合法文件名块后回调一次(可为 0), 供上层置 DOWNLOADING 状态。
  37. * write_cb: 把解出的数据块写到偏移 off 处, 返回 0=OK。
  38. */
  39. ym_result_t ymodem_receive(uint32_t idle_ms,
  40. void (*on_start_cb)(void),
  41. uint8_t (*write_cb)(uint32_t off, const uint8_t *d, uint32_t n));
  42. #endif /* YMODEM_H */