示例#1
1
        /******Cypress USB EEPROM Programming****/
        //# USB Lg EEPROM programming 
        //This is the method that does the actual work.
        bool I2CEEPROM_Programming(BackgroundWorker worker, DoWorkEventArgs e)
        {
            // USBDeviceList USBdevList,
            // ref CyUSBDevice TE_USB_FX2_USBDevice
            // The parameter n must be >= 0 and <= 91.
            // Fib(n), with n > 91, overflows a long.  

            if (bClearLogTextBeforeEveryProgrammingOperation == true)
                LogTextLine = String.Empty;

            if (USBdevList == null)
            {
                throw new ArgumentException(
                    "At least one TE USB FX2 module must be attached");
            }

            bool result = false;
            bool opened = false;

            // Abort the operation if the user has canceled.
            // Note that a call to CancelAsync may have set 
            // CancellationPending to true just after the
            // last invocation of this method exits, so this 
            // code will not have the opportunity to set the 
            // DoWorkEventArgs.Cancel flag to true. This means
            // that RunWorkerCompletedEventArgs.Cancelled will
            // not be set to true in your RunWorkerCompleted
            // event handler. This is a race condition.

            if (worker.CancellationPending)
            {
                //TextLine = "line 1063";
                e.Cancel = true;
                return result;
            }
            else if (bFileUSB_Selected)
            {
                //TextLine = "line 1069";
                //timeout_ms = c_ulong(1000)		  # Timeout 1s
                //CardNumber = c_int(0)			   # Card Number 0
                //DriverBufferSize = c_int(132072)	# Driver Buffer Size 132072
                uint timeout_ms = 1000;
                //int CardNumber = 0;
                //int DriverBufferSizeWrite = 131072;
                //int DriverBufferSizeRead = 131072;

                byte[] Command = new byte[64];
                byte[] Reply = new byte[64];
                int CmdLength = 64;
                int ReplyLength = 64;

                UInt16 PID = 0;
                UInt16 VID = 0;

                op_error = 0;

                //Stopwatch stopWatch = new Stopwatch();
                //stopWatch.Start();
                //printlog("Programming EEPROM")
                //fpga_bitstream_size = len(usb_bitstream)			 # Calculate size
                //printlog("Firmware size " + str(fpga_bitstream_size))
                if (bVerboseLogText == true)
                {
                    LogTextLine += "-----------------------------------------------------------------------------------------------------------------" + "\r\n";
                    LogTextLine += "Programming FX2 microcontroller EEPROM and RAM using I2C" + "\r\n";
                    LogTextLine += "INFO: The operation STARTS " + (DateTime.Today).ToString() + "\r\n";
                    LogTextLine += "INFO: OpenFutNET version running on the host: " + OpenFutNETversion  + "\r\n";
                    LogTextLine += "INFO: Operating system of the host:" + GetOSFriendlyName() + "\r\n";
                    LogTextLine += "INFO: .NET version running on the host: " + Environment.Version.ToString() + "\r\n";
                    LogTextLine += "-----------------------------------------------------------------------------------------------------------------" + "\r\n";
                }
                
                if (bVerboseLogText == false)
                    //LogTextLine += "\r\n" + "-----------------------------------------------------------------------------------------------------------------" + "\r\n";
                    LogTextLine += "FX2 microcontroller EEPROM programming: START.....";
                else
                    LogTextLine += "FX2 microcontroller EEPROM programming: START" + "\r\n";

                if (bVerboseLogText == true)
                    LogTextLine += "Firmware size " + usb_bitstream_size.ToString() + " bytes" + "\r\n";

                if (USBFile_FilePath.Length > 0)
                    bFileUSB_Selected = true;

                USBdevList = new USBDeviceList(CyConst.DEVICES_CYUSB);

                if (USBdevList.Count != 0)
                {
                    fx2 = USBdevList[0] as CyFX2Device;
                    PID = fx2.ProductID;
                    VID = fx2.VendorID;
                    if (USBdevList.Count > 1)
                    {
                        MessageBox.Show("In this version the program must be used with a single Trenz Electronic or Cypress module attached. You must remove Cypress and/or Trenz Electronic micromodule(s) until only one module remains. This must be the module that you desire to write.");
                    }
                }

                if (((USBdevList.Count == 1) && (PID == PIDCypress) && (VID == VIDCypress) && bEEPROMSwitchSetToOn) || (((USBdevList.Count == 1) && (PID == PIDTrenzElectronic) && (VID == VIDTrenzElectronic))))
                {
                    if (bVerboseLogText == true)
                    {
                        LogTextLine += "INFO: VID and PID identify a Cypress Device: therefore EEPROM will be programmed using Cypress DLL 'CyUSB.dll'" + "\r\n";
                        LogTextLine += "INFO: Found " + (USBdevList.Count).ToString() + " card(s)" + "\r\n";
                        LogTextLine += "INFO: Connected to card 1" + "\r\n";
                    }

                    if ((USBdevList.Count == 1) && (PID == PIDCypress) && (VID == VIDCypress) && bEEPROMSwitchSetToOn)
                    {
                        PID_String = "0x8613";
                        VID_String = "0x04B4";
                    }
                    else
                    {
                        //PID and VID of Trenz Electronic device     
                        PID_String = "0x0300";
                        VID_String = "0x0BD0";         
                    }

                    opened = true;
 
                    if ((op_error == 0) && (opened == true))  //# No errors in past
                    {
                        StatusTextLine = "Programming FX2 microcontroller EEPROM";
                        if (bVerboseLogText == true)
                            LogTextLine += "Writing firmware to EEPROM: START" + "\r\n";
                        bResultLoadEEPROM = fx2.LoadEEPROM(USBFile_FilePath, true);

                        worker.ReportProgress(percentComplete_USB_EEPROMWrite);
                        if (bResultLoadEEPROM == false)
                        {
                            StatusTextLine = "Error: it is not possible to write EEPROM.";

                            if (bVerboseLogText == true)
                            {
                                LogTextLine += "ERROR: Can't call API function TE_USB_FX2_SendCommand + CMD_FX2_EEPROM_WRITE" + "\r\n";
                                LogTextLine += "INFO/WARNING: probably, you should move the EEPROM switch to ON" + "\r\n";
                            }
                            else
                                LogTextLine += " STOP. " + "ERROR: it is not possible to write the EEPROM. Probably, you should move the EEPROM switch to ON" + "\r\n";
                            op_error = 5;
                            LogTextLine += "-----------------------------------------------------------------------------------------------------------------" + "\r\n" + "\r\n";
                            worker.ReportProgress(percentComplete_USB_EEPROMWrite);                          
                            return false;
                        }
                        else
                        {
                            backgroundWakeEvent.WaitOne(750);
                            percentComplete_USB_EEPROMWrite = 100;
                            backgroundWakeEvent.WaitOne(750);
                            if (bVerboseLogText == true)
                                LogTextLine += "Writing firmware to EEPROM: STOP" + "\r\n";
                            else
                                LogTextLine += " STOP. " + "SUCCESS: FX2 microcontroller EEPROM programmed." + "\r\n";
                            StatusTextLine = "SUCCESS: FX2 microcontroller EEPROM programmed.";
                            worker.ReportProgress(percentComplete_USB_EEPROMWrite);
                            //break;
                        }
                    }
                }

               
            }             
            else          
            {
                StatusTextLine = "Error: file not selected";
                if (bVerboseLogText == true)
                    LogTextLine += "ERROR: File not selected" + "\r\n";
                op_error = 6;
                LogTextLine += "-----------------------------------------------------------------------------------------------------------------" + "\r\n" + "\r\n";
                return false;
                //op.set("Error")
                //printlog("ERROR: File not selected")          
            }

              
            if ((op_error == 0) || (bResultLoadEEPROM))           
            {
            
                //backgroundWakeEvent.WaitOne(5000);
                    
                if (bVerboseLogText == true)
                    LogTextLine += "FX2 microcontroller RAM Programming: START" + "\r\n";
                else
                    LogTextLine += "FX2 microcontroller RAM Programming: START.....";

                bResultLoadExternalRam = fx2.LoadExternalRam(USBFile_FilePath);
                    
                if (bResultLoadExternalRam == true)
                {
                    StatusTextLine = "SUCCESS: FX2 microcontroller EEPROM and RAM programmed";
                        
                    if (bVerboseLogText == true)
                    {                        
                        LogTextLine += "FX2 microcontroller RAM Programming: STOP" + "\r\n";
                        LogTextLine += "INFO: Programming FX2 microcontroller RAM with TE FX2 firmware v3.02 it is equivalent to remove and insert the module." + "\r\n";
                        LogTextLine += "INFO: Programming FX2 microcontroller RAM with TE FX2 firmware version earlier than v3.02 it is NOT equivalent to remove and inssert the module:" +"\r\n" 
                                        +"in this case you should click the button 'Refresh information about FPGA and FX2 microcontroller' to obtain the last value of firmware version" + "\r\n";
                        LogTextLine += "-----------------------------------------------------------------------------------------------------------------" + "\r\n";
                        LogTextLine += "Programming EEPROM and RAM of FX2 microcontroller using I2C" + "\r\n";
                        LogTextLine += "INFO: The operation STOPS " + (DateTime.Today).ToString() + "\r\n";
                        LogTextLine += "-----------------------------------------------------------------------------------------------------------------" + "\r\n" + "\r\n";                
                    }
                    else
                    {
                        LogTextLine += " STOP. " + "SUCCESS: FX2 microcontroller RAM programmed" + "\r\n";
                        LogTextLine += "-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------" + "\r\n" + "\r\n";
                        //LogTextLine += "-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------" + "\r\n" + "\r\n";
                        
                    }
                }
                else
                {
                    StatusTextLine = "Warning: RAM has not been written";
                    if (bVerboseLogText == true)
                        LogTextLine += "Warning: RAM has not been written" + "\r\n";
                    else
                        LogTextLine += " STOP. " + "Warning: it is not possible to write the RAM. You should power off/on cycle the TE USB FX2 module to load the RAM with EEPROM firmware." + "\r\n";
                    LogTextLine += "-----------------------------------------------------------------------------------------------------------------" + "\r\n" + "\r\n";
                    worker.ReportProgress(percentComplete_USB_EEPROMWrite);           
                }
         
                if (USBdevList != null)
                {
                        USBdevList.DeviceRemoved -= USBdevList_DeviceRemoved;
                        USBdevList.DeviceAttached -= USBdevList_DeviceAttached;
                        USBdevList.Dispose();           
                }

                //backgroundWakeEvent.WaitOne(6000);
                backgroundWakeEvent.WaitOne(500);
                USBdevList = new USBDeviceList(CyConst.DEVICES_CYUSB);
                USBdevList.DeviceAttached += new EventHandler(USBdevList_DeviceAttached);
                USBdevList.DeviceRemoved += new EventHandler(USBdevList_DeviceRemoved);
                TE_USB_FX2_USBDevice = USBdevList[0] as CyUSBDevice;
                //FX2MicronctrollerRamProgrammed = true;
                
                backgroundWakeEvent.WaitOne(500);

                //IF Trenz Electronic, non Cypress o DEWESoft
                /*
                while (bResultLoadExternalRam == true  ) 
                {
                    textBox_PID.Text = PID_String;
                    textBox_VID.Text = VID_String;



                    backgroundWakeEvent.WaitOne(1000);
                    Command1[0] = (byte)FX2_Commands.CMD_FX2_READ_VERSION;
                    //bSendCommand = false;
                    //if TE_USB_FX2_USBDevice.
                    bSendCommand = TE_USB_FX2.TE_USB_FX2.TE_USB_FX2_SendCommand(ref TE_USB_FX2_USBDevice, ref Command1, ref CmdLength1, ref Reply1, ref ReplyLength1, TIMEOUT_MS);
                    backgroundWakeEvent.WaitOne(1000);
                    if ( bSendCommand )
                    {                  
                        if (ReplyLength1 >= 4)             
                        {
                            bResultLoadExternalRam = false;
                            break;
                        }                 
                        else                    
                        {                  
                    
                        }     
                    }
                }
                */
                       
                worker.ReportProgress(percentComplete_USB_EEPROMWrite);
                backgroundWakeEvent.WaitOne(1000);
                worker.ReportProgress(percentComplete_USB_EEPROMWrite);                 
                backgroundWakeEvent.WaitOne(1000);                

                //LogTextLine += "Programming RAM of FX2 microcontroller: STOP" + "\r\n";
                worker.ReportProgress(percentComplete_USB_EEPROMWrite);

                return bResultLoadEEPROM;
                       
            }
            else
            {
                StatusTextLine = "Error: EEPROM has not been written";               
                if (bVerboseLogText == true)
                    LogTextLine += "ERROR: EEPROM has not been written" + "\r\n";
                else
                    LogTextLine += " STOP. " + "ERROR: it is not possible to write the EEPROM." + "\r\n";
                LogTextLine += "-----------------------------------------------------------------------------------------------------------------" + "\r\n" + "\r\n";
                worker.ReportProgress(percentComplete_USB_EEPROMWrite);
                result = false;
                return result;
            }    

            //return result;
        }
