|
|
@@ -11,6 +11,11 @@ import (
|
|
|
)
|
|
|
|
|
|
func StartMCUUpgrade() error {
|
|
|
+ if !Board.upgradeMu.TryLock() {
|
|
|
+ return fmt.Errorf("upgrade busy")
|
|
|
+ }
|
|
|
+ defer Board.upgradeMu.Unlock()
|
|
|
+
|
|
|
filePath := filepath.Join(baseapp.VAR_DIR, "mcu_firmware.rbl")
|
|
|
defer os.Remove(filePath) //升级完成后删除升级文件
|
|
|
|
|
|
@@ -19,20 +24,18 @@ func StartMCUUpgrade() error {
|
|
|
return err
|
|
|
}
|
|
|
if !info.Mode().IsRegular() {
|
|
|
- return fmt.Errorf("固件文件无效: %s", filePath)
|
|
|
+ return fmt.Errorf("升级文件无效: %s", filePath)
|
|
|
}
|
|
|
if info.Size() <= 0 {
|
|
|
- return fmt.Errorf("固件文件为空: %s", filePath)
|
|
|
+ return fmt.Errorf("升级文件为空: %s", filePath)
|
|
|
}
|
|
|
|
|
|
fileSize := info.Size()
|
|
|
startTime := time.Now()
|
|
|
- upgradeOK := false
|
|
|
|
|
|
- Board.uartMu.Lock()
|
|
|
- defer Board.uartMu.Unlock()
|
|
|
+ Board.uartUseMu.Lock()
|
|
|
+ defer Board.uartUseMu.Unlock()
|
|
|
|
|
|
- baseapp.Logger.Warnf("====[ MCU UPGRADE PREPARE ]==== [%s] stop MCU communication success", MODULE_NAME)
|
|
|
mcuCtrlBoard_ComExit() /////////////// 1-关闭与控制板的串口通信
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
|
|
@@ -41,28 +44,16 @@ func StartMCUUpgrade() error {
|
|
|
baseapp.Logger.Warnf("##########################################################")
|
|
|
baseapp.Logger.Warnf("====[ MCU UPGRADE FILE ]====, file=%s, size=%dBytes", filePath, fileSize)
|
|
|
|
|
|
- bytes := ymodem.Send_file(filePath) // 2-使用ymodem发送升级文件
|
|
|
- if bytes > 0 && bytes == int(fileSize) {
|
|
|
- upgradeOK = true
|
|
|
- }
|
|
|
-
|
|
|
- elapsed := time.Since(startTime)
|
|
|
-
|
|
|
- if !upgradeOK {
|
|
|
- baseapp.Logger.Errorf("!!!![ MCU UPGRADE FAILED ]!!!!, written=%dBytes, expect=%dBytes", bytes, fileSize)
|
|
|
- return fmt.Errorf("MCU firmware upgrade failed, written=%dBytes, expect=%dBytes", bytes, fileSize)
|
|
|
+ size := ymodem.Send_file(filePath) // 2-使用ymodem发送升级文件
|
|
|
+ if size <= 0 || size != int(fileSize) {
|
|
|
+ baseapp.Logger.Errorf("!!!![ MCU UPGRADE FAILED ]!!!!, written=%dBytes, expect=%dBytes", size, fileSize)
|
|
|
+ } else {
|
|
|
+ baseapp.Logger.Warnf("====[ MCU UPGRADE SUCCESS ]====, 用时: %v", time.Since(startTime)) ////////// 成功
|
|
|
}
|
|
|
|
|
|
- baseapp.Logger.Warnf("====[ MCU UPGRADE SUCCESS ]====, 用时: %v", elapsed) //成功
|
|
|
-
|
|
|
- for range 5 { ////////// 3-升级结束后,重新打开与控制板的串口通信
|
|
|
- if baseapp.IsExit1() {
|
|
|
- return nil
|
|
|
- }
|
|
|
-
|
|
|
+ for range 3 { ////////// 3-升级结束后,重新打开与控制板的串口通信
|
|
|
_, err = mcuCtrlBoard_ComInit()
|
|
|
if err == nil {
|
|
|
- baseapp.Logger.Warnf("====[ MCU UPGRADE DONE ]==== [%s] restart MCU communication success", MODULE_NAME)
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
@@ -70,5 +61,5 @@ func StartMCUUpgrade() error {
|
|
|
time.Sleep(time.Second)
|
|
|
}
|
|
|
|
|
|
- return fmt.Errorf("failed to reopen MCU communication port: %w", err)
|
|
|
+ return fmt.Errorf("failed to reopen MCU serial port: %w", err)
|
|
|
}
|