Example #1
0
        private void UploadFile(STDfuDevice device, string filepath, uint address)
        {
            ushort blockSize = device.BlockTransferSize;

            byte[] hexFileBytes = File.ReadAllBytes(filepath);

            // set our download address pointer
            if (device.SetAddressPointer(address) == false)
            {
                throw new Exception("Could not set base address for flash operation.");
            }

            // write blocks to the board and verify; we must have already erased our sectors before this point
            for (ushort index = 0; index <= (hexFileBytes.Length / blockSize); index++)
            {
                // write block to the board
                byte[] buffer = new byte[Math.Min(hexFileBytes.Length - (index * blockSize), blockSize)];
                Array.Copy(hexFileBytes, index * blockSize, buffer, 0, buffer.Length);
                bool success = device.WriteMemoryBlock(index, buffer);
                if (!success)
                {
                    Console.WriteLine("write failed");
                }
            }
        }
Example #2
0
        private async Task <bool> DfuFlash(string filepath, uint address)
        {
            FileInfo fi = new FileInfo(filepath);

            string display = $"file: {filepath}";

            if (filepath.StartsWith(Globals.FirmwareDownloadsFilePath))
            {
                var payload = File.ReadAllText(Path.Combine(Globals.FirmwareDownloadsFilePath, VersionCheckFile));
                display = $"downloaded version: {ExtractJsonValue(payload, "version")}";
            }

            var query = "SELECT * FROM Win32_USBHub";
            ManagementObjectSearcher device_searcher = new ManagementObjectSearcher(query);
            string deviceId = string.Empty;

            foreach (ManagementObject usb_device in device_searcher.Get())
            {
                if (usb_device.Properties["Name"].Value.ToString() == "STM Device in DFU Mode")
                {
                    deviceId = usb_device.Properties["DeviceID"].Value.ToString();
                    break;
                }
            }

            if (!string.IsNullOrEmpty(deviceId))
            {
                using (var device = new STDfuDevice($@"\\?\{deviceId.Replace("\\", "#")}#{{{DEVICE_INTERFACE_GUID_STDFU.ToString()}}}"))
                {
                    try
                    {
                        await OutputMessageAsync($"Upload {fi.Name} (~2 mins)");

                        await Task.Run(() =>
                        {
                            device.EraseAllSectors();
                            UploadFile(device, filepath, address);
                            device.LeaveDfuMode();
                        });

                        return(true);
                    }
                    catch (Exception ex)
                    {
                        await OutputMessageAsync($"An error occurred while flashing the device: {ex.Message}");
                    }
                    finally
                    {
                        device.Dispose();
                    }
                    return(false);
                }
            }
            else
            {
                if (_skipFlashToSelectDevice)
                {
                    return(true);
                }
                else
                {
                    await OutputMessageAsync("Device not found. Connect the device in bootloader mode by plugging in the device while holding down the BOOT button.");
                    await OutputMessageAsync("For more help, visit http://developer.wildernesslabs.co/Meadow/Meadow_Basics/Troubleshooting/VisualStudio/");
                }
            }

            return(false);
        }
        private async void Flash_Device(object sender, RoutedEventArgs e)
        {
            OutputMessage("Preparing to update device firmware...", true);

            List <FileInfo> files = new List <FileInfo>();

            if (Directory.Exists(Globals.FirmwareDownloadsFilePath))
            {
                DirectoryInfo di = new DirectoryInfo(Globals.FirmwareDownloadsFilePath);
                files = di.GetFiles().ToList();
            }

            if (!files.Any(x => x.Name == osFileName) || !files.Any(x => x.Name == latestJson))
            {
                OutputMessage("Download the latest firmware before flashing the device.");
                return;
            }

            var query = "SELECT * FROM Win32_USBHub";
            ManagementObjectSearcher device_searcher = new ManagementObjectSearcher(query);
            string deviceId = string.Empty;

            foreach (ManagementObject usb_device in device_searcher.Get())
            {
                if (usb_device.Properties["Name"].Value.ToString() == "STM Device in DFU Mode")
                {
                    deviceId = usb_device.Properties["DeviceID"].Value.ToString();
                }
            }

            var payload = File.ReadAllText(Path.Combine(Globals.FirmwareDownloadsFilePath, latestJson));

            if (!string.IsNullOrEmpty(deviceId))
            {
                OutputMessage($"Deploying firmare version {GetVersionFromPayload(payload)} to device.");

                var device = new STDfuDevice($@"\\?\{deviceId.Replace("\\", "#")}#{{{DEVICE_INTERFACE_GUID_STDFU.ToString()}}}");

                await task.Task.Run(async() =>
                {
                    await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
                    OutputMessage("Erasing sectors");

                    await TaskScheduler.Default;
                    device.EraseAllSectors();

                    OutputMessage($"Starting upload, this may take a couple minutes...");

                    await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
                    OutputMessage($"Uploading {osFileName}");

                    await TaskScheduler.Default;
                    UploadFile(device, Path.Combine(Globals.FirmwareDownloadsFilePath, osFileName), 0x08000000);

                    await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
                    OutputMessage($"Resetting device");

                    await TaskScheduler.Default;
                    device.LeaveDfuMode();
                    device.Dispose();

                    System.Threading.Thread.Sleep(2000);

                    await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
                    OutputMessage($"Complete");
                });
            }
            else
            {
                OutputMessage("Device not found. Connect the device in bootloader mode by plugging in the device while holding down the BOOT button.");
                OutputMessage("For more help, visit http://developer.wildernesslabs.co/Meadow/Meadow_Basics/Troubleshooting/VisualStudio/");
            }

            RefreshDeviceList();
        }