--- /dev/null
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <termios.h>
+#include <sys/select.h>
+#include <sys/stat.h>
+#include <errno.h>
+#include <time.h>
+
+// 协议定义
+#define INIT_COMMAND_PREFIX "whmi-wri "
+#define INIT_COMMAND_SUFFIX "\xFF\xFF\xFF"
+#define ACK_BYTE 0x05
+#define PACKET_SIZE 4096
+#define RESPONSE_TIMEOUT_MS (500*6)
+
+// 串口配置结构体
+struct uart_config{
+ int baudrate;
+ const char *device;
+};
+
+// 错误处理函数
+void error_exit(const char *msg){
+ perror(msg);
+ exit(EXIT_FAILURE);
+}
+
+// 配置串口
+int configure_uart(int fd, int baudrate)
+{
+ struct termios tty;
+ memset(&tty, 0, sizeof(tty));
+
+ if (tcgetattr(fd, &tty) != 0){
+ return -1;
+ }
+
+ // 设置波特率
+ speed_t speed;
+ switch (baudrate){
+ case 9600: speed = B9600; break;
+ case 19200: speed = B19200; break;
+ case 38400: speed = B38400; break;
+ case 57600: speed = B57600; break;
+ case 115200: speed = B115200; break;
+ case 230400: speed = B230400; break;
+ case 460800: speed = B460800; break;
+ case 500000: speed = B500000; break;
+ case 921600: speed = B921600; break;
+ default: speed = B115200; break; // 默认115200
+ }
+ cfsetospeed(&tty, speed);
+ cfsetispeed(&tty, speed);
+
+ // 设置串口参数
+ tty.c_cflag &= ~PARENB; // 无奇偶校验
+ tty.c_cflag &= ~CSTOPB; // 1位停止位
+ tty.c_cflag &= ~CSIZE;
+ tty.c_cflag |= CS8; // 8位数据位
+ tty.c_cflag &= ~CRTSCTS; // 无硬件流控
+ tty.c_cflag |= CREAD | CLOCAL;
+
+ tty.c_lflag &= ~ICANON; // 非规范模式
+ tty.c_lflag &= ~(ECHO | ECHOE | ECHONL | ISIG); // 禁用回显和信号
+
+ tty.c_iflag &= ~(IXON | IXOFF | IXANY); // 禁用软件流控
+ tty.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL);
+
+ tty.c_oflag &= ~OPOST; // 原始输出
+ tty.c_oflag &= ~ONLCR;
+
+ // 设置超时
+ tty.c_cc[VTIME] = 0; // 不使用间隔定时器
+ tty.c_cc[VMIN] = 0; // 非阻塞模式
+
+ if (tcsetattr(fd, TCSANOW, &tty) != 0){
+ return -1;
+ }
+ return 0;
+}
+
+// 等待单个字节响应
+int wait_for_ack(int uart_fd)
+{
+ unsigned char response;
+ fd_set set;
+ struct timeval timeout;
+ time_t start_time = time(NULL);
+
+ while (1){
+ // 检查超时
+ if (time(NULL) - start_time > RESPONSE_TIMEOUT_MS/1000){
+ return -1;
+ }
+
+ FD_ZERO(&set);
+ FD_SET(uart_fd, &set);
+ timeout.tv_sec = 0;
+ timeout.tv_usec = 100000*20; // 100ms
+
+ if (select(uart_fd + 1, &set, NULL, NULL, &timeout) > 0){
+ if (read(uart_fd, &response, 1) > 0){
+ if (response == ACK_BYTE){
+ return 0;
+ }
+ }
+ }
+ }
+}
+
+// 发送固件数据包
+int send_firmware_packets(int uart_fd, const char *firmware_path)
+{
+ int fd = open(firmware_path, O_RDONLY);
+ if (fd < 0){
+ return -1;
+ }
+
+ unsigned char buffer[PACKET_SIZE];
+ ssize_t bytes_read;
+ int packet_count = 0;
+
+ while ((bytes_read = read(fd, buffer, PACKET_SIZE)) > 0){
+ // 发送数据包
+ if (write(uart_fd, buffer, bytes_read) != bytes_read){
+ close(fd);
+ return -1;
+ }
+
+ packet_count++;
+ printf("Sent packet %d, size %zd bytes\n", packet_count, bytes_read);
+
+ // 等待ACK
+ if (wait_for_ack(uart_fd) ){
+ fprintf(stderr, "ACK timeout after packet %d\n", packet_count);
+ close(fd);
+ return -1;
+ }
+ }
+
+ close(fd);
+ return packet_count;
+}
+
+// 固件升级主函数
+int upgrade_firmware(const char *device, int initial_baud, int download_baud, const char *firmware_path)
+{
+ // 打开串口
+ int uart_fd = open(device, O_RDWR | O_NOCTTY | O_SYNC);
+ if (uart_fd < 0){
+ error_exit("Failed to open serial port");
+ }
+
+ // 配置初始波特率
+ if (configure_uart(uart_fd, initial_baud)){
+ close(uart_fd);
+ error_exit("Failed to configure UART");
+ }
+
+ // 获取固件文件大小
+ struct stat st;
+ if (stat(firmware_path, &st) ){
+ close(uart_fd);
+ error_exit("Failed to get firmware size");
+ }
+ size_t filesize = st.st_size;
+
+ // 创建初始化命令
+ char init_cmd[256];
+ snprintf(init_cmd, sizeof(init_cmd), "%s%zu,%d,0%s",
+ INIT_COMMAND_PREFIX, filesize, download_baud, INIT_COMMAND_SUFFIX);
+
+ // 发送初始化命令
+ ssize_t cmd_len = strlen(init_cmd);
+ if (write(uart_fd, init_cmd, cmd_len) != cmd_len){
+ close(uart_fd);
+ error_exit("Failed to send init command");
+ }
+
+ printf("Sent init command: %s (size: %zu bytes)\n", init_cmd, filesize);
+
+ // 立即切换到下载波特率
+ if (configure_uart(uart_fd, download_baud)){
+ close(uart_fd);
+ error_exit("Failed to switch to download baudrate");
+ }
+
+ printf("Switched to download baudrate: %d\n", download_baud);
+
+ // 等待ACK
+ if (wait_for_ack(uart_fd)){
+ close(uart_fd);
+ error_exit("Timeout waiting for initial ACK");
+ }
+
+ printf("Received initial ACK, starting firmware transfer...\n");
+
+ // 发送固件数据
+ int packets_sent = send_firmware_packets(uart_fd, firmware_path);
+ if (packets_sent < 0){
+ close(uart_fd);
+ error_exit("Firmware transfer failed");
+ }
+
+ printf("Firmware upgrade completed successfully! Sent %d packets.\n", packets_sent);
+ close(uart_fd);
+ return 0;
+}
+#if 0
+
+int lcd_upgrade_test2(int argc, char *argv[])
+{
+ if (argc != 5){
+ fprintf(stderr, "Usage: %s <serial_device> <initial_baud> <download_baud> <firmware_path>\n", argv[0]);
+ fprintf(stderr, "Example: %s /dev/ttyS4 115200 921600 /userdata/lcd.HMI\n", argv[0]);
+ exit(EXIT_FAILURE);
+ }
+
+ const char *device = argv[1];
+ int initial_baud = atoi(argv[2]);
+ int download_baud = atoi(argv[3]);
+ const char *firmware_path = argv[4];
+
+ return upgrade_firmware(device, initial_baud, download_baud, firmware_path);
+}
+
+#endif
+