예제 #1
0
        private void progMemEdit(object sender, DataGridViewCellEventArgs e)
        {
            int    row      = e.RowIndex;
            int    col      = e.ColumnIndex;
            string editText = "0x" + dataGridProgramMemory[col, row].FormattedValue.ToString();
            int    value    = 0;

            try
            {
                value = UTIL.Convert_Value_To_Int(editText);
            }
            catch
            {
                value = 0;
            }
            int numColumns = dataGridProgramMemory.ColumnCount - 1;

            if (comboBoxProgMemView.SelectedIndex >= 1) // ascii view
            {
                numColumns /= 2;
            }

            int index = ((row * numColumns) + col - 1);

            if (Pk2.FamilyIsPIC32())
            {
                int progMemP32 = (int)Pk2.DevFile.PartsList[Pk2.ActivePart].ProgramMem;
                int bootMemP32 = (int)Pk2.DevFile.PartsList[Pk2.ActivePart].BootFlash;
                progMemP32 -= bootMemP32; // boot flash at upper end of prog mem.

                index -= numColumns;      // first row has "Program Flash" text

                if (index > progMemP32)
                {
                    index -= numColumns; // subtract row with "Boot Flash" text
                }
            }

            Pk2.DeviceBuffers.ProgramMemory[index] =
                (uint)(value & Pk2.DevFile.Families[Pk2.GetActiveFamily()].BlankValue);

            TellMainFormProgMemEdited();

            progMemJustEdited = true;
            TellMainFormUpdateGUI();
        }
예제 #2
0
        private void redraw()
        {
            // Scale window to number of config words:
            int numUnusedConfigs = K_MAXCONFIGS - Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigWords;

            numUnusedConfigs *= (int)(48 * ScalefactH);
            numUnusedConfigs -= (int)(24 * ScalefactH); // add a bit of length to the window for Asian OSs
            this.Size         = new Size((int)(this.Size.Width), (int)(this.Size.Height - numUnusedConfigs));

            // defaults for Baseline, Midrange
            string[] cfgNames = new string[K_MAXCONFIGS];
            for (int i = 1; i <= cfgNames.Length; i++)
            {
                cfgNames[i - 1] = string.Format("CONFIG{0:G}", i);
            }

            int configAddress   = (int)Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigAddr / 2;
            int configIncrement = 1;

            if (Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigWords == 1)
            { // baseline or midrange
                cfgNames[0] = "CONFIG";
            }

            // PIC18
            if (Pk2.DevFile.Families[Pk2.GetActiveFamily()].BlankValue == 0xFFFF)
            {
                configAddress   = (int)Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigAddr;
                configIncrement = 2;
            }
            // 16-bit
            else if (Pk2.DevFile.Families[Pk2.GetActiveFamily()].BlankValue == 0xFFFFFF)
            {
                // PIC24F
                if (Pk2.FamilyIsPIC24FJ())
                {
                    for (int cw = 1; cw <= Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigWords; cw++)
                    {
                        cfgNames[Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigWords - cw] = string.Format("CW{0:G}", cw);
                    }
                    configAddress   = (int)Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigAddr / 2;
                    configIncrement = 2;
                }
                // PIC24H, dsPIC33, dsPIC30 SMPS, PIC24F-KA-
                else if (Pk2.FamilyIsPIC24H() || Pk2.FamilyIsdsPIC33F() || Pk2.FamilyIsdsPIC30SMPS() ||
                         (Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigWords == 9))
                {
                    cfgNames[0]     = "FBS";
                    cfgNames[1]     = "FSS";
                    cfgNames[2]     = "FGS";
                    cfgNames[3]     = "FOSCSEL";
                    cfgNames[4]     = "FOSC";
                    cfgNames[5]     = "FWDT";
                    cfgNames[6]     = "FPOR";
                    cfgNames[7]     = "FICD";
                    cfgNames[8]     = "FDS";
                    configAddress   = (int)Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigAddr / 2;
                    configIncrement = 2;
                }
                // dsPIC30
                else
                {
                    cfgNames[0]     = "FOSC";
                    cfgNames[1]     = "FWDT";
                    cfgNames[2]     = "FBORPOR";
                    cfgNames[3]     = "FBS";
                    cfgNames[4]     = "FSS";
                    cfgNames[5]     = "FGS";
                    cfgNames[6]     = "FICD";
                    configAddress   = (int)Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigAddr / 2;
                    configIncrement = 2;
                }
            }
            // PIC32
            else if (Pk2.FamilyIsPIC32())
            {
                cfgNames[0]     = "DEVCFG2L";
                cfgNames[1]     = "DEVCFG2H";
                cfgNames[2]     = "DEVCFG1L";
                cfgNames[3]     = "DEVCFG1H";
                cfgNames[4]     = "DEVCFG0L";
                cfgNames[5]     = "DEVCFG0H";
                configAddress   = (int)Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigAddr;
                configIncrement = 2;
            }

            for (int w = 0; w < K_MAXCONFIGS; w++)
            {
                if (w < Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigWords)
                {
                    if (Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigMasks[w] == 0)
                    {
                    }
                    configWords[w].name.Text = cfgNames[w];
                    configWords[w].addr.Text = string.Format("{0:X}", configAddress + (w * configIncrement));
                    ushort cword = (ushort)Pk2.DeviceBuffers.ConfigWords[w];
                    if (displayMask == 0)
                    {
                        cword &= (ushort)Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigMasks[w];
                    }
                    else if (displayMask == 1)
                    {
                        cword |= (ushort)~Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigMasks[w];
                    }
                    cword &= (ushort)Pk2.DevFile.Families[Pk2.GetActiveFamily()].BlankValue;
                    configWords[w].value.Text = string.Format("{0:X4}", cword);
                    ushort mask = 1;
                    for (int b = 0; b < 16; b++)
                    {
                        if ((Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigMasks[w] & mask) > 0)
                        {
                            if ((Pk2.DeviceBuffers.ConfigWords[w] & mask) > 0)
                            {
                                configWords[w].bits[b].Text = "1";
                            }
                            else
                            {
                                configWords[w].bits[b].Text = "0";
                            }
                        }
                        else
                        {
                            configWords[w].bits[b].Text      = "-";
                            configWords[w].bits[b].BackColor = System.Drawing.SystemColors.Control;
                            configWords[w].bits[b].Enabled   = false;
                        }
                        mask <<= 1;
                    }
                    if (Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigMasks[w] == 0)
                    {
                        configWords[w].configPanel.Enabled = false;
                    }
                }
                else
                {
                    configWords[w].configPanel.Visible = false;
                }
            }
        }
