| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- // ymodem.c —— 稳定工程版
- #include "ymodem.h"
- #include "w25qxx.h"
- #include "main.h"
- #include <string.h>
- #include <stdlib.h>
- /* ================= 协议常量 ================= */
- #define SOH 0x01
- #define STX 0x02
- #define EOT 0x04
- #define ACK 0x06
- #define NAK 0x15
- #define CAN 0x18
- #define CRC_REQ 'C'
- #define PACKET_128 128
- #define PACKET_1K 1024
- #define CRC_SIZE 2
- #define MAX_ERRORS 30
- /* ================= 串口配置 ================= */
- #define YMODEM_USART USART2
- #define YMODEM_TMO 1000
- /* ================= UART ================= */
- static int uart_recv_byte(uint8_t *data, uint32_t timeout_ms)
- {
- uint32_t start = HAL_GetTick();
- while (!LL_USART_IsActiveFlag_RXNE(YMODEM_USART))
- {
- if ((HAL_GetTick() - start) > timeout_ms)
- return -1;
- }
- *data = LL_USART_ReceiveData8(YMODEM_USART);
- return 0;
- }
- static void uart_send_byte(uint8_t data)
- {
- while (!LL_USART_IsActiveFlag_TXE(YMODEM_USART));
- LL_USART_TransmitData8(YMODEM_USART, data);
- }
- static void ymodem_send_cancel(void)
- {
- uart_send_byte(CAN);
- uart_send_byte(CAN);
- }
- /* ================= CRC16 ================= */
- static uint16_t crc16_ccitt(const uint8_t *buf, uint32_t len)
- {
- uint16_t crc = 0;
- for (uint32_t i = 0; i < len; i++)
- {
- crc ^= (uint16_t)buf[i] << 8;
- for (uint8_t j = 0; j < 8; j++)
- crc = (crc & 0x8000) ? (crc << 1) ^ 0x1021 : (crc << 1);
- }
- return crc;
- }
- /* ================= 接收一个包 ================= */
- /* return: 0 OK, 1 EOT, -1 CAN, -2 ERROR */
- static int ymodem_receive_packet(uint8_t *data,
- uint32_t *len,
- uint8_t *pkt_no)
- {
- uint8_t c;
- uint32_t size;
- if (uart_recv_byte(&c, YMODEM_TMO) != 0)
- return -2;
- if (c == SOH) size = PACKET_128;
- else if (c == STX) size = PACKET_1K;
- else if (c == EOT) return 1;
- else if (c == CAN)
- {
- uart_recv_byte(&c, YMODEM_TMO);
- if (c == CAN) return -1;
- return -2;
- }
- else return -2;
- uint8_t no, no_inv;
- if (uart_recv_byte(&no, YMODEM_TMO) != 0) return -2;
- if (uart_recv_byte(&no_inv, YMODEM_TMO) != 0) return -2;
- if ((no ^ no_inv) != 0xFF) return -2;
- for (uint32_t i = 0; i < size + CRC_SIZE; i++)
- if (uart_recv_byte(&data[i], YMODEM_TMO) != 0)
- return -2;
- uint16_t crc_rx = (data[size] << 8) | data[size + 1];
- if (crc_rx != crc16_ccitt(data, size))
- return -2;
- *pkt_no = no;
- *len = size;
- return 0;
- }
- /* ================= Flash 写 ================= */
- static void flash_write(uint32_t addr, const uint8_t *buf, uint32_t len)
- {
- while (len)
- {
- uint32_t page = addr / w25qxx.PageSize;
- uint32_t offset = addr % w25qxx.PageSize;
- uint32_t chunk = w25qxx.PageSize - offset;
- if (chunk > len) chunk = len;
- W25qxx_WritePage((uint8_t *)buf, page, offset, chunk);
- addr += chunk;
- buf += chunk;
- len -= chunk;
- }
- }
- /* ================= 主接口 ================= */
- int32_t Ymodem_ReceiveToW25Qxx(uint32_t flashAddr,
- uint32_t flashMax,
- uint32_t *recvSize)
- {
- uint8_t packet[PACKET_1K + CRC_SIZE];
- uint32_t pktLen, total = 0, fileSize = 0;
- uint8_t pktNo, expect = 0;
- uint32_t err = 0;
- *recvSize = 0;
- /* ---- 包 0 ---- */
- while (1)
- {
- uart_send_byte(CRC_REQ);
- int r = ymodem_receive_packet(packet, &pktLen, &pktNo);
- if (r == 0 && pktNo == 0)
- {
- if (packet[0] == 0)
- {
- uart_send_byte(ACK);
- return -2;
- }
- char *name = (char *)packet;
- char *size = name + strlen(name) + 1;
- fileSize = strtoul(size, NULL, 10);
- if (fileSize == 0 || fileSize > flashMax)
- {
- ymodem_send_cancel();
- return -3;
- }
- uint32_t end = flashAddr + fileSize - 1;
- for (uint32_t s = flashAddr / w25qxx.SectorSize;
- s <= end / w25qxx.SectorSize; s++)
- W25qxx_EraseSector(s);
- uart_send_byte(ACK);
- uart_send_byte(CRC_REQ);
- expect = 1;
- break;
- }
- if (++err > MAX_ERRORS)
- {
- ymodem_send_cancel();
- return -4;
- }
- }
- /* ---- 数据包 ---- */
- err = 0;
- while (1)
- {
- int r = ymodem_receive_packet(packet, &pktLen, &pktNo);
- if (r == 0)
- {
- if (pktNo == expect)
- {
- uint32_t copy = pktLen;
- if (total + copy > fileSize)
- copy = fileSize - total;
- if (copy)
- {
- flash_write(flashAddr + total, packet, copy);
- total += copy;
- }
- expect++;
- }
- uart_send_byte(ACK);
- }
- else if (r == 1) /* EOT */
- {
- uart_send_byte(NAK);
- ymodem_receive_packet(packet, &pktLen, &pktNo);
- uart_send_byte(ACK);
- *recvSize = total;
- return 0;
- }
- else if (r == -1)
- return -5;
- else
- {
- if (++err > MAX_ERRORS)
- {
- ymodem_send_cancel();
- return -6;
- }
- uart_send_byte(NAK);
- }
- }
- }
|