| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- package mcu_ctrl_board
- import (
- "errors"
- "fmt"
- "os"
- "path/filepath"
- "time"
- "hnyfkj.com.cn/rtu/linux/baseapp"
- "hnyfkj.com.cn/rtu/linux/ymodem"
- )
- const (
- DefaultSendFileName = "rtthread.rbl" // 默认发送的升级文件名
- )
- func StartMCUUpgrade() error {
- fwPath := filepath.Join(baseapp.VAR_DIR, DefaultSendFileName)
- defer os.Remove(fwPath)
- info, err := os.Stat(fwPath)
- if err != nil {
- return err
- }
- if !info.Mode().IsRegular() {
- return fmt.Errorf("固件文件无效: %s", fwPath)
- }
- if info.Size() <= 0 {
- return fmt.Errorf("固件文件为空: %s", fwPath)
- }
- fwSize := info.Size()
- startTime := time.Now()
- upgradeOK := false
- baseapp.Logger.Warnf("====[ MCU UPGRADE PREPARE ]==== [%s] stop MCU communication", MODULE_NAME)
- ModuleExit() // 先退出MCU控制板的通信
- time.Sleep(100 * time.Millisecond) /// 延时等待串口安全关闭
- baseapp.Logger.Warnf("##########################################################")
- baseapp.Logger.Warnf("############# MCU FIRMWARE UPGRADE START #############")
- baseapp.Logger.Warnf("##########################################################")
- baseapp.Logger.Warnf("====[ MCU UPGRADE FILE ]====, file=%s, size=%dBytes", fwPath, fwSize)
- bytes := ymodem.Send_file(fwPath) // 使用ymodem发送升级文件
- if bytes > 0 && bytes == int(fwSize) {
- upgradeOK = true
- }
- if ok := ModuleInit(Board.ExtInfo); !ok { /// 重新打开串口
- return errors.New("mcu serial open failed")
- }
- elapsed := time.Since(startTime)
- if !upgradeOK {
- baseapp.Logger.Errorf("!!!![ MCU UPGRADE FAILED ]!!!!, written=%dBytes, expect=%dBytes", bytes, fwSize)
- return fmt.Errorf("MCU firmware upgrade failed, written=%dBytes, expect=%dBytes", bytes, fwSize)
- }
- baseapp.Logger.Warnf("====[ MCU UPGRADE SUCCESS ]====, 用时: %v", elapsed) //成功
- return nil
- }
|