public override bool ProgramFirmware(string FilePath, Action <double> Progress) { try { uploader.IHex Hex = new uploader.IHex(); Hex.load(FilePath); if (SearchHex(Hex, GetFirmwareSearchTokens())) { uploader.Uploader UL = new uploader.Uploader(); UL.ProgressEvent += (d) => Progress(d); UL.upload(_Session.Port, Hex); return(true); } else { ShowWrongFirmwareMessageBox(); return(false); } } catch { return(false); } }
private void upload_and_verify(IHex image_data) { if (image_data.bankingDetected && ((byte)id & 0x80) != 0x80) { log("This Firmware requires banking support"); throw new Exception("This Firmware requires banking support"); } if (((byte)id & 0x80) == 0x80) { banking = true; log("Using 24bit addresses"); } // erase the program area first log("erasing program flash\n"); cmdErase(); // progress fractions bytes_to_process = 0; foreach (var bytes in image_data.Values) { bytes_to_process += bytes.Length; } bytes_to_process *= 2; // once to program, once to verify bytes_processed = 0; // program the flash blocks log("programming\n"); foreach (var kvp in image_data) { // move the program pointer to the base of this block cmdSetAddress(kvp.Key); log(string.Format("prog 0x{0:X}/{1}\n", kvp.Key, kvp.Value.Length), 1); upload_block_multi(kvp.Value); } // and read them back to verify that they were programmed log("verifying\n"); foreach (var kvp in image_data) { // move the program pointer to the base of this block cmdSetAddress(kvp.Key); log(string.Format("verf 0x{0:X}/{1}\n", kvp.Key, kvp.Value.Length), 1); verify_block_multi(kvp.Value); bytes_processed += kvp.Value.GetLength(0); progress((double)bytes_processed / bytes_to_process); } log("Success\n"); }
/// <summary> /// Upload the specified image_data. /// </summary> /// <param name='image_data'> /// Image_data to be uploaded. /// </param> public void upload(SerialPort on_port, IHex image_data) { progress(0); port = on_port; try { connect_and_sync(); upload_and_verify(image_data); cmdReboot(); } catch (Exception e) { if (port.IsOpen) { port.Close(); } throw e; } }
/// <summary> /// Upload the specified image_data. /// </summary> /// <param name='image_data'> /// Image_data to be uploaded. /// </param> public void upload(ICommsSerial on_port, IHex image_data, bool use_mavlink = false) { progress(0); port = on_port; try { connect_and_sync(); upload_and_verify(image_data); cmdReboot(); } catch { if (port.IsOpen) { port.Close(); } throw; } }
public AppLogic() { // Get the current configuration file. config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); // Look for our settings and add/create them if missing if (config.Sections [config_name] == null) { config_section = new ConfigSection(); config.Sections.Add(config_name, config_section); config_section.SectionInformation.ForceSave = true; config.Save(ConfigurationSaveMode.Full); } config_section = config.GetSection(config_name) as ConfigSection; // Hook up main window events win = new MainWindow(); win.MonitorEvent += show_monitor; win.UploadEvent += do_upload; win.LogEvent += log; win.QuitEvent += at_exit; // restore the last path that we uploaded win.FileName = config_section.lastPath; // restore the last port that we opened win.PortName = config_section.lastPort; // Create the intelhex loader ihex = new IHex(); ihex.LogEvent += log; // And the uploader upl = new Uploader(); upl.LogEvent += log; upl.ProgressEvent += win.set_progress; // Emit some basic help log("Select a serial port and a .hex file to be uploaded, then hit Upload.\n"); win.Show(); }
/// <summary> /// Search the given hex file for the given tokens. /// </summary> /// <param name="File">The file to search. Must not be null.</param> /// <param name="Tokens">The tokens to search for. Must not be null.</param> /// <returns>true if at least one found, false if not found.</returns> protected static bool SearchHex(uploader.IHex File, string[] Tokens) { foreach (var Token in Tokens) { int TokenIndex = 0; byte[] BinToken = System.Text.ASCIIEncoding.ASCII.GetBytes(Token); foreach (var Part in File.Values) { foreach (byte b in Part) { if (SearchTokenUpdate(BinToken, ref TokenIndex, b)) { return(true); } } } } return(false); }
private void upload_and_verify(IHex image_data) { // erase the program area first log("erasing program flash\n"); cmdErase(); // progress fractions bytes_to_process = 0; foreach (byte[] bytes in image_data.Values) { bytes_to_process += bytes.Length; } bytes_to_process *= 2; // once to program, once to verify bytes_processed = 0; // program the flash blocks log("programming\n"); foreach (KeyValuePair <UInt32, byte[]> kvp in image_data) { // move the program pointer to the base of this block cmdSetAddress(kvp.Key); log(string.Format("prog 0x{0:X}/{1}\n", kvp.Key, kvp.Value.Length), 1); upload_block_multi(kvp.Value); } // and read them back to verify that they were programmed log("verifying\n"); foreach (KeyValuePair <UInt32, byte[]> kvp in image_data) { // move the program pointer to the base of this block cmdSetAddress(kvp.Key); log(string.Format("verf 0x{0:X}/{1}\n", kvp.Key, kvp.Value.Length), 1); verify_block_multi(kvp.Value); bytes_processed += kvp.Value.GetLength(0); progress((double)bytes_processed / bytes_to_process); } log("Success\n"); }
private void UploadFW() { SerialPort comPort = new SerialPort(); var uploader = new Uploader(); try { comPort.PortName = PortName; comPort.BaudRate = 115200; comPort.Open(); } catch { log("Invalid ComPort or in use"); return; } uploader.ProgressEvent += progress; uploader.LogEvent += log; // prep what we are going to upload var iHex = new IHex(); var bootloadermode = false; // attempt bootloader mode try { comPort.BaudRate = 115200; log("Trying Bootloader Mode"); uploader.port = comPort; uploader.connect_and_sync(); log("In Bootloader Mode"); bootloadermode = true; } catch (Exception ex1) { log(ex1.Message); // cleanup bootloader mode fail, and try firmware mode comPort.Close(); if (comPort.IsOpen) { // default baud... guess comPort.BaudRate = 57600; } else { comPort.BaudRate = BaudRate; } try { comPort.Open(); } catch { log("Error opening port"); return; } log("Trying Firmware Mode"); bootloadermode = false; } // check for either already bootloadermode, or if we can do a ATI to ID the firmware if (bootloadermode || doConnect(comPort)) { // put into bootloader mode/update mode if (!bootloadermode) { try { comPort.Write("AT&UPDATE\r\n"); var left = comPort.ReadExisting(); log(left); Sleep(700); comPort.BaudRate = 115200; } catch { } comPort.BaudRate = 115200; } try { // force sync after changing baudrate uploader.connect_and_sync(); } catch { log("Failed to sync with Radio"); goto exit; } var device = Uploader.Board.FAILED; var freq = Uploader.Frequency.FAILED; // get the device type and frequency in the bootloader uploader.getDevice(ref device, ref freq); // load the hex try { iHex.load(FileName); } catch { log("Bad Firmware File"); goto exit; } // upload the hex and verify try { uploader.upload(comPort, iHex); } catch (Exception ex) { log("Upload Failed " + ex.Message); } } else { log("Failed to identify Radio"); } exit: if (comPort.IsOpen) { comPort.Close(); } }