public void __verify_v3(Firmware fw) { //Expected 0x33E22A51 //Got 0x8117524C //System.Exception: Program CRC failed //python //Expected 0x4c521781 //Got 0x4c521781 int expect_crc = fw.crc(self.fw_maxsize); __send(new byte[] { (byte)Code.GET_CRC, (byte)Code.EOC }); int report_crc = __recv_int(); __getSync(); print("Expected 0x" + hexlify(BitConverter.GetBytes(expect_crc)) + " " + expect_crc); print("Got 0x" + hexlify(BitConverter.GetBytes(report_crc)) + " " + report_crc); if (report_crc != expect_crc) { throw new Exception("Program CRC failed"); } }
public void currentChecksum(Firmware fw) { if (self.bl_rev < 3) { return; } this.port.ReadTimeout = 1000; // 1 sec int expect_crc = fw.crc(self.fw_maxsize); __send(new byte[] { (byte)Code.GET_CRC, (byte)Code.EOC }); int report_crc = __recv_int(); self.__getSync(); print("FW File 0x" + hexlify(BitConverter.GetBytes(expect_crc)) + " " + expect_crc); print("Current 0x" + hexlify(BitConverter.GetBytes(report_crc)) + " " + report_crc); if (expect_crc == report_crc) { throw new Exception("Same Firmware. Not uploading"); } }
public void upload(Firmware fw) { this.port.ReadTimeout = 1000; // 1 sec //Make sure we are doing the right thing if (self.board_type != fw.board_id) { if (!(self.board_type == 33 && fw.board_id == 9)) { throw new Exception("Firmware not suitable for this board fw:" + fw.board_id + " - board:" + self.board_type); } } if (self.fw_maxsize < fw.image_size && self.fw_maxsize != 0) { throw new Exception("Firmware image is too large for this board"); } if (self.extf_maxsize < fw.extf_image_size && self.extf_maxsize != 0) { throw new Exception("extf image is too large for this board"); } if (fw.image_size > 0) { print("erase..."); self.__erase(); print("program..."); self.__program(fw); print("verify..."); if (self.bl_rev == 2) { self.__verify_v2(fw); } else { self.__verify_v3(fw); } } if (fw.extf_image_size > 0) { print("erase extf..."); self.__erase_extf(fw); print("program extf..."); self.__program_extf(fw); print("verify extf..."); self.__verify_extf(fw); } print("done, rebooting."); self.__reboot(); try { self.port.Close(); } catch { } }
public void __verify_v2(Firmware fw) { self.__send(new byte[] { (byte)Code.CHIP_VERIFY , (byte)Code.EOC }); self.__getSync(); byte[] code = fw.imagebyte; List <byte[]> groups = self.__split_len(code, (byte)Code.READ_MULTI_MAX); int a = 1; foreach (byte[] bytes in groups) { if (!self.__verify_multi(bytes)) { throw new Exception("Verification failed"); } Console.WriteLine("Verify {0}/{1}", a, groups.Count); a++; if (ProgressEvent != null) { ProgressEvent((a / (float)groups.Count) * 100.0); } } }
public static Firmware ProcessFirmware(string path) { // JavaScriptSerializer serializer = new JavaScriptSerializer(); Console.WriteLine("Read File " + path); // read the file StreamReader f = new StreamReader(File.OpenRead(path)); //fw = serializer.Deserialize<Firmware>(f.ReadToEnd()); fw = JSON.Instance.ToObject <Firmware>(f.ReadToEnd()); f.Close(); byte[] data = Convert.FromBase64String(fw.image); MemoryStream imagems = new MemoryStream(data, true); ZlibStream decompressionStream = new ZlibStream(imagems, CompressionMode.Decompress); int size = fw.image_size + (fw.image_size % 4); fw.imagebyte = new byte[size]; for (int a = 0; a < fw.imagebyte.Length; a++) { fw.imagebyte[a] = 0xff; } try { decompressionStream.Read(fw.imagebyte, 0, fw.image_size); } catch { Console.WriteLine("Possible bad file - usually safe to ignore"); } Console.WriteLine("image_size {0} size {1}", fw.image_size, size); using (BinaryWriter sw = new BinaryWriter(File.Open("px4fw.bin", FileMode.Create))) { foreach (byte by in fw.imagebyte) { sw.Write(by); // Console.Write("{0:x2}", by); } sw.Close(); } // pad image to 4-byte length //while ((fw.imagebyte.Length % 4) != 0) { //fw.imagebyte. += b'\x00' return(fw); }
public static Firmware ProcessFirmware(string path) { // JavaScriptSerializer serializer = new JavaScriptSerializer(); Console.WriteLine("Read File " + path); // read the file StreamReader f = new StreamReader(File.OpenRead(path)); //fw = serializer.Deserialize<Firmware>(f.ReadToEnd()); fw = JSON.Instance.ToObject<Firmware>(f.ReadToEnd()); f.Close(); byte[] data = Convert.FromBase64String(fw.image); MemoryStream imagems = new MemoryStream(data, true); ZlibStream decompressionStream = new ZlibStream(imagems, CompressionMode.Decompress); int size = fw.image_size + (fw.image_size % 4); fw.imagebyte = new byte[size]; for (int a = 0; a < fw.imagebyte.Length; a++) { fw.imagebyte[a] = 0xff; } try { decompressionStream.Read(fw.imagebyte, 0, fw.image_size); } catch { Console.WriteLine("Possible bad file - usualy safe to ignore"); } Console.WriteLine("image_size {0} size {1}",fw.image_size,size); BinaryWriter sw = new BinaryWriter(File.Open("px4fw.bin", FileMode.Create)); foreach (byte by in fw.imagebyte) { sw.Write(by); // Console.Write("{0:x2}", by); } sw.Close(); // pad image to 4-byte length //while ((fw.imagebyte.Length % 4) != 0) { //fw.imagebyte. += b'\x00' return fw; }
public void currentChecksum(Firmware fw) { if (self.bl_rev < 3) { return; } __sync(); this.port.ReadTimeout = 1000; // 1 sec if (self.fw_maxsize > 0) { int expect_crc = fw.crc(self.fw_maxsize); __send(new byte[] { (byte)Code.GET_CRC, (byte)Code.EOC }); int report_crc = __recv_int(); self.__getSync(); print("FW File 0x" + hexlify(BitConverter.GetBytes(expect_crc)) + " " + expect_crc); print("Current 0x" + hexlify(BitConverter.GetBytes(report_crc)) + " " + report_crc); if (expect_crc == report_crc) { throw new Exception("Same Firmware. Not uploading"); } } if (self.extf_maxsize > 0) { int expect_crc = fw.extf_crc(fw.extf_image_size); byte[] size_bytes = BitConverter.GetBytes(fw.extf_image_size); __send(new byte[] { (byte)Code.EXTF_GET_CRC, size_bytes[0], size_bytes[1], size_bytes[2], size_bytes[3], (byte)Code.EOC }); // crc can be slow, give it 10s __wait_for_bytes(4, 30); int report_crc = __recv_int(); self.__getSync(); print("Ext FW File 0x" + hexlify(BitConverter.GetBytes(expect_crc)) + " " + expect_crc); print("Current 0x" + hexlify(BitConverter.GetBytes(report_crc)) + " " + report_crc); if (expect_crc == report_crc) { throw new Exception("Same Firmware. Not uploading"); } } }
public void __verify_extf(Firmware fw) { int expect_crc = fw.crc(self.extf_maxsize); __send(new byte[] { (byte)Code.EXTF_GET_CRC, (byte)Code.EOC }); int report_crc = __recv_int(); __getSync(); print("extf Expected 0x" + hexlify(BitConverter.GetBytes(expect_crc)) + " " + expect_crc); print("extf Got 0x" + hexlify(BitConverter.GetBytes(report_crc)) + " " + report_crc); if (report_crc != expect_crc) { throw new Exception("Program extf CRC failed"); } }
public void __verify_extf(Firmware fw) { int expect_crc = fw.extf_crc(fw.extf_image_size); byte[] size_bytes = BitConverter.GetBytes(fw.extf_image_size); __send(new byte[] { (byte)Code.EXTF_GET_CRC, size_bytes[0], size_bytes[1], size_bytes[2], size_bytes[3], (byte)Code.EOC }); DateTime deadline = DateTime.Now.AddSeconds(10); while (DateTime.Now < deadline) { port.BaseStream.Flush(); if (port.BytesToRead >= 4) { if (ProgressEvent != null) { ProgressEvent(100); } break; } if (ProgressEvent != null) { TimeSpan ts = new TimeSpan(deadline.Ticks - DateTime.Now.Ticks); ProgressEvent(((10.0 - ts.TotalSeconds) / 10.0) * 100.0); } Thread.Yield(); } __wait_for_bytes(4, 20); int report_crc = __recv_int(); __getSync(); print("extf Expected 0x" + hexlify(BitConverter.GetBytes(expect_crc)) + " " + expect_crc); print("extf Got 0x" + hexlify(BitConverter.GetBytes(report_crc)) + " " + report_crc); if (report_crc != expect_crc) { throw new Exception("Program extf CRC failed"); } }
public void __erase_extf(Firmware fw) { __sync(); // fix for bootloader bug - must see a sync and a get_device __getInfo(Info.BL_REV); byte[] size_bytes = BitConverter.GetBytes(fw.extf_image_size); __send(new byte[] { (byte)Code.EXTF_ERASE, size_bytes[0], size_bytes[1], size_bytes[2], size_bytes[3], (byte)Code.EOC }); __getSync(); int last_pct = 0; while (true) { if (last_pct < 95) { int pct = __recv()[0]; if (last_pct != pct) { last_pct = pct; if (ProgressEvent != null) { ProgressEvent(pct); } } } else if (__trySync()) { if (ProgressEvent != null) { ProgressEvent(100); } break; } } }
public void __program(Firmware fw) { byte[] code = fw.imagebyte; List <byte[]> groups = self.__split_len(code, PROG_MULTI_MAX); int a = 1; foreach (Byte[] bytes in groups) { self.__program_multi(bytes); System.Diagnostics.Debug.WriteLine("Program {0}/{1}", a, groups.Count); a++; if (ProgressEvent != null) { ProgressEvent((a / (float)groups.Count) * 100.0); } } }
public void upload(Firmware fw) { this.port.ReadTimeout = 1000; // 1 sec //Make sure we are doing the right thing if (self.board_type != fw.board_id) { throw new Exception("Firmware not suitable for this board"); } if (self.fw_maxsize < fw.image_size) { throw new Exception("Firmware image is too large for this board"); } if (!verifyotp()) { throw new Exception("Bad board OTP"); } print("erase..."); self.__erase(); print("program..."); self.__program(fw); print("verify..."); if (self.bl_rev == 2) { self.__verify_v2(fw); } else { self.__verify_v3(fw); } print("done, rebooting."); self.__reboot(); try { self.port.Close(); } catch { } }
public void __program(Firmware fw) { byte[] code = fw.imagebyte; List <byte[]> groups = self.__split_len(code, (byte)Code.PROG_MULTI_MAX); Console.WriteLine("Programing packet total: " + groups.Count); int a = 1; foreach (Byte[] bytes in groups) { self.__program_multi(bytes); Console.WriteLine("Program {0}/{1}", a, groups.Count); a++; if (ProgressEvent != null) { ProgressEvent((a / (float)groups.Count) * 100.0); } } }
public void upload(Firmware fw) { this.port.ReadTimeout = 1000; // 1 sec //Make sure we are doing the right thing if (self.board_type != fw.board_id) throw new Exception("Firmware not suitable for this board"); if (self.fw_maxsize < fw.image_size) throw new Exception("Firmware image is too large for this board"); print("erase..."); self.__erase(); print("program..."); self.__program(fw); print("verify..."); if (self.bl_rev == 2) self.__verify_v2(fw); else self.__verify_v3(fw); print("done, rebooting."); self.__reboot(); try { self.port.Close(); } catch { } }
public void currentChecksum(Firmware fw) { if (self.bl_rev < 3) return; this.port.ReadTimeout = 1000; // 1 sec int expect_crc = fw.crc(self.fw_maxsize); __send(new byte[] {(byte)Code.GET_CRC, (byte) Code.EOC}); int report_crc = __recv_int(); self.__getSync(); print("FW File 0x" + hexlify(BitConverter.GetBytes(expect_crc)) + " " + expect_crc); print("Current 0x" + hexlify(BitConverter.GetBytes(report_crc)) + " " + report_crc); if (expect_crc == report_crc) throw new Exception("Same Firmware. Not uploading"); }
public void __verify_v3(Firmware fw) { //Expected 0x33E22A51 //Got 0x8117524C //System.Exception: Program CRC failed //python //Expected 0x4c521781 //Got 0x4c521781 int expect_crc = fw.crc(self.fw_maxsize); __send(new byte[] {(byte)Code.GET_CRC, (byte) Code.EOC}); int report_crc = __recv_int(); __getSync(); print("Expected 0x" + hexlify(BitConverter.GetBytes(expect_crc)) + " " + expect_crc); print("Got 0x" + hexlify(BitConverter.GetBytes(report_crc)) + " " + report_crc); if (report_crc != expect_crc) { throw new Exception("Program CRC failed"); } }
public void __verify_v2(Firmware fw) { self.__send(new byte[] {(byte)Code.CHIP_VERIFY , (byte)Code.EOC}); self.__getSync(); byte[] code = fw.imagebyte; List<byte[]> groups = self.__split_len(code, READ_MULTI_MAX); int a = 1; foreach (byte[] bytes in groups) { if (!self.__verify_multi(bytes)) { throw new Exception("Verification failed"); } Console.WriteLine("Verify {0}/{1}", a, groups.Count); a++; if (ProgressEvent != null) ProgressEvent((a / (float)groups.Count) * 100.0); } }
public void __program(Firmware fw) { byte[] code = fw.imagebyte; List<byte[]> groups = self.__split_len(code, PROG_MULTI_MAX); Console.WriteLine("Programing packet total: "+groups.Count); int a = 1; foreach (Byte[] bytes in groups) { self.__program_multi(bytes); Console.WriteLine("Program {0}/{1}",a, groups.Count); a++; if (ProgressEvent != null) ProgressEvent((a / (float)groups.Count) * 100.0); } }
public void UploadPX4(string filename) { DateTime DEADLINE = DateTime.Now.AddSeconds(30); Uploader up; px4uploader.Firmware fw = px4uploader.Firmware.ProcessFirmware(filename); CustomMessageBox.Show("Press reset the board, and then press OK within 5 seconds.\nMission Planner will look for 30 seconds to find the board"); while (DateTime.Now < DEADLINE) { string port = MainV2.comPortName; Console.WriteLine(DateTime.Now.Millisecond + " Trying Port " + port); lbl_status.Text = "Connecting"; Application.DoEvents(); try { up = new Uploader(port, 115200); } catch (Exception ex) { //System.Threading.Thread.Sleep(50); Console.WriteLine(ex.Message); continue; } try { up.identify(); lbl_status.Text = "Identify"; Application.DoEvents(); Console.WriteLine("Found board type {0} boardrev {1} bl rev {2} fwmax {3} on {4}", up.board_type, up.board_rev, up.bl_rev, up.fw_maxsize, port); up.currentChecksum(fw); } catch (Exception) { Console.WriteLine("Not There.."); //Console.WriteLine(ex.Message); up.close(); continue; } try { up.ProgressEvent += new Uploader.ProgressEventHandler(up_ProgressEvent); up.LogEvent += new Uploader.LogEventHandler(up_LogEvent); progress.Value = 0; Application.DoEvents(); up.upload(fw); lbl_status.Text = "Done"; Application.DoEvents(); } catch (Exception ex) { lbl_status.Text = "ERROR: " + ex.Message; Application.DoEvents(); Console.WriteLine(ex.ToString()); } up.close(); break; } CustomMessageBox.Show("Please unplug, and plug back in your px4, before you try connecting"); }
public static Firmware ProcessFirmware(StreamReader f) { Firmware fw = JSON.Instance.ToObject <Firmware>(f.ReadToEnd()); f.Close(); if (fw.image_size > 0) { byte[] data = Convert.FromBase64String(fw.image); MemoryStream imagems = new MemoryStream(data, true); ZlibStream decompressionStream = new ZlibStream(imagems, CompressionMode.Decompress); int size = fw.image_size + (fw.image_size % 4); fw.imagebyte = new byte[size]; for (int a = 0; a < fw.imagebyte.Length; a++) { fw.imagebyte[a] = 0xff; } try { decompressionStream.Read(fw.imagebyte, 0, fw.image_size); } catch { Console.WriteLine("Possible bad file - usually safe to ignore"); } Console.WriteLine("image_size {0} size {1}", fw.image_size, size); // pad image to 4-byte length while ((fw.imagebyte.Length % 4) != 0) { Array.Resize(ref fw.imagebyte, fw.imagebyte.Length + (4 - (fw.imagebyte.Length % 4))); } } if (fw.extf_image_size > 0) { byte[] data = Convert.FromBase64String(fw.extf_image); MemoryStream imagems = new MemoryStream(data, true); ZlibStream decompressionStream = new ZlibStream(imagems, CompressionMode.Decompress); int size = fw.extf_image_size + (fw.extf_image_size % 4); fw.extf_imagebyte = new byte[size]; for (int a = 0; a < fw.extf_imagebyte.Length; a++) { fw.extf_imagebyte[a] = 0xff; } try { decompressionStream.Read(fw.extf_imagebyte, 0, fw.extf_image_size); } catch { Console.WriteLine("Possible bad file - usually safe to ignore"); } Console.WriteLine("extf_image_size {0} size {1}", fw.extf_image_size, size); // pad image to 4-byte length while ((fw.extf_imagebyte.Length % 4) != 0) { Array.Resize(ref fw.extf_imagebyte, fw.extf_imagebyte.Length + (4 - (fw.extf_imagebyte.Length % 4))); } } return(fw); }
/// <summary> /// upload to px4 standalone /// </summary> /// <param name="filename"></param> public bool UploadPX4(string filename) { Uploader up; updateProgress(0, "Reading Hex File"); px4uploader.Firmware fw = px4uploader.Firmware.ProcessFirmware(filename); try { // MainV2.comPort.BaseStream.Open(); // if (MainV2.comPort.BaseStream.IsOpen) { // MainV2.comPort.doReboot(true); // MainV2.comPort.Close(); } // else { CustomMessageBox.Show("Please unplug the board, and then press OK and plug back in.\nMission Planner will look for 30 seconds to find the board"); } } catch { // MainV2.comPort.Close(); } DateTime DEADLINE = DateTime.Now.AddSeconds(30); updateProgress(0, "Scanning comports"); while (DateTime.Now < DEADLINE) { string[] allports = SerialPort.GetPortNames(); foreach (string port in allports) { log.Info(DateTime.Now.Millisecond + " Trying Port " + port); updateProgress(-1, "Connecting"); try { up = new Uploader(port, 115200); } catch (Exception ex) { //System.Threading.Thread.Sleep(50); Console.WriteLine(ex.Message); continue; } try { up.identify(); updateProgress(-1, "Identify"); log.InfoFormat("Found board type {0} boardrev {1} bl rev {2} fwmax {3} on {4}", up.board_type, up.board_rev, up.bl_rev, up.fw_maxsize, port); } catch (Exception) { Console.WriteLine("Not There.."); //Console.WriteLine(ex.Message); up.close(); continue; } try { up.currentChecksum(fw); } catch { CustomMessageBox.Show("No need to upload. already on the board"); break; } try { up.ProgressEvent += new Uploader.ProgressEventHandler(up_ProgressEvent); up.LogEvent += new Uploader.LogEventHandler(up_LogEvent); updateProgress(0, "Upload"); up.upload(fw); updateProgress(100, "Upload Done"); } catch (Exception ex) { updateProgress(0, "ERROR: " + ex.Message); log.Info(ex); Console.WriteLine(ex.ToString()); } up.close(); CustomMessageBox.Show("Please unplug, and plug back in your px4, before you try connecting"); return(true); } } updateProgress(0, "ERROR: No Responce from board"); return(false); }
/// <summary> /// upload to px4 standalone /// </summary> /// <param name="filename"></param> public bool UploadPX4(string filename) { DateTime DEADLINE = DateTime.Now.AddSeconds(30); Uploader up; px4uploader.Firmware fw = px4uploader.Firmware.ProcessFirmware(filename); CustomMessageBox.Show("Press reset the board, and then press OK within 5 seconds.\nMission Planner will look for 30 seconds to find the board"); while (DateTime.Now < DEADLINE) { string[] allports = SerialPort.GetPortNames(); foreach (string port in allports) { Console.WriteLine(DateTime.Now.Millisecond + " Trying Port " + port); updateProgress(-1, "Connecting"); try { up = new Uploader(port, 115200); } catch (Exception ex) { //System.Threading.Thread.Sleep(50); Console.WriteLine(ex.Message); continue; } try { up.identify(); updateProgress(-1, "Identify"); Console.WriteLine("Found board type {0} boardrev {1} bl rev {2} fwmax {3} on {4}", up.board_type, up.board_rev, up.bl_rev, up.fw_maxsize, port); up.currentChecksum(fw); } catch (Exception) { Console.WriteLine("Not There.."); //Console.WriteLine(ex.Message); up.close(); continue; } try { up.ProgressEvent += new Uploader.ProgressEventHandler(up_ProgressEvent); up.LogEvent += new Uploader.LogEventHandler(up_LogEvent); updateProgress(0, "Upload"); up.upload(fw); updateProgress(100, "Upload Done"); } catch (Exception ex) { updateProgress(0, "ERROR: " + ex.Message); Console.WriteLine(ex.ToString()); } up.close(); CustomMessageBox.Show("Please unplug, and plug back in your px4, before you try connecting"); return(true); } } return(false); }
public static bool Uploader(string fn) { Firmware fw; Uploader up; fw = Firmware.ProcessFirmware(fn); Console.WriteLine("Loaded firmware for {0},{1} waiting for the bootloader...", fw.board_id, fw.board_revision); while (true) { string[] ports = GetPortNames(); //ports = new string[] { "COM9"}; foreach (string port in ports) { if (!port.StartsWith("COM") && !port.Contains("APM") && !port.Contains("ACM") && !port.Contains("usb")) { continue; } Console.WriteLine(DateTime.Now.Millisecond + " Trying Port " + port); try { up = new Uploader(port, 115200); } catch (Exception ex) { //System.Threading.Thread.Sleep(50); Console.WriteLine(DateTime.Now.Millisecond + " " + ex.Message); continue; } try { up.identify(); Console.WriteLine("Found board type {0} boardrev {1} bl rev {2} fwmax {3} on {4}", up.board_type, up.board_rev, up.bl_rev, up.fw_maxsize, port); } catch (Exception) { try { //System.Threading.ThreadPool.QueueUserWorkItem(up.__mavlinkreboot); } catch { //up.close(); } Console.WriteLine(DateTime.Now.Millisecond + " " + "Not There.."); //Console.WriteLine(ex.Message); try { up.close(); } catch { } continue; } try { up.currentChecksum(fw); } catch (Exception ex) { Console.WriteLine("No need to upload. already on the board" + ex.ToString()); up.close(); return(true); } try { up.upload(fw); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } up.close(); return(true); } } }