Example #1
0
        protected void Write(IFTDI device, byte[] data)
        {
            ValidateDeviceParameter(device);
            var status = device.Write(data, data.Length, out var writtenBytes);

            if (status != FT_STATUS.FT_OK)
            {
                throw new FTDIWriteException();
            }
        }
Example #2
0
        private void DoFirmwareUpdate()
        {
            bool success = false;

            try
            {
                const int chunkSize       = 4 * 1024;
                const int flashSectorSize = 64 * 1024;

                int origSize   = _firmwareData.Length;
                int extraSize  = flashSectorSize - _firmwareData.Length % flashSectorSize;
                int paddedSize = origSize + extraSize;

                Array.Resize(ref _firmwareData, paddedSize);

                int  dataIndex   = 0;
                bool haveFailure = false;
                while (dataIndex < _firmwareData.Length)
                {
                    int    numBytesInChunk = 0;
                    UInt32 chunkChecksum   = 0;
                    while (((dataIndex + numBytesInChunk) < _firmwareData.Length) && (numBytesInChunk < chunkSize))
                    {
                        chunkChecksum += _firmwareData[dataIndex + numBytesInChunk];
                        numBytesInChunk++;
                    }

                    // Request to send the chunk
                    String cmd = String.Format("F {0:x} {1:x} {2:x}", dataIndex, numBytesInChunk, chunkChecksum);
                    String answer;
                    int    numRetries = 3;
                    while (numRetries > 0)
                    {
                        WriteLine(cmd);
                        answer = SendCmdGetResponse(cmd);
                        if (!answer.StartsWith("Y"))
                        {
                            numRetries--;
                            WriteLine("Retrying");
                            Thread.Sleep(500);
                        }
                        else
                        {
                            break;
                        }
                    }

                    if (0 == numRetries)
                    {
                        haveFailure = true;
                        break;
                    }

                    // We can now send the chunk
                    _uart.DiscardInBuffer();
                    _uart.DiscardOutBuffer();
                    byte[] chunk = new byte[chunkSize];
                    Buffer.BlockCopy(_firmwareData, dataIndex, chunk, 0, numBytesInChunk);
                    _uart.Write(chunk);
                    dataIndex += numBytesInChunk;

                    // Verify the response
                    answer = _uart.ReadLineTimeout(30000);
                    if (!answer.StartsWith("Y"))
                    {
                        haveFailure = true;
                        break;
                    }

                    Thread.Sleep(500);
                }

                if (false == haveFailure)
                {
                    success = true;
                }
            }
            catch
            {
                success = false;
            }

            WriteLine(success ? "Firmware update complete. Restart device!" : "Firmware update failed!");
        }