예제 #1
0
        /// <summary>
        /// This function queries the attached device for the programmable memory regions
        /// and stores the information returned into the memoryRegions array.
        /// </summary>
        public void Query()
        {
            if (HidDevice.DevicePath == null)
            {
                throw new NullReferenceException("HID device not connected");
            }

            //Create the write file and read file handles the to the USB device
            //  that we want to talk to
            using (var stream = HidDevice.Open()) {
                QueryDeviceStruct  myCommand  = new QueryDeviceStruct();
                QueryResultsStruct myResponse = new QueryResultsStruct();

                // Prepare the command that we want to send, in this case the QUERY device command
                myCommand.ReportID = 0;
                myCommand.Command  = BootloaderCommand.Query;

                // Send the command that we prepared
                stream.WriteStructure(myCommand);

                // Try to read a packet from the device
                myResponse = stream.ReadStructure <QueryResultsStruct>();

                // for each of the possible memory regions
                var memRegions = new List <MemoryRegionStruct>();
                for (byte i = 0; i < MaxDataRegions; i++)
                {
                    //If the type of region is 0xFF that means that we have
                    //  reached the end of the regions array.
                    if (myResponse.MemoryRegions[i].Type == MemoryRegionType.End)
                    {
                        break;
                    }

                    //copy the data from the packet to the local memory regions array
                    memRegions.Add(myResponse.MemoryRegions[i]);
                }
                _memoryRegions = memRegions.ToArray();

                //copy the last of the data out of the results packet
                switch (myResponse.DeviceFamily)
                {
                case DeviceFamilyType.PIC18:
                    _bytesPerAddress = 1;
                    break;

                case DeviceFamilyType.PIC24:
                    _bytesPerAddress = 2;
                    break;

                case DeviceFamilyType.PIC32:
                    _bytesPerAddress = 1;
                    break;
                }
                _deviceFamily   = myResponse.DeviceFamily;
                _bytesPerPacket = myResponse.BytesPerPacket;
            }
        }
예제 #2
0
        /// <summary>
        /// This function queries the attached device for the programmable memory regions
        /// and stores the information returned into the memoryRegions array.
        /// </summary>
        public override void Query()
        {
            // Attempt to connect to the HidDevice
            this.Scan();

            if (HidDevice.DevicePath == null)
            {
                throw new BootloaderException("HID device not connected");
            }

            //Create the write file and read file handles the to the USB device
            //  that we want to talk to
            using (var WriteDevice = HidDevice.GetWriteFile())
            {
                using (var ReadDevice = HidDevice.GetReadFile())
                {
                    QueryDeviceStruct  myCommand  = new QueryDeviceStruct();
                    QueryResultsStruct myResponse = new QueryResultsStruct();

                    //Prepare the command that we want to send, in this case the QUERY
                    //  device command
                    myCommand.WindowsReserved = 0;
                    myCommand.Command         = QUERY_DEVICE;

                    //Send the command that we prepared
                    WriteDevice.WriteStructure <QueryDeviceStruct>(myCommand);

                    //Try to read a packet from the device
                    myResponse = ReadDevice.ReadStructure <QueryResultsStruct>();

                    //If we were able to successfully read from the device

                    /*#if defined(DEBUG_THREADS) && defined(DEBUG_USB)
                     *  DEBUG_OUT("*** QUERY RESULTS ***");
                     *  printBuffer(myResponse.PacketData.Data,64);
                     #endif*/

                    //for each of the possible memory regions
                    var memRegions = new List <MemoryRegionStruct>();
                    for (byte i = 0; i < MAX_DATA_REGIONS; i++)
                    {
                        //If the type of region is 0xFF that means that we have
                        //  reached the end of the regions array.
                        if (myResponse.MemoryRegions[i].Type == MemoryRegionType.END)
                        {
                            break;
                        }

                        //copy the data from the packet to the local memory regions array
                        memRegions.Add(myResponse.MemoryRegions[i]);

                        /*#if defined(DEBUG_THREADS)
                         *  DEBUG_OUT(HexToString(memoryRegions[i].Type,1));
                         *  DEBUG_OUT(HexToString(memoryRegions[i].Address,4));
                         *  DEBUG_OUT(HexToString(memoryRegions[i].Size,4));
                         *  DEBUG_OUT("********************************************");
                         #endif*/
                    }
                    memoryRegions = memRegions.ToArray();

                    /*#if defined(DEBUG_THREADS)
                     *  DEBUG_OUT(HexToString(memoryRegionsDetected,1));
                     #endif*/

                    //copy the last of the data out of the results packet
                    switch ((DeviceFamilyType)myResponse.DeviceFamily)
                    {
                    case DeviceFamilyType.PIC18:
                        bytesPerAddress = 1;
                        //ckbox_ConfigWordProgramming_restore = true;
                        break;

                    case DeviceFamilyType.PIC24:
                        bytesPerAddress = 2;
                        //ckbox_ConfigWordProgramming_restore = true;
                        break;

                    case DeviceFamilyType.PIC32:
                        bytesPerAddress = 1;
                        //ckbox_ConfigWordProgramming_restore = false;
                        break;

                    default:
                        break;
                    }
                    deviceFamily   = (DeviceFamilyType)myResponse.DeviceFamily;
                    bytesPerPacket = myResponse.BytesPerPacket;

                    /*#if defined(DEBUG_THREADS)
                     *  DEBUG_OUT("********************************************");
                     *  DEBUG_OUT(String::Concat("Bytes per address = 0x",HexToString(bytesPerAddress,1)));
                     *  DEBUG_OUT(String::Concat("Bytes per packet = 0x",HexToString(bytesPerPacket,1)));
                     *  DEBUG_OUT("********************************************");
                     #endif*/
                }
            }
        }