示例#2
0
        public USBDeviceList(byte DeviceMask, App_PnP_Callback fnCallBack)
        {
            Items             = new ArrayList();
            hDevNotifications = new ArrayList();
            USBDriverGuids    = new ArrayList();

            EventCallBack = new App_PnP_Callback(PnP_Event_Handler);

            MsgWin.AppCallback = EventCallBack;
            AppCallBack        = fnCallBack;

            // Get the HID GUID
            PInvoke.HidD_GetHidGuid(ref HidGuid);

            // Create list of driver GUIDs for this instance
            FillDriverGuids(DeviceMask);

            USBDevice tmpDev, tmp;
            int       devs = 0;

            foreach (Guid guid in USBDriverGuids)
            {
                // tmpDev is just used for the DeviceCount functionality
                if (guid.Equals(CyConst.StorGuid))
                {
                    tmpDev = new CyUSBStorDevice(guid);
                }
                else if (guid.Equals(HidGuid))
                {
                    tmpDev = new CyHidDevice(HidGuid);
                }
                else
                {
                    tmpDev = new CyFX2Device(guid);
                }

                // DeviceCount is IO intensive. Don't use it as for loop limit
                devs = tmpDev.DeviceCount;

                for (int d = 0; d < devs; d++)
                {
                    // Create the new USBDevice objects of the correct type, based on guid
                    if (guid.Equals(CyConst.StorGuid))
                    {
                        tmp = new CyUSBStorDevice(guid);
                    }
                    else if (guid.Equals(HidGuid))
                    {
                        tmp = new CyHidDevice(HidGuid);
                    }
                    else
                    {
                        tmp = new CyFX2Device(guid);
                        if (tmp.Open((byte)d))
                        {// open handle to check device type
                            CyUSBDevice t = tmp as CyUSBDevice;
                            if (!t.CheckDeviceTypeFX3FX2())
                            {//FX3
                                tmp.Close();
                                tmp = new CyFX3Device(guid);
                            }
                            else
                            {
                                tmp.Close();
                            }
                        }
                    }

                    if (tmp.Open((byte)d))
                    {
                        Items.Add(tmp); // This creates new reference to tmp in Items
                        tmp.RegisterForPnPEvents(MsgWin.Handle);
                    }
                }

                if (guid.Equals(CyConst.StorGuid))   // We're not sure which drivers were identified, so setup PnP with both
                {
                    RegisterForPnpEvents(MsgWin.Handle, CyConst.DiskGuid);
                    RegisterForPnpEvents(MsgWin.Handle, CyConst.CdGuid);
                }
                else
                {
                    RegisterForPnpEvents(MsgWin.Handle, guid);
                }
            } // foreach guid
        }
