ymodem.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. // ymodem.c —— 稳定工程版
  2. #include "ymodem.h"
  3. #include "w25qxx.h"
  4. #include "main.h"
  5. #include <string.h>
  6. #include <stdlib.h>
  7. /* ================= 协议常量 ================= */
  8. #define SOH 0x01
  9. #define STX 0x02
  10. #define EOT 0x04
  11. #define ACK 0x06
  12. #define NAK 0x15
  13. #define CAN 0x18
  14. #define CRC_REQ 'C'
  15. #define PACKET_128 128
  16. #define PACKET_1K 1024
  17. #define CRC_SIZE 2
  18. #define MAX_ERRORS 30
  19. /* ================= 串口配置 ================= */
  20. #define YMODEM_USART USART2
  21. #define YMODEM_TMO 1000
  22. /* ================= UART ================= */
  23. static int uart_recv_byte(uint8_t *data, uint32_t timeout_ms)
  24. {
  25. uint32_t start = HAL_GetTick();
  26. while (!LL_USART_IsActiveFlag_RXNE(YMODEM_USART))
  27. {
  28. if ((HAL_GetTick() - start) > timeout_ms)
  29. return -1;
  30. }
  31. *data = LL_USART_ReceiveData8(YMODEM_USART);
  32. return 0;
  33. }
  34. static void uart_send_byte(uint8_t data)
  35. {
  36. while (!LL_USART_IsActiveFlag_TXE(YMODEM_USART));
  37. LL_USART_TransmitData8(YMODEM_USART, data);
  38. }
  39. static void ymodem_send_cancel(void)
  40. {
  41. uart_send_byte(CAN);
  42. uart_send_byte(CAN);
  43. }
  44. /* ================= CRC16 ================= */
  45. static uint16_t crc16_ccitt(const uint8_t *buf, uint32_t len)
  46. {
  47. uint16_t crc = 0;
  48. for (uint32_t i = 0; i < len; i++)
  49. {
  50. crc ^= (uint16_t)buf[i] << 8;
  51. for (uint8_t j = 0; j < 8; j++)
  52. crc = (crc & 0x8000) ? (crc << 1) ^ 0x1021 : (crc << 1);
  53. }
  54. return crc;
  55. }
  56. /* ================= 接收一个包 ================= */
  57. /* return: 0 OK, 1 EOT, -1 CAN, -2 ERROR */
  58. static int ymodem_receive_packet(uint8_t *data,
  59. uint32_t *len,
  60. uint8_t *pkt_no)
  61. {
  62. uint8_t c;
  63. uint32_t size;
  64. if (uart_recv_byte(&c, YMODEM_TMO) != 0)
  65. return -2;
  66. if (c == SOH) size = PACKET_128;
  67. else if (c == STX) size = PACKET_1K;
  68. else if (c == EOT) return 1;
  69. else if (c == CAN)
  70. {
  71. uart_recv_byte(&c, YMODEM_TMO);
  72. if (c == CAN) return -1;
  73. return -2;
  74. }
  75. else return -2;
  76. uint8_t no, no_inv;
  77. if (uart_recv_byte(&no, YMODEM_TMO) != 0) return -2;
  78. if (uart_recv_byte(&no_inv, YMODEM_TMO) != 0) return -2;
  79. if ((no ^ no_inv) != 0xFF) return -2;
  80. for (uint32_t i = 0; i < size + CRC_SIZE; i++)
  81. if (uart_recv_byte(&data[i], YMODEM_TMO) != 0)
  82. return -2;
  83. uint16_t crc_rx = (data[size] << 8) | data[size + 1];
  84. if (crc_rx != crc16_ccitt(data, size))
  85. return -2;
  86. *pkt_no = no;
  87. *len = size;
  88. return 0;
  89. }
  90. /* ================= Flash 写 ================= */
  91. static void flash_write(uint32_t addr, const uint8_t *buf, uint32_t len)
  92. {
  93. while (len)
  94. {
  95. uint32_t page = addr / w25qxx.PageSize;
  96. uint32_t offset = addr % w25qxx.PageSize;
  97. uint32_t chunk = w25qxx.PageSize - offset;
  98. if (chunk > len) chunk = len;
  99. W25qxx_WritePage((uint8_t *)buf, page, offset, chunk);
  100. addr += chunk;
  101. buf += chunk;
  102. len -= chunk;
  103. }
  104. }
  105. /* ================= 主接口 ================= */
  106. int32_t Ymodem_ReceiveToW25Qxx(uint32_t flashAddr,
  107. uint32_t flashMax,
  108. uint32_t *recvSize)
  109. {
  110. uint8_t packet[PACKET_1K + CRC_SIZE];
  111. uint32_t pktLen, total = 0, fileSize = 0;
  112. uint8_t pktNo, expect = 0;
  113. uint32_t err = 0;
  114. *recvSize = 0;
  115. /* ---- 包 0 ---- */
  116. while (1)
  117. {
  118. uart_send_byte(CRC_REQ);
  119. int r = ymodem_receive_packet(packet, &pktLen, &pktNo);
  120. if (r == 0 && pktNo == 0)
  121. {
  122. if (packet[0] == 0)
  123. {
  124. uart_send_byte(ACK);
  125. return -2;
  126. }
  127. char *name = (char *)packet;
  128. char *size = name + strlen(name) + 1;
  129. fileSize = strtoul(size, NULL, 10);
  130. if (fileSize == 0 || fileSize > flashMax)
  131. {
  132. ymodem_send_cancel();
  133. return -3;
  134. }
  135. uint32_t end = flashAddr + fileSize - 1;
  136. for (uint32_t s = flashAddr / w25qxx.SectorSize;
  137. s <= end / w25qxx.SectorSize; s++)
  138. W25qxx_EraseSector(s);
  139. uart_send_byte(ACK);
  140. uart_send_byte(CRC_REQ);
  141. expect = 1;
  142. break;
  143. }
  144. if (++err > MAX_ERRORS)
  145. {
  146. ymodem_send_cancel();
  147. return -4;
  148. }
  149. }
  150. /* ---- 数据包 ---- */
  151. err = 0;
  152. while (1)
  153. {
  154. int r = ymodem_receive_packet(packet, &pktLen, &pktNo);
  155. if (r == 0)
  156. {
  157. if (pktNo == expect)
  158. {
  159. uint32_t copy = pktLen;
  160. if (total + copy > fileSize)
  161. copy = fileSize - total;
  162. if (copy)
  163. {
  164. flash_write(flashAddr + total, packet, copy);
  165. total += copy;
  166. }
  167. expect++;
  168. }
  169. uart_send_byte(ACK);
  170. }
  171. else if (r == 1) /* EOT */
  172. {
  173. uart_send_byte(NAK);
  174. ymodem_receive_packet(packet, &pktLen, &pktNo);
  175. uart_send_byte(ACK);
  176. *recvSize = total;
  177. return 0;
  178. }
  179. else if (r == -1)
  180. return -5;
  181. else
  182. {
  183. if (++err > MAX_ERRORS)
  184. {
  185. ymodem_send_cancel();
  186. return -6;
  187. }
  188. uart_send_byte(NAK);
  189. }
  190. }
  191. }