예제 #1
0
        public void WriteData(byte[] buffer, int size)
        {
            byte[] data = new byte[64];

            //First Byte in buffer holds the Report ID
            _device.Outputs.DataBuf[0] = _device.Outputs.ID;
            Array.Copy(buffer, 0, _device.Outputs.DataBuf, 1, size);

            if (!_device.WriteOutput())
            {
                throw new CommunicationsError();
            }

            /* Cypress's code suggests a delay is needed here for successful bootloading, but
             * empirical testing suggests otherwise. Uncomment this if it becomes a problem. */
            //System.Threading.Thread.Sleep(100);
        }
예제 #2
0
        public static void BlockingBLUSBManager(string fullPathCyacd, BackgroundWorker BLCommThread)
        {
            CyHidDevice   myHidDevice_BLMgr = null;                                  // Handle of USB device
            USBDeviceList usbDevices_BLMgr = new USBDeviceList(CyConst.DEVICES_HID); // Pointer to list of USB devices
            uint          JTAGIDMgr, JTAGIDfileMgr, BLVERMgr;
            byte          DEVREVMgr;

            string[] CYACDLinesMgr;
            BLCommThread.WorkerReportsProgress = true;

            myHidDevice_BLMgr = usbDevices_BLMgr[Vendor_ID, Product_ID_BL] as CyHidDevice;
            if (myHidDevice_BLMgr != null)
            {
                myHidDevice_BLMgr = usbDevices_BLMgr[Vendor_ID, Product_ID_BL] as CyHidDevice;
            }
            else
            {
                return;
            }

            ///////////////////////////////////////////////////////////////////////////
            // Prepare and Send
            #region // --Enter Bootloader Command
            List <byte> cmdBuffer = new List <byte>();
            List <byte> rspBuffer = new List <byte>();

            // Build Enter Bootloader Packet
            CreateEnterBootLoaderCmd(ref cmdBuffer);

            // ____----____----Send the packet
            //First Byte in buffer holds the Report ID
            myHidDevice_BLMgr.Outputs.DataBuf[0] = myHidDevice_BLMgr.Outputs.ID;

            //Load 64 bytes of data
            for (int i = 1; i <= cmdBuffer.Count; i++)
            {
                myHidDevice_BLMgr.Outputs.DataBuf[i] = cmdBuffer[i - 1];
            }

            // Write to HID output buffer
            myHidDevice_BLMgr.WriteOutput();

            // Read from HID input buffer
            myHidDevice_BLMgr.ReadInput();

            // Parse the response
            rspBuffer.Clear();
            foreach (byte b in myHidDevice_BLMgr.Inputs.DataBuf)
            {
                rspBuffer.Add(b);
            }

            if (rspBuffer.Count != 0 && rspBuffer[2] == 0x00)
            {
                BLCommThread.ReportProgress(0);

                JTAGIDMgr  = rspBuffer[5];
                JTAGIDMgr |= (uint)rspBuffer[6] << 8;
                JTAGIDMgr |= (uint)rspBuffer[7] << 16;
                JTAGIDMgr |= (uint)rspBuffer[8] << 24;
                DEVREVMgr  = rspBuffer[9];
                BLVERMgr   = rspBuffer[10];
                BLVERMgr  |= (uint)rspBuffer[11] << 8;
                BLVERMgr  |= (uint)rspBuffer[12] << 16;

                CYACDLinesMgr = File.ReadAllLines(fullPathCyacd);
                JTAGIDfileMgr = uint.Parse(CYACDLinesMgr[0].Substring(0, 8), System.Globalization.NumberStyles.HexNumber);

                ///////////////////////////////////////////////////////////////////////////
                // Prepare and Send
                // --Programming Actions
                if (JTAGIDMgr == JTAGIDfileMgr)
                {
                    byte   ArrayID         = 0;
                    ushort RowNum          = 0;
                    ushort DataLen         = 0;
                    byte   rowChkSum       = 0;
                    byte   rowChkSumVerify = 0;
                    int    rowCnt          = 0;

                    // Row Data - Data Length bytes - 2*datalength ascii chars
                    List <byte[]> RowDataBytes = new List <byte[]>();
                    List <string> CyacdLines   = CYACDLinesMgr.ToList();
                    CyacdLines.RemoveAt(0);

                    foreach (string line in CyacdLines)
                    {
                        #region // parse
                        // Array ID - 1 byte - 2 ascii chars
                        ArrayID = byte.Parse(line.Substring(1, 2), System.Globalization.NumberStyles.HexNumber);

                        // Row Number - 2 bytes - 4 ascii chars
                        RowNum = ushort.Parse(line.Substring(3, 4), System.Globalization.NumberStyles.HexNumber);

                        // Data Length - 2 bytes- 4 ascii chars
                        DataLen = ushort.Parse(line.Substring(7, 4), System.Globalization.NumberStyles.HexNumber);

                        // Row Data - Data Length bytes - 2*datalength ascii chars
                        RowDataBytes.Clear();
                        RowDataBytes = ASCIIHexByteArray2Bytes(line.Substring(11, 2 * DataLen));

                        // Row Checksum
                        rowChkSum = byte.Parse(line.Substring(11 + 2 * DataLen, 2), System.Globalization.NumberStyles.HexNumber);

                        #endregion

                        #region  // erase
                        // Build Erase Row Packet
                        CreateEraseRowCmd(ref cmdBuffer, ArrayID, RowNum);

                        // ____----____----Send the packet
                        //First Byte in buffer holds the Report ID
                        myHidDevice_BLMgr.Outputs.DataBuf[0] = myHidDevice_BLMgr.Outputs.ID;

                        //Load 64 bytes of data
                        for (int i = 1; i <= cmdBuffer.Count; i++)
                        {
                            myHidDevice_BLMgr.Outputs.DataBuf[i] = cmdBuffer[i - 1];
                        }

                        // Write to HID output buffer
                        myHidDevice_BLMgr.WriteOutput();

                        // Read from HID input buffer
                        myHidDevice_BLMgr.ReadInput();

                        // Parse the response
                        rspBuffer.Clear();
                        foreach (byte b in myHidDevice_BLMgr.Inputs.DataBuf)
                        {
                            rspBuffer.Add(b);
                        }

                        if (rspBuffer.Count == 0)
                        {
                            MessageBox.Show("Failure of Erase Row Command - no response bytes"); break;
                        }
                        if (rspBuffer[2] != 0x00)
                        {
                            MessageBox.Show("Failure of Erase Row Command - " + rspBuffer[2].ToString("X2")); break;
                        }

                        #endregion

                        #region  // send data

                        // Buffer Row on Master PSoC
                        foreach (byte[] rowDat in RowDataBytes)
                        {
                            // Build Send Data Packet
                            CreateSendDataCmd(ref cmdBuffer, rowDat);

                            // ____----____----Send the packet
                            //First Byte in buffer holds the Report ID
                            myHidDevice_BLMgr.Outputs.DataBuf[0] = myHidDevice_BLMgr.Outputs.ID;

                            //Load data
                            for (int i = 1; i <= cmdBuffer.Count; i++)
                            {
                                myHidDevice_BLMgr.Outputs.DataBuf[i] = cmdBuffer[i - 1];
                            }

                            // Write to HID output buffer
                            myHidDevice_BLMgr.WriteOutput();

                            // Read from HID input buffer
                            myHidDevice_BLMgr.ReadInput();

                            // Parse the response
                            rspBuffer.Clear();
                            foreach (byte b in myHidDevice_BLMgr.Inputs.DataBuf)
                            {
                                rspBuffer.Add(b);
                            }

                            if (rspBuffer.Count == 0)
                            {
                                MessageBox.Show("Failure of Send Data Commands - no response bytes"); break;
                            }
                            if (rspBuffer[2] != 0x00)
                            {
                                MessageBox.Show("Failure of Send Data Commands - " + rspBuffer[2].ToString("X2")); break;
                            }
                        }
                        if (rspBuffer.Count == 0)
                        {
                            MessageBox.Show("Failure of Send Data Commands Detected - no response bytes"); break;
                        }
                        if (rspBuffer[2] != 0x00)
                        {
                            MessageBox.Show("Failure of Send Data Commands Detected- " + rspBuffer[2].ToString("X2")); break;
                        }

                        #endregion +		new System.Collections.Generic.Mscorlib_CollectionDebugView<byte[]>(RowDataBytes).Items[8]	{byte[0x00000020]}	byte[]

                        #region  // program
                        // Build Program Row Packet
                        CreateProgramRowCmd(ref cmdBuffer, ArrayID, RowNum);

                        // ____----____----Send the packet
                        //First Byte in buffer holds the Report ID
                        myHidDevice_BLMgr.Outputs.DataBuf[0] = myHidDevice_BLMgr.Outputs.ID;

                        //Load 64 bytes of data
                        for (int i = 1; i <= cmdBuffer.Count; i++)
                        {
                            myHidDevice_BLMgr.Outputs.DataBuf[i] = cmdBuffer[i - 1];
                        }

                        // Write to HID output buffer
                        myHidDevice_BLMgr.WriteOutput();

                        // Read from HID input buffer
                        myHidDevice_BLMgr.ReadInput();

                        // Parse the response
                        rspBuffer.Clear();
                        foreach (byte b in myHidDevice_BLMgr.Inputs.DataBuf)
                        {
                            rspBuffer.Add(b);
                        }

                        if (rspBuffer.Count == 0)
                        {
                            MessageBox.Show("Failure of Program Row Command - no response bytes"); break;
                        }
                        if (rspBuffer[2] != 0x00)
                        {
                            MessageBox.Show("Failure of Program Row Command - " + rspBuffer[2].ToString("X2")); break;
                        }

                        #endregion

                        #region  // verify
                        rowChkSumVerify = (byte)(rowChkSum + ArrayID + RowNum + (RowNum >> 8) + DataLen + (DataLen >> 8));

                        // Build Verify Row Packet
                        CreateVerifyRowCmd(ref cmdBuffer, ArrayID, RowNum, rowChkSumVerify);

                        // ____----____----Send the packet
                        //First Byte in buffer holds the Report ID
                        myHidDevice_BLMgr.Outputs.DataBuf[0] = myHidDevice_BLMgr.Outputs.ID;

                        //Load 64 bytes of data
                        for (int i = 1; i <= cmdBuffer.Count; i++)
                        {
                            myHidDevice_BLMgr.Outputs.DataBuf[i] = cmdBuffer[i - 1];
                        }

                        // Write to HID output buffer
                        myHidDevice_BLMgr.WriteOutput();

                        // Read from HID input buffer
                        myHidDevice_BLMgr.ReadInput();

                        // Parse the response
                        rspBuffer.Clear();
                        foreach (byte b in myHidDevice_BLMgr.Inputs.DataBuf)
                        {
                            rspBuffer.Add(b);
                        }

                        if (rspBuffer[2] != 0x00)
                        {
                            MessageBox.Show("Failure of Verify Row CheckSum Command - " + rspBuffer[2].ToString("X2")); break;
                        }
                        if (rspBuffer[5] != rowChkSumVerify)
                        {
                            MessageBox.Show("Failure of Verify Row CheckSum Command - Failed CheckSum\nExpecting " + rowChkSumVerify.ToString("X") + " Received " + rspBuffer[5].ToString("X") + "\nArrayID: " + ArrayID.ToString("X") + " RowNum: " + RowNum.ToString("X")); break;
                        }
                        //else
                        //{ MessageBox.Show("Verify Row CheckSum - Passed CheckSum\nExpecting " + rowChkSumVerify.ToString("X") + " Received " + rspBuffer[5].ToString("X") + "\nArrayID: " + ArrayID.ToString("X") + " RowNum: " + RowNum.ToString("X")); }
                        #endregion

                        BLCommThread.ReportProgress((int)(((rowCnt * 1.0f) / CyacdLines.Count) * 100));
                        rowCnt++;
                    }

                    ////////////////////////////////////////////////////////////////////////////
                    // Prepare and Send
                    #region  // -- Verify Application Checksum
                    // Build Verify Application Packet
                    CreateVerifyApplicationCmd(ref cmdBuffer);

                    // ____----____----Send the packet
                    //First Byte in buffer holds the Report ID
                    myHidDevice_BLMgr.Outputs.DataBuf[0] = myHidDevice_BLMgr.Outputs.ID;

                    //Load 64 bytes of data
                    for (int i = 1; i <= cmdBuffer.Count; i++)
                    {
                        myHidDevice_BLMgr.Outputs.DataBuf[i] = cmdBuffer[i - 1];
                    }

                    // Write to HID output buffer
                    myHidDevice_BLMgr.WriteOutput();

                    // Read from HID input buffer
                    myHidDevice_BLMgr.ReadInput();

                    // Parse the response
                    rspBuffer.Clear();
                    foreach (byte b in myHidDevice_BLMgr.Inputs.DataBuf)
                    {
                        rspBuffer.Add(b);
                    }

                    if (rspBuffer[2] != 0x00)
                    {
                        MessageBox.Show("Failure of Verify Application CheckSum Command - " + rspBuffer[2].ToString("X2"));
                    }
                    if (rspBuffer[5] == 0x00)
                    {
                        MessageBox.Show("Failure of Verify Application CheckSum Command - Failed CheckSum");
                    }

                    #endregion

                    ///////////////////////////////////////////////////////////////////////////
                    // Prepare and Send
                    #region  // --Exit the bootloader
                    // Build Exit Bootloader Packet
                    CreateExitBootLoaderCmd(ref cmdBuffer);

                    // ____----____----Send the packet
                    //First Byte in buffer holds the Report ID
                    myHidDevice_BLMgr.Outputs.DataBuf[0] = myHidDevice_BLMgr.Outputs.ID;

                    //Load 64 bytes of data
                    for (int i = 1; i <= cmdBuffer.Count; i++)
                    {
                        myHidDevice_BLMgr.Outputs.DataBuf[i] = cmdBuffer[i - 1];
                    }

                    // Write to HID output buffer
                    myHidDevice_BLMgr.WriteOutput();
                    MessageBox.Show("  - Remember to Disconnect the USB cable from the POC; then,\n\n  --  Please disconnect and reconnect power to the FS Comfort to complete the programming process.\n\n", "Boot Load Operation Exited!");
                    #endregion
                }
            }
            #endregion
        }
