Example #1
0
        /// <summary>
        /// This method is used to write the provided bits into their corresponding sectors on the device.
        ///
        /// Due to chipset limitations, the control transfers cannot write the whole sector at once
        /// </summary>
        /// <returns>Total number of bytes written to the device</returns>
        private async Task <UInt32> WriteSectorAsync(FirmwareSector firmwareSector)
        {
            UInt32 totalBytesWritten = 0;

            if (!cancellationTokenSource.IsCancellationRequested)
            {
                // Convert the binary array into a buffer so we can write pieces of the firmware at a time
                var dataWriter = new DataWriter();
                dataWriter.WriteBytes(firmwareSector.BinaryArray);

                var bufferFirmwareForSector = dataWriter.DetachBuffer();

                // The data reader that will be used to read blocks of the firmware out of the firmware buffer
                var firmwareToWriteReader = DataReader.FromBuffer(bufferFirmwareForSector);

                // Setup packet that will be used for the control transfer
                var writeSectorSetupPacket = ControlTransferSetupPacketsFactory.CreateSetupPacket(ControlTransferSetupPacketsFactory.SetupPacketPurpose.WriteSector);

                var addressToWriteTo = firmwareSector.Sector;

                // Sequentially write the sector in blocks because of chipset limitations
                while (totalBytesWritten < firmwareSector.BinaryArray.Length)
                {
                    if (!cancellationTokenSource.IsCancellationRequested)
                    {
                        IBuffer bufferToWrite = null;

                        // Don't read more data than the buffer has
                        if (firmwareToWriteReader.UnconsumedBufferLength < Firmware.MaxBytesToWritePerControlTransfer)
                        {
                            bufferToWrite = firmwareToWriteReader.ReadBuffer(firmwareToWriteReader.UnconsumedBufferLength);
                        }
                        else
                        {
                            bufferToWrite = firmwareToWriteReader.ReadBuffer(Firmware.MaxBytesToWritePerControlTransfer);
                        }

                        // The follow properties are device specific
                        writeSectorSetupPacket.Value = addressToWriteTo >> 16;
                        writeSectorSetupPacket.Index = addressToWriteTo & 0xFFFF;

                        // Amount of data to be written to the device
                        writeSectorSetupPacket.Length = bufferToWrite.Length;

                        var bytesWritten = await device.SendControlOutTransferAsync(writeSectorSetupPacket, bufferToWrite).AsTask(cancellationTokenSource.Token);

                        totalBytesWritten += bytesWritten;

                        addressToWriteTo += bytesWritten;

                        // Give the device the opportunity to write the data packet to the EEPROM
                        await Task.Delay(100);
                    }
                    else
                    {
                        break;
                    }
                }
            }

            return(totalBytesWritten);
        }
Example #2
0
        /// <summary>
        /// This method is used to write the provided bits into their corresponding sectors on the device.
        /// 
        /// Due to chipset limitations, the control transfers cannot write the whole sector at once
        /// </summary>
        /// <returns>Total number of bytes written to the device</returns>
        private async Task<UInt32> WriteSectorAsync(FirmwareSector firmwareSector)
        {
            UInt32 totalBytesWritten = 0;
            
            if (!cancellationTokenSource.IsCancellationRequested)
            {
                // Convert the binary array into a buffer so we can write pieces of the firmware at a time
                var dataWriter = new DataWriter();
                dataWriter.WriteBytes(firmwareSector.BinaryArray);

                var bufferFirmwareForSector = dataWriter.DetachBuffer();

                // The data reader that will be used to read blocks of the firmware out of the firmware buffer
                var firmwareToWriteReader = DataReader.FromBuffer(bufferFirmwareForSector);

                // Setup packet that will be used for the control transfer
                var writeSectorSetupPacket = ControlTransferSetupPacketsFactory.CreateSetupPacket(ControlTransferSetupPacketsFactory.SetupPacketPurpose.WriteSector);

                var addressToWriteTo = firmwareSector.Sector;

                // Sequentially write the sector in blocks because of chipset limitations
                while (totalBytesWritten < firmwareSector.BinaryArray.Length)
                {
                    if (!cancellationTokenSource.IsCancellationRequested)
                    {
                        IBuffer bufferToWrite = null;

                        // Don't read more data than the buffer has
                        if (firmwareToWriteReader.UnconsumedBufferLength < Firmware.MaxBytesToWritePerControlTransfer)
                        {
                            bufferToWrite = firmwareToWriteReader.ReadBuffer(firmwareToWriteReader.UnconsumedBufferLength);
                        }
                        else
                        {
                            bufferToWrite = firmwareToWriteReader.ReadBuffer(Firmware.MaxBytesToWritePerControlTransfer);
                        }

                        // The follow properties are device specific
                        writeSectorSetupPacket.Value = addressToWriteTo >> 16;
                        writeSectorSetupPacket.Index = addressToWriteTo & 0xFFFF;

                        // Amount of data to be written to the device
                        writeSectorSetupPacket.Length = bufferToWrite.Length;

                        var bytesWritten = await device.SendControlOutTransferAsync(writeSectorSetupPacket, bufferToWrite).AsTask(cancellationTokenSource.Token);

                        totalBytesWritten += bytesWritten;

                        addressToWriteTo += bytesWritten;

                        // Give the device the opportunity to write the data packet to the EEPROM
                        await Task.Delay(100);
                    }
                    else
                    {
                        break;
                    }
                }
            }

            return totalBytesWritten;
        }