// ###################### VPP TEST ########################## private void testVPP_Enter() { Pk2.VddOff(); byte[] pinscript = new byte[2]; pinscript[0] = KONST._SET_ICSP_PINS; pinscript[1] = 0x03; // set both pins to inputs. Pk2.SendScript(pinscript); timerPGxToggle.Enabled = false; buttonCancel.Text = "Cancel"; if (Pk2.DevFile.Families[Pk2.GetActiveFamily()].Vpp < 1) { labelStep2FamilyVPP.Text = "1) VPP for this family: " + string.Format("{0:0.0}V (=VDD)", numericUpDown1.Value); } else { labelStep2FamilyVPP.Text = "1) VPP for this family: " + string.Format("{0:0.0} Volts.", Pk2.DevFile.Families[Pk2.GetActiveFamily()].Vpp); } labelReadVPP.Text = ""; labelVPPLow.Visible = false; labelVPPMCLR.Visible = false; labelVPPMCLROff.Visible = false; labelVPPPass.Visible = false; labelVPPShort.Visible = false; labelVPPVDDShort.Visible = false; }
public void ClearTestMemory() { for (int i = 0; i < TestMemory.Length; i++) { TestMemory[i] = Pk2.DevFile.Families[Pk2.GetActiveFamily()].BlankValue; } }
public void UpdateMultiWinMem() { if ((lastPart != Pk2.ActivePart) || (lastFam != Pk2.GetActiveFamily())) { ReCalcMultiWinMem(); } else { updateDisplay(); } }
private int getTestMemAddress() { if (Pk2.GetActiveFamily() == 2) { // baseline return((int)Pk2.DevFile.PartsList[Pk2.ActivePart].ProgramMem); } else { return((int)(Pk2.DevFile.Families[Pk2.GetActiveFamily()].TestMemoryStart / Pk2.DevFile.Families[Pk2.GetActiveFamily()].ProgMemHexBytes)); } }
private void dataGridTestMemory_CellEndEdit(object sender, DataGridViewCellEventArgs e) { int row = e.RowIndex; int col = e.ColumnIndex; string editText = "0x" + dataGridTestMemory[col, row].FormattedValue.ToString(); int value = UTIL.Convert_Value_To_Int(editText); int numColumns = dataGridTestMemory.ColumnCount - 1; int testMemAddress = (row * numColumns) + col - 1; int testMemStart = getTestMemAddress() * Pk2.DevFile.Families[Pk2.GetActiveFamily()].ProgMemHexBytes; int userIDAddr = (int)Pk2.DevFile.PartsList[Pk2.ActivePart].UserIDAddr; if (Pk2.GetActiveFamily() == 2) { // baseline userIDAddr = testMemStart; } TestMemory[testMemAddress] = (uint)(value & Pk2.DevFile.Families[Pk2.GetActiveFamily()].BlankValue); if ((Pk2.DevFile.PartsList[Pk2.ActivePart].UserIDWords > 0) && (userIDAddr >= testMemStart) && (userIDAddr < (testMemStart + FormPICkit2.TestMemoryWords))) { // if user IDs are in test memory int userIDIndex = (int)(userIDAddr - testMemStart) / Pk2.DevFile.Families[Pk2.GetActiveFamily()].BytesPerLocation; if ((testMemAddress >= userIDIndex) && (testMemAddress < (userIDIndex + Pk2.DevFile.PartsList[Pk2.ActivePart].UserIDWords))) { Pk2.DeviceBuffers.UserIDs[testMemAddress - userIDIndex] = (uint)(value & Pk2.DevFile.Families[Pk2.GetActiveFamily()].BlankValue); if (Pk2.GetActiveFamily() == 3) { // 18F Pk2.DeviceBuffers.UserIDs[testMemAddress - userIDIndex] &= 0xFF; } } } if ((Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigWords > 0) && (Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigAddr >= testMemStart) && (Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigAddr < (testMemStart + FormPICkit2.TestMemoryWords))) { // if config words are in test memory int cfgWordsIndex = (int)(Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigAddr - testMemStart) / Pk2.DevFile.Families[Pk2.GetActiveFamily()].BytesPerLocation; if ((testMemAddress >= cfgWordsIndex) && (testMemAddress < (cfgWordsIndex + Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigWords))) { Pk2.DeviceBuffers.ConfigWords[testMemAddress - cfgWordsIndex] = (uint)(value & Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigMasks[testMemAddress - cfgWordsIndex]); } } testMemJustEdited = true; UpdateMainFormGUI(); }
private void textBox1_15_Click(object sender, EventArgs e) { int boxNum = (int)((TextBox)sender).Tag; int configNum = boxNum / 16; boxNum = boxNum % 16; uint bitMask = 1; bitMask <<= boxNum; //update box value if (configWords[configNum].bits[boxNum].Text == "1") {// change to 0 configWords[configNum].bits[boxNum].Text = "0"; Pk2.DeviceBuffers.ConfigWords[configNum] &= ~bitMask; } else { // change to 1 configWords[configNum].bits[boxNum].Text = "1"; Pk2.DeviceBuffers.ConfigWords[configNum] |= bitMask; } if (configWords[configNum].bits[boxNum].ForeColor == Color.Crimson) // red means edited { configWords[configNum].bits[boxNum].ForeColor = System.Drawing.SystemColors.WindowText; } else { configWords[configNum].bits[boxNum].ForeColor = Color.Crimson; } // update value column ushort cword = (ushort)Pk2.DeviceBuffers.ConfigWords[configNum]; if (displayMask == 0) { cword &= (ushort)Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigMasks[configNum]; } else if (displayMask == 1) { cword |= (ushort)~Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigMasks[configNum]; } cword &= (ushort)Pk2.DevFile.Families[Pk2.GetActiveFamily()].BlankValue; configWords[configNum].value.Text = string.Format("{0:X4}", cword); configWords[configNum].value.ForeColor = System.Drawing.SystemColors.ActiveCaption; // default blue for (int b = 0; b < 16; b++) // look for any edited bits in the word { if (configWords[configNum].bits[b].ForeColor == Color.Crimson) { configWords[configNum].value.ForeColor = Color.Crimson; // red means edited break; } } }
public void UpdateMultiWinProgMem(string dataSource) { if ((lastPart != Pk2.ActivePart) || (lastFam != Pk2.GetActiveFamily())) { lastPart = Pk2.ActivePart; lastFam = Pk2.GetActiveFamily(); ReCalcMultiWinProgMem(); } else { updateDisplay(); } displayDataSource.Text = dataSource; }
public void ReadTestMemory() { byte[] upload_buffer = new byte[KONST.UploadBufferSize]; Pk2.RunScript(KONST.PROG_ENTRY, 1); int bytesPerWord = Pk2.DevFile.Families[Pk2.GetActiveFamily()].BytesPerLocation; int scriptRunsToFillUpload = KONST.UploadBufferSize / (Pk2.DevFile.PartsList[Pk2.ActivePart].TestMemoryRdWords * bytesPerWord); int wordsPerLoop = scriptRunsToFillUpload * Pk2.DevFile.PartsList[Pk2.ActivePart].TestMemoryRdWords; int wordsRead = 0; prepTestMem(); do { //Pk2.RunScriptUploadNoLen2(KONST.TESTMEM_RD, scriptRunsToFillUpload); Pk2.RunScriptUploadNoLen(KONST.TESTMEM_RD, scriptRunsToFillUpload); Array.Copy(Pk2.Usb_read_array, 1, upload_buffer, 0, KONST.USB_REPORTLENGTH); //Pk2.GetUpload(); Pk2.UploadDataNoLen(); Array.Copy(Pk2.Usb_read_array, 1, upload_buffer, KONST.USB_REPORTLENGTH, KONST.USB_REPORTLENGTH); int uploadIndex = 0; for (int word = 0; word < wordsPerLoop; word++) { int bite = 0; uint memWord = (uint)upload_buffer[uploadIndex + bite++]; if (bite < bytesPerWord) { memWord |= (uint)upload_buffer[uploadIndex + bite++] << 8; } if (bite < bytesPerWord) { memWord |= (uint)upload_buffer[uploadIndex + bite++] << 16; } if (bite < bytesPerWord) { memWord |= (uint)upload_buffer[uploadIndex + bite++] << 24; } uploadIndex += bite; // shift if necessary if (Pk2.DevFile.Families[Pk2.GetActiveFamily()].ProgMemShift > 0) { memWord = (memWord >> 1) & Pk2.DevFile.Families[Pk2.GetActiveFamily()].BlankValue; } TestMemory[wordsRead++] = memWord; } } while (wordsRead < FormPICkit2.TestMemoryWords); Pk2.RunScript(KONST.PROG_EXIT, 1); }
private void buttonWriteCalWords_Click(object sender, EventArgs e) { uint[] cals = new uint[Pk2.DevFile.PartsList[Pk2.ActivePart].CalibrationWords]; int testMemStart = getTestMemAddress() * Pk2.DevFile.Families[Pk2.GetActiveFamily()].ProgMemHexBytes; int cfgWordsIndex = (int)(Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigAddr - testMemStart) / Pk2.DevFile.Families[Pk2.GetActiveFamily()].BytesPerLocation; int calWordsIndex = cfgWordsIndex + Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigWords; for (int i = 0; i < cals.Length; i++) { cals[i] = TestMemory[calWordsIndex + i]; } CallMainFormEraseWrCal(cals); }
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(); }
private int getTestMemAddress() { if (Pk2.DevFile.Families[Pk2.GetActiveFamily()].TestMemoryStart == 0x800) { // baseline int tMemAddr = (int)Pk2.DevFile.PartsList[Pk2.ActivePart].ProgramMem; if (Pk2.DevFile.PartsList[Pk2.ActivePart].EEMem > 0) { // baseline with dataflash // test mem starts after data flash tMemAddr += Pk2.DevFile.PartsList[Pk2.ActivePart].EEMem; } return(tMemAddr); } else { return((int)(Pk2.DevFile.Families[Pk2.GetActiveFamily()].TestMemoryStart / Pk2.DevFile.Families[Pk2.GetActiveFamily()].ProgMemHexBytes)); } }
public void UpdateTestMemForm() { // Test Memory Words textBoxTestMemSize.Text = FormPICkit2.TestMemoryWords.ToString(); if (Pk2.DevFile.Families[Pk2.GetActiveFamily()].TestMemoryStart > 0) { // baseline, midrange, PIC18F textBoxBaselineConfig.Enabled = true; textBoxTestMemSize.Enabled = true; checkBoxTestMemImportExport.Enabled = true; labelNotSupported.Visible = false; } else { textBoxBaselineConfig.Enabled = false; textBoxTestMemSize.Enabled = false; checkBoxTestMemImportExport.Enabled = false; dataGridTestMemory.Enabled = false; labelNotSupported.Visible = true; } }
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; } uint blankValue = 0xFF; if (Pk2.DevFile.Families[Pk2.GetActiveFamily()].BlankValue > 0xFFFF) { blankValue = 0xFFFF; // dsPIC } else if (Pk2.DevFile.Families[Pk2.GetActiveFamily()].BlankValue == 0xFFF) { blankValue = 0xFFF; // BL dataflash } Pk2.DeviceBuffers.EEPromMemory[((row * numColumns) + col - 1)] = (uint)(value & blankValue); TellMainFormProgMemEdited(); progMemJustEdited = true; TellMainFormUpdateGUI(); }
public static Constants.FileRead ImportBINFile(string filePath) { // for serial EEPROMS only try { FileInfo binDataFile = new FileInfo(filePath); LastWriteTime = binDataFile.LastWriteTime; FileStream binFile = File.OpenRead(filePath); int bytesPerWord = Pk2.DevFile.Families[Pk2.GetActiveFamily()].ProgMemHexBytes; int memLoc = 0; int bytePosition = 0; byte[] fileByte = new byte[1]; while (binFile.Read(fileByte, 0, 1) > 0) { if (memLoc >= Pk2.DevFile.PartsList[Pk2.ActivePart].ProgramMem) { return(Constants.FileRead.largemem); } uint memByte = 0xFFFFFF00 | (uint)fileByte[0]; for (int shift = 0; shift < bytePosition; shift++) { // shift byte into proper position memByte <<= 8; memByte |= 0xFF; // shift in ones. } Pk2.DeviceBuffers.ProgramMemory[memLoc] &= memByte; if (++bytePosition >= bytesPerWord) { memLoc++; bytePosition = 0; } } binFile.Close(); } catch { return(Constants.FileRead.failed); } return(Constants.FileRead.success); }
public static bool ExportBINFile(string filePath) { // for serial EEPROMS only try { FileStream binFile = File.Open(filePath, System.IO.FileMode.Create); int bytesPerWord = Pk2.DevFile.Families[Pk2.GetActiveFamily()].ProgMemHexBytes; for (int memLoc = 0; memLoc < Pk2.DevFile.PartsList[Pk2.ActivePart].ProgramMem; memLoc++) { for (int byteNum = 0; byteNum < bytesPerWord; byteNum++) { byte outputByte = (byte)((Pk2.DeviceBuffers.ProgramMemory[memLoc] >> (8 * byteNum)) & 0xFF); binFile.WriteByte(outputByte); } } binFile.Close(); } catch { return(false); } return(true); }
private void updateDisplay() { int numRows = dataGridProgramMemory.RowCount - 1; int numCols = dataGridProgramMemory.ColumnCount - 1; int addressIncrement = (int)Pk2.DevFile.Families[Pk2.GetActiveFamily()].EEMemAddressIncrement; if (comboBoxProgMemView.SelectedIndex > 0) { // ascii view numCols /= 2; } for (int i = 0, idx = 0, address = 0; i < numRows; i++) { for (int j = 1; j <= numCols; j++) { dataGridProgramMemory[j, i].Value = string.Format(dataFormat, Pk2.DeviceBuffers.EEPromMemory[idx++]); dataGridProgramMemory[j, i].ToolTipText = string.Format(addrFormat, (address)); address += addressIncrement; } } int lastCol = (int)Pk2.DevFile.PartsList[Pk2.ActivePart].EEMem % numCols; if (lastCol == 0) { lastCol = numCols; } int rowidx = numRows * numCols; // fill last row for (int j = 1; j <= numCols; j++) { if (j <= lastCol) { dataGridProgramMemory[j, numRows].Value = string.Format(dataFormat, Pk2.DeviceBuffers.EEPromMemory[rowidx++]); dataGridProgramMemory[j, numRows].ToolTipText = string.Format(addrFormat, (rowidx * addressIncrement)); } else { dataGridProgramMemory[j, numRows].Value = ""; dataGridProgramMemory[j, numRows].ToolTipText = ""; dataGridProgramMemory[j, numRows].ReadOnly = true; } } if (comboBoxProgMemView.SelectedIndex > 0) { // ascii view for (int c = numCols + 1; c <= (2 * numCols); c++) { // ASCII not editable dataGridProgramMemory.Columns[c].ReadOnly = true; } if (comboBoxProgMemView.SelectedIndex == 1) { // WORD view for (int i = 0, idx = 0, address = 0; i < numRows; i++) { for (int j = numCols + 1; j <= (2 * numCols); j++) { dataGridProgramMemory[j, i].Value = UTIL.ConvertIntASCII((int)Pk2.DeviceBuffers.EEPromMemory[idx++], asciiBytes); dataGridProgramMemory[j, i].ToolTipText = string.Format(addrFormat, (address)); address += addressIncrement; } } // fill last row rowidx = numRows * numCols; for (int j = numCols + 1; j <= (2 * numCols); j++) { if (j <= (numCols + lastCol)) { dataGridProgramMemory[j, numRows].Value = UTIL.ConvertIntASCII((int)Pk2.DeviceBuffers.EEPromMemory[rowidx++], asciiBytes); dataGridProgramMemory[j, numRows].ToolTipText = string.Format(addrFormat, (rowidx * addressIncrement)); } else { dataGridProgramMemory[j, numRows].Value = ""; dataGridProgramMemory[j, numRows].ToolTipText = ""; dataGridProgramMemory[j, numRows].ReadOnly = true; } } } else { // BYTE view for (int i = 0, idx = 0, address = 0; i < numRows; i++) { for (int j = numCols + 1; j <= (2 * numCols); j++) { dataGridProgramMemory[j, i].Value = UTIL.ConvertIntASCIIReverse((int)Pk2.DeviceBuffers.EEPromMemory[idx++], asciiBytes); dataGridProgramMemory[j, i].ToolTipText = string.Format(addrFormat, (address)); address += addressIncrement; } } // fill last row rowidx = numRows * numCols; for (int j = numCols + 1; j <= (2 * numCols); j++) { if (j <= (numCols + lastCol)) { dataGridProgramMemory[j, numRows].Value = UTIL.ConvertIntASCIIReverse((int)Pk2.DeviceBuffers.EEPromMemory[rowidx++], asciiBytes); dataGridProgramMemory[j, numRows].ToolTipText = string.Format(addrFormat, (rowidx * addressIncrement)); } else { dataGridProgramMemory[j, numRows].Value = ""; dataGridProgramMemory[j, numRows].ToolTipText = ""; dataGridProgramMemory[j, numRows].ReadOnly = true; } } } } if ((dataGridProgramMemory.FirstDisplayedCell != null) && !progMemJustEdited) { int currentRow = dataGridProgramMemory.FirstDisplayedCell.RowIndex; dataGridProgramMemory.MultiSelect = false; dataGridProgramMemory[0, currentRow].Selected = true; // these 4 statements remove the "select" box dataGridProgramMemory[0, currentRow].Selected = false; dataGridProgramMemory.MultiSelect = true; } else if (dataGridProgramMemory.FirstDisplayedCell == null) { // remove select box when app first opened. dataGridProgramMemory.MultiSelect = false; dataGridProgramMemory[0, 0].Selected = true; // these 4 statements remove the "select" box dataGridProgramMemory[0, 0].Selected = false; dataGridProgramMemory.MultiSelect = true; } progMemJustEdited = false; }
private void buttonTestVPP_Click(object sender, EventArgs e) { float vdd = 0; float vpp = 0; labelVPPLow.Visible = false; labelVPPMCLR.Visible = false; labelVPPMCLROff.Visible = false; labelVPPPass.Visible = false; labelVPPShort.Visible = false; labelVPPVDDShort.Visible = false; labelReadVPP.Text = ""; Thread.Sleep(250); // sleep a bit to let VDD bleed down. // check for a powered target first if (Pk2.CheckTargetPower(ref vdd, ref vpp) == KONST.PICkit2PWR.selfpowered) { Pk2.VddOff(); } else { Pk2.SetVDDVoltage((float)numericUpDown1.Value, 0.85F); Pk2.VddOn(); } // Set VPP voltage float expectedVPP; if (Pk2.DevFile.Families[Pk2.GetActiveFamily()].Vpp > 1) { expectedVPP = Pk2.DevFile.Families[Pk2.GetActiveFamily()].Vpp; } else { expectedVPP = (float)numericUpDown1.Value; } Pk2.SetVppVoltage(expectedVPP, 0.50F); byte[] vppscript = new byte[8]; vppscript[0] = KONST._VPP_OFF; vppscript[1] = KONST._VPP_PWM_ON; vppscript[2] = KONST._DELAY_LONG; vppscript[3] = 30; vppscript[4] = KONST._MCLR_GND_OFF; vppscript[5] = KONST._VPP_ON; vppscript[6] = KONST._DELAY_LONG; vppscript[7] = 20; Pk2.SendScript(vppscript); // check status first for shorts KONST.PICkit2PWR status = Pk2.PowerStatus(); if ((status == KONST.PICkit2PWR.vdderror) || (status == Constants.PICkit2PWR.vddvpperrors)) { //VDD short! labelVPPVDDShort.Visible = true; } else if (status == KONST.PICkit2PWR.vpperror) {//VPP short labelVPPShort.Visible = true; labelReadVPP.Text = "Short!"; } else if (status != Constants.PICkit2PWR.no_response) { // status OK, read VPP voltage if (Pk2.ReadPICkitVoltages(ref vdd, ref vpp)) { labelReadVPP.Text = string.Format("{0:0.0} V", vpp); if ((expectedVPP - vpp) > 0.3F) { labelVPPLow.Visible = true; } else { labelVPPPass.Visible = true; } } } }
private void prepTestMem() { // Send pre-requisite scripts if (Pk2.GetActiveFamily() == 0) { // midrange = Load Config command byte[] prepscript = new byte[7]; prepscript[0] = KONST._WRITE_BITS_LITERAL; prepscript[1] = 0x06; prepscript[2] = 0x00; prepscript[3] = KONST._WRITE_BYTE_LITERAL; prepscript[4] = 0; prepscript[5] = KONST._WRITE_BYTE_LITERAL; prepscript[6] = 0; Pk2.SendScript(prepscript); } else if (Pk2.GetActiveFamily() == 2) { // baseline int tmStart = (getTestMemAddress() / 4) - 1; byte[] prepscript = new byte[18]; prepscript[0] = KONST._CONST_WRITE_DL; // put # loops in buffer for LOOPBUFFER prepscript[1] = (byte)(tmStart & 0xFF); prepscript[2] = KONST._CONST_WRITE_DL; prepscript[3] = (byte)((tmStart >> 8) & 0xFF); prepscript[4] = KONST._WRITE_BITS_LITERAL; prepscript[5] = 0x06; prepscript[6] = 0x06; prepscript[7] = KONST._WRITE_BITS_LITERAL; prepscript[8] = 0x06; prepscript[9] = 0x06; prepscript[10] = KONST._WRITE_BITS_LITERAL; prepscript[11] = 0x06; prepscript[12] = 0x06; prepscript[13] = KONST._WRITE_BITS_LITERAL; prepscript[14] = 0x06; prepscript[15] = 0x06; prepscript[16] = KONST._LOOPBUFFER; prepscript[17] = 12; Pk2.SendScript(prepscript); } else if (Pk2.GetActiveFamily() == 3) { // PIC18F - Set address pointer to 0x200000 byte[] prepscript = new byte[18]; prepscript[0] = KONST._COREINST18; prepscript[1] = 0x20; prepscript[2] = 0x0E; prepscript[3] = KONST._COREINST18; prepscript[4] = 0xF8; prepscript[5] = 0x6E; prepscript[6] = KONST._COREINST18; prepscript[7] = 0x00; prepscript[8] = 0x0E; prepscript[9] = KONST._COREINST18; prepscript[10] = 0xF7; prepscript[11] = 0x6E; prepscript[12] = KONST._COREINST18; prepscript[13] = 0x00; prepscript[14] = 0x0E; prepscript[15] = KONST._COREINST18; prepscript[16] = 0xF6; prepscript[17] = 0x6E; Pk2.SendScript(prepscript); } }
public void ReCalcMultiWinProgMem() { // call on 1) window init, 2) window resize, 3) new part, 4) Change view combo box uint memSize = Pk2.DevFile.PartsList[Pk2.ActivePart].ProgramMem; if (memSize == 0) { return; } if (this.WindowState == FormWindowState.Minimized) { return; } // first, calculate the address column size uint blankValue = Pk2.DevFile.Families[Pk2.GetActiveFamily()].BlankValue; // ASCII display disabled for PIC32 if (blankValue == 0xFFFFFFFF) { comboBoxProgMemView.SelectedIndex = 0; comboBoxProgMemView.Enabled = false; } else { comboBoxProgMemView.Enabled = true; } uint maxAddr = (memSize * Pk2.DevFile.Families[Pk2.GetActiveFamily()].AddressIncrement) - 1; int addrColWidth = 32; // FFF max addrFormat = "{0:X3}"; if (blankValue == 0xFFFFFFFF) {// PIC32 addrColWidth = 65; addrFormat = "{0:X8}"; } else if (maxAddr > 0xFFFF) { addrColWidth = 44; // FFFFF max addrFormat = "{0:X5}"; } else if (maxAddr > 0xFFF) { addrColWidth = 38; // FFFF max addrFormat = "{0:X4}"; } addrColWidth = (int)(addrColWidth * FormPICkit2.ScalefactW); // adjust for DPI // Figure data column size minimum int dataColWidth = 24; int asciiColWidth = 16; asciiBytes = 1; dataFormat = "{0:X2}"; if (blankValue == 0xFFFFFFFF) { // PIC32 dataColWidth = 65; asciiColWidth = 58; asciiBytes = 4; dataFormat = "{0:X8}"; } else if (blankValue > 0xFFFF) { // FFFFFF dataColWidth = 50; asciiColWidth = 43; asciiBytes = 3; dataFormat = "{0:X6}"; } else if (blankValue > 0xFFF) { // FFFF or 3FFF dataColWidth = 36; asciiColWidth = 28; asciiBytes = 2; dataFormat = "{0:X4}"; } else if (blankValue > 0xFF) { // FFF dataColWidth = 28; asciiColWidth = 28; asciiBytes = 2; dataFormat = "{0:X3}"; } float colRatio = 1; if (comboBoxProgMemView.SelectedIndex > 0) { // ascii view colRatio = (float)asciiColWidth / ((float)asciiColWidth + (float)dataColWidth); dataColWidth += asciiColWidth; } dataColWidth = (int)(dataColWidth * FormPICkit2.ScalefactW); // adjust for DPI // calculate the number of data columns that will fit (must be mult of 2) // space left after address column and fudge factor for scroll bar: int dataSpace = dataGridProgramMemory.Size.Width - addrColWidth - (int)(20 * FormPICkit2.ScalefactW); int maxCols = dataSpace / dataColWidth; for (int mult = 1; mult <= 256; mult *= 2) { if (mult > maxCols) { maxCols = mult / 2; break; } } if (maxCols > (int)memSize) { maxCols = (int)memSize; // no point in having more columns than memory locations! } // get the real width of the final column count dataColWidth = dataSpace / maxCols; if (comboBoxProgMemView.SelectedIndex > 0) { // ascii view asciiColWidth = (int)(colRatio * (float)dataColWidth); dataColWidth -= asciiColWidth; } // format the display box dataGridProgramMemory.Rows.Clear(); if (comboBoxProgMemView.SelectedIndex > 0) { // ascii view dataGridProgramMemory.ColumnCount = (2 * maxCols) + 1; // add in address column } else { // hex only dataGridProgramMemory.ColumnCount = maxCols + 1; // add in address column } // addr col dataGridProgramMemory.Columns[0].Width = addrColWidth; // data cols for (int i = 1; i <= maxCols; i++) { dataGridProgramMemory.Columns[i].Width = dataColWidth; } if (comboBoxProgMemView.SelectedIndex > 0) { // ascii view for (int i = maxCols + 1; i <= (2 * maxCols); i++) { dataGridProgramMemory.Columns[i].Width = asciiColWidth; } } // determine row count int numRows = (int)memSize / maxCols; if ((memSize % maxCols) > 0) { // mem size not an even divider, need an extra row numRows++; } if (blankValue == 0xFFFFFFFF) { numRows += 2; } dataGridProgramMemory.RowCount = (int)numRows; // fill address column int rowAddrIncrement = maxCols * (int)Pk2.DevFile.Families[Pk2.GetActiveFamily()].AddressIncrement; if (blankValue == 0xFFFFFFFF) {// PIC32 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. progMemP32 /= maxCols; dataGridProgramMemory.ShowCellToolTips = false; // Program Flash addresses dataGridProgramMemory[0, 0].Value = "Program"; dataGridProgramMemory[1, 0].Value = "Flash"; for (int col = 0; col < dataGridProgramMemory.ColumnCount; col++) { dataGridProgramMemory[col, 0].Style.BackColor = System.Drawing.SystemColors.ControlDark; dataGridProgramMemory[col, 0].ReadOnly = true; } for (int row = 1, address = (int)KONST.P32_PROGRAM_FLASH_START_ADDR; row <= progMemP32; row++) { dataGridProgramMemory[0, row].Value = string.Format(addrFormat, address); dataGridProgramMemory[0, row].Style.BackColor = System.Drawing.SystemColors.ControlLight; address += rowAddrIncrement; } // Boot Flash addresses dataGridProgramMemory[0, progMemP32 + 1].Value = "Boot"; dataGridProgramMemory[1, progMemP32 + 1].Value = "Flash"; for (int col = 0; col < dataGridProgramMemory.ColumnCount; col++) { dataGridProgramMemory[col, progMemP32 + 1].Style.BackColor = System.Drawing.SystemColors.ControlDark; dataGridProgramMemory[col, progMemP32 + 1].ReadOnly = true; } for (int row = progMemP32 + 2, address = (int)KONST.P32_BOOT_FLASH_START_ADDR; row < dataGridProgramMemory.RowCount; row++) { dataGridProgramMemory[0, row].Value = string.Format(addrFormat, address); dataGridProgramMemory[0, row].Style.BackColor = System.Drawing.SystemColors.ControlLight; address += rowAddrIncrement; } } else { dataGridProgramMemory.ShowCellToolTips = true; for (int row = 0, addr = 0; row < numRows; row++) { dataGridProgramMemory[0, row].Value = string.Format(addrFormat, addr); dataGridProgramMemory[0, row].ReadOnly = true; dataGridProgramMemory[0, row].Style.BackColor = System.Drawing.SystemColors.ControlLight; addr += rowAddrIncrement; } } updateDisplay(); }
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; } } }
public void UpdateTestMemoryGrid() { bool userIDInTestMem = false; int userIDIndex = 0; int testMemStart = getTestMemAddress() * Pk2.DevFile.Families[Pk2.GetActiveFamily()].ProgMemHexBytes; int userIDAddr = (int)Pk2.DevFile.PartsList[Pk2.ActivePart].UserIDAddr; // reset buffers if new family or part. if ((Pk2.ActivePart != lastPart) || (Pk2.GetActiveFamily() != lastFamily)) { ClearTestMemory(); lastPart = Pk2.ActivePart; lastFamily = Pk2.GetActiveFamily(); } if (Pk2.GetActiveFamily() == 2) { // baseline userIDAddr = testMemStart; } if ((Pk2.DevFile.PartsList[Pk2.ActivePart].UserIDWords > 0) && (userIDAddr >= testMemStart) && (userIDAddr < (testMemStart + FormPICkit2.TestMemoryWords))) { // if user IDs are in test memory userIDInTestMem = true; userIDIndex = (int)(userIDAddr - testMemStart) / Pk2.DevFile.Families[Pk2.GetActiveFamily()].BytesPerLocation; } bool cfgWordsInTestMem = false; int cfgWordsIndex = 0; if ((Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigWords > 0) && (Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigAddr >= testMemStart) && (Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigAddr < (testMemStart + FormPICkit2.TestMemoryWords))) { // if user IDs are in test memory cfgWordsInTestMem = true; cfgWordsIndex = (int)(Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigAddr - testMemStart) / Pk2.DevFile.Families[Pk2.GetActiveFamily()].BytesPerLocation; } // Update Test Memory display // Set address columns first // Set # columns based on blank memory size. int dataColumns, columnWidth, columnCount; columnCount = 9; dataGridTestMemory.Columns[0].Width = 51; // address column dataColumns = 8; columnWidth = 35; int startAddress = getTestMemAddress(); if (Pk2.GetActiveFamily() == 3) { startAddress *= 2; } if (dataGridTestMemory.ColumnCount != columnCount) { dataGridTestMemory.Rows.Clear(); dataGridTestMemory.ColumnCount = columnCount; } for (int column = 1; column < dataGridTestMemory.ColumnCount; column++) { dataGridTestMemory.Columns[column].Width = columnWidth; // data columns } int addressIncrement = (int)Pk2.DevFile.Families[Pk2.GetActiveFamily()].AddressIncrement; int rowAddressIncrement; int hexColumns, rowCount; // if (comboBoxProgMemView.SelectedIndex == 0) // hex view // { hexColumns = dataColumns; rowCount = (int)FormPICkit2.TestMemoryWords / hexColumns; rowAddressIncrement = addressIncrement * dataColumns; // } // else // { // hexColumns = dataColumns / 2; // rowCount = (int)TestMemoryWords / hexColumns; // rowAddressIncrement = addressIncrement * (dataColumns / 2); // } if (dataGridTestMemory.RowCount != rowCount) { dataGridTestMemory.Rows.Clear(); dataGridTestMemory.RowCount = rowCount; } int maxAddress = dataGridTestMemory.RowCount * rowAddressIncrement - 1; String addressFormat = "{0:X3}"; if (maxAddress > 0xFFFF) { addressFormat = "{0:X5}"; } else if (maxAddress > 0xFFF) { addressFormat = "{0:X4}"; } for (int row = 0, address = 0; row < dataGridTestMemory.RowCount; row++) { dataGridTestMemory[0, row].Value = string.Format(addressFormat, startAddress + address); dataGridTestMemory[0, row].Style.BackColor = System.Drawing.SystemColors.ControlLight; address += rowAddressIncrement; } // Now fill data string dataFormat = "{0:X3}"; //int asciiBytes = 2; if (Pk2.DevFile.Families[Pk2.GetActiveFamily()].BlankValue > 0xFFF) { dataFormat = "{0:X4}"; } if (Pk2.DevFile.Families[Pk2.GetActiveFamily()].BlankValue > 0xFFFF) { dataFormat = "{0:X6}"; // asciiBytes = 3; } for (int col = 0; col < hexColumns; col++) { // only allow UserIDs and Config words to be edited. dataGridTestMemory.Columns[col + 1].ReadOnly = true; } for (int row = 0, idx = 0; row < dataGridTestMemory.RowCount; row++) { for (int col = 0; col < hexColumns; col++) { if (userIDInTestMem && (idx >= userIDIndex) && (idx < (userIDIndex + Pk2.DevFile.PartsList[Pk2.ActivePart].UserIDWords))) { TestMemory[idx] = Pk2.DeviceBuffers.UserIDs[idx - userIDIndex]; dataGridTestMemory[col + 1, row].ToolTipText = string.Format(addressFormat, startAddress + (idx * addressIncrement)); dataGridTestMemory[col + 1, row].Value = string.Format(dataFormat, Pk2.DeviceBuffers.UserIDs[idx++ - userIDIndex]); dataGridTestMemory[col + 1, row].Style.BackColor = Color.LightSteelBlue; dataGridTestMemory[col + 1, row].ReadOnly = false; } else if (cfgWordsInTestMem && (idx >= cfgWordsIndex) && (idx < (cfgWordsIndex + Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigWords))) { TestMemory[idx] = Pk2.DeviceBuffers.ConfigWords[idx - cfgWordsIndex]; dataGridTestMemory[col + 1, row].ToolTipText = string.Format(addressFormat, startAddress + (idx * addressIncrement)); dataGridTestMemory[col + 1, row].Value = string.Format(dataFormat, Pk2.DeviceBuffers.ConfigWords[idx++ - cfgWordsIndex]); dataGridTestMemory[col + 1, row].Style.BackColor = Color.LightSalmon; dataGridTestMemory[col + 1, row].ReadOnly = false; } else if ((Pk2.DevFile.PartsList[Pk2.ActivePart].CalibrationWords > 0) && (idx >= (cfgWordsIndex + Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigWords)) && (idx < (cfgWordsIndex + Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigWords + Pk2.DevFile.PartsList[Pk2.ActivePart].CalibrationWords))) { dataGridTestMemory[col + 1, row].ToolTipText = string.Format(addressFormat, startAddress + (idx * addressIncrement)); dataGridTestMemory[col + 1, row].Value = string.Format(dataFormat, TestMemory[idx++]); dataGridTestMemory[col + 1, row].Style.BackColor = Color.Gold; dataGridTestMemory[col + 1, row].ReadOnly = false; } else { dataGridTestMemory[col + 1, row].ToolTipText = string.Format(addressFormat, startAddress + (idx * addressIncrement)); dataGridTestMemory[col + 1, row].Value = string.Format(dataFormat, TestMemory[idx++]); dataGridTestMemory[col + 1, row].Style.BackColor = System.Drawing.SystemColors.Window; } } } // Fill ASCII if selected /* if (comboBoxProgMemView.SelectedIndex == 1) * { * for (int col = 0; col < hexColumns; col++) * { // ascii not editable * dataGridTestMemory.Columns[col + hexColumns + 1].ReadOnly = true; * } * for (int row = 0, idx = 0; row < dataGridTestMemory.RowCount; row++) * { * for (int col = 0; col < hexColumns; col++) * { * dataGridTestMemory[col + hexColumns + 1, row].ToolTipText = * string.Format(addressFormat, (idx * addressIncrement)); * dataGridTestMemory[col + hexColumns + 1, row].Value = * UTIL.ConvertIntASCII((int)testMemory[idx++], asciiBytes); * } * * } * } */ if ((dataGridTestMemory.FirstDisplayedCell != null) && !testMemJustEdited) { //currentCol = dataGridProgramMemory.FirstDisplayedCell.ColumnIndex; int currentRow = dataGridTestMemory.FirstDisplayedCell.RowIndex; dataGridTestMemory[0, currentRow].Selected = true; // these 2 statements remove the "select" box dataGridTestMemory[0, currentRow].Selected = false; } else if (dataGridTestMemory.FirstDisplayedCell == null) { // remove select box when app first opened. dataGridTestMemory[0, 0].Selected = true; // these 2 statements remove the "select" box dataGridTestMemory[0, 0].Selected = false; } testMemJustEdited = false; if (Pk2.GetActiveFamily() == 2) { // baseline labelBLConfig.Visible = true; textBoxBaselineConfig.Visible = true; textBoxBaselineConfig.Text = string.Format("{0:X4}", Pk2.DeviceBuffers.ConfigWords[0]); } else { labelBLConfig.Visible = false; textBoxBaselineConfig.Visible = false; } if (Pk2.GetActiveFamily() == 0) { // midrange labelCalWarning.Visible = true; buttonWriteCalWords.Visible = true; buttonWriteCalWords.Enabled = (Pk2.DevFile.PartsList[Pk2.ActivePart].CalibrationWords > 0); } else { labelCalWarning.Visible = false; buttonWriteCalWords.Visible = false; } }
public static bool ExportHexFile(string filePath, bool progMem, bool eeMem) { StreamWriter hexFile = new StreamWriter(filePath); // Start with segment zero hexFile.WriteLine(":020000040000FA"); // Program Memory ---------------------------------------------------------------------------- int fileSegment = 0; int fileAddress = 0; 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 = 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 < Pk2.DeviceBuffers.ProgramMemory.Length); } // 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 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 * bytesPerWord - 1) / 16) + 1); lines++) { int cfgsLeft = configWords - cfgsWritten; if (cfgsLeft >= (16 / bytesPerWord)) { cfgsLeft = (16 / bytesPerWord); } string hexLine = string.Format(":{0:X2}{1:X4}00", (cfgsLeft * bytesPerWord), fileAddress); fileAddress += (cfgsLeft * bytesPerWord); for (int i = 0; i < cfgsLeft; i++) { // convert entire array word to hex string of 4 bytes. string hexWord = string.Format("{0:X8}", Pk2.DeviceBuffers.ConfigWords[cfgsWritten + i]); for (int j = 0; j < bytesPerWord; 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); }
public void ReCalcMultiWinMem() { // call on 1) window init, 2) window resize, 3) new part, 4) Change view combo box uint memSize = Pk2.DevFile.PartsList[Pk2.ActivePart].EEMem; if (memSize == 0) { return; } if (this.WindowState == FormWindowState.Minimized) { return; } // first, calculate the address column size uint maxAddr = (memSize * Pk2.DevFile.Families[Pk2.GetActiveFamily()].EEMemAddressIncrement) - 1; int addrColWidth = 30; // FF max addrFormat = "{0:X2}"; if (maxAddr > 0xFFF) { addrColWidth = 40; addrFormat = "{0:X4}"; } else if (maxAddr > 0xFF) { addrColWidth = 32; // FFF max addrFormat = "{0:X3}"; } addrColWidth = (int)(addrColWidth * FormPICkit2.ScalefactW); // adjust for DPI // Figure data column size minimum uint blankValue = Pk2.DevFile.Families[Pk2.GetActiveFamily()].BlankValue; // col width min for PIC32 is 62 int dataColWidth = 24; //FF int asciiColWidth = 16; asciiBytes = 1; dataFormat = "{0:X2}"; if (blankValue > 0xFFFF) { // FFFF dataColWidth = 36; asciiColWidth = 28; asciiBytes = 2; dataFormat = "{0:X4}"; } else if (blankValue == 0xFFF) { // FFF baseline dataflash dataColWidth = 28; asciiColWidth = 28; asciiBytes = 2; dataFormat = "{0:X3}"; } float colRatio = 1; if (comboBoxProgMemView.SelectedIndex > 0) { // ascii view colRatio = (float)asciiColWidth / ((float)asciiColWidth + (float)dataColWidth); dataColWidth += asciiColWidth; } dataColWidth = (int)(dataColWidth * FormPICkit2.ScalefactW); // adjust for DPI // calculate the number of data columns that will fit (must be mult of 2) // space left after address column and fudge factor for scroll bar: int dataSpace = dataGridProgramMemory.Size.Width - addrColWidth - (int)(20 * FormPICkit2.ScalefactW); int maxCols = dataSpace / dataColWidth; for (int mult = 1; mult <= 256; mult *= 2) { if (mult > maxCols) { maxCols = mult / 2; break; } } if (maxCols > (int)memSize) { maxCols = (int)memSize; // no point in having more columns than memory locations! } // get the real width of the final column count dataColWidth = dataSpace / maxCols; if (comboBoxProgMemView.SelectedIndex > 0) { // ascii view asciiColWidth = (int)(colRatio * (float)dataColWidth); dataColWidth -= asciiColWidth; } // clear selection if ((dataGridProgramMemory.FirstDisplayedCell != null) && !progMemJustEdited) { int currentRow = dataGridProgramMemory.FirstDisplayedCell.RowIndex; dataGridProgramMemory.MultiSelect = false; dataGridProgramMemory[0, currentRow].Selected = true; // these 4 statements remove the "select" box dataGridProgramMemory[0, currentRow].Selected = false; dataGridProgramMemory.MultiSelect = true; } else if ((dataGridProgramMemory.FirstDisplayedCell == null) && (dataGridProgramMemory.RowCount > 0)) { // remove select box when app first opened. dataGridProgramMemory.MultiSelect = false; dataGridProgramMemory[0, 0].Selected = true; // these 4 statements remove the "select" box dataGridProgramMemory[0, 0].Selected = false; dataGridProgramMemory.MultiSelect = true; } progMemJustEdited = false; // format the display box dataGridProgramMemory.Rows.Clear(); if (comboBoxProgMemView.SelectedIndex > 0) { // ascii view dataGridProgramMemory.ColumnCount = (2 * maxCols) + 1; // add in address column } else { // hex only dataGridProgramMemory.ColumnCount = maxCols + 1; // add in address column } // addr col dataGridProgramMemory.Columns[0].Width = addrColWidth; // data cols for (int i = 1; i <= maxCols; i++) { dataGridProgramMemory.Columns[i].Width = dataColWidth; } if (comboBoxProgMemView.SelectedIndex > 0) { // ascii view for (int i = maxCols + 1; i <= (2 * maxCols); i++) { dataGridProgramMemory.Columns[i].Width = asciiColWidth; } } // determine row count int numRows = (int)memSize / maxCols; if ((memSize % maxCols) > 0) { // mem size not an even divider, need an extra row numRows++; } dataGridProgramMemory.RowCount = (int)numRows; // fill address column int rowAddrIncrement = maxCols * (int)Pk2.DevFile.Families[Pk2.GetActiveFamily()].EEMemAddressIncrement; for (int row = 0, addr = 0; row < numRows; row++) { dataGridProgramMemory[0, row].Value = string.Format(addrFormat, addr); dataGridProgramMemory[0, row].ReadOnly = true; dataGridProgramMemory[0, row].Style.BackColor = System.Drawing.SystemColors.ControlLight; addr += rowAddrIncrement; } updateDisplay(); }
public static Constants.FileRead ImportHexFile(String filePath, bool progMem, bool eeMem) { // NOTE: The device buffers being read into must all be set to blank value before getting here! try { FileInfo hexFile = new FileInfo(filePath); LastWriteTime = hexFile.LastWriteTime; TextReader hexRead = hexFile.OpenText(); int bytesPerWord = Pk2.DevFile.Families[Pk2.GetActiveFamily()].ProgMemHexBytes; int eeMemBytes = Pk2.DevFile.Families[Pk2.GetActiveFamily()].EEMemHexBytes; uint eeAddr = Pk2.DevFile.PartsList[Pk2.ActivePart].EEAddr; int progMemSizeBytes = (int)Pk2.DevFile.PartsList[Pk2.ActivePart].ProgramMem * bytesPerWord; int segmentAddress = 0; bool configRead = false; bool lineExceedsFlash = true; bool fileExceedsFlash = false; int userIDs = Pk2.DevFile.PartsList[Pk2.ActivePart].UserIDWords; uint userIDAddr = Pk2.DevFile.PartsList[Pk2.ActivePart].UserIDAddr; if (userIDAddr == 0) { userIDAddr = 0xFFFFFFFF; } int userIDMemBytes = Pk2.DevFile.Families[Pk2.GetActiveFamily()].UserIDHexBytes; // need to set config words to memory blank. int configWords = Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigWords; for (int cw = 0; cw < configWords; cw++) { Pk2.DeviceBuffers.ConfigWords[cw] = Pk2.DevFile.Families[Pk2.GetActiveFamily()].BlankValue; } string fileLine = hexRead.ReadLine(); while (fileLine != null) { if ((fileLine[0] == ':') && (fileLine.Length >= 11)) { // skip line if not hex line entry,or not minimum length ":BBAAAATTCC" int byteCount = Int32.Parse(fileLine.Substring(1, 2), System.Globalization.NumberStyles.HexNumber); int fileAddress = segmentAddress + Int32.Parse(fileLine.Substring(3, 4), System.Globalization.NumberStyles.HexNumber); int recordType = Int32.Parse(fileLine.Substring(7, 2), System.Globalization.NumberStyles.HexNumber); if (recordType == 0) { // Data Record} if (fileLine.Length >= (11 + (2 * byteCount))) { // skip if line isn't long enough for bytecount. for (int lineByte = 0; lineByte < byteCount; lineByte++) { int byteAddress = fileAddress + lineByte; // compute array address from hex file address # bytes per memory location int arrayAddress = byteAddress / bytesPerWord; // compute byte position withing memory word int bytePosition = byteAddress % bytesPerWord; // get the byte value from hex file uint wordByte = 0xFFFFFF00 | UInt32.Parse(fileLine.Substring((9 + (2 * lineByte)), 2), System.Globalization.NumberStyles.HexNumber); // shift the byte into its proper position in the word. for (int shift = 0; shift < bytePosition; shift++) { // shift byte into proper position wordByte <<= 8; wordByte |= 0xFF; // shift in ones. } lineExceedsFlash = true; // if not in any memory section, then error // program memory section -------------------------------------------------- if (byteAddress < progMemSizeBytes) { if (progMem) { // if importing program memory Pk2.DeviceBuffers.ProgramMemory[arrayAddress] &= wordByte; // add byte. } lineExceedsFlash = false; //NOTE: program memory locations containing config words may get modified // by the config section below that applies the config masks. } // EE data section --------------------------------------------------------- if ((byteAddress >= eeAddr) && (eeAddr > 0) && (Pk2.DevFile.PartsList[Pk2.ActivePart].EEMem > 0)) { int eeAddress = (int)(byteAddress - eeAddr) / eeMemBytes; if (eeAddress < Pk2.DevFile.PartsList[Pk2.ActivePart].EEMem) { lineExceedsFlash = false; if (eeMem) { // skip if not importing EE Memory if (eeMemBytes == bytesPerWord) { // same # hex bytes per EE location as ProgMem location Pk2.DeviceBuffers.EEPromMemory[eeAddress] &= wordByte; // add byte. } else { // PIC18F/J int eeshift = (bytePosition / eeMemBytes) * eeMemBytes; for (int reshift = 0; reshift < eeshift; reshift++) { // shift byte into proper position wordByte >>= 8; } Pk2.DeviceBuffers.EEPromMemory[eeAddress] &= wordByte; // add byte. } } } } // Some 18F parts without EEPROM have hex files created with blank EEPROM by MPLAB else if ((byteAddress >= eeAddr) && (eeAddr > 0) && (Pk2.DevFile.PartsList[Pk2.ActivePart].EEMem == 0)) { lineExceedsFlash = false; // don't give too-large file error. } // Config words section ---------------------------------------------------- if ((byteAddress >= Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigAddr) && (configWords > 0)) { int configNum = (byteAddress - ((int)Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigAddr)) / bytesPerWord; if (configNum < Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigWords) { lineExceedsFlash = false; configRead = true; if (progMem) { // if importing program memory Pk2.DeviceBuffers.ConfigWords[configNum] &= (wordByte & Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigMasks[configNum]); if (byteAddress < progMemSizeBytes) { // also mask off the word if in program memory. Pk2.DeviceBuffers.ProgramMemory[arrayAddress] &= (wordByte & Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigMasks[configNum]); // add byte. } } } } // User IDs section --------------------------------------------------------- if (userIDs > 0) { if (byteAddress >= userIDAddr) { int uIDAddress = (int)(byteAddress - userIDAddr) / userIDMemBytes; if (uIDAddress < userIDs) { lineExceedsFlash = false; if (progMem) { // if importing program memory if (userIDMemBytes == bytesPerWord) { // same # hex bytes per EE location as ProgMem location Pk2.DeviceBuffers.UserIDs[uIDAddress] &= wordByte; // add byte. } else { // PIC18F/J, PIC24H/dsPIC33 int uIDshift = (bytePosition / userIDMemBytes) * userIDMemBytes; for (int reshift = 0; reshift < uIDshift; reshift++) { // shift byte into proper position wordByte >>= 8; } Pk2.DeviceBuffers.UserIDs[uIDAddress] &= wordByte; // add byte. } } } } } // ignore data in hex file if (Pk2.DevFile.PartsList[Pk2.ActivePart].IgnoreBytes > 0) { if (byteAddress >= Pk2.DevFile.PartsList[Pk2.ActivePart].IgnoreAddress) { if (byteAddress < (Pk2.DevFile.PartsList[Pk2.ActivePart].IgnoreAddress + Pk2.DevFile.PartsList[Pk2.ActivePart].IgnoreBytes)) { // if data is in the ignore region, don't do anything with it // but don't generate a "hex file larger than device" warning. lineExceedsFlash = false; } } } // test memory section --------------------------------------------------------- if (FormPICkit2.TestMemoryEnabled && FormPICkit2.TestMemoryOpen) { if (FormPICkit2.formTestMem.HexImportExportTM()) { if ((byteAddress >= Pk2.DevFile.Families[Pk2.GetActiveFamily()].TestMemoryStart) && (Pk2.DevFile.Families[Pk2.GetActiveFamily()].TestMemoryStart > 0) && (FormPICkit2.TestMemoryWords > 0)) { int tmAddress = (int)(byteAddress - Pk2.DevFile.Families[Pk2.GetActiveFamily()].TestMemoryStart) / bytesPerWord; if (tmAddress < FormPICkit2.TestMemoryWords) { lineExceedsFlash = false; FormTestMemory.TestMemory[tmAddress] &= wordByte; // add byte. } } } } } } if (lineExceedsFlash) { fileExceedsFlash = true; } } // end if (recordType == 0) if ((recordType == 2) || (recordType == 4)) { // Segment address if (fileLine.Length >= (11 + (2 * byteCount))) { // skip if line isn't long enough for bytecount. segmentAddress = Int32.Parse(fileLine.Substring(9, 4), System.Globalization.NumberStyles.HexNumber); } if (recordType == 2) { segmentAddress <<= 4; } else { segmentAddress <<= 16; } } // end if ((recordType == 2) || (recordType == 4)) if (recordType == 1) { // end of record break; } if (hexFile.Extension.ToUpper() == ".NUM") { // Only read first line of SQTP file break; } } fileLine = hexRead.ReadLine(); } hexRead.Close(); if (!configRead && (configWords > 0)) { return(Constants.FileRead.noconfig); } if (fileExceedsFlash) { return(Constants.FileRead.largemem); } return(Constants.FileRead.success); } catch { return(Constants.FileRead.failed); } }