| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333 |
- /**
- * @file ymodem.c
- * @brief YMODEM-CRC 接收器(阻塞轮询式,无动态内存)
- *
- * 时序: [文件名块 block0] ACK C [数据块 1..N] C EOT ACK C [结束块00] ACK
- * - 1024/128 块自适应(逐块以块头 SOH/STX 判定);
- * - 块号 + 反码 + CRC16(XMODEM) 三重校验,重复块静默吞掉;
- * - 写 NOR 通过回调注入,本模块不感知存储介质;
- * - idle_ms 为无进展超时:握手阶段到点仍无起始块返回 YM_ERR_NO_START
- * (调用方据此放行跳 APP),半途超时返回 YM_ERR_TIMEOUT。
- * - 握手阶段(等文件名块)一旦看到 App 协议帧头 AA 55 就立刻返回 NO_START:
- * 那说明对面在讲 App 协议,本次不是升级;数据阶段不采信(固件里必然有 AA 55)。
- */
- #include "ymodem.h"
- #include "boot_crc.h"
- #include "boot_layout.h"
- #include "usart.h"
- #include <string.h>
- #include <stdlib.h>
- #define YM_SOH 0x01U
- #define YM_STX 0x02U
- #define YM_EOT 0x04U
- #define YM_ACK 0x06U
- #define YM_NAK 0x15U
- #define YM_CAN 0x18U
- #define YM_C 0x43U
- /* 上位机 App 协议帧头(AA 55)。与 YMODEM 共用一根线,握手阶段看到它即说明
- * 对面在讲 App 协议,本次不可能是升级 —— 由 ym_recv_frame 的返回码 4 上报。 */
- #define YM_APP_HDR0 0xAAU
- #define YM_APP_HDR1 0x55U
- #define YM_BLOCK_TIMEOUT_MS 1500U /* 单块到达等待(含发送方处理 ACK 时间) */
- #define YM_CHAR_TIMEOUT_MS 50U /* 块内字节间隔 */
- #define YM_C_INTERVAL_MS 1000U /* 'C' 广播周期 */
- #define YM_IDLE_SESSION_MS 30000U /* 数据流阶段的无进展超时 */
- static UART_HandleTypeDef *s_hu;
- /* ---------------- 底层 ---------------- */
- static void ym_send(uint8_t b)
- {
- HAL_UART_Transmit(s_hu, &b, 1, 50);
- }
- /* 轮询收 1 字节;返回 0=超时。ORE 溢出标志即时清除不丢后续数据。 */
- static uint8_t ym_recv_byte(uint32_t timeout_ms, uint8_t *out)
- {
- uint32_t start = HAL_GetTick();
- while ((uint32_t)(HAL_GetTick() - start) < timeout_ms)
- {
- if (__HAL_UART_GET_FLAG(s_hu, UART_FLAG_ORE))
- {
- /* 注意!H7 的 ORE 必须写 ICR.ORECF 清(HAL 全程如此)。原先靠"读 RDR"清,
- * 一旦锁死,本函数就停在这条分支里把每个字节都丢掉 ⇒ 表现为"只发不收"
- * (RXNE 永远看不到)。2026-10-26 实测:BL 收不到任何字节,App 同线正常。 */
- __HAL_UART_CLEAR_FLAG(s_hu, UART_CLEAR_OREF);
- continue;
- }
- if (__HAL_UART_GET_FLAG(s_hu, UART_FLAG_RXNE))
- {
- *out = (uint8_t)(s_hu->Instance->RDR & 0xFFU);
- return 1;
- }
- }
- return 0;
- }
- void ymodem_init(UART_HandleTypeDef *huart)
- {
- s_hu = huart;
- }
- void ymodem_flush(void)
- {
- uint8_t d;
- uint32_t last = HAL_GetTick();
- while ((uint32_t)(HAL_GetTick() - last) < 20U)
- {
- if (ym_recv_byte(5, &d)) last = HAL_GetTick();
- }
- }
- /* ---------------- 帧接收 ---------------- */
- /* 返回: 0=收到完整帧(字段已填, 有效性待定) 1=EOT 2=CAN 3=超时/杂字节/帧结构错
- * 4=疑似上位机协议帧头(AA 55) —— 由调用方按所处阶段决定是否放行 */
- static uint8_t ym_recv_frame(ym_frame_t *fr)
- {
- uint8_t b, b2, h, l;
- uint32_t i;
- if (!ym_recv_byte(YM_BLOCK_TIMEOUT_MS, &b)) return 3;
- if (b == YM_EOT) return 1;
- if (b == YM_CAN) return 2;
- if (b == YM_APP_HDR0)
- {
- /* 多等一个字节确认是不是 55;不是就只丢这 1 字节(本阶段本来就是噪声) */
- if (ym_recv_byte(YM_CHAR_TIMEOUT_MS, &b2) && b2 == YM_APP_HDR1) return 4;
- return 3;
- }
- if (b != YM_SOH && b != YM_STX) return 3;
- fr->size = (b == YM_SOH) ? 128U : 1024U;
- if (!ym_recv_byte(YM_CHAR_TIMEOUT_MS, &fr->blk)) return 3;
- if (!ym_recv_byte(YM_CHAR_TIMEOUT_MS, &fr->blk_inv)) return 3;
- for (i = 0; i < fr->size; i++)
- if (!ym_recv_byte(YM_CHAR_TIMEOUT_MS, &fr->data[i])) return 3;
- if (!ym_recv_byte(YM_CHAR_TIMEOUT_MS, &h)) return 3;
- if (!ym_recv_byte(YM_CHAR_TIMEOUT_MS, &l)) return 3;
- fr->crc = ((uint16_t)h << 8) | l;
- return 0;
- }
- static uint8_t ym_frame_valid(const ym_frame_t *fr, uint8_t expect_blk)
- {
- if (fr->blk != expect_blk) return 0;
- if ((uint8_t)(fr->blk ^ 0xFFU) != fr->blk_inv) return 0;
- if (boot_ymodem_crc16(fr->data, fr->size) != fr->crc) return 0;
- return 1;
- }
- /* ---------------- 会话 ---------------- */
- ym_result_t ymodem_receive(uint32_t idle_ms,
- void (*on_start_cb)(void),
- uint8_t (*write_cb)(uint32_t off, const uint8_t *d, uint32_t n))
- {
- ym_result_t res;
- ym_frame_t fr;
- uint8_t state = 0; /* 0=等文件名块 1=收数据块 2=等结束块 */
- uint32_t blk_exp = 0; /* 绝对块序号, 不回绕; 与线上块号比对时取低 8 位 */
- uint8_t retries = 0;
- uint32_t t_last_c;
- uint32_t now;
- uint32_t last_progress;
- memset(&res, 0, sizeof(res));
- t_last_c = HAL_GetTick() - YM_C_INTERVAL_MS; /* 立即广播首个 'C' */
- last_progress = HAL_GetTick();
- for (;;)
- {
- now = HAL_GetTick();
- /* 空闲时周期性广播 'C'(state 1 由 ACK 后补发驱动) */
- if ((state == 0 || state == 2) &&
- (uint32_t)(now - t_last_c) >= YM_C_INTERVAL_MS)
- {
- ym_send(YM_C);
- t_last_c = now;
- }
- {
- uint8_t r = ym_recv_frame(&fr);
- if (r == 1) /* EOT */
- {
- if (state == 1)
- {
- ym_send(YM_ACK);
- state = 2;
- blk_exp = 0;
- ym_send(YM_C);
- t_last_c = HAL_GetTick();
- continue;
- }
- if (state == 2)
- {
- ym_send(YM_ACK); /* 结束块后又来 EOT → 完成 */
- res.result = YM_ERR_NONE;
- return res;
- }
- /* state==0:还没收到文件名块,此时的 0x04 只能是噪声 —— 上位机
- * 0x04 查询帧 "AA 55 04 ..." 的第 3 字节就是 0x04(=EOT)。若在这里
- * 当"接收完成",调用方会拿 NOR staging 里的残留包重烧一遍(现场表现:
- * 发一条查询就自动升级,版本号不变)。 */
- continue;
- }
- if (r == 2) { res.result = YM_ERR_ABORT; return res; }
- if (r == 4)
- {
- /* 只在"等文件名块"阶段采信:此时对面若在讲 App 协议,本次就不可能是
- * 升级,直接放行跳 APP。数据阶段绝不能采信 —— 固件二进制里必然有 AA 55。 */
- if (state == 0)
- {
- res.result = YM_ERR_NO_START;
- return res;
- }
- continue;
- }
- if (r == 3)
- {
- /* 无进展超时: 握手阶段用 idle_ms,
- * 数据流阶段放宽到 YM_IDLE_SESSION_MS(发送方写盘/换块可能停顿) */
- uint32_t idle_eff = (state == 0) ? idle_ms : YM_IDLE_SESSION_MS;
- if ((uint32_t)(HAL_GetTick() - last_progress) >= idle_eff)
- {
- /* 死在哪一块: state 1 说明数据块阶段, blk_exp-1 = 已接受块数 */
- res.blocks_done = (state == 1U) ? (uint32_t)(blk_exp - 1U) : 0U;
- if (state == 0 && retries == 0)
- res.result = YM_ERR_NO_START; /* 探测窗口放行 */
- else
- {
- ym_send(YM_CAN); ym_send(YM_CAN);
- res.result = YM_ERR_TIMEOUT;
- }
- return res;
- }
- continue;
- }
- last_progress = HAL_GetTick();
- }
- if (state == 0)
- {
- /* ---- 文件名块 ---- */
- if (ym_frame_valid(&fr, 0))
- {
- char *q;
- fr.data[fr.size - 1] = '\0';
- q = strchr((char *)fr.data, ',');
- if (q)
- {
- uint32_t sz = strtoul(q + 1, 0, 10);
- if (sz > 64U && sz <= NOR_IMG_STAGING_SIZE)
- {
- res.image_size = sz;
- res.size_known = 1;
- }
- else if (sz > NOR_IMG_STAGING_SIZE)
- {
- ym_send(YM_CAN); ym_send(YM_CAN);
- res.result = YM_ERR_TOOLARGE;
- return res;
- }
- }
- /* on_start_cb 必须在 ACK 之前执行: 它写 EEPROM 期间 CPU 不读 RDR,
- * 若放在 ACK 之后, 对端收到 ACK 立刻发第 1 个数据块, 块首若干字节
- * 被 ORE 吞掉->帧边界自此永久错位, 之后每次重传都拼不出完整帧。 */
- if (on_start_cb) on_start_cb();
- ym_send(YM_ACK);
- ym_send(YM_C);
- t_last_c = HAL_GetTick();
- state = 1;
- blk_exp = 1;
- retries = 0;
- continue;
- }
- ym_send(YM_NAK);
- if (++retries > BL_YMODEM_MAX_RETRIES) goto fail_retries;
- continue;
- }
- if (state == 2)
- {
- /* ---- 结束块: blk 0, 全 0 填充, CRC 完整校验 ---- */
- if (ym_frame_valid(&fr, 0))
- {
- ym_send(YM_ACK);
- /* 一个字节都没落盘就"完成",数据只可能来自 NOR 残留包 —— 宁可判失败
- * 也不能烧。已解析出大小时,还要求实收不少于声明大小。 */
- if (res.recv_bytes == 0U ||
- (res.size_known && res.recv_bytes < res.image_size))
- {
- ym_send(YM_CAN); ym_send(YM_CAN);
- res.result = YM_ERR_ABORT;
- return res;
- }
- res.result = YM_ERR_NONE;
- return res;
- }
- ym_send(YM_NAK);
- if (++retries > BL_YMODEM_MAX_RETRIES) goto fail_retries;
- continue;
- }
- /* state==1 数据块 */
- if (!ym_frame_valid(&fr, (uint8_t)blk_exp))
- {
- if (blk_exp > 1 && fr.blk == (uint8_t)(blk_exp - 1U) &&
- boot_ymodem_crc16(fr.data, fr.size) == fr.crc)
- {
- /* 上一块的重发(ACK 丢失): 重发 ACK, 不重复写 */
- ym_send(YM_ACK);
- continue;
- }
- ym_send(YM_NAK);
- if (++retries > BL_YMODEM_MAX_RETRIES) goto fail_retries;
- continue;
- }
- retries = 0;
- {
- uint32_t offset = (blk_exp - 1U) * fr.size;
- /* 容量检查必须用 64 位: 若 offset 被算成回绕值(如 0xFFFFFC00),
- * 32 位加法 offset+size 会溢出成 0 从而"看起来"合法, 于是越界地址
- * 被静默送给 NOR (24 位地址还会被截断) —— 2026-09-17 现场故障
- * (卡在第 257 块, 设备 30s 不理人) 正是这条路。 */
- if ((uint64_t)offset + fr.size > NOR_IMG_STAGING_SIZE)
- {
- res.blocks_done = (uint32_t)(blk_exp - 1U);
- ym_send(YM_CAN); ym_send(YM_CAN);
- res.result = YM_ERR_OVERSIZE;
- return res;
- }
- if (write_cb(offset, fr.data, fr.size) != 0)
- {
- res.blocks_done = (uint32_t)(blk_exp - 1U);
- ym_send(YM_CAN); ym_send(YM_CAN);
- res.result = YM_ERR_WRITE;
- return res;
- }
- if (offset + fr.size > res.recv_bytes)
- res.recv_bytes = offset + fr.size;
- res.blocks_done = blk_exp; /* 已接受块数 = 绝对序号 */
- }
- ym_send(YM_ACK);
- ym_send(YM_C);
- t_last_c = HAL_GetTick();
- blk_exp++;
- }
- fail_retries:
- ym_send(YM_CAN); ym_send(YM_CAN);
- res.result = YM_ERR_RETRIES;
- return res;
- }
|