Просмотр исходного кода

MCU控制板固件升级联调BUG修复

niujiuru 1 неделя назад
Родитель
Сommit
82b557a0b1

+ 3 - 0
mcu_ctrl_board/mcu_ctrl_board.c

@@ -190,6 +190,9 @@ drop_p: //enter DROP
    char *pReply = go_handle_serial_json_info((const char *)pRecvBuf); // 回调处理一条JSON报文
    if(!pReply) goto idle_p;
 
+   if(strcmp(pReply, "__START_MCU_UPGRADE__") == 0) { free(pReply); return -1; } // 控制板即将进入升级流程, 退出正常业务报文接收线程, 避免与
+   // 升级通信争用串口; 待升级完成后, 由上层逻辑重新启动业务报文接收线程.
+
    int nReplyBytes = strlen(pReply);
    int sendBytes = serial_send_data(pSerial, (const unsigned char *)pReply, nReplyBytes);
    if(sendBytes == nReplyBytes) sw_log_debug("[%s] %s sent a reply(%d bytes): %s", MODULE_NAME, log_prefix, nReplyBytes, pReply);

+ 3 - 0
mcu_ctrl_board/proc_json.go

@@ -143,6 +143,9 @@ func processJson(jsonStr string) *C.char {
 		if strings.Contains(jsonStr, "system will update") { ////// MCU控制板YMODEM升级
 			filePath := filepath.Join(baseapp.VAR_DIR, "MCUUpgrade.rbl")
 			err = StartMCUUpgrade(filePath)
+			if err == nil {
+				return C.CString("__START_MCU_UPGRADE__") // 返回特殊标识, 让串口业务线程退出
+			}
 			break
 		}
 

+ 4 - 4
mcu_ctrl_board/upgrade_hmi.go

@@ -14,8 +14,8 @@ import (
 )
 
 const (
-	UPGRADE_PACKET_SIZE    = 512 // 单位: 字节
-	UPGRADE_PACKET_TIMEOUT = 10 * time.Second
+	UPGRADE_PACKET_SIZE    = 1024 // 单位: 字节
+	UPGRADE_PACKET_TIMEOUT = 3 * time.Second
 )
 
 // 开始HMI屏幕固件升级(异步协程执行)
@@ -31,10 +31,9 @@ func StartHMIUpgrade(filePath string, onSuccess func(any), callbackArg any) erro
 		}()
 
 		utils.PlayAudioAsync(filepath.Join(baseapp.EXEC_DIR, "voices",
-			"startupdate.wav")) ///// 播报开始升级语音
+			"startupdate.wav")) //// 播报开始升级语音
 
 		startTime := time.Now()
-		baseapp.Logger.Warnf("====[ HMI UPGRADE PREPARE ]==== [%s]", MODULE_NAME)
 		baseapp.Logger.Warnf("##########################################################")
 		baseapp.Logger.Warnf("#############   HMI FIRMWARE UPGRADE START   #############")
 		baseapp.Logger.Warnf("##########################################################")
