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(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; } }
private void UploadFW(bool custom = false) { ICommsSerial comPort = new SerialPort(); var uploader = new Uploader(); if (MainV2.comPort.BaseStream.IsOpen) { try { getTelemPortWithRadio(ref comPort); uploader.PROG_MULTI_MAX = 64; } catch (Exception ex) { MsgBox.CustomMessageBox.Show("Error " + ex); } } try { comPort.PortName = MainV2.comPortName; comPort.BaudRate = 115200; comPort.Open(); } catch { MsgBox.CustomMessageBox.Show("Invalid ComPort or in use"); return; } // prep what we are going to upload var iHex = new IHex(); iHex.LogEvent += iHex_LogEvent; iHex.ProgressEvent += iHex_ProgressEvent; var bootloadermode = false; // attempt bootloader mode try { if (upload_xmodem(comPort)) { comPort.Close(); return; } comPort.BaudRate = 115200; uploader_ProgressEvent(0); uploader_LogEvent("Trying Bootloader Mode"); uploader.port = comPort; uploader.connect_and_sync(); uploader.ProgressEvent += uploader_ProgressEvent; uploader.LogEvent += uploader_LogEvent; uploader_LogEvent("In Bootloader Mode"); bootloadermode = true; } catch (Exception ex1) { log.Error(ex1); // cleanup bootloader mode fail, and try firmware mode comPort.Close(); if (MainV2.comPort.BaseStream.IsOpen) { // default baud... guess comPort.BaudRate = 57600; } else { comPort.BaudRate = MainV2.comPort.BaseStream.BaudRate; } try { comPort.Open(); } catch { MsgBox.CustomMessageBox.Show("Error opening port", "Error"); return; } uploader.ProgressEvent += uploader_ProgressEvent; uploader.LogEvent += uploader_LogEvent; uploader_LogEvent("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.Info(left); Sleep(700); comPort.BaudRate = 115200; } catch { } if (upload_xmodem(comPort)) { comPort.Close(); return; } comPort.BaudRate = 115200; } try { // force sync after changing baudrate uploader.connect_and_sync(); } catch { MsgBox.CustomMessageBox.Show("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); // get firmware for this device if (getFirmware(device, custom)) { // load the hex try { iHex.load(firmwarefile); } catch { MsgBox.CustomMessageBox.Show("Bad Firmware File"); goto exit; } // upload the hex and verify try { uploader.upload(comPort, iHex); } catch (Exception ex) { MsgBox.CustomMessageBox.Show("Upload Failed " + ex.Message); } } else { MsgBox.CustomMessageBox.Show("Failed to download new firmware"); } } else { MsgBox.CustomMessageBox.Show("Failed to identify Radio"); } exit: if (comPort.IsOpen) { comPort.Close(); } }