public bool UpdateFirmware(FileStream fw) { bool rs = true; string name = _sp.PortName; STMisp stmisp = new STMisp(); stmisp.Write = isp_write; stmisp.Read = isp_read; ResetToISP().Wait(); // 退出接收线程 _keepthread = false; _datarecvevent.Set(); _sp.Close(); Thread.Sleep(100); _ispcom = new SerialPort(name, 115200, Parity.Even, 8, StopBits.One); _ispcom.ReadTimeout = 200; _ispcom.Open(); ISPACK ack; ack = stmisp.InitBL(); if (ack != ISPACK.ISP_ACK) { rs = false; } #if true ack = stmisp.Erase(); if (ack == ISPACK.ISP_TO) { rs = false; } if (ack == ISPACK.ISP_NACK) { ack = stmisp.Read_unprotect(); if (ack != ISPACK.ISP_ACK) { rs = false; } Thread.Sleep(50); if (stmisp.InitBL() == ISPACK.ISP_TO) { rs = false; } } if (rs) { long len_rem = fw.Length; uint start_addr = 0x08000000; byte[] wm = new byte[256]; while (len_rem >= 256) { fw.Read(wm, 0, 256); if (WriteMemoryResult.OK != stmisp.WriteMemory(start_addr, 256, wm)) { rs = false; break; } len_rem -= 256; start_addr += 256; Console.WriteLine("remain bytes:{0}", len_rem); Thread.Sleep(50); } if (len_rem > 0 && rs == true) { fw.Read(wm, 0, (int)len_rem); if (WriteMemoryResult.OK != stmisp.WriteMemory(start_addr, 256, wm)) { rs = false; } Thread.Sleep(30); } fw.Close(); } #endif if (rs) { if (stmisp.Go() != ISPACK.ISP_ACK) { Console.WriteLine("go fail"); } else { Console.WriteLine("go succ"); } } _ispcom.Close(); _sp.Open(); _sp.DiscardInBuffer(); _keepthread = true; Thread _t = new Thread(new ThreadStart(recv_thread)); _t.Start(); return(rs); }
public int UpdateFirmware(string fw_path, int deviceidx) { string portname = scomm.PortName; STMisp stmisp = new STMisp(); stmisp.Write = isp_write; stmisp.Read = isp_read; // 成功重启标志 bool is_reboot = false; // 成功下载标志 bool is_download = false; int repeat_cnt = 0; while (repeat_cnt < 2 && !is_download) { this.CloseSerialBusProcessor(); this.OpenSerialBusProcessor(); bool proc_fail = false; //总线重新上电 RepowerupCallback?.Invoke(); Thread.Sleep(1000); repeat_cnt++; int rbrs = reboot(deviceidx, is_reboot); if (rbrs != 1) { is_reboot = true; } if (rbrs != 0) { continue; } Console.WriteLine(string.Format("module {0:d} reboot succ", deviceorder[deviceidx])); scomm.DiscardInBuffer(); this.CloseSerialBusProcessor(); Thread.Sleep(500); scomm.PortName = portname; scomm.Parity = Parity.Even; scomm.BaudRate = 115200; scomm.StopBits = StopBits.One; scomm.DataBits = 8; scomm.ReadTimeout = 2000; scomm.Open(); ISPACK ack; ack = stmisp.InitBL(); if (ack == ISPACK.ISP_TO) { continue; } while (ack == ISPACK.ISP_NACK) { ack = stmisp.InitBL(); } // 当前设备尚未下载成功,进入的ISP下载流程 if (!is_download) { ack = stmisp.Erase(); if (ack == ISPACK.ISP_TO) { continue; } if (ack == ISPACK.ISP_NACK) { ack = stmisp.Read_unprotect(); if (ack != ISPACK.ISP_ACK) { continue; } Thread.Sleep(50); if (stmisp.InitBL() == ISPACK.ISP_TO) { continue; } } System.IO.FileStream fs = new System.IO.FileStream(fw_path, System.IO.FileMode.Open); long len_rem = fs.Length; uint start_addr = 0x08000000; byte[] wm = new byte[256]; if (proc_fail == false) { while (len_rem >= 256) { fs.Read(wm, 0, 256); if (WriteMemoryResult.OK != stmisp.WriteMemory(start_addr, 256, wm)) { proc_fail = true; break; } len_rem -= 256; start_addr += 256; Console.WriteLine("remain bytes:{0}", len_rem); Thread.Sleep(100); } if (len_rem > 0 && proc_fail == false) { fs.Read(wm, 0, (int)len_rem); if (WriteMemoryResult.OK != stmisp.WriteMemory(start_addr, 256, wm)) { proc_fail = true; } Thread.Sleep(100); } fs.Close(); } } if (proc_fail) { continue; } Console.WriteLine("write memory succ"); is_download = true; // 下载成功后一定要执行GoApp命令,确保重新上电后切换到userboot模式 if (stmisp.Go(0x08000000) != ISPACK.ISP_ACK) { Console.WriteLine("go fail"); continue; } Console.WriteLine("go succ"); repeat_cnt = 0; scomm.Close(); break; } Thread.Sleep(1000); this.CloseSerialBusProcessor(); this.OpenSerialBusProcessor(); if (is_download) { return(0); } return(-1); }