public static int Upload(byte[] Image, PJRC_Board board, uint Serialnumber, bool reboot = true) { // Obtain a HalfKayed board with the required serialnumber HidDevice device = null; var Timeout = DateTime.Now + TimeSpan.FromSeconds(3); while (DateTime.Now < Timeout) //Try for some time in case HalfKay is just booting up { var devices = HidDevices.Enumerate(0x16C0, 0x0478); // Get all boards with running HalfKay device = devices.FirstOrDefault(x => GetSerialNumber(x) == Serialnumber); // check if the correct one is online if (device != null) { break; // found it } Thread.Sleep(500); } if (device == null) { return(1); // Didn't find a HalfKayed board with requested serialnumber } var BoardDef = BoardDefinitions[board]; int addr = 0; //Slice the flash image in dataBlocks and transfer the blocks if they are not empty (!=0xFF) foreach (var dataBlock in Image.Batch(BoardDef.BlockSize)) { if (dataBlock.Any(d => d != 0xFF) || addr == 0) //skip empty blocks but always write first block to erase chip { var report = PrepareReport(addr, dataBlock.ToArray(), BoardDef); if (!device.WriteReport(report)) //if write fails (happens if teensy still busy) wait and retry once { Thread.Sleep(10); if (!device.WriteReport(report)) { return(2); } } Thread.Sleep(addr == 0 ? 100 : 1); // First block needs more time since it erases the complete chip } addr += BoardDef.BlockSize; } // Reboot the device to start the downloaded firmware if (reboot) { var rebootReport = device.CreateReport(); rebootReport.Data[0] = 0xFF; rebootReport.Data[1] = 0xFF; rebootReport.Data[2] = 0xFF; device.WriteReport(rebootReport); } return(0); // all good }
public static int Upload(byte[] Image, PJRC_Board board, uint Serialnumber, bool reboot = true) { // Obtain a HalfKayed board with the required serialnumber HidDevice device = null; var Timeout = DateTime.Now + TimeSpan.FromSeconds(3); while (DateTime.Now < Timeout ) //Try for some time in case HalfKay is just booting up { var devices = HidDevices.Enumerate(0x16C0, 0x0478); // Get all boards with running HalfKay device = devices.FirstOrDefault(x => GetSerialNumber(x) == Serialnumber); // check if the correct one is online if (device != null) break; // found it Thread.Sleep(500); } if (device == null) return 1; // Didn't find a HalfKayed board with requested serialnumber var BoardDef = BoardDefinitions[board]; int addr = 0; //Slice the flash image in dataBlocks and transfer the blocks if they are not empty (!=0xFF) foreach (var dataBlock in Image.Batch(BoardDef.BlockSize)) { if (dataBlock.Any(d => d != 0xFF) || addr == 0) //skip empty blocks but always write first block to erase chip { var report = PrepareReport(addr, dataBlock.ToArray(), BoardDef); if (!device.WriteReport(report)) //if write fails (happens if teensy still busy) wait and retry once { Thread.Sleep(10); if (!device.WriteReport(report)) return 2; } Thread.Sleep(addr == 0 ? 100 : 1); // First block needs more time since it erases the complete chip } addr += BoardDef.BlockSize; } // Reboot the device to start the downloaded firmware if (reboot) { var rebootReport = device.CreateReport(); rebootReport.Data[0] = 0xFF; rebootReport.Data[1] = 0xFF; rebootReport.Data[2] = 0xFF; device.WriteReport(rebootReport); } return 0; // all good }
public bool connect() { var watcher = new TeensyWatcher(); teensy = watcher.ConnectedDevices .FirstOrDefault(); if (teensy != null && !String.IsNullOrEmpty(teensy.Port)) { try { port = new SerialPort(teensy.Port); port.WriteBufferSize = 1024 * 30; // port.WriteTimeout = 1000; port.Open(); //port.Close(); } catch { port?.Dispose(); port = null; TeensyID = $"Can't open port {teensy.BoardId}"; return(false); } TeensyID = $"{teensy.BoardId} connected"; serialnumber = teensy.Serialnumber; board = teensy.BoardType; isConnected = true; return(true); } else { TeensyID = $"No compatible Teensy found"; return(false); } }
public static byte[] GetEmptyFlashImage(PJRC_Board board) { int FlashSize = BoardDefinitions[board].FlashSize; return(Enumerable.Repeat((byte)0xFF, FlashSize).ToArray()); }
public static byte[] GetEmptyFlashImage(PJRC_Board board) { int FlashSize = BoardDefinitions[board].FlashSize; return Enumerable.Repeat((byte)0xFF, FlashSize).ToArray(); }