@@ -133,6 +132,7 @@ func sendFilePackets(filePath string) error {
 	var offset int64 /////// 已发送文件字节数
 	buf := make([]byte, UPGRADE_PACKET_SIZE)
 
+	baseapp.Logger.Warn("====[ HMI UPGRADING... ]====")
 	for {
 		n, err := file.Read(buf) /// 读取发送帧
 

+ 56 - 44
mcu_ctrl_board/upgrade_mcu.go

@@ -16,58 +16,70 @@ func StartMCUUpgrade(filePath string) error {
 		return fmt.Errorf("upgrade busy")
 	}
 
-	defer func() {
-		os.Remove(filePath) /////// 升级完成后删除
-		Board.upgradeMu.Unlock()
-	}()
+	go func() {
+		defer func() {
+			os.Remove(filePath) // 升级完成后删除文件
+			Board.upgradeMu.Unlock()
+		}()
 
-	info, err := os.Stat(filePath)
-	if err != nil {
-		return err
-	}
-	if !info.Mode().IsRegular() {
-		return fmt.Errorf("升级文件无效: %s", filePath)
-	}
-	if info.Size() <= 0 {
-		return fmt.Errorf("升级文件为空: %s", filePath)
-	}
+		utils.PlayAudioAsync(filepath.Join(baseapp.EXEC_DIR, "voices",
+			"startupdate.wav")) //// 播报开始升级语音
+
+		startTime := time.Now()
+		baseapp.Logger.Warnf("##########################################################")
+		baseapp.Logger.Warnf("#############   MCU FIRMWARE UPGRADE START   #############")
+		baseapp.Logger.Warnf("##########################################################")
+
+		info, err := os.Stat(filePath)
+		if err != nil {
+			baseapp.Logger.Errorf("!!!![ MCU UPGRADE FAILED ]!!!!, %v", err)
+			return
+		}
+		if !info.Mode().IsRegular() {
+			baseapp.Logger.Errorf("!!!![ MCU UPGRADE FAILED ]!!!!, 升级文件无效: %s", filePath)
+			return
+		}
+		if info.Size() <= 0 {
+			baseapp.Logger.Errorf("!!!![ MCU UPGRADE FAILED ]!!!!, 升级文件为空: %s", filePath)
+			return
+		}
 
-	fileSize := info.Size()
-	startTime := time.Now()
+		fileSize := info.Size()
+		baseapp.Logger.Warnf("====[ MCU UPGRADE FILE ]====, file=%s, size=%dBytes", filePath, fileSize)
 
-	Board.uartUseMu.Lock()
-	defer Board.uartUseMu.Unlock()
+		Board.uartUseMu.Lock()
+		defer Board.uartUseMu.Unlock()
 
-	mcuCtrlBoard_ComExit() /////////////// 1-关闭与控制板的串口通信
-	time.Sleep(100 * time.Millisecond)
+		time.Sleep(100 * time.Millisecond)
+		mcuCtrlBoard_ComExit() /////////////// 1-关闭与控制板的串口通信
+		time.Sleep(100 * time.Millisecond)
 
-	utils.PlayAudioAsync(filepath.Join(baseapp.EXEC_DIR, "voices",
-		"startupdate.wav")) ////// 播报开始升级语音
-	baseapp.Logger.Warnf("##########################################################")
-	baseapp.Logger.Warnf("#############   MCU FIRMWARE UPGRADE START   #############")
-	baseapp.Logger.Warnf("##########################################################")
-	baseapp.Logger.Warnf("====[ MCU UPGRADE FILE ]====, file=%s, size=%dBytes", filePath, fileSize)
+		baseapp.Logger.Warn("====[ MCU UPGRADING... ]====")
+		size := ymodem.Send_file(filePath) /// 2-使用ymodem发送升级文件
+		if size <= 0 || size != int(fileSize) {
+			utils.PlayAudioAsync(filepath.Join(baseapp.EXEC_DIR, "voices",
+				"updatefailed.wav")) /// 播报升级失败语音
+			baseapp.Logger.Errorf("!!!![ MCU UPGRADE FAILED ]!!!!, written=%dBytes, expect=%dBytes", size, fileSize)
+		} else {
+			utils.PlayAudioAsync(filepath.Join(baseapp.EXEC_DIR, "voices",
+				"updatesuccess.wav")) // 播报升级成功语音
+			baseapp.Logger.Warnf("====[ MCU UPGRADE SUCCESS ]====, 用时: %v", time.Since(startTime)) ////////// 成功
+		}
 
-	size := ymodem.Send_file(filePath) /// 2-使用ymodem发送升级文件
-	if size <= 0 || size != int(fileSize) {
-		utils.PlayAudioAsync(filepath.Join(baseapp.EXEC_DIR, "voices",
-			"updatefailed.wav")) /// 播报升级失败语音
-		baseapp.Logger.Errorf("!!!![ MCU UPGRADE FAILED ]!!!!, written=%dBytes, expect=%dBytes", size, fileSize)
-	} else {
-		utils.PlayAudioAsync(filepath.Join(baseapp.EXEC_DIR, "voices",
-			"updatesuccess.wav")) // 播报升级成功语音
-		baseapp.Logger.Warnf("====[ MCU UPGRADE SUCCESS ]====, 用时: %v", time.Since(startTime)) ////////// 成功
-	}
+		for range 3 { ////////// 3-升级结束后,重新打开与控制板的串口通信
+			_, err = mcuCtrlBoard_ComInit()
+			if err == nil {
+				return
+			}
 
-	for range 3 { ////////// 3-升级结束后,重新打开与控制板的串口通信
-		_, err = mcuCtrlBoard_ComInit()
-		if err == nil {
-			return nil
+			mcuCtrlBoard_ComExit()
+			time.Sleep(time.Second)
 		}
 
-		mcuCtrlBoard_ComExit()
-		time.Sleep(time.Second)
-	}
+		baseapp.Logger.Errorf("[%s] 打开与MCU控制板通信的串口失败: %v!!",
+			MODULE_NAME, err)
+		os.Exit(1)
+	}()
 
-	return fmt.Errorf("failed to reopen MCU serial port: %w", err)
+	return nil
 }