예제 #1
0
파일: dfu.cs 프로젝트: glocklueng/NXP_DFU
    public bool make_idle(bool initial_abort)
    {
      int retries = 4;
      DFU_Status status;
      IUsbDevice thisDev = myUSBDevice as IUsbDevice;
      
      if (initial_abort)
        {
          abort();
        }

      while (retries > 0)
        {
          if (!get_status(out status))
            {
              clear_status();
              continue;
            }
          switch (status.State)
            {
            case DFUStateVals.STATE_DFU_IDLE:
              if( DFUStatusVals.DFU_STATUS_OK == status.Status ) {
                return(true);
              }

              /* We need the device to have the DFU_STATUS_OK status. */
              clear_status();
              break;

            case DFUStateVals.STATE_DFU_DOWNLOAD_SYNC:   /* abort -> idle */
            case DFUStateVals.STATE_DFU_DOWNLOAD_IDLE:   /* abort -> idle */
            case DFUStateVals.STATE_DFU_MANIFEST_SYNC:   /* abort -> idle */
            case DFUStateVals.STATE_DFU_UPLOAD_IDLE:     /* abort -> idle */
            case DFUStateVals.STATE_DFU_DOWNLOAD_BUSY:   /* abort -> error */
            case DFUStateVals.STATE_DFU_MANIFEST:        /* abort -> error */
              abort();
              break;

            case DFUStateVals.STATE_DFU_ERROR:
              clear_status();
              break;

            case DFUStateVals.STATE_APP_IDLE:
              detach( DFU_DETACH_TIMEOUT );
              break;

            case DFUStateVals.STATE_APP_DETACH:
            case DFUStateVals.STATE_DFU_MANIFEST_WAIT_RESET:
              //DEBUG( "Resetting the device\n" );
              thisDev.ResetDevice();
              return(false);
            }
          retries--;
        }
      return(false);
    }
예제 #2
0
    void Reset()
    {
        if (UsbDev == null)
        {
            /* USB device not initilized, nothing to reset */
            return;
        }

        Log.WriteLine("Resetting M4ATX device");
        UsbDev.ResetDevice();
        UsbDev = null;
    }
예제 #3
0
 public void ResetDevice()
 {
     _device.ResetDevice();
 }
예제 #4
0
        public static byte[] WriteAndRead2(int vid, int pid, byte[] byteArrayWritten)
        {
            ErrorCode       ec          = ErrorCode.None;
            UsbDevice       MyUsbDevice = null;
            UsbDeviceFinder MyUsbFinder = new UsbDeviceFinder(vid, pid);

            try
            {
                // Find and open the usb device.
                MyUsbDevice = UsbDevice.OpenUsbDevice(MyUsbFinder);

                // If the device is open and ready
                if (MyUsbDevice == null)
                {
                    throw new Exception("Device Not Found. vid=" + vid + ", pid=" + pid);
                }

                // If this is a "whole" usb device (libusb-win32, linux libusb)
                // it will have an IUsbDevice interface. If not (WinUSB) the
                // variable will be null indicating this is an interface of a
                // device.
                IUsbDevice wholeUsbDevice = MyUsbDevice as IUsbDevice;
                if (!ReferenceEquals(wholeUsbDevice, null))
                {
                    // This is a "whole" USB device. Before it can be used,
                    // the desired configuration and interface must be selected.

                    // Select config #1
                    wholeUsbDevice.SetConfiguration(1);

                    // Claim interface #0.
                    wholeUsbDevice.ClaimInterface(0);
                }

                // open read endpoint 1.
                UsbEndpointReader reader = MyUsbDevice.OpenEndpointReader(ReadEndpointID.Ep02);

                // open write endpoint 1.
                UsbEndpointWriter writer = MyUsbDevice.OpenEndpointWriter(WriteEndpointID.Ep01);

                // Remove the exepath/startup filename text from the begining of the CommandLine.

                if (byteArrayWritten.Length > 0)
                {
                    int bytesWritten;

                    byteArrayWritten = GetBytes("@PJL ECHO wrong sample 3-6-1993 23:12:00");
                    ec = writer.Write(byteArrayWritten, 2000, out bytesWritten);
                    if (ec != ErrorCode.None)
                    {
                        throw new Exception(UsbDevice.LastErrorString);
                    }

                    byte[] readBuffer = new byte[4096];
                    while (ec == ErrorCode.None)
                    {
                        int bytesRead;

                        // If the device hasn't sent data in the last 100 milliseconds,
                        // a timeout error (ec = IoTimedOut) will occur.
                        ec = reader.Read(readBuffer, 2000, out bytesRead);
                        if (ec != ErrorCode.None)
                        {
                            throw new Exception(UsbDevice.LastErrorString);
                        }

                        if (bytesRead == 0)
                        {
                            throw new Exception("No more bytes!");
                        }

                        // Write that output to the console.
                        //Console.Write(Encoding.Default.GetString(readBuffer, 0, bytesRead));

                        reader.ReadFlush();

                        return(readBuffer);
                    }

                    //Console.WriteLine("\r\nDone!\r\n");
                }
                else
                {
                    throw new Exception("Nothing to do.");
                }
            }
            catch (Exception ex)
            {
                throw ex;
                //Console.WriteLine();
                //Console.WriteLine((ec != ErrorCode.None ? ec + ":" : String.Empty) + ex.Message);
            }
            finally
            {
                try
                {
                    if (MyUsbDevice != null)
                    {
                        if (MyUsbDevice.IsOpen)
                        {
                            // If this is a "whole" usb device (libusb-win32, linux libusb-1.0)
                            // it exposes an IUsbDevice interface. If not (WinUSB) the
                            // 'wholeUsbDevice' variable will be null indicating this is
                            // an interface of a device; it does not require or support
                            // configuration and interface selection.
                            IUsbDevice wholeUsbDevice = MyUsbDevice as IUsbDevice;
                            if (!ReferenceEquals(wholeUsbDevice, null))
                            {
                                wholeUsbDevice.ResetDevice();
                                // Release interface #0.
                                wholeUsbDevice.ReleaseInterface(0);
                            }

                            MyUsbDevice.Close();
                        }
                        MyUsbDevice = null;

                        // Free usb resources
                        UsbDevice.Exit();
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            return(null);
        }