示例#1
0
        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
        }
示例#2
0
        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
        }
示例#3
0
        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);
            }
        }
示例#4
0
        public static byte[] GetEmptyFlashImage(PJRC_Board board)
        {
            int FlashSize = BoardDefinitions[board].FlashSize;

            return(Enumerable.Repeat((byte)0xFF, FlashSize).ToArray());
        }
示例#5
0
 public static byte[] GetEmptyFlashImage(PJRC_Board board)
 {
     int FlashSize = BoardDefinitions[board].FlashSize;
     return Enumerable.Repeat((byte)0xFF, FlashSize).ToArray();
 }