// returns true if successful public bool SetAddressPointer(uint address) { DFU_STATUS dfuStatus = new DFU_STATUS(); STDFU_Getstatus(ref _handle, ref dfuStatus); if (dfuStatus.bState != STATE_DFU_IDLE && dfuStatus.bState != STATE_DFU_DOWNLOAD_IDLE) { // reset our dfu state to idle, preparing for the current operation. if (ResetDfuStateToIdle() == false) { return(false); // timeout resetting our DFU state; we will not be able to execute this operation } } // call STDFU_Dnload with the SetAddressPointer command (0x21) and address; this will set up the SetAddressPointer operation. STDFU_Dnload(ref _handle, new byte[] { 0x21, (byte)(address & 0xFF), (byte)((address >> 08) & 0xFF), (byte)((address >> 16) & 0xFF), (byte)((address >> 24) & 0xFF) }, 5, 0); // call STDFU_Getstatus to begin the operation STDFU_Getstatus(ref _handle, ref dfuStatus); // DFU state should now be "download busy" if (dfuStatus.bState != STATE_DFU_DOWNLOAD_BUSY) { // operation was unable to begin. ResetDfuStateToIdle(); return(false); } // call STDFU_Getstatus again to verify that the operation completed successfully STDFU_Getstatus(ref _handle, ref dfuStatus); // DFU state should now be "download idle" if (dfuStatus.bState != STATE_DFU_DOWNLOAD_IDLE || dfuStatus.bStatus != STATUS_OK) { // operation failed. ResetDfuStateToIdle(); return(false); } // address pointer has been successfully set; return true. return(true); }
// returns true if successful public bool EraseSector(uint sectorBaseAddress) { DFU_STATUS dfuStatus = new DFU_STATUS(); STDFU_Getstatus(ref _handle, ref dfuStatus); if (dfuStatus.bState != STATE_DFU_IDLE && dfuStatus.bState != STATE_DFU_DOWNLOAD_IDLE) { // reset our dfu state to idle, preparing for the current operation. if (ResetDfuStateToIdle() == false) { return(false); // timeout resetting our DFU state; we will not be able to execute this operation } } // call STDFU_Dnload with the Erase command (0x41) and sector base address; this will set up the erase operation. STDFU_Dnload(ref _handle, new byte[] { 0x41, (byte)(sectorBaseAddress & 0xFF), (byte)((sectorBaseAddress >> 08) & 0xFF), (byte)((sectorBaseAddress >> 16) & 0xFF), (byte)((sectorBaseAddress >> 24) & 0xFF) }, 5, 0); // call STDFU_Getstatus to begin the erase STDFU_Getstatus(ref _handle, ref dfuStatus); // DFU state should now be "download busy" if (dfuStatus.bState != STATE_DFU_DOWNLOAD_BUSY) { // erase operation was unable to begin. ResetDfuStateToIdle(); return(false); } // call STDFU_Getstatus again to complete the erase STDFU_Getstatus(ref _handle, ref dfuStatus); // DFU state should now be "download idle" if (dfuStatus.bState != STATE_DFU_DOWNLOAD_IDLE || dfuStatus.bStatus != STATUS_OK) { // erase operation failed. ResetDfuStateToIdle(); return(false); } // sector has been succesfully erased; return true. return(true); }
// returns true if successful public bool ReadMemoryBlock(ushort blockNumber, byte[] buffer) { if (!_isUploadCapable) { throw new NotSupportedException(); } DFU_STATUS dfuStatus = new DFU_STATUS(); STDFU_Getstatus(ref _handle, ref dfuStatus); if (dfuStatus.bState != STATE_DFU_IDLE && dfuStatus.bState != STATE_DFU_UPLOAD_IDLE) { // reset our dfu state to idle, preparing for the current operation. if (ResetDfuStateToIdle() == false) { return(false); // timeout resetting our DFU state; we will not be able to execute this operation } } // call STDFU_Upload with the blockNumber+2; this will set up the operation. STDFU_Upload(ref _handle, buffer, (uint)buffer.Length, (ushort)(blockNumber + 2)); // call STDFU_Getstatus to begin the operation STDFU_Getstatus(ref _handle, ref dfuStatus); // DFU state should now be "upload busy" while (dfuStatus.bState == STATE_DFU_UPLOAD_BUSY) { STDFU_Clrstatus(ref _handle); STDFU_Getstatus(ref _handle, ref dfuStatus); } // DFU state should now be "upload idle" if (dfuStatus.bState != STATE_DFU_UPLOAD_IDLE || dfuStatus.bStatus != STATUS_OK) { // operation failed. ResetDfuStateToIdle(); return(false); } // operation succeeded; return true. return(true); }
// returns true if successful public bool ReadUnprotect() { DFU_STATUS dfuStatus = new DFU_STATUS(); STDFU_Getstatus(ref _handle, ref dfuStatus); if (dfuStatus.bState != STATE_DFU_IDLE && dfuStatus.bState != STATE_DFU_DOWNLOAD_IDLE) { // reset our dfu state to idle, preparing for the current operation. if (ResetDfuStateToIdle() == false) { return(false); // timeout resetting our DFU state; we will not be able to execute this operation } } // call STDFU_Dnload with the Read Unprotect command (0x92) only; this will set up the operation. STDFU_Dnload(ref _handle, new byte[] { 0x92 }, 1, 0); // call STDFU_Getstatus to begin the operation STDFU_Getstatus(ref _handle, ref dfuStatus); // DFU state should now be "download busy" if (dfuStatus.bState != STATE_DFU_DOWNLOAD_BUSY) { // operation was unable to begin. ResetDfuStateToIdle(); return(false); } // call STDFU_Getstatus again to verify that the operation did not error out. State will be IDLE or, if the device has already detached, DFU_DOWNLOAD_BUSY STDFU_Getstatus(ref _handle, ref dfuStatus); if (dfuStatus.bState != STATE_IDLE && dfuStatus.bState != STATE_DFU_DOWNLOAD_BUSY) { // operation failed. ResetDfuStateToIdle(); return(false); } // the operation has successfully completed; return true. return(true); }
// returns true if successful public bool WriteMemoryBlock(ushort blockNumber, byte[] buffer) { DFU_STATUS dfuStatus = new DFU_STATUS(); STDFU_Getstatus(ref _handle, ref dfuStatus); if (dfuStatus.bState != STATE_DFU_IDLE && dfuStatus.bState != STATE_DFU_DOWNLOAD_IDLE) { // reset our dfu state to idle, preparing for the current operation. if (ResetDfuStateToIdle() == false) { return(false); // timeout resetting our DFU state; we will not be able to execute this operation } } // call STDFU_Dnload with the blockNumber+2; this will set up the operation. STDFU_Dnload(ref _handle, buffer, (uint)buffer.Length, (ushort)(blockNumber + 2)); // call STDFU_Getstatus to begin the operation STDFU_Getstatus(ref _handle, ref dfuStatus); // DFU state should now be "download busy" if (dfuStatus.bState != STATE_DFU_DOWNLOAD_BUSY) { // operation was unable to begin. ResetDfuStateToIdle(); return(false); } // call STDFU_Getstatus again to verify that the operation completed successfully STDFU_Getstatus(ref _handle, ref dfuStatus); // DFU state should now be "download idle" if (dfuStatus.bState != STATE_DFU_DOWNLOAD_IDLE || dfuStatus.bStatus != STATUS_OK) { // operation failed. ResetDfuStateToIdle(); return(false); } // operation succeeded; return true. return(true); }
// returns true if successful private bool GetDownloadCommands(out byte[] commands) { commands = new byte[4]; DFU_STATUS dfuStatus = new DFU_STATUS(); STDFU_Getstatus(ref _handle, ref dfuStatus); if (dfuStatus.bState != STATE_DFU_IDLE && dfuStatus.bState != STATE_DFU_UPLOAD_IDLE) { // reset our dfu state to idle, preparing for the current operation. if (ResetDfuStateToIdle() == false) { return(false); // timeout resetting our DFU state; we will not be able to execute this operation } } // call STDFU_Upload with nBlock=0; this will set up the operation. STDFU_Upload(ref _handle, commands, (uint)commands.Length, 0); // call STDFU_Getstatus to begin the operation STDFU_Getstatus(ref _handle, ref dfuStatus); // DFU state should now be "upload busy" while (dfuStatus.bState == STATE_DFU_UPLOAD_BUSY) { STDFU_Clrstatus(ref _handle); STDFU_Getstatus(ref _handle, ref dfuStatus); } // DFU state should now be "upload idle" if (dfuStatus.bState != STATE_DFU_IDLE) { // operation failed. ResetDfuStateToIdle(); return(false); } // operation succeeded; return true. return(true); }
static extern uint STDFU_Getstatus( ref IntPtr phDevice, ref DFU_STATUS DfuStatus );