示例#3
0
        public USBDeviceList(byte DeviceMask, App_PnP_Callback fnCallBack)
        {
            Items = new ArrayList();
            hDevNotifications = new ArrayList();
            USBDriverGuids = new ArrayList();

            EventCallBack = new App_PnP_Callback(PnP_Event_Handler);

            MsgWin.AppCallback = EventCallBack;
            AppCallBack = fnCallBack;

            // Get the HID GUID
            PInvoke.HidD_GetHidGuid(ref HidGuid);

            // Create list of driver GUIDs for this instance
            FillDriverGuids(DeviceMask);

            USBDevice tmpDev, tmp;
            int devs = 0;

            foreach (Guid guid in USBDriverGuids)
            {
                // tmpDev is just used for the DeviceCount functionality
                if (guid.Equals(CyConst.StorGuid))
                    tmpDev = new CyUSBStorDevice(guid);
                else if (guid.Equals(HidGuid))
                    tmpDev = new CyHidDevice(HidGuid);
                else
                    tmpDev = new CyFX2Device(guid);

                // DeviceCount is IO intensive. Don't use it as for loop limit
                devs = tmpDev.DeviceCount;

                for (int d = 0; d < devs; d++)
                {
                    // Create the new USBDevice objects of the correct type, based on guid
                    if (guid.Equals(CyConst.StorGuid))
                        tmp = new CyUSBStorDevice(guid);
                    else if (guid.Equals(HidGuid))
                        tmp = new CyHidDevice(HidGuid);
                    else
                    {
                        tmp = new CyFX2Device(guid);
                        if (tmp.Open((byte)d))
                        {// open handle to check device type
                            CyUSBDevice t = tmp as CyUSBDevice;
                            if (!t.CheckDeviceTypeFX3FX2())
                            {//FX3
                                tmp.Close();
                                tmp = new CyFX3Device(guid);
                            }
                            else
                                tmp.Close();
                        }

                    }

                    if (tmp.Open((byte)d))
                    {
                        Items.Add(tmp); // This creates new reference to tmp in Items
                        tmp.RegisterForPnPEvents(MsgWin.Handle);
                    }
                }

                if (guid.Equals(CyConst.StorGuid))   // We're not sure which drivers were identified, so setup PnP with both
                {
                    RegisterForPnpEvents(MsgWin.Handle, CyConst.DiskGuid);
                    RegisterForPnpEvents(MsgWin.Handle, CyConst.CdGuid);
                }
                else
                    RegisterForPnpEvents(MsgWin.Handle, guid);

            } // foreach guid
        }