예제 #3
0
        public Form1()
        {
            InitializeComponent();


            // Create a list of CYUSB devices
            oUSBDeviceList = new USBDeviceList(CyConst.DEVICES_HID);

            oCyHidDevice = null;
            this.Text    = "No device";

            foreach (CyHidDevice oCyHidDeviceTemp in oUSBDeviceList)
            {
                if ((oCyHidDeviceTemp.VendorID == 0x0483) && (oCyHidDeviceTemp.ProductID == 0x5750))
                {
                    oCyHidDevice = oCyHidDeviceTemp;

                    this.Text = oCyHidDevice.Product + " by " + oCyHidDevice.Manufacturer + " - Connected";
                    //     this.Text = "DFDFD";

                    break;
                }
            }

            oCyHidDevice.Outputs.DataBuf[1] = 0;
            oCyHidDevice.Outputs.DataBuf[2] = 0;
            oCyHidDevice.Outputs.DataBuf[3] = 0;
            //oCyHidDevice.Outputs.DataBuf[4] = 4;
            // oCyHidDevice.Outputs.DataBuf[5] = 5;
            // oCyHidDevice.Outputs.DataBuf[6] = 6;

            this.Show();


            bool bResult;

            Stopwatch time10kOperations = Stopwatch.StartNew();
            UInt16    iPort             = 0x2FF8;

            time10kOperations.Stop();
            Freq.Text = Stopwatch.Frequency.ToString() + " per second";
            while (true)
            {
                Application.DoEvents();


                if (!doit)
                {
                    continue;
                }

                ;


                bResult = oCyHidDevice.ReadInput();
                time10kOperations.Stop();



                t = oCyHidDevice.Inputs.DataBuf[7];
                t = (t << 8) + oCyHidDevice.Inputs.DataBuf[6];
                t = (t << 8) + oCyHidDevice.Inputs.DataBuf[5];
                t = (t << 8) + oCyHidDevice.Inputs.DataBuf[4];

                s = oCyHidDevice.Inputs.DataBuf[11];
                s = (s << 8) + oCyHidDevice.Inputs.DataBuf[10];
                s = (s << 8) + oCyHidDevice.Inputs.DataBuf[9];
                s = (s << 8) + oCyHidDevice.Inputs.DataBuf[8];


                array1[idx++] = s;

                if (idx == 4990)
                {
                    idx = 9;
                }

                // continue;

                int tt = (int)oCyHidDevice.Inputs.DataBuf[2] + (oCyHidDevice.Inputs.DataBuf[3] << 8);

                //DB.AppendText(oCyHidDevice.Inputs.DataBuf[2].ToString() + " - " + oCyHidDevice.Inputs.DataBuf[3].ToString() + " - " + (time10kOperations.ElapsedMilliseconds).ToString() + "\r\n");
                DB.AppendText(tt.ToString() + " - " + (time10kOperations.ElapsedTicks).ToString() + " " + (time10kOperations.ElapsedMilliseconds).ToString() + " - " + t + " - " + s.ToString() + "\r\n");

                if (0 != c)
                {
                    c--;
                }
                if (0 == c)
                {
                    if (time10kOperations.ElapsedMilliseconds > 0)
                    {
                        over++;
                        Over.Text = over.ToString();
                    }
                    if (time10kOperations.ElapsedMilliseconds > 1)
                    {
                        over2++;
                        Over2.Text = over2.ToString();
                    }
                    if (time10kOperations.ElapsedMilliseconds > 5)
                    {
                        over5++;
                        Over5.Text = over5.ToString();
                    }
                }

                time10kOperations.Reset();
                time10kOperations.Start();



                continue;

                oCyHidDevice.Outputs.DataBuf[1] = (byte)time10kOperations.ElapsedMilliseconds;

                if (oCyHidDevice.Outputs.DataBuf[1] > 3)
                {
                    oCyHidDevice.Outputs.DataBuf[2]++;
                }

                if (oCyHidDevice.Outputs.DataBuf[1] > 6)
                {
                    oCyHidDevice.Outputs.DataBuf[3]++;
                }


                oCyHidDevice.Outputs.DataBuf[1] = 0x73;
                try
                {
                    oCyHidDevice.Outputs.DataBuf[2] = Convert.ToByte(textBox1.Text);
                    oCyHidDevice.Outputs.DataBuf[3] = Convert.ToByte(textBox2.Text);
                }
                catch
                {
                    textBox1.Text = "255";
                    textBox2.Text = "255";
                    oCyHidDevice.Outputs.DataBuf[2] = Convert.ToByte(textBox1.Text);
                    oCyHidDevice.Outputs.DataBuf[3] = Convert.ToByte(textBox2.Text);
                }
                //oCyHidDevice.Outputs.DataBuf[4] = 0x0D;
                // oCyHidDevice.Outputs.DataBuf[5] = 0x0A;
                //oCyHidDevice.Outputs.DataBuf[6]++;



                time10kOperations.Reset();
                time10kOperations.Start();

                bResult = oCyHidDevice.WriteOutput();


                //Out32((short)iPort, (short)0xFF);

                time10kOperations.Stop();
                // DB.AppendText(time10kOperations.ElapsedMilliseconds.ToString() + "\r\n") ;
                DB.AppendText(time10kOperations.ElapsedMilliseconds.ToString());
            }
            //oCyHidDevice.Outputs.DataBuf[2] = (byte)(oCyHidDevice.Inputs.DataBuf[1] + 1);
            //oCyHidDevice.Outputs.DataBuf[3] = (byte)(oCyHidDevice.Inputs.DataBuf[1] + 2);


            // if (bResult)
            // {
            //     bResult = oCyHidDevice.ReadInput();
            //     DB.AppendText(oCyHidDevice.Inputs.DataBuf[1].ToString() + "\r\n");
            // }
        }