예제 #3
0
        public static bool ExportHexFile(string filePath, bool progMem, bool eeMem)
        {
            if (filePath.Length > 4)
            {
                if ((filePath.Substring(filePath.Length - 4).ToUpper() == ".BIN") && Pk2.FamilyIsEEPROM())
                {
                    return(ExportBINFile(filePath));
                }
            }

            StreamWriter hexFile = new StreamWriter(filePath);

            // Start with segment zero
            if (Pk2.DevFile.Families[Pk2.GetActiveFamily()].BlankValue > 0xFFFFFF)
            { // PIC32
                hexFile.WriteLine(":020000041D00DD");
            }
            else
            {
                hexFile.WriteLine(":020000040000FA");
            }
            // Program Memory ----------------------------------------------------------------------------
            int fileSegment = 0;
            int fileAddress = 0;
            int programEnd  = Pk2.DeviceBuffers.ProgramMemory.Length;

            if (Pk2.DevFile.Families[Pk2.GetActiveFamily()].BlankValue > 0xFFFFFF)
            { // PIC32
                fileSegment = (int)(Constants.P32_PROGRAM_FLASH_START_ADDR >> 16);
                fileAddress = (int)(Constants.P32_PROGRAM_FLASH_START_ADDR & 0xFFFF);
                programEnd -= (int)Pk2.DevFile.PartsList[Pk2.ActivePart].BootFlash;
            }
            int arrayIndex     = 0;
            int bytesPerWord   = Pk2.DevFile.Families[Pk2.GetActiveFamily()].ProgMemHexBytes;
            int arrayIncrement = 16 / bytesPerWord;     // # array words per hex line.

            if (progMem)
            {
                do
                {
                    string hexLine = string.Format(":10{0:X4}00", fileAddress);
                    for (int i = 0; i < arrayIncrement; i++)
                    {
                        // convert entire array word to hex string of 4 bytes.
                        string hexWord = "00000000";
                        if ((arrayIndex + i) < Pk2.DeviceBuffers.ProgramMemory.Length)
                        {
                            hexWord = string.Format("{0:X8}", Pk2.DeviceBuffers.ProgramMemory[arrayIndex + i]);
                        }
                        for (int j = 0; j < bytesPerWord; j++)
                        {
                            hexLine += hexWord.Substring((6 - 2 * j), 2);
                        }
                    }
                    hexLine += string.Format("{0:X2}", computeChecksum(hexLine));
                    hexFile.WriteLine(hexLine);

                    fileAddress += 16;
                    arrayIndex  += arrayIncrement;

                    // check for segment boundary
                    if ((fileAddress > 0xFFFF) && (arrayIndex < Pk2.DeviceBuffers.ProgramMemory.Length))
                    {
                        fileSegment += fileAddress >> 16;
                        fileAddress &= 0xFFFF;
                        string segmentLine = string.Format(":02000004{0:X4}", fileSegment);
                        segmentLine += string.Format("{0:X2}", computeChecksum(segmentLine));
                        hexFile.WriteLine(segmentLine);
                    }
                } while (arrayIndex < programEnd);
            }
            // Boot Memory ----------------------------------------------------------------------------
            if ((Pk2.DevFile.PartsList[Pk2.ActivePart].BootFlash > 0) && Pk2.FamilyIsPIC32())
            {
                hexFile.WriteLine(":020000041FC01B");
                arrayIndex  = programEnd;
                programEnd  = Pk2.DeviceBuffers.ProgramMemory.Length;
                fileSegment = (int)(Constants.P32_BOOT_FLASH_START_ADDR >> 16);
                fileAddress = (int)(Constants.P32_BOOT_FLASH_START_ADDR & 0xFFFF);
                if (progMem)
                {
                    do
                    {
                        string hexLine = string.Format(":10{0:X4}00", fileAddress);
                        for (int i = 0; i < arrayIncrement; i++)
                        {
                            // convert entire array word to hex string of 4 bytes.
                            string hexWord = string.Format("{0:X8}", Pk2.DeviceBuffers.ProgramMemory[arrayIndex + i]);
                            for (int j = 0; j < bytesPerWord; j++)
                            {
                                hexLine += hexWord.Substring((6 - 2 * j), 2);
                            }
                        }
                        hexLine += string.Format("{0:X2}", computeChecksum(hexLine));
                        hexFile.WriteLine(hexLine);

                        fileAddress += 16;
                        arrayIndex  += arrayIncrement;

                        // check for segment boundary
                        if ((fileAddress > 0xFFFF) && (arrayIndex < Pk2.DeviceBuffers.ProgramMemory.Length))
                        {
                            fileSegment += fileAddress >> 16;
                            fileAddress &= 0xFFFF;
                            string segmentLine = string.Format(":02000004{0:X4}", fileSegment);
                            segmentLine += string.Format("{0:X2}", computeChecksum(segmentLine));
                            hexFile.WriteLine(segmentLine);
                        }
                    } while (arrayIndex < programEnd);
                }
            }
            // EEPROM -------------------------------------------------------------------------------------
            if (eeMem)
            {
                int eeSize = Pk2.DevFile.PartsList[Pk2.ActivePart].EEMem;
                arrayIndex = 0;
                if (eeSize > 0)
                {
                    uint eeAddr = Pk2.DevFile.PartsList[Pk2.ActivePart].EEAddr;
                    if ((eeAddr & 0xFFFF0000) > 0)
                    { // need a segment address
                        string segmentLine = string.Format(":02000004{0:X4}", (eeAddr >> 16));
                        segmentLine += string.Format("{0:X2}", computeChecksum(segmentLine));
                        hexFile.WriteLine(segmentLine);
                    }

                    fileAddress = (int)eeAddr & 0xFFFF;
                    int eeBytesPerWord = Pk2.DevFile.Families[Pk2.GetActiveFamily()].EEMemHexBytes;
                    arrayIncrement = 16 / eeBytesPerWord;     // # array words per hex line.
                    do
                    {
                        string hexLine = string.Format(":10{0:X4}00", fileAddress);
                        for (int i = 0; i < arrayIncrement; i++)
                        {
                            // convert entire array word to hex string of 4 bytes.
                            string hexWord = string.Format("{0:X8}", Pk2.DeviceBuffers.EEPromMemory[arrayIndex + i]);
                            for (int j = 0; j < eeBytesPerWord; j++)
                            {
                                hexLine += hexWord.Substring((6 - 2 * j), 2);
                            }
                        }
                        hexLine += string.Format("{0:X2}", computeChecksum(hexLine));
                        hexFile.WriteLine(hexLine);

                        fileAddress += 16;
                        arrayIndex  += arrayIncrement;
                    }while (arrayIndex < Pk2.DeviceBuffers.EEPromMemory.Length);
                }
            }
            // Configuration Words ------------------------------------------------------------------------
            if (progMem)
            {
                int cfgBytesPerWord = bytesPerWord;
                if (Pk2.DevFile.Families[Pk2.GetActiveFamily()].BlankValue > 0xFFFFFF)
                { // PIC32
                    cfgBytesPerWord = 2;
                }
                int configWords = Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigWords;
                if ((configWords > 0) && (Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigAddr >
                                          (Pk2.DevFile.PartsList[Pk2.ActivePart].ProgramMem * bytesPerWord)))
                { // If there are Config words and they aren't at the end of program flash
                    uint configAddr = Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigAddr;
                    if ((configAddr & 0xFFFF0000) > 0)
                    { // need a segment address
                        string segmentLine = string.Format(":02000004{0:X4}", (configAddr >> 16));
                        segmentLine += string.Format("{0:X2}", computeChecksum(segmentLine));
                        hexFile.WriteLine(segmentLine);
                    }

                    fileAddress = (int)configAddr & 0xFFFF;

                    int cfgsWritten = 0;
                    for (int lines = 0; lines < (((configWords * cfgBytesPerWord - 1) / 16) + 1); lines++)
                    {
                        int cfgsLeft = configWords - cfgsWritten;
                        if (cfgsLeft >= (16 / cfgBytesPerWord))
                        {
                            cfgsLeft = (16 / cfgBytesPerWord);
                        }
                        string hexLine = string.Format(":{0:X2}{1:X4}00", (cfgsLeft * cfgBytesPerWord), fileAddress);
                        fileAddress += (cfgsLeft * cfgBytesPerWord);
                        for (int i = 0; i < cfgsLeft; i++)
                        {
                            // convert entire array word to hex string of 4 bytes.
                            uint cfgWord = Pk2.DeviceBuffers.ConfigWords[cfgsWritten + i];
                            if (Pk2.DevFile.Families[Pk2.GetActiveFamily()].BlankValue > 0xFFFFFF)
                            {// PIC32
                                cfgWord |= ~(uint)Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigMasks[cfgsWritten + i];
                                cfgWord &= Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigBlank[cfgsWritten + i];
                            }
                            string hexWord = string.Format("{0:X8}", cfgWord);
                            for (int j = 0; j < cfgBytesPerWord; j++)
                            {
                                hexLine += hexWord.Substring(8 - ((j + 1) * 2), 2);
                            }
                        }
                        hexLine += string.Format("{0:X2}", computeChecksum(hexLine));
                        hexFile.WriteLine(hexLine);
                        cfgsWritten += cfgsLeft;
                    }
                }
            }

            // UserIDs ------------------------------------------------------------------------------------
            if (progMem)
            {
                int userIDs = Pk2.DevFile.PartsList[Pk2.ActivePart].UserIDWords;
                arrayIndex = 0;
                if (userIDs > 0)
                {
                    uint uIDAddr = Pk2.DevFile.PartsList[Pk2.ActivePart].UserIDAddr;
                    if ((uIDAddr & 0xFFFF0000) > 0)
                    { // need a segment address
                        string segmentLine = string.Format(":02000004{0:X4}", (uIDAddr >> 16));
                        segmentLine += string.Format("{0:X2}", computeChecksum(segmentLine));
                        hexFile.WriteLine(segmentLine);
                    }

                    fileAddress = (int)uIDAddr & 0xFFFF;
                    int idBytesPerWord = Pk2.DevFile.Families[Pk2.GetActiveFamily()].UserIDHexBytes;
                    arrayIncrement = 16 / idBytesPerWord;     // # array words per hex line.
                    string hexLine;
                    do
                    {
                        int remainingBytes = (userIDs - arrayIndex) * idBytesPerWord;
                        if (remainingBytes < 16)
                        {
                            hexLine        = string.Format(":{0:X2}{1:X4}00", remainingBytes, fileAddress);
                            arrayIncrement = (userIDs - arrayIndex);
                        }
                        else
                        {
                            hexLine = string.Format(":10{0:X4}00", fileAddress);
                        }
                        for (int i = 0; i < arrayIncrement; i++)
                        {
                            // convert entire array word to hex string of 4 bytes.
                            string hexWord = string.Format("{0:X8}", Pk2.DeviceBuffers.UserIDs[arrayIndex + i]);
                            for (int j = 0; j < idBytesPerWord; j++)
                            {
                                hexLine += hexWord.Substring((6 - 2 * j), 2);
                            }
                        }
                        hexLine += string.Format("{0:X2}", computeChecksum(hexLine));
                        hexFile.WriteLine(hexLine);

                        fileAddress += 16;
                        arrayIndex  += arrayIncrement;
                    } while (arrayIndex < Pk2.DeviceBuffers.UserIDs.Length);
                }
            }
            // Test Memory --------------------------------------------------------------------------------
            if (FormPICkit2.TestMemoryEnabled && FormPICkit2.TestMemoryOpen)
            {
                if (FormPICkit2.formTestMem.HexImportExportTM())
                {
                    int tmSize = FormPICkit2.TestMemoryWords;
                    arrayIndex = 0;
                    if (tmSize > 0)
                    {
                        uint tmAddr = Pk2.DevFile.Families[Pk2.GetActiveFamily()].TestMemoryStart;
                        if ((tmAddr & 0xFFFF0000) > 0)
                        { // need a segment address
                            string segmentLine = string.Format(":02000004{0:X4}", (tmAddr >> 16));
                            segmentLine += string.Format("{0:X2}", computeChecksum(segmentLine));
                            hexFile.WriteLine(segmentLine);
                        }

                        fileAddress = (int)tmAddr & 0xFFFF;
                        int tmBytesPerWord = Pk2.DevFile.Families[Pk2.GetActiveFamily()].ProgMemHexBytes;
                        arrayIncrement = 16 / tmBytesPerWord;     // # array words per hex line.
                        do
                        {
                            string hexLine = string.Format(":10{0:X4}00", fileAddress);
                            for (int i = 0; i < arrayIncrement; i++)
                            {
                                // convert entire array word to hex string of 4 bytes.
                                string hexWord = string.Format("{0:X8}", FormTestMemory.TestMemory[arrayIndex + i]);
                                for (int j = 0; j < tmBytesPerWord; j++)
                                {
                                    hexLine += hexWord.Substring((6 - 2 * j), 2);
                                }
                            }
                            hexLine += string.Format("{0:X2}", computeChecksum(hexLine));
                            if ((fileAddress != ((int)tmAddr & 0xFFFF)) || (Pk2.GetActiveFamily() != 3))
                            { // skip User ID line on PIC18F
                                hexFile.WriteLine(hexLine);
                            }

                            fileAddress += 16;
                            arrayIndex  += arrayIncrement;
                        } while (arrayIndex < FormPICkit2.TestMemoryWords);
                    }
                }
            }
            //end of record line.
            hexFile.WriteLine(":00000001FF");
            hexFile.Close();
            return(true);
        }