示例#4
0
        public USBDevice Add()
        {
            if (_alreadyDisposed)
            {
                throw new ObjectDisposedException("");
            }

            USBDevice tmp, tmpDev;

            foreach (Guid guid in USBDriverGuids)
            {
                // tmpDev is just used for the DeviceCount functionality
                if (guid.Equals(CyConst.StorGuid))
                {
                    tmpDev = new CyUSBStorDevice(guid);
                }
                else if (guid.Equals(HidGuid))
                {
                    tmpDev = new CyHidDevice(guid);
                }
                else
                {
                    tmpDev = new CyFX2Device(guid);
                }

                // The number of devices now connected to this GUID
                int connectedDevs = tmpDev.DeviceCount;
                tmpDev.Dispose();

                // Find out how many items have this guid
                int listedDevs = 0;
                foreach (USBDevice dev in Items)
                {
                    if (guid.Equals(CyConst.StorGuid) && (dev._drvGuid.Equals(CyConst.CdGuid) || dev._drvGuid.Equals(CyConst.DiskGuid)))
                    {
                        listedDevs++;
                    }
                    else if (dev._drvGuid.Equals(guid))
                    {
                        listedDevs++;
                    }
                }

                // If greater, add
                if (connectedDevs > listedDevs)
                {
                    for (byte d = 0; d < connectedDevs; d++)
                    {
                        // Create the new USBDevice object of the correct type, based on guid
                        if (guid.Equals(CyConst.StorGuid))
                        {
                            tmp = new CyUSBStorDevice(guid);
                        }
                        else if (guid.Equals(HidGuid))
                        {
                            tmp = new CyHidDevice(guid);
                        }
                        else
                        {
                            tmp = new CyFX2Device(guid);
                            if (tmp.Open((byte)d))
                            {// open handle to check device type
                                CyUSBDevice t = tmp as CyUSBDevice;
                                if (!t.CheckDeviceTypeFX3FX2())
                                {//FX3
                                    tmp.Close();
                                    tmp = new CyFX3Device(guid);
                                }
                                else
                                {
                                    tmp.Close();
                                }
                            }
                        }


                        // If this device not already in the list
                        if (tmp.Open(d) && (DeviceIndex(tmp) == 0xFF))
                        {
                            Items.Add(tmp);
                            tmp.RegisterForPnPEvents(MsgWin.Handle);
                            return(tmp);
                        }
                        else
                        {
                            tmp.Dispose();
                        }
                    }
                }
            }

            return(null);
        }
示例#5
0
        public USBDevice Add()
        {
            if (_alreadyDisposed) throw new ObjectDisposedException("");

            USBDevice tmp, tmpDev;

            foreach (Guid guid in USBDriverGuids)
            {
                // tmpDev is just used for the DeviceCount functionality
                if (guid.Equals(CyConst.StorGuid))
                    tmpDev = new CyUSBStorDevice(guid);
                else if (guid.Equals(HidGuid))
                    tmpDev = new CyHidDevice(guid);
                else
                    tmpDev = new CyFX2Device(guid);

                // The number of devices now connected to this GUID
                int connectedDevs = tmpDev.DeviceCount;
                tmpDev.Dispose();

                // Find out how many items have this guid
                int listedDevs = 0;
                foreach (USBDevice dev in Items)
                    if (guid.Equals(CyConst.StorGuid) && (dev._drvGuid.Equals(CyConst.CdGuid) || dev._drvGuid.Equals(CyConst.DiskGuid)))
                        listedDevs++;
                    else if (dev._drvGuid.Equals(guid))
                        listedDevs++;

                // If greater, add
                if (connectedDevs > listedDevs)
                {
                    for (byte d = 0; d < connectedDevs; d++)
                    {
                        // Create the new USBDevice object of the correct type, based on guid
                        if (guid.Equals(CyConst.StorGuid))
                            tmp = new CyUSBStorDevice(guid);
                        else if (guid.Equals(HidGuid))
                            tmp = new CyHidDevice(guid);
                        else
                        {
                            tmp = new CyFX2Device(guid);
                            if (tmp.Open((byte)d))
                            {// open handle to check device type
                                CyUSBDevice t = tmp as CyUSBDevice;
                                if (!t.CheckDeviceTypeFX3FX2())
                                {//FX3
                                    tmp.Close();
                                    tmp = new CyFX3Device(guid);
                                }
                                else
                                    tmp.Close();
                            }

                        }

                        // If this device not already in the list
                        if (tmp.Open(d) && (DeviceIndex(tmp) == 0xFF))
                        {
                            Items.Add(tmp);
                            tmp.RegisterForPnPEvents(MsgWin.Handle);
                            if (connectedDevs == 1 && d == 0) return tmp;
                        }
                    }

                }
            }

            return null;
        }