private bool blankCheckDevice() { if (Pk2.FamilyIsKeeloq() || Pk2.FamilyIsMCP()) { displayStatusWindow.Text = "Blank Check not supported for this device type."; statusWindowColor = Constants.StatusColor.yellow; updateGUI(KONST.DontUpdateMemDisplays); return false; // abort } if (!preProgrammingCheck(Pk2.GetActiveFamily())) { return false; // abort } if (Pk2.FamilyIsPIC32()) { if (P32.PIC32BlankCheck()) { statusWindowColor = Constants.StatusColor.green; conditionalVDDOff(); updateGUI(KONST.UpdateMemoryDisplays); return true; } else { statusWindowColor = Constants.StatusColor.red; conditionalVDDOff(); updateGUI(KONST.UpdateMemoryDisplays); return true; } } DeviceData blankDevice = new DeviceData(Pk2.DevFile.PartsList[Pk2.ActivePart].ProgramMem, Pk2.DevFile.PartsList[Pk2.ActivePart].EEMem, Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigWords, Pk2.DevFile.PartsList[Pk2.ActivePart].UserIDWords, Pk2.DevFile.Families[Pk2.GetActiveFamily()].BlankValue, Pk2.DevFile.Families[Pk2.GetActiveFamily()].EEMemAddressIncrement, Pk2.DevFile.Families[Pk2.GetActiveFamily()].UserIDBytes, Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigBlank, Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigMasks[KONST.OSCCAL_MASK]); // handle situation where configs are in program memory. int configLocation = (int)Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigAddr / Pk2.DevFile.Families[Pk2.GetActiveFamily()].ProgMemHexBytes; int configWords = Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigWords; if (configLocation < Pk2.DevFile.PartsList[Pk2.ActivePart].ProgramMem) for (int i = 0; i < configWords; i++) { uint template = blankDevice.ProgramMemory[configLocation + i] & 0xFFFF0000; if (Pk2.DevFile.Families[Pk2.GetActiveFamily()].BlankValue == 0xFFFF) { template |= 0xF000; } blankDevice.ProgramMemory[configLocation + i] = (template | Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigBlank[i]); } displayStatusWindow.Text = "Checking if Device is blank:\n"; //displayStatusWindow.Update(); this.Update(); Pk2.SetMCLRTemp(true); // assert /MCLR to prevent code execution before programming mode entered. Pk2.VddOn(); byte[] upload_buffer = new byte[KONST.UploadBufferSize]; //Check Program Memory ---------------------------------------------------------------------------- displayStatusWindow.Text += "Program Memory... "; //displayStatusWindow.Update(); this.Update(); if (useProgExec33()) { if (!PE33.PE33BlankCheck(displayStatusWindow.Text)) { conditionalVDDOff(); displayStatusWindow.Text = "Program Memory is not blank."; statusWindowColor = Constants.StatusColor.red; updateGUI(KONST.UpdateMemoryDisplays); return false; } } else if (useProgExec24F()) { if (!PE24.PE24FBlankCheck(displayStatusWindow.Text)) { conditionalVDDOff(); statusWindowColor = Constants.StatusColor.red; updateGUI(KONST.UpdateMemoryDisplays); return false; } } else { Pk2.RunScript(KONST.PROG_ENTRY, 1); if ((Pk2.DevFile.PartsList[Pk2.ActivePart].ProgMemAddrSetScript != 0) && (Pk2.DevFile.PartsList[Pk2.ActivePart].ProgMemAddrBytes != 0)) { // if prog mem address set script exists for this part if (Pk2.FamilyIsEEPROM()) { Pk2.DownloadAddress3MSBFirst(eeprom24BitAddress(0, KONST.WRITE_BIT)); Pk2.RunScript(KONST.PROGMEM_ADDRSET, 1); if (eeprom_CheckBusErrors()) { return false; } } else { Pk2.DownloadAddress3(0); Pk2.RunScript(KONST.PROGMEM_ADDRSET, 1); } } int bytesPerWord = Pk2.DevFile.Families[Pk2.GetActiveFamily()].BytesPerLocation; int scriptRunsToFillUpload = KONST.UploadBufferSize / (Pk2.DevFile.PartsList[Pk2.ActivePart].ProgMemRdWords * bytesPerWord); int wordsPerLoop = scriptRunsToFillUpload * Pk2.DevFile.PartsList[Pk2.ActivePart].ProgMemRdWords; int wordsRead = 0; progressBar1.Value = 0; // reset bar progressBar1.Maximum = (int)Pk2.DevFile.PartsList[Pk2.ActivePart].ProgramMem / wordsPerLoop; do { if (Pk2.FamilyIsEEPROM()) { if ((Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigMasks[KONST.PROTOCOL_CFG] == KONST.I2C_BUS) && (wordsRead > Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigMasks[KONST.ADR_MASK_CFG]) && (wordsRead % (Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigMasks[KONST.ADR_MASK_CFG] + 1) == 0)) { Pk2.DownloadAddress3MSBFirst(eeprom24BitAddress(wordsRead, KONST.WRITE_BIT)); Pk2.RunScript(KONST.PROGMEM_ADDRSET, 1); } Pk2.Download3Multiples(eeprom24BitAddress(wordsRead, KONST.READ_BIT), scriptRunsToFillUpload, Pk2.DevFile.PartsList[Pk2.ActivePart].ProgMemRdWords); } //Pk2.RunScriptUploadNoLen2(KONST.PROGMEM_RD, scriptRunsToFillUpload); Pk2.RunScriptUploadNoLen(KONST.PROGMEM_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; } // if OSCCAL save, force last word to be blank if ((Pk2.DevFile.PartsList[Pk2.ActivePart].OSSCALSave) && wordsRead == (Pk2.DevFile.PartsList[Pk2.ActivePart].ProgramMem - 1)) { memWord = Pk2.DevFile.Families[Pk2.GetActiveFamily()].BlankValue; } if (memWord != blankDevice.ProgramMemory[wordsRead++]) { Pk2.RunScript(KONST.PROG_EXIT, 1); conditionalVDDOff(); if (Pk2.FamilyIsEEPROM()) { displayStatusWindow.Text = "EEPROM is not blank starting at address\n"; } else { displayStatusWindow.Text = "Program Memory is not blank starting at address\n"; } displayStatusWindow.Text += string.Format("0x{0:X6}", (--wordsRead * Pk2.DevFile.Families[Pk2.GetActiveFamily()].AddressIncrement)); statusWindowColor = Constants.StatusColor.red; updateGUI(KONST.UpdateMemoryDisplays); return false; } if (wordsRead == Pk2.DevFile.PartsList[Pk2.ActivePart].ProgramMem) { break; // for cases where ProgramMemSize%WordsPerLoop != 0 } if (((wordsRead % 0x8000) == 0) && (Pk2.DevFile.PartsList[Pk2.ActivePart].ProgMemAddrSetScript != 0) && (Pk2.DevFile.PartsList[Pk2.ActivePart].ProgMemAddrBytes != 0) && (Pk2.DevFile.Families[Pk2.GetActiveFamily()].BlankValue > 0xFFFF)) { //PIC24 must update TBLPAG Pk2.DownloadAddress3(0x10000 * (wordsRead / 0x8000)); Pk2.RunScript(KONST.PROGMEM_ADDRSET, 1); break; } } progressBar1.PerformStep(); } while (wordsRead < Pk2.DevFile.PartsList[Pk2.ActivePart].ProgramMem); Pk2.RunScript(KONST.PROG_EXIT, 1); } //Check EEPROM ------------------------------------------------------------------------------------ if (Pk2.DevFile.PartsList[Pk2.ActivePart].EEMem > 0) { displayStatusWindow.Text += "EE... "; this.Update(); Pk2.RunScript(KONST.PROG_ENTRY, 1); if (Pk2.DevFile.PartsList[Pk2.ActivePart].EERdPrepScript > 0) { if (Pk2.DevFile.Families[Pk2.GetActiveFamily()].EEMemHexBytes == 4) { // 16-bit parts Pk2.DownloadAddress3((int)(Pk2.DevFile.PartsList[Pk2.ActivePart].EEAddr / 2)); } else { Pk2.DownloadAddress3(0); } Pk2.RunScript(KONST.EE_RD_PREP, 1); } int bytesPerLoc = Pk2.DevFile.Families[Pk2.GetActiveFamily()].EEMemBytesPerWord; uint eeBlank = getEEBlank(); int scriptRuns2FillUpload = KONST.UploadBufferSize / (Pk2.DevFile.PartsList[Pk2.ActivePart].EERdLocations * bytesPerLoc); int locPerLoop = scriptRuns2FillUpload * Pk2.DevFile.PartsList[Pk2.ActivePart].EERdLocations; int locsRead = 0; progressBar1.Value = 0; // reset bar progressBar1.Maximum = (int)Pk2.DevFile.PartsList[Pk2.ActivePart].EEMem / locPerLoop; do { //Pk2.RunScriptUploadNoLen2(KONST.EE_RD, scriptRuns2FillUpload); Pk2.RunScriptUploadNoLen(KONST.EE_RD, scriptRuns2FillUpload); 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 < locPerLoop; word++) { int bite = 0; uint memWord = (uint)upload_buffer[uploadIndex + bite++]; if (bite < bytesPerLoc) { memWord |= (uint)upload_buffer[uploadIndex + bite++] << 8; } uploadIndex += bite; // shift if necessary if (Pk2.DevFile.Families[Pk2.GetActiveFamily()].ProgMemShift > 0) { memWord = (memWord >> 1) & eeBlank; } locsRead++; if (memWord != eeBlank) { Pk2.RunScript(KONST.PROG_EXIT, 1); conditionalVDDOff(); displayStatusWindow.Text = "EE Data Memory is not blank starting at address\n"; if (eeBlank == 0xFFFF) { displayStatusWindow.Text += string.Format("0x{0:X4}", (--locsRead * 2)); } else { displayStatusWindow.Text += string.Format("0x{0:X4}", --locsRead); } statusWindowColor = Constants.StatusColor.red; updateGUI(KONST.UpdateMemoryDisplays); return false; } if (locsRead >= Pk2.DevFile.PartsList[Pk2.ActivePart].EEMem) { break; // for cases where ProgramMemSize%WordsPerLoop != 0 } } progressBar1.PerformStep(); } while (locsRead < Pk2.DevFile.PartsList[Pk2.ActivePart].EEMem); Pk2.RunScript(KONST.PROG_EXIT, 1); } //Check User IDs ---------------------------------------------------------------------------------- if ((Pk2.DevFile.PartsList[Pk2.ActivePart].UserIDWords > 0) && !Pk2.DevFile.PartsList[Pk2.ActivePart].BlankCheckSkipUsrIDs) { displayStatusWindow.Text += "UserIDs... "; this.Update(); Pk2.RunScript(KONST.PROG_ENTRY, 1); if (Pk2.DevFile.PartsList[Pk2.ActivePart].UserIDRdPrepScript > 0) { Pk2.RunScript(KONST.USERID_RD_PREP, 1); } int bytesPerWord = Pk2.DevFile.Families[Pk2.GetActiveFamily()].UserIDBytes; int wordsRead = 0; int bufferIndex = 0; Pk2.RunScriptUploadNoLen(KONST.USERID_RD, 1); Array.Copy(Pk2.Usb_read_array, 1, upload_buffer, 0, KONST.USB_REPORTLENGTH); if ((Pk2.DevFile.PartsList[Pk2.ActivePart].UserIDWords * bytesPerWord) > KONST.USB_REPORTLENGTH) { Pk2.UploadDataNoLen(); Array.Copy(Pk2.Usb_read_array, 1, upload_buffer, KONST.USB_REPORTLENGTH, KONST.USB_REPORTLENGTH); } Pk2.RunScript(KONST.PROG_EXIT, 1); do { int bite = 0; uint memWord = (uint)upload_buffer[bufferIndex + bite++]; if (bite < bytesPerWord) { memWord |= (uint)upload_buffer[bufferIndex + bite++] << 8; } if (bite < bytesPerWord) { memWord |= (uint)upload_buffer[bufferIndex + bite++] << 16; } if (bite < bytesPerWord) { memWord |= (uint)upload_buffer[bufferIndex + bite++] << 24; } bufferIndex += bite; // shift if necessary if (Pk2.DevFile.Families[Pk2.GetActiveFamily()].ProgMemShift > 0) { memWord = ((memWord >> 1) & Pk2.DevFile.Families[Pk2.GetActiveFamily()].BlankValue); } wordsRead++; uint blank = Pk2.DevFile.Families[Pk2.GetActiveFamily()].BlankValue; if (bytesPerWord == 1) { blank &= 0xFF; } if (memWord != blank) { conditionalVDDOff(); displayStatusWindow.Text = "User IDs are not blank."; statusWindowColor = Constants.StatusColor.red; updateGUI(KONST.UpdateMemoryDisplays); return false; } } while (wordsRead < Pk2.DevFile.PartsList[Pk2.ActivePart].UserIDWords); } // Blank Check Configuration -------------------------------------------------------------------- if ((configWords > 0) && (configLocation > Pk2.DevFile.PartsList[Pk2.ActivePart].ProgramMem)) { // Don't read config words for any part where they are stored in program memory. displayStatusWindow.Text += "Config... "; //displayStatusWindow.Update(); this.Update(); Pk2.RunScript(KONST.PROG_ENTRY, 1); Pk2.RunScript(KONST.CONFIG_RD, 1); Pk2.UploadData(); Pk2.RunScript(KONST.PROG_EXIT, 1); int bufferIndex = 2; // report starts on index 1, which is #bytes uploaded. for (int word = 0; word < configWords; word++) { uint config = (uint)Pk2.Usb_read_array[bufferIndex++]; config |= (uint)Pk2.Usb_read_array[bufferIndex++] << 8; if (Pk2.DevFile.Families[Pk2.GetActiveFamily()].ProgMemShift > 0) { config = (config >> 1) & Pk2.DevFile.Families[Pk2.GetActiveFamily()].BlankValue; } config &= Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigMasks[word]; int configBlank = Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigMasks[word] & Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigBlank[word]; if (configBlank != config) { conditionalVDDOff(); displayStatusWindow.Text = "Configuration is not blank."; statusWindowColor = Constants.StatusColor.red; updateGUI(KONST.UpdateMemoryDisplays); return false; } } } Pk2.RunScript(KONST.PROG_EXIT, 1); conditionalVDDOff(); statusWindowColor = Constants.StatusColor.green; displayStatusWindow.Text = "Device is Blank."; updateGUI(KONST.UpdateMemoryDisplays); return true; }
/********************************************************************************************************** ********************************************************************************************************** *** *** *** VERIFY DEVICE *** *** *** ********************************************************************************************************** **********************************************************************************************************/ private void verifyDevice(object sender, EventArgs e) { if (Pk2.FamilyIsKeeloq()) { displayStatusWindow.Text = "Verify not supported for this device type."; statusWindowColor = Constants.StatusColor.yellow; updateGUI(KONST.DontUpdateMemDisplays); return; // abort } deviceVerify(false, 0, false); }
private bool verifyOSCCAL() { if (!Pk2.ValidateOSSCAL() && verifyOSCCALValue) { if (MessageBox.Show ("Invalid OSCCAL Value detected:\n\nTo abort, click 'Cancel'\nTo continue, click 'OK'", "Warning!", MessageBoxButtons.OKCancel) == DialogResult.Cancel) { conditionalVDDOff(); displayStatusWindow.Text = "Operation Aborted.\n"; statusWindowColor = Constants.StatusColor.red; updateGUI(KONST.UpdateMemoryDisplays); return false; } } return true; }
private void updateGUI(bool updateMemories) { // Check for veiw changed if (viewChanged) { updateGUIView(); viewChanged = false; } // update family name statusGroupBox.Text = Pk2.DevFile.Families[Pk2.GetActiveFamily()].FamilyName + " Configuration"; // Update menu if family supports VPP First Program Entry if (Pk2.DevFile.Families[Pk2.GetActiveFamily()].ProgEntryVPPScript > 0) { VppFirstToolStripMenuItem.Enabled = true; } else { VppFirstToolStripMenuItem.Checked = false; VppFirstToolStripMenuItem.Enabled = false; } // Update menu for LVP program entry if (Pk2.DevFile.PartsList[Pk2.ActivePart].LVPScript > 0) { string scriptname = Pk2.DevFile.Scripts[Pk2.DevFile.PartsList[Pk2.ActivePart].LVPScript - 1].ScriptName; scriptname = scriptname.Substring(scriptname.Length - 2); if (scriptname == "HV") { toolStripMenuItemLVPEnabled.Text = "Use &High Voltage Program Entry"; labelLVP.Text = "HVP"; } else { toolStripMenuItemLVPEnabled.Text = "Use &LVP Program Entry"; labelLVP.Text = "LVP"; } toolStripMenuItemLVPEnabled.Enabled = true; if (toolStripMenuItemLVPEnabled.Checked) labelLVP.Visible = true; else labelLVP.Visible = false; } else { toolStripMenuItemLVPEnabled.Text = "Use &LVP Program Entry"; toolStripMenuItemLVPEnabled.Checked = false; toolStripMenuItemLVPEnabled.Enabled = false; labelLVP.Text = "LVP"; labelLVP.Visible = false; } // Update menu for serial EEPROM BIN import/export, and GUI for config display if (Pk2.FamilyIsEEPROM()) { importFileToolStripMenuItem.Text = "&Import Hex/Bin"; exportFileToolStripMenuItem.Text = "&Export Hex/Bin"; toolStripMenuItemDisplayUnimplConfigAs.Enabled = false; } else { importFileToolStripMenuItem.Text = "&Import Hex"; exportFileToolStripMenuItem.Text = "&Export Hex"; toolStripMenuItemDisplayUnimplConfigAs.Enabled = true; } // update device name displayDevice.Text = Pk2.DevFile.PartsList[Pk2.ActivePart].PartName; if (Pk2.ActivePart == 0) { if (Pk2.LastDeviceID == 0) { displayDevice.Text = "No Device Found"; } else { displayDevice.Text += " (ID=" + string.Format("{0:X4}", Pk2.LastDeviceID) + ")"; } } displayDevice.Update(); // update rev displayRev.Text = " <" + string.Format("{0:X2}", Pk2.LastDeviceRev) + ">"; if (updateMemories) { // update User IDs if (Pk2.DevFile.PartsList[Pk2.ActivePart].UserIDWords > 0) { labelUserIDs.Enabled = true; if (Pk2.DevFile.PartsList[Pk2.ActivePart].UserIDWords < 9) { displayUserIDs.Visible = true; buttonShowIDMem.Visible = false; // display the lower byte of each entry string userIDLine = ""; for (int i = 0; i < Pk2.DevFile.PartsList[Pk2.ActivePart].UserIDWords; i++) { userIDLine += string.Format("{0:X2} ", (0xFF & Pk2.DeviceBuffers.UserIDs[i])); } displayUserIDs.Text = userIDLine; } else { //dsPIC30 unit ID displayUserIDs.Visible = false; buttonShowIDMem.Visible = true; if (DialogUserIDs.IDMemOpen) { dialogIDMemory.UpdateIDMemoryGrid(); } } } else { labelUserIDs.Enabled = false; displayUserIDs.Text = ""; displayUserIDs.Visible = false; buttonShowIDMem.Visible = false; } } if (checkBoxProgMemEnabled.Checked) { // Indicate UserIDs not active when Program Memory not selected displayUserIDs.ForeColor = System.Drawing.SystemColors.WindowText; } else { displayUserIDs.ForeColor = System.Drawing.SystemColors.GrayText; } // checksum value if (updateMemories) { displayChecksum.Text = string.Format("{0:X4}", Pk2.ComputeChecksum(enableCodeProtectToolStripMenuItem.Checked, enableDataProtectStripMenuItem.Checked)); } // update configuration display if (updateMemories) { if ((Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigWords == 0) || (Pk2.ActivePart == 0) || !allowDataEdits) { labelConfig.Enabled = false; } else { labelConfig.Enabled = true; } int configIndex = 0; for (int row = 0; row < KONST.ConfigRows; row++) { for (int column = 0; column < KONST.ConfigColumns; column++) { if (configIndex < Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigWords) { uint configWord = Pk2.DeviceBuffers.ConfigWords[configIndex]; // how to display unimplemented bits? if (as0BitValueToolStripMenuItem.Checked) // as '0' configWord &= (uint)Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigMasks[configIndex]; else if (as1BitValueToolStripMenuItem.Checked) configWord |= ~(uint)Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigMasks[configIndex]; // else display as-is // But kill off high bits. configWord &= (Pk2.DevFile.Families[Pk2.GetActiveFamily()].BlankValue & 0xFFFF); if (Pk2.DevFile.PartsList[Pk2.ActivePart].CPConfig-1 == configIndex) { if (enableCodeProtectToolStripMenuItem.Checked) { if ((Pk2.DeviceBuffers.ConfigWords[Pk2.DevFile.PartsList[Pk2.ActivePart].CPConfig - 1] & Pk2.DevFile.PartsList[Pk2.ActivePart].CPMask) == Pk2.DevFile.PartsList[Pk2.ActivePart].CPMask) { // no CPs asserted in file configWord &= (uint)~Pk2.DevFile.PartsList[Pk2.ActivePart].CPMask; } } if (enableDataProtectStripMenuItem.Checked) { if ((Pk2.DeviceBuffers.ConfigWords[Pk2.DevFile.PartsList[Pk2.ActivePart].CPConfig - 1] & Pk2.DevFile.PartsList[Pk2.ActivePart].DPMask) == Pk2.DevFile.PartsList[Pk2.ActivePart].DPMask) { // no DPs asserted in file configWord &= (uint)~Pk2.DevFile.PartsList[Pk2.ActivePart].DPMask; } } } dataGridConfigWords[column, row].Value = string.Format("{0:X4}", configWord); configIndex++; } else { dataGridConfigWords[column, row].Value = " "; } if (Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigWords == 9) { uint configWord = Pk2.DeviceBuffers.ConfigWords[8]; // how to display unimplemented bits? if (as0BitValueToolStripMenuItem.Checked) // as '0' configWord &= (uint)Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigMasks[8]; else if (as1BitValueToolStripMenuItem.Checked) configWord |= ~(uint)Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigMasks[8]; // else display as-is // But kill off high bits. configWord &= (Pk2.DevFile.Families[Pk2.GetActiveFamily()].BlankValue & 0xFFFF); labelConfig9.Text = string.Format("{0:X4}", configWord); labelConfig9.Visible = true; } else { labelConfig9.Visible = false; } } } } if (checkBoxProgMemEnabled.Checked) { // Indicate Config Words not active when Program Memory not selected dataGridConfigWords.ForeColor = System.Drawing.SystemColors.WindowText; } else { dataGridConfigWords.ForeColor = System.Drawing.SystemColors.GrayText; } // update I2C Serial EEPROM Chip Selects if (Pk2.FamilyIsEEPROM() && (Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigMasks[KONST.PROTOCOL_CFG] == KONST.I2C_BUS)) { checkBoxA0CS.Visible = true; checkBoxA1CS.Visible = true; checkBoxA2CS.Visible = true; if (Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigMasks[KONST.CS_PINS_CFG] == 1) { checkBoxA0CS.Enabled = true; checkBoxA1CS.Enabled = false; checkBoxA1CS.Checked = false; checkBoxA2CS.Enabled = false; checkBoxA2CS.Checked = false; } else if (Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigMasks[KONST.CS_PINS_CFG] == 2) { checkBoxA0CS.Enabled = true; checkBoxA1CS.Enabled = true; checkBoxA2CS.Enabled = false; checkBoxA2CS.Checked = false; } else if (Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigMasks[KONST.CS_PINS_CFG] == 3) { checkBoxA0CS.Enabled = true; checkBoxA1CS.Enabled = true; checkBoxA2CS.Enabled = true; } else { checkBoxA0CS.Enabled = false; checkBoxA0CS.Checked = false; checkBoxA1CS.Enabled = false; checkBoxA1CS.Checked = false; checkBoxA2CS.Enabled = false; checkBoxA2CS.Checked = false; } } else { checkBoxA0CS.Visible = false; checkBoxA1CS.Visible = false; checkBoxA2CS.Visible = false; } // Update OSCCAL if (Pk2.DevFile.PartsList[Pk2.ActivePart].OSSCALSave) { setOSCCALToolStripMenuItem.Enabled = true; labelOSCCAL.Enabled = true; displayOSCCAL.Text = string.Format("{0:X4}", Pk2.DeviceBuffers.OSCCAL); // check for valid OSCCAL value if (Pk2.ValidateOSSCAL()) { labelOSSCALInvalid.Visible = false; displayOSCCAL.ForeColor = SystemColors.ControlText; } else { labelOSSCALInvalid.Visible = true; displayOSCCAL.ForeColor = Color.Red; } } else { labelOSSCALInvalid.Visible = false; setOSCCALToolStripMenuItem.Enabled = false; labelOSCCAL.Enabled = false; displayOSCCAL.Text = ""; } // Update BandGap if (Pk2.DevFile.PartsList[Pk2.ActivePart].BandGapMask > 0) { labelBandGap.Enabled = true; if (Pk2.DeviceBuffers.BandGap == Pk2.DevFile.Families[Pk2.GetActiveFamily()].BlankValue) { displayBandGap.Text = ""; } else { displayBandGap.Text = string.Format("{0:X4}", Pk2.DeviceBuffers.BandGap); } } else { labelBandGap.Enabled = false; displayBandGap.Text = ""; } // Status Window Color switch (statusWindowColor) { case Constants.StatusColor.green: displayStatusWindow.BackColor = Color.LimeGreen; if (PlaySuccessWav) { wavPlayer.SoundLocation = @SuccessWavFile; wavPlayer.Play(); } break; case Constants.StatusColor.yellow: displayStatusWindow.BackColor = Color.Yellow; if (PlayWarningWav) { wavPlayer.SoundLocation = WarningWavFile; wavPlayer.Play(); } break; case Constants.StatusColor.red: displayStatusWindow.BackColor = Color.Salmon; if (PlayErrorWav) { wavPlayer.SoundLocation = ErrorWavFile; wavPlayer.Play(); } break; default: displayStatusWindow.BackColor = System.Drawing.SystemColors.Info; break; } statusWindowColor = Constants.StatusColor.normal; // MCLR checkbox if (Pk2.FamilyIsEEPROM()) { checkBoxMCLR.Checked = false; checkBoxMCLR.Enabled = false; MCLRtoolStripMenuItem.Checked = false; MCLRtoolStripMenuItem.Enabled = false; Pk2.HoldMCLR(false); } else { checkBoxMCLR.Enabled = true; MCLRtoolStripMenuItem.Enabled = true; } // PIC32 doesn't use "Fast Programming" menu item if (Pk2.FamilyIsPIC32()) { fastProgrammingToolStripMenuItem.Checked = true; fastProgrammingToolStripMenuItem.Enabled = false; } else { fastProgrammingToolStripMenuItem.Enabled = true; } // Update Program Memory display if (updateMemories) { // update CP menu if (Pk2.DevFile.PartsList[Pk2.ActivePart].CPMask == 0) { // no CP enableCodeProtectToolStripMenuItem.Checked = false; enableCodeProtectToolStripMenuItem.Enabled = false; } else { enableCodeProtectToolStripMenuItem.Enabled = true; } } if (updateMemories && multiWindow) { // init multi window if (!programMemMultiWin.InitDone) programMemMultiWin.InitProgMemDisplay(comboBoxProgMemView.SelectedIndex); // update programMemMultiWin.UpdateMultiWinProgMem(displayDataSource.Text); } if (updateMemories && !multiWindow) { if (Pk2.DevFile.Families[Pk2.GetActiveFamily()].BlankValue > 0xFFFFFF) { // PIC32 - no ASCII view supported comboBoxProgMemView.SelectedIndex = 0; comboBoxProgMemView.Enabled = false; } else { comboBoxProgMemView.Enabled = true; } // Set address columns first // Set # columns based on blank memory size. int dataColumns, columnWidth, columnCount; if (Pk2.DevFile.Families[Pk2.GetActiveFamily()].BlankValue <= 0xFFF) { if (Pk2.FamilyIsEEPROM()) { // need wider address columnCount = 17; //dataGridProgramMemory.Columns[0].Width = 51; // address column dataGridProgramMemory.Columns[0].Width = (int)(51 * ScalefactW); // address column dataColumns = 16; //columnWidth = 27; columnWidth = (int)(27 * ScalefactW); } else { columnCount = 17; //dataGridProgramMemory.Columns[0].Width = 35; // address column dataGridProgramMemory.Columns[0].Width = (int)(35 * ScalefactW); // address column dataColumns = 16; //columnWidth = 28; columnWidth = (int)(28 * ScalefactW); } } else if (Pk2.DevFile.Families[Pk2.GetActiveFamily()].BlankValue > 0xFFFFFF) { // PIC32 columnCount = 5; dataGridProgramMemory.Columns[0].Width = (int)(99 * ScalefactW); // address column dataColumns = 4; columnWidth = (int)(96 * ScalefactW); } else { columnCount = 9; //dataGridProgramMemory.Columns[0].Width = 59; // address column dataGridProgramMemory.Columns[0].Width = (int)(59 * ScalefactW); // address column dataColumns = 8; //columnWidth = 53; columnWidth = (int)(53 * ScalefactW); } if (dataGridProgramMemory.ColumnCount != columnCount) { dataGridProgramMemory.Rows.Clear(); dataGridProgramMemory.ColumnCount = columnCount; } for (int column = 1; column < dataGridProgramMemory.ColumnCount; column++) { dataGridProgramMemory.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)Pk2.DevFile.PartsList[Pk2.ActivePart].ProgramMem / hexColumns; if ((Pk2.DevFile.PartsList[Pk2.ActivePart].ProgramMem % hexColumns) > 0) { rowCount++; } rowAddressIncrement = addressIncrement * dataColumns; } else { hexColumns = dataColumns / 2; rowCount = (int)Pk2.DevFile.PartsList[Pk2.ActivePart].ProgramMem / hexColumns; if ((Pk2.DevFile.PartsList[Pk2.ActivePart].ProgramMem % hexColumns) > 0) { rowCount++; } rowAddressIncrement = addressIncrement * (dataColumns / 2); } if (Pk2.DevFile.Families[Pk2.GetActiveFamily()].BlankValue > 0xFFFFFF) { // PIC32 - add rows for memory section titles rowCount += 2; } if (dataGridProgramMemory.RowCount != rowCount) { dataGridProgramMemory.Rows.Clear(); dataGridProgramMemory.RowCount = rowCount; } for (int col = 0; col < hexColumns; col++) { // hex editable dataGridProgramMemory.Columns[col + 1].ReadOnly = false; } int maxAddress = dataGridProgramMemory.RowCount * rowAddressIncrement - 1; String addressFormat = "{0:X3}"; if (Pk2.DevFile.Families[Pk2.GetActiveFamily()].BlankValue > 0xFFFFFF) { // PIC32 addressFormat = "{0:X8}"; } else if (maxAddress > 0xFFFF) { addressFormat = "{0:X5}"; } else if (maxAddress > 0xFFF) { addressFormat = "{0:X4}"; } // PIC32 info variables 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 /= hexColumns; if (Pk2.DevFile.Families[Pk2.GetActiveFamily()].BlankValue > 0xFFFFFF) { // PIC32 - insert titles dataGridProgramMemory.ShowCellToolTips = false; // Program Flash addresses dataGridProgramMemory[0, 0].Value = "Program 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(addressFormat, address); dataGridProgramMemory[0, row].Style.BackColor = System.Drawing.SystemColors.ControlLight; address += rowAddressIncrement; } // Boot Flash addresses dataGridProgramMemory[0, progMemP32 + 1].Value = "Boot 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(addressFormat, address); dataGridProgramMemory[0, row].Style.BackColor = System.Drawing.SystemColors.ControlLight; address += rowAddressIncrement; } } else { dataGridProgramMemory.ShowCellToolTips = true; for (int row = 0, address = 0; row < dataGridProgramMemory.RowCount; row++) { dataGridProgramMemory[0, row].Value = string.Format(addressFormat, address); dataGridProgramMemory[0, row].Style.BackColor = System.Drawing.SystemColors.ControlLight; address += rowAddressIncrement; } } // Now fill data string dataFormat = "{0:X2}"; int asciiBytes = 1; if (Pk2.DevFile.Families[Pk2.GetActiveFamily()].BlankValue > 0xFF) { dataFormat = "{0:X3}"; asciiBytes = 2; } if (Pk2.DevFile.Families[Pk2.GetActiveFamily()].BlankValue > 0xFFF) { dataFormat = "{0:X4}"; asciiBytes = 2; } if (Pk2.DevFile.Families[Pk2.GetActiveFamily()].BlankValue > 0xFFFF) { dataFormat = "{0:X6}"; asciiBytes = 3; } if (Pk2.DevFile.Families[Pk2.GetActiveFamily()].BlankValue > 0xFFFFFF) { dataFormat = "{0:X8}"; asciiBytes = 4; } if (Pk2.DevFile.Families[Pk2.GetActiveFamily()].BlankValue > 0xFFFFFF) { // PIC32 // Program Flash int idx = 0; for (int row = 1; row <= progMemP32; row++) { for (int col = 0; col < hexColumns; col++) { //dataGridProgramMemory[col + 1, row].ToolTipText = // string.Format(addressFormat, (idx * addressIncrement)); dataGridProgramMemory[col + 1, row].Value = string.Format(dataFormat, Pk2.DeviceBuffers.ProgramMemory[idx++]); } } // Boot Flash for (int row = progMemP32 + 2; row < dataGridProgramMemory.RowCount; row++) { for (int col = 0; col < hexColumns; col++) { //dataGridProgramMemory[col + 1, row].ToolTipText = // string.Format(addressFormat, (idx * addressIncrement)); dataGridProgramMemory[col + 1, row].Value = string.Format(dataFormat, Pk2.DeviceBuffers.ProgramMemory[idx++]); } } } else { for (int row = 0, idx = 0; row < (dataGridProgramMemory.RowCount-1); row++) { // all except last row for (int col = 0; col < hexColumns; col++) { dataGridProgramMemory[col + 1, row].ToolTipText = string.Format(addressFormat, (idx * addressIncrement)); dataGridProgramMemory[col + 1, row].Value = string.Format(dataFormat, Pk2.DeviceBuffers.ProgramMemory[idx++]); } } } int lastrow = dataGridProgramMemory.RowCount-1; int rowidx = lastrow * hexColumns; int lastcol = (int)Pk2.DevFile.PartsList[Pk2.ActivePart].ProgramMem % hexColumns; if (lastcol == 0) { lastcol = hexColumns; } if (Pk2.DevFile.Families[Pk2.GetActiveFamily()].BlankValue > 0xFFFFFF) { // PIC32 // "last row" not used for PIC32 } else { for (int col = 0; col < hexColumns; col++) { // fill last row if (col < lastcol) { dataGridProgramMemory[col + 1, lastrow].ToolTipText = string.Format(addressFormat, (rowidx * addressIncrement)); dataGridProgramMemory[col + 1, lastrow].Value = string.Format(dataFormat, Pk2.DeviceBuffers.ProgramMemory[rowidx++]); } else { dataGridProgramMemory[col + 1, lastrow].ReadOnly = true; } } } // Fill ASCII if selected if (comboBoxProgMemView.SelectedIndex >= 1) { for (int col = 0; col < hexColumns; col++) { // ascii not editable dataGridProgramMemory.Columns[col + hexColumns + 1].ReadOnly = true; } if (comboBoxProgMemView.SelectedIndex == 1) { //word view for (int row = 0, idx = 0; row < dataGridProgramMemory.RowCount; row++) { for (int col = 0; col < hexColumns; col++) { dataGridProgramMemory[col + hexColumns + 1, row].ToolTipText = string.Format(addressFormat, (idx * addressIncrement)); dataGridProgramMemory[col + hexColumns + 1, row].Value = UTIL.ConvertIntASCII((int)Pk2.DeviceBuffers.ProgramMemory[idx++], asciiBytes); } } } else { //byte view for (int row = 0, idx = 0; row < dataGridProgramMemory.RowCount; row++) { for (int col = 0; col < hexColumns; col++) { dataGridProgramMemory[col + hexColumns + 1, row].ToolTipText = string.Format(addressFormat, (idx * addressIncrement)); dataGridProgramMemory[col + hexColumns + 1, row].Value = UTIL.ConvertIntASCIIReverse((int)Pk2.DeviceBuffers.ProgramMemory[idx++], asciiBytes); } } } } if ((dataGridProgramMemory.FirstDisplayedCell != null) && !progMemJustEdited) { //currentCol = dataGridProgramMemory.FirstDisplayedCell.ColumnIndex; 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; } // Update EEPROM display if (updateMemories && (Pk2.DevFile.PartsList[Pk2.ActivePart].EEMem > 0)) { // prog mem checkbox only active when EE exists checkBoxProgMemEnabled.Enabled = true; comboBoxEE.Enabled = true; if (!checkBoxEEMem.Enabled) { // if we're just enabling it checkBoxEEMem.Checked = true; checkBoxEEDATAMemoryEnabledAlt.Checked = true; } checkBoxEEMem.Enabled = true; enableDataProtectStripMenuItem.Enabled = true; checkBoxEEDATAMemoryEnabledAlt.Enabled = true; checkBoxProgMemEnabledAlt.Enabled = true; if (multiWindow) { // init multi window if (!eepromDataMultiWin.InitDone) eepromDataMultiWin.InitMemDisplay(comboBoxEE.SelectedIndex); // update if (!toolStripMenuItemShowEEPROMData.Enabled) { toolStripMenuItemShowEEPROMData.Enabled = true; if (multiWinEEMemOpen) { eepromDataMultiWin.Show(); this.Focus(); } } eepromDataMultiWin.UpdateMultiWinMem(); } else { dataGridViewEEPROM.Visible = true; // Set address columns first // Set # columns based on blank memory size. int rowAddressIncrement = (int) Pk2.DevFile.Families[Pk2.GetActiveFamily()].EEMemAddressIncrement; int addressIncrement = rowAddressIncrement; int dataColumns, columnWidth, columnCount; if ((rowAddressIncrement == 1) && (Pk2.DevFile.Families[Pk2.GetActiveFamily()].BlankValue != 0xFFF)) { columnCount = 17; //dataGridViewEEPROM.Columns[0].Width = 32; // address column dataGridViewEEPROM.Columns[0].Width = (int)(32 * ScalefactW); // address column dataColumns = 16; //columnWidth = 21; columnWidth = (int)(21 * ScalefactW); } else { // 16-bit parts and basline columnCount = 9; //dataGridViewEEPROM.Columns[0].Width = 40; // address column dataGridViewEEPROM.Columns[0].Width = (int)(40 * ScalefactW); // address column dataColumns = 8; //columnWidth = 41; columnWidth = (int)(41 * ScalefactW); } if (dataGridViewEEPROM.ColumnCount != columnCount) { dataGridViewEEPROM.Rows.Clear(); dataGridViewEEPROM.ColumnCount = columnCount; } dataGridViewEEPROM.Columns[0].ReadOnly = true; for (int column = 1; column < dataGridViewEEPROM.ColumnCount; column++) { dataGridViewEEPROM.Columns[column].Width = columnWidth; // data columns } int hexColumns, rowCount; if (comboBoxEE.SelectedIndex == 0) // hex view { hexColumns = dataColumns; rowCount = (int)Pk2.DevFile.PartsList[Pk2.ActivePart].EEMem / hexColumns; rowAddressIncrement *= dataColumns; hexColumns = dataColumns; } else { hexColumns = dataColumns / 2; rowCount = (int)Pk2.DevFile.PartsList[Pk2.ActivePart].EEMem / hexColumns; rowAddressIncrement*= (dataColumns / 2); } if (dataGridViewEEPROM.RowCount != rowCount) { dataGridViewEEPROM.Rows.Clear(); dataGridViewEEPROM.RowCount = rowCount; } int maxAddress = dataGridViewEEPROM.RowCount * rowAddressIncrement - 1; String addressFormat = "{0:X2}"; if (maxAddress > 0xFF) { addressFormat = "{0:X3}"; } else if (maxAddress > 0xFFF) { addressFormat = "{0:X4}"; } for (int row = 0, address = 0; row < dataGridViewEEPROM.RowCount; row++) { dataGridViewEEPROM[0, row].Value = string.Format(addressFormat, address); dataGridViewEEPROM[0, row].Style.BackColor = System.Drawing.SystemColors.ControlLight; address += rowAddressIncrement; } // Now fill data string dataFormat = "{0:X2}"; int asciiBytes = 1; if (Pk2.DevFile.Families[Pk2.GetActiveFamily()].EEMemAddressIncrement > 1) { dataFormat = "{0:X4}"; asciiBytes = 2; } if (Pk2.DevFile.Families[Pk2.GetActiveFamily()].BlankValue == 0xFFF) { // baseline with data flash dataFormat = "{0:X3}"; asciiBytes = 2; } for (int col = 0; col < hexColumns; col++) { // hex editable dataGridViewEEPROM.Columns[col + 1].ReadOnly = false; } for (int row = 0, idx = 0; row < dataGridViewEEPROM.RowCount; row++) { for (int col = 0; col < hexColumns; col++) { dataGridViewEEPROM[col + 1, row].ToolTipText = string.Format(addressFormat, (idx * addressIncrement)); dataGridViewEEPROM[col + 1, row].Value = string.Format(dataFormat, Pk2.DeviceBuffers.EEPromMemory[idx++]); } } // Fill ASCII if selected if (comboBoxEE.SelectedIndex >= 1) { for (int col = 0; col < hexColumns; col++) { // ascii not editable dataGridViewEEPROM.Columns[col + hexColumns + 1].ReadOnly = true; } if (comboBoxEE.SelectedIndex == 1) { // word view for (int row = 0, idx = 0; row < dataGridViewEEPROM.RowCount; row++) { for (int col = 0; col < hexColumns; col++) { dataGridViewEEPROM[col + hexColumns + 1, row].ToolTipText = string.Format(addressFormat, (idx * addressIncrement)); dataGridViewEEPROM[col + hexColumns + 1, row].Value = UTIL.ConvertIntASCII((int)Pk2.DeviceBuffers.EEPromMemory[idx++], asciiBytes); } } } else { //byte view for (int row = 0, idx = 0; row < dataGridViewEEPROM.RowCount; row++) { for (int col = 0; col < hexColumns; col++) { dataGridViewEEPROM[col + hexColumns + 1, row].ToolTipText = string.Format(addressFormat, (idx * addressIncrement)); dataGridViewEEPROM[col + hexColumns + 1, row].Value = UTIL.ConvertIntASCIIReverse((int)Pk2.DeviceBuffers.EEPromMemory[idx++], asciiBytes); } } } } if ((dataGridViewEEPROM.FirstDisplayedCell != null) && !eeMemJustEdited) { //currentCol = dataGridViewEEPROM.FirstDisplayedCell.ColumnIndex; int currentRow = dataGridViewEEPROM.FirstDisplayedCell.RowIndex; dataGridViewEEPROM.MultiSelect = false; dataGridViewEEPROM[0, currentRow].Selected = true; // these 4 statements remove the "select" box dataGridViewEEPROM[0, currentRow].Selected = false; dataGridViewEEPROM.MultiSelect = true; } else if (dataGridViewEEPROM.FirstDisplayedCell == null) { // remove select box when app first opened. dataGridViewEEPROM.MultiSelect = false; dataGridViewEEPROM[0, 0].Selected = true; // these 4 statements remove the "select" box dataGridViewEEPROM[0, 0].Selected = false; dataGridViewEEPROM.MultiSelect = true; } eeMemJustEdited = false; } } else if (Pk2.DevFile.PartsList[Pk2.ActivePart].EEMem == 0) { dataGridViewEEPROM.Visible = false; comboBoxEE.Enabled = false; checkBoxEEMem.Checked = false; checkBoxEEDATAMemoryEnabledAlt.Checked = false; checkBoxEEMem.Enabled = false; checkBoxEEDATAMemoryEnabledAlt.Enabled = false; enableDataProtectStripMenuItem.Enabled = false; enableDataProtectStripMenuItem.Checked = false; checkBoxProgMemEnabled.Checked = true; checkBoxProgMemEnabledAlt.Checked = true; checkBoxProgMemEnabled.Enabled = false; checkBoxProgMemEnabledAlt.Enabled = false; eepromDataMultiWin.Hide(); toolStripMenuItemShowEEPROMData.Enabled = false; } // Update "Code Protect" label // First check to see if loaded data has code protects asserted. if ((Pk2.DevFile.PartsList[Pk2.ActivePart].CPMask != 0) && ((Pk2.DeviceBuffers.ConfigWords[Pk2.DevFile.PartsList[Pk2.ActivePart].CPConfig - 1] & Pk2.DevFile.PartsList[Pk2.ActivePart].CPMask) != Pk2.DevFile.PartsList[Pk2.ActivePart].CPMask)) { // loaded hex has CP asserted enableCodeProtectToolStripMenuItem.Checked = true; enableCodeProtectToolStripMenuItem.Enabled = false; // can't "deassert" CP in hex if ((Pk2.DevFile.PartsList[Pk2.ActivePart].EEMem > 0) && (Pk2.DevFile.PartsList[Pk2.ActivePart].DPMask == 0)) {// if part has EE but no CPMask, then both CP/DP get selected at same time (ie dsPIC30) enableDataProtectStripMenuItem.Checked = true; enableDataProtectStripMenuItem.Enabled = false; } } // Then check for data protect asserted if ((Pk2.DevFile.PartsList[Pk2.ActivePart].EEMem > 0) && ((Pk2.DeviceBuffers.ConfigWords[Pk2.DevFile.PartsList[Pk2.ActivePart].CPConfig - 1] & Pk2.DevFile.PartsList[Pk2.ActivePart].DPMask) != Pk2.DevFile.PartsList[Pk2.ActivePart].DPMask)) { enableDataProtectStripMenuItem.Checked = true; enableDataProtectStripMenuItem.Enabled = false; } if ((enableCodeProtectToolStripMenuItem.Checked) || (enableDataProtectStripMenuItem.Checked)) { labelCodeProtect.Visible = true; if ((enableCodeProtectToolStripMenuItem.Checked) && (enableDataProtectStripMenuItem.Checked)) { labelCodeProtect.Text = "All Protect"; } else if (enableCodeProtectToolStripMenuItem.Checked) { labelCodeProtect.Text = "Code Protect"; } else { labelCodeProtect.Text = "Data Protect"; } } else { labelCodeProtect.Visible = false; } // Update EEPROM status label if (!checkBoxProgMemEnabled.Checked) { displayEEProgInfo.Text = "Write and Read EEPROM data only."; displayEEProgInfo.Visible = true; eepromDataMultiWin.DisplayEETextOn("Write and Read EEPROM data only."); } else if (!checkBoxEEMem.Checked && checkBoxEEMem.Enabled) { if (Pk2.DevFile.PartsList[Pk2.ActivePart].ProgMemEraseScript != 0) { displayEEProgInfo.Text = "Preserve device EEPROM data on write."; eepromDataMultiWin.DisplayEETextOn("Preserve device EEPROM data on write."); } else { displayEEProgInfo.Text = "Read/Restore device EEPROM on write."; eepromDataMultiWin.DisplayEETextOn("Read/Restore device EEPROM on write."); } displayEEProgInfo.Visible = true; } else { displayEEProgInfo.Visible = false; eepromDataMultiWin.DisplayEETextOff(); } // update test memory if being used if (TestMemoryEnabled && TestMemoryOpen) { formTestMem.UpdateTestMemForm(); if (updateMemories) { formTestMem.UpdateTestMemoryGrid(); } } // update test forms if connected if (testConnected) { updateTestGUI(); } }
private bool verifyConfig(int configWords, int configLocation) { // Verify Configuration -------------------------------------------------------------------- if ((configWords > 0) && (configLocation > Pk2.DevFile.PartsList[Pk2.ActivePart].ProgramMem) && checkBoxProgMemEnabled.Checked) { // Don't read config words for any part where they are stored in program memory. displayStatusWindow.Text += "Config... "; //displayStatusWindow.Update(); this.Update(); Pk2.RunScript(KONST.PROG_ENTRY, 1); Pk2.RunScript(KONST.CONFIG_RD, 1); Pk2.UploadData(); Pk2.RunScript(KONST.PROG_EXIT, 1); int bufferIndex = 2; // report starts on index 1, which is #bytes uploaded. for (int word = 0; word < configWords; word++) { uint config = (uint)Pk2.Usb_read_array[bufferIndex++]; config |= (uint)Pk2.Usb_read_array[bufferIndex++] << 8; if (Pk2.DevFile.Families[Pk2.GetActiveFamily()].ProgMemShift > 0) { config = (config >> 1) & Pk2.DevFile.Families[Pk2.GetActiveFamily()].BlankValue; } config &= Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigMasks[word]; uint configExpected = Pk2.DeviceBuffers.ConfigWords[word] & Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigMasks[word]; if (word == Pk2.DevFile.PartsList[Pk2.ActivePart].CPConfig - 1) { if (enableCodeProtectToolStripMenuItem.Enabled && enableCodeProtectToolStripMenuItem.Checked) { configExpected &= (uint)~Pk2.DevFile.PartsList[Pk2.ActivePart].CPMask; } if (enableDataProtectStripMenuItem.Enabled && enableDataProtectStripMenuItem.Checked) { configExpected &= (uint)~Pk2.DevFile.PartsList[Pk2.ActivePart].DPMask; } } if ((configExpected != config) && !Pk2.LearnMode) { conditionalVDDOff(); displayStatusWindow.Text = "Verification of configuration failed."; statusWindowColor = Constants.StatusColor.red; updateGUI(KONST.UpdateMemoryDisplays); return false; } } } return true; }
private bool importHexFileGo() { int lastPart = Pk2.ActivePart; // save last part, if part doesn't change keep buffers. bool verifySave = deviceVerification; deviceVerification = false; // in manual mode, don't check for device on import. if (!preProgrammingCheck(Pk2.GetActiveFamily())) { deviceVerification = verifySave; displayStatusWindow.Text = "Device Error - hex file not loaded."; statusWindowColor = Constants.StatusColor.red; displayDataSource.Text = "None."; importGo = false; return false; } deviceVerification = verifySave; // clear device buffers. if ((lastPart != Pk2.ActivePart) // a new part is detected || (Pk2.DevFile.PartsList[Pk2.ActivePart].EEMem == 0) // the part has no EE Data || (checkBoxProgMemEnabled.Checked && checkBoxEEMem.Checked)) // Both memory regions are checked { // reset everything Pk2.ResetBuffers(); } else { // just clear checked regions if (checkBoxProgMemEnabled.Checked) { Pk2.DeviceBuffers.ClearProgramMemory(Pk2.DevFile.Families[Pk2.GetActiveFamily()].BlankValue); Pk2.DeviceBuffers.ClearConfigWords(Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigBlank); Pk2.DeviceBuffers.ClearUserIDs(Pk2.DevFile.Families[Pk2.GetActiveFamily()].UserIDBytes, Pk2.DevFile.Families[Pk2.GetActiveFamily()].BlankValue); } if (checkBoxEEMem.Checked) { Pk2.DeviceBuffers.ClearEEPromMemory(Pk2.DevFile.Families[Pk2.GetActiveFamily()].EEMemAddressIncrement, Pk2.DevFile.Families[Pk2.GetActiveFamily()].BlankValue); } } if (TestMemoryEnabled && TestMemoryOpen) { if (formTestMem.HexImportExportTM()) { formTestMem.ClearTestMemory(); } } string fileType = "Hex"; if ((openHexFileDialog.FileName.Substring(openHexFileDialog.FileName.Length - 4).ToUpper() == ".BIN") && Pk2.FamilyIsEEPROM()) fileType = "Bin"; switch (ImportExportHex.ImportHexFile(openHexFileDialog.FileName, checkBoxProgMemEnabled.Checked, checkBoxEEMem.Checked)) { case Constants.FileRead.success: displayStatusWindow.Text = fileType + " file sucessfully imported."; if (multiWindow) displayDataSource.Text = openHexFileDialog.FileName; else displayDataSource.Text = shortenHex(openHexFileDialog.FileName); checkImportFile = true; importGo = true; break; case Constants.FileRead.noconfig: statusWindowColor = Constants.StatusColor.yellow; displayStatusWindow.Text = "Warning: No configuration words in hex file.\nIn MPLAB use File-Export to save hex with config."; if (multiWindow) displayDataSource.Text = openHexFileDialog.FileName; else displayDataSource.Text = shortenHex(openHexFileDialog.FileName); checkImportFile = true; importGo = true; break; case Constants.FileRead.partialcfg: statusWindowColor = Constants.StatusColor.yellow; displayStatusWindow.Text = "Warning: Some configuration words not in hex file.\nEnsure default values above right are acceptable."; if (multiWindow) displayDataSource.Text = openHexFileDialog.FileName; else displayDataSource.Text = shortenHex(openHexFileDialog.FileName); checkImportFile = true; importGo = true; break; case Constants.FileRead.largemem: statusWindowColor = Constants.StatusColor.yellow; displayStatusWindow.Text = "Warning: " + fileType + " File Loaded is larger than device."; if (multiWindow) displayDataSource.Text = openHexFileDialog.FileName; else displayDataSource.Text = shortenHex(openHexFileDialog.FileName); checkImportFile = true; importGo = true; break; default: statusWindowColor = Constants.StatusColor.red; displayStatusWindow.Text = "Error reading " + fileType + " file."; displayDataSource.Text = "None (Empty/Erased)"; checkImportFile = false; importGo = false; Pk2.ResetBuffers(); break; } if (checkImportFile) { // Get OSCCAL if need be if (Pk2.DevFile.PartsList[Pk2.ActivePart].OSSCALSave) { Pk2.SetMCLRTemp(true); // assert /MCLR to prevent code execution before programming mode entered. Pk2.VddOn(); Pk2.ReadOSSCAL(); Pk2.DeviceBuffers.ProgramMemory[Pk2.DeviceBuffers.ProgramMemory.Length - 1] = Pk2.DeviceBuffers.OSCCAL; } // Get BandGap if need be if (Pk2.DevFile.PartsList[Pk2.ActivePart].BandGapMask > 0) { Pk2.SetMCLRTemp(true); // assert /MCLR to prevent code execution before programming mode entered. Pk2.VddOn(); Pk2.ReadBandGap(); } conditionalVDDOff(); // Add file to quick hex import links bool fallThru = false; bool foundFile = false; do { if ((openHexFileDialog.FileName == hex4) || fallThru) { if ((!hex4ToolStripMenuItem.Visible) && (hex3.Length > 3)) { hex4ToolStripMenuItem.Visible = true; } hex4 = hex3; hex4ToolStripMenuItem.Text = "&4" + hex3ToolStripMenuItem.Text.Substring(2); fallThru = true; foundFile = true; } if ((openHexFileDialog.FileName == hex3) || fallThru) { if ((!hex3ToolStripMenuItem.Visible) && (hex2.Length > 3)) { hex3ToolStripMenuItem.Visible = true; } hex3 = hex2; hex3ToolStripMenuItem.Text = "&3" + hex2ToolStripMenuItem.Text.Substring(2); fallThru = true; foundFile = true; } if ((openHexFileDialog.FileName == hex2) || fallThru) { if ((!hex2ToolStripMenuItem.Visible) && (hex1.Length > 3)) { hex2ToolStripMenuItem.Visible = true; } hex2 = hex1; hex2ToolStripMenuItem.Text = "&2" + hex1ToolStripMenuItem.Text.Substring(2); foundFile = true; } fallThru = true; if (openHexFileDialog.FileName == hex1) { foundFile = true; } } while (!foundFile); if (!hex1ToolStripMenuItem.Visible) { hex1ToolStripMenuItem.Visible = true; toolStripMenuItem5.Visible = true; } hex1 = openHexFileDialog.FileName; hex1ToolStripMenuItem.Text = "&1 " + shortenHex(openHexFileDialog.FileName); } return checkImportFile; }
private bool preProgrammingCheck(int family) { statusGroupBox.Update(); if (Pk2.LearnMode) { Pk2.SetVDDVoltage((float)numUpDnVDD.Value, 0.85F); // ensure voltage set to expected value. return true; } if (!detectPICkit2(KONST.NoMessage, false)) { return false; } if (checkForPowerErrors()) { updateGUI(KONST.DontUpdateMemDisplays); return false; } lookForPoweredTarget(KONST.ShowMessage & !timerAutoImportWrite.Enabled); // don't show message if AutoImportWrite mode enabled. if (Pk2.DevFile.Families[family].PartDetect) { if (Pk2.DetectDevice(family, false, chkBoxVddOn.Checked)) { setGUIVoltageLimits(false); fullEnableGUIControls(); updateGUI(KONST.DontUpdateMemDisplays); } else { semiEnableGUIControls(); statusWindowColor = Constants.StatusColor.yellow; displayStatusWindow.Text = "No device detected."; if (Pk2.DevFile.Families[family].Vpp < 1) {// PIC18J, PIC24, dsPIC33 // remind about VCAP displayStatusWindow.Text += "\nEnsure proper capacitance on VDDCORE/VCAP pin."; } checkForPowerErrors(); updateGUI(KONST.DontUpdateMemDisplays); return false; } } else if ((Pk2.DevFile.Families[family].DeviceIDMask > 0) && deviceVerification) { // device's that have IDs, but are in Manual Select mode. if (!Pk2.VerifyDeviceID(false, chkBoxVddOn.Checked)) { statusWindowColor = Constants.StatusColor.yellow; if ((Pk2.LastDeviceID == 0) || (Pk2.LastDeviceID == Pk2.DevFile.Families[family].DeviceIDMask)) { displayStatusWindow.Text = "No device detected."; } else { displayStatusWindow.Text = "Selected device not detected."; for (int part = 0; part < Pk2.DevFile.PartsList.Length; part ++) { if ((Pk2.DevFile.PartsList[part].Family == family) && (Pk2.DevFile.PartsList[part].DeviceID == Pk2.LastDeviceID)) { displayStatusWindow.Text += "\nDetected a " + Pk2.DevFile.PartsList[part].PartName + " instead."; break; } } } checkForPowerErrors(); updateGUI(KONST.DontUpdateMemDisplays); return false; } } else { Pk2.SetMCLRTemp(true); // assert /MCLR to prevent code execution before programming mode entered. Pk2.SetVDDVoltage((float)numUpDnVDD.Value, 0.85F); // ensure voltage set Pk2.VddOn(); Pk2.RunScript(KONST.PROG_ENTRY, 1); Thread.Sleep(300); // give some delay for error to develop Pk2.RunScript(KONST.PROG_EXIT, 1); conditionalVDDOff(); if (checkForPowerErrors()) { updateGUI(KONST.DontUpdateMemDisplays); return false; } } Pk2.SetVDDVoltage((float)numUpDnVDD.Value, 0.85F); // ensure voltage set to expected value. if (!checkBoxEEMem.Enabled && (Pk2.DevFile.PartsList[Pk2.ActivePart].EEMem > 0)) { // if previous part had no EEPROM and this one does, make sure EE checkbox is checked // otherwise EEPROM won't be read or imported if these are the first operations with a new part. checkBoxEEMem.Checked = true; } return true; }
private bool eeprom_CheckBusErrors() { if (Pk2.BusErrorCheck()) { Pk2.RunScript(KONST.PROG_EXIT, 1); conditionalVDDOff(); if (Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigMasks[KONST.PROTOCOL_CFG] == KONST.UNIO_BUS) { displayStatusWindow.Text = "UNI/O Bus Error (NoSAK) - Aborted.\n"; } else { displayStatusWindow.Text = "I2C Bus Error (No Acknowledge) - Aborted.\n"; } statusWindowColor = Constants.StatusColor.yellow; updateGUI(KONST.UpdateMemoryDisplays); return true; } return false; // no errors }
private void eraseDeviceAll(bool forceOSSCAL, uint[] calWords) { if (Pk2.FamilyIsKeeloq() || Pk2.FamilyIsMCP()) { displayStatusWindow.Text = "Erase not supported for this device type."; statusWindowColor = Constants.StatusColor.yellow; updateGUI(KONST.DontUpdateMemDisplays); return; // abort } if (!preProgrammingCheck(Pk2.GetActiveFamily())) { return; // abort } //Save off the buffers if not clearing them DeviceData savedBuffers = Pk2.CloneBuffers(Pk2.DeviceBuffers); if ((Pk2.FamilyIsEEPROM()) && (Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigMasks[KONST.PROTOCOL_CFG] != KONST.MICROWIRE_BUS) && (Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigMasks[KONST.PROTOCOL_CFG] != KONST.UNIO_BUS)) { // Microwire / UNIO have a true "chip erase". Other devices must write every byte blank. Pk2.ResetBuffers(); // just erase buffers checkImportFile = false; if (!eepromWrite(KONST.EraseEE)) // and write blank value. { if (!toolStripMenuItemClearBuffersErase.Checked) {// restore buffers Pk2.DeviceBuffers = savedBuffers; } return; //abort. } displayStatusWindow.Text += "Complete"; //displayStatusWindow.Update(); if (!toolStripMenuItemClearBuffersErase.Checked) {// restore buffers Pk2.DeviceBuffers = savedBuffers; } else { displayDataSource.Text = "None (Empty/Erased)"; } updateGUI(KONST.UpdateMemoryDisplays); return; } if (!checkEraseVoltage(false)) { return; // abort } progressBar1.Value = 0; // reset bar Pk2.SetMCLRTemp(true); // assert /MCLR to prevent code execution before programming mode entered. Pk2.VddOn(); // Get OSCCAL if need be if ((Pk2.DevFile.PartsList[Pk2.ActivePart].OSSCALSave) && !forceOSSCAL) { // if forcing OSCCAL, don't read it; use the value in memory. Pk2.ReadOSSCAL(); // verify OSCCAL if (!verifyOSCCAL()) { return; } } uint oscCal = Pk2.DeviceBuffers.OSCCAL; // Get BandGap if need be if (Pk2.DevFile.PartsList[Pk2.ActivePart].BandGapMask > 0) { Pk2.ReadBandGap(); } uint bandGap = Pk2.DeviceBuffers.BandGap; displayStatusWindow.Text = "Erasing device..."; //displayStatusWindow.Update(); this.Update(); // dsPIC30F5011, 5013 need configs cleared before erase // but don't run this script if a row erase is defined if ((Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigMemEraseScript > 0) && (Pk2.DevFile.PartsList[Pk2.ActivePart].DebugRowEraseSize == 0)) { Pk2.RunScript(KONST.PROG_ENTRY, 1); if (Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigWrPrepScript > 0) { Pk2.DownloadAddress3(0); Pk2.RunScript(KONST.CONFIG_WR_PREP, 1); } Pk2.ExecuteScript(Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigMemEraseScript); Pk2.RunScript(KONST.PROG_EXIT, 1); } Pk2.RunScript(KONST.PROG_ENTRY, 1); if (TestMemoryEnabled && TestMemoryOpen && (calWords.Length > 0)) { // write calibration words in midrange parts byte[] calBytes = new byte[2 * calWords.Length]; for (int werd = 0; werd < calWords.Length; werd++) { calWords[werd] <<= 1; calBytes[2*werd] = (byte) (calWords[werd] & 0xFF); calBytes[(2*werd) + 1] = (byte) (calWords[werd] >> 8); } Pk2.DataClrAndDownload(calBytes, 0); Pk2.RunScript(KONST.OSSCAL_WR, 1); } else { if (Pk2.DevFile.PartsList[Pk2.ActivePart].ChipErasePrepScript > 0) { Pk2.RunScript(KONST.ERASE_CHIP_PREP, 1); } Pk2.RunScript(KONST.ERASE_CHIP, 1); } Pk2.RunScript(KONST.PROG_EXIT, 1); // clear all the buffers Pk2.ResetBuffers(); if (TestMemoryEnabled && TestMemoryOpen && !toolStripMenuItemClearBuffersErase.Checked) { formTestMem.ClearTestMemory(); } // restore OSCCAL if need be if (Pk2.DevFile.PartsList[Pk2.ActivePart].OSSCALSave) { Pk2.DeviceBuffers.OSCCAL = oscCal; savedBuffers.OSCCAL = oscCal; Pk2.WriteOSSCAL(); Pk2.DeviceBuffers.ProgramMemory[Pk2.DeviceBuffers.ProgramMemory.Length - 1] = Pk2.DeviceBuffers.OSCCAL; savedBuffers.ProgramMemory[savedBuffers.ProgramMemory.Length - 1] = Pk2.DeviceBuffers.OSCCAL; } // restore BandGap if need be if (Pk2.DevFile.PartsList[Pk2.ActivePart].BandGapMask > 0) { Pk2.DeviceBuffers.BandGap = bandGap; savedBuffers.BandGap = bandGap; Pk2.WriteConfigOutsideProgMem(false, false); } // write "erased" config words for parts that don't bulk erase configs (ex 18F6520) // Also do this for some parts to match MPLAB (ie PIC18J) if (Pk2.DevFile.PartsList[Pk2.ActivePart].WriteCfgOnErase) { // compute configration information. int configLocation = (int)Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigAddr / Pk2.DevFile.Families[Pk2.GetActiveFamily()].ProgMemHexBytes; int configWords = Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigWords; int endOfBuffer = Pk2.DeviceBuffers.ProgramMemory.Length; if ((configLocation < Pk2.DevFile.PartsList[Pk2.ActivePart].ProgramMem) && (configWords > 0)) {// if config in program memory, set them to clear. uint orMask = 0; if (Pk2.DevFile.Families[Pk2.GetActiveFamily()].BlankValue == 0xFFFF) { // PIC18J orMask = 0xF000; } else { // PIC24FJ orMask = 0xFF0000; } for (int cfg = configWords; cfg > 0; cfg--) { Pk2.DeviceBuffers.ProgramMemory[endOfBuffer - cfg] = Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigBlank[configWords - cfg] | orMask; } writeConfigInsideProgramMem(); } else { Pk2.WriteConfigOutsideProgMem(false, false); } } if (!toolStripMenuItemClearBuffersErase.Checked) {// restore buffers Pk2.DeviceBuffers = savedBuffers; } displayStatusWindow.Text += "Complete"; //displayStatusWindow.Update(); this.Update(); if (toolStripMenuItemClearBuffersErase.Checked) { displayDataSource.Text = "None (Empty/Erased)"; } checkImportFile = false; conditionalVDDOff(); updateGUI(KONST.UpdateMemoryDisplays); }
private bool eepromWrite(bool eraseWrite) { if (!preProgrammingCheck(Pk2.GetActiveFamily())) { return false; // abort } updateGUI(KONST.DontUpdateMemDisplays); this.Update(); if (checkImportFile && !eraseWrite) { FileInfo hexFile = new FileInfo(openHexFileDialog.FileName); if (ImportExportHex.LastWriteTime != hexFile.LastWriteTime) { displayStatusWindow.Text = "Reloading Hex File\n"; //displayStatusWindow.Update(); this.Update(); Thread.Sleep(300); if (!importHexFileGo()) { displayStatusWindow.Text = "Error Loading Hex File: Write aborted.\n"; statusWindowColor = Constants.StatusColor.red; updateGUI(KONST.UpdateMemoryDisplays); return false; } } } Pk2.VddOn(); if (eraseWrite) { displayStatusWindow.Text = "Erasing device:\n"; } else { displayStatusWindow.Text = "Writing device:\n"; } this.Update(); int endOfBuffer = (int)Pk2.DevFile.PartsList[Pk2.ActivePart].ProgramMem; // Write Memory if (checkBoxProgMemEnabled.Checked) { Pk2.RunScript(KONST.PROG_ENTRY, 1); displayStatusWindow.Text += "EEPROM... "; this.Update(); progressBar1.Value = 0; // reset bar int extraBytes = 3; // 3 extra bytes for address. if (Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigMasks[KONST.PROTOCOL_CFG] == KONST.UNIO_BUS) { extraBytes = 4; } int wordsPerWrite = Pk2.DevFile.PartsList[Pk2.ActivePart].ProgMemWrWords; int bytesPerWord = Pk2.DevFile.Families[Pk2.GetActiveFamily()].BytesPerLocation; int dataDownloadSize = KONST.DownLoadBufferSize; if (endOfBuffer < dataDownloadSize) { dataDownloadSize = endOfBuffer + (endOfBuffer / (wordsPerWrite * bytesPerWord)) * extraBytes; } if (dataDownloadSize > KONST.DownLoadBufferSize) { dataDownloadSize = KONST.DownLoadBufferSize; } int scriptRunsToUseDownload = dataDownloadSize / ((wordsPerWrite * bytesPerWord) + extraBytes); int wordsPerLoop = scriptRunsToUseDownload * wordsPerWrite; int wordsWritten = 0; progressBar1.Maximum = (int)endOfBuffer / wordsPerLoop; byte[] downloadBuffer = new byte[KONST.DownLoadBufferSize]; if (Pk2.DevFile.PartsList[Pk2.ActivePart].ProgMemWrPrepScript != 0) { // if prog mem write prep script exists for this part Pk2.RunScript(KONST.PROGMEM_WR_PREP, 1); } do { int downloadIndex = 0; for (int word = 0; word < wordsPerLoop; word++) { if (wordsWritten == endOfBuffer) { // we may not have filled download buffer, so adjust number of script runs scriptRunsToUseDownload = downloadIndex / ((wordsPerWrite * bytesPerWord) + extraBytes); break; // for cases where ProgramMemSize%WordsPerLoop != 0 } if ((wordsWritten % wordsPerWrite) == 0) { // beginning of each script call // EEPROM address in download buffer int eeAddress = eeprom24BitAddress(wordsWritten, KONST.WRITE_BIT); if (Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigMasks[KONST.PROTOCOL_CFG] == KONST.UNIO_BUS) { downloadBuffer[downloadIndex++] = 0x96; //WREN } downloadBuffer[downloadIndex++] = (byte)((eeAddress >> 16) & 0xFF); // upper byte downloadBuffer[downloadIndex++] = (byte)((eeAddress >> 8) & 0xFF); // high byte downloadBuffer[downloadIndex++] = (byte)(eeAddress & 0xFF); // low byte } uint memWord = Pk2.DeviceBuffers.ProgramMemory[wordsWritten++]; downloadBuffer[downloadIndex++] = (byte)(memWord & 0xFF); for (int bite = 1; bite < bytesPerWord; bite++) { memWord >>= 8; downloadBuffer[downloadIndex++] = (byte)(memWord & 0xFF); } if ((Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigMasks[KONST.PROTOCOL_CFG] == KONST.MICROWIRE_BUS) && (bytesPerWord == 2)) { // "Endian-ness" of Microwire 16-bit words need to be swapped byte swapTemp = downloadBuffer[downloadIndex - 2]; downloadBuffer[downloadIndex - 2] = downloadBuffer[downloadIndex - 1]; downloadBuffer[downloadIndex - 1] = swapTemp; } } // download data int dataIndex = Pk2.DataClrAndDownload(downloadBuffer, 0); while (dataIndex < downloadIndex) { dataIndex = Pk2.DataDownload(downloadBuffer, dataIndex, downloadIndex); } Pk2.RunScript(KONST.PROGMEM_WR, scriptRunsToUseDownload); if ((Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigMasks[KONST.PROTOCOL_CFG] == KONST.I2C_BUS) || (Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigMasks[KONST.PROTOCOL_CFG] == KONST.UNIO_BUS)) { if (eeprom_CheckBusErrors()) { return false; } } progressBar1.PerformStep(); } while (wordsWritten < endOfBuffer); } Pk2.RunScript(KONST.PROG_EXIT, 1); //Verify bool verifySuccess = true; if (verifyOnWriteToolStripMenuItem.Checked && !eraseWrite) { if (Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigMasks[KONST.PROTOCOL_CFG] == KONST.UNIO_BUS) conditionalVDDOff(); // POR for UNIO verifySuccess = deviceVerify(true, (endOfBuffer - 1), false); } conditionalVDDOff(); if (verifySuccess && !eraseWrite) { statusWindowColor = Constants.StatusColor.green; displayStatusWindow.Text = "Programming Successful.\n"; updateGUI(KONST.UpdateMemoryDisplays); return true; } return verifySuccess; }
private bool deviceWrite() { uint checksumPk2Go = 0; if (Pk2.FamilyIsEEPROM()) { return eepromWrite(KONST.WriteEE); } bool useLowVoltageRowErase = false; if (!preProgrammingCheck(Pk2.GetActiveFamily())) { return false; // abort } if (!checkEraseVoltage(true) && !Pk2.FamilyIsPIC32()) { if (Pk2.DevFile.PartsList[Pk2.ActivePart].DebugRowEraseScript > 0) { // if device supports row erases, use them useLowVoltageRowErase = true; } else { return false; // abort } } updateGUI(KONST.DontUpdateMemDisplays); this.Update(); if (checkImportFile && !Pk2.LearnMode) { FileInfo hexFile = new FileInfo(openHexFileDialog.FileName); if (ImportExportHex.LastWriteTime != hexFile.LastWriteTime) { displayStatusWindow.Text = "Reloading Hex File\n"; //displayStatusWindow.Update(); this.Update(); Thread.Sleep(300); if (!importHexFileGo()) { displayStatusWindow.Text = "Error Loading Hex File: Write aborted.\n"; statusWindowColor = Constants.StatusColor.red; updateGUI(KONST.UpdateMemoryDisplays); return false; } } } if (Pk2.FamilyIsPIC32()) { if (P32.P32Write(verifyOnWriteToolStripMenuItem.Checked, enableCodeProtectToolStripMenuItem.Checked)) { statusWindowColor = Constants.StatusColor.green; conditionalVDDOff(); updateGUI(KONST.UpdateMemoryDisplays); return true; } else { statusWindowColor = Constants.StatusColor.red; conditionalVDDOff(); updateGUI(KONST.UpdateMemoryDisplays); return true; } } Pk2.SetMCLRTemp(true); // assert /MCLR to prevent code execution before programming mode entered. Pk2.VddOn(); //Thread.Sleep(100); // check device ID in LearnMode if (Pk2.LearnMode && (Pk2.DevFile.Families[Pk2.GetActiveFamily()].DeviceIDMask > 0)) { Pk2.MetaCmd_CHECK_DEVICE_ID(); } // Get OSCCAL if need be if (Pk2.DevFile.PartsList[Pk2.ActivePart].OSSCALSave) { if (Pk2.LearnMode) { // leave OSCCAL blank so we can rewrite it later. Pk2.DeviceBuffers.ProgramMemory[Pk2.DeviceBuffers.ProgramMemory.Length - 1] = Pk2.DevFile.Families[Pk2.GetActiveFamily()].BlankValue; // Send meta command Pk2.MetaCmd_READ_OSCCAL(); } else { // normal operation Pk2.ReadOSSCAL(); // put OSCCAL into part memory so it doesn't have to be written seperately. Pk2.DeviceBuffers.ProgramMemory[Pk2.DeviceBuffers.ProgramMemory.Length - 1] = Pk2.DeviceBuffers.OSCCAL; // verify OSCCAL if (!verifyOSCCAL()) { return false; } } } uint oscCal = Pk2.DeviceBuffers.OSCCAL; // Get BandGap if need be if (Pk2.DevFile.PartsList[Pk2.ActivePart].BandGapMask > 0) { if (Pk2.LearnMode) { Pk2.MetaCmd_READ_BANDGAP(); } else { Pk2.ReadBandGap(); } } uint bandGap = Pk2.DeviceBuffers.BandGap; // Erase Device First bool reWriteEE = false; if (checkBoxProgMemEnabled.Checked && (checkBoxEEMem.Checked || !checkBoxEEMem.Enabled)) { // chip erase when programming all if (useLowVoltageRowErase) { // use row erases displayStatusWindow.Text = "Erasing Part with Low Voltage Row Erase...\n"; this.Update(); Pk2.RowEraseDevice(); } else { // bulk erase // dsPIC30F5011, 5013 need configs cleared before erase // but don't run this script if a row erase is defined if ((Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigMemEraseScript > 0) && (Pk2.DevFile.PartsList[Pk2.ActivePart].DebugRowEraseSize == 0)) { Pk2.RunScript(KONST.PROG_ENTRY, 1); if (Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigWrPrepScript > 0) { Pk2.DownloadAddress3(0); Pk2.RunScript(KONST.CONFIG_WR_PREP, 1); } Pk2.ExecuteScript(Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigMemEraseScript); Pk2.RunScript(KONST.PROG_EXIT, 1); } Pk2.RunScript(KONST.PROG_ENTRY, 1); if (Pk2.DevFile.PartsList[Pk2.ActivePart].ChipErasePrepScript > 0) { Pk2.RunScript(KONST.ERASE_CHIP_PREP, 1); } Pk2.RunScript(KONST.ERASE_CHIP, 1); Pk2.RunScript(KONST.PROG_EXIT, 1); } } else if (checkBoxProgMemEnabled.Checked &&(Pk2.DevFile.PartsList[Pk2.ActivePart].ProgMemEraseScript != 0)) { // Don't erase EE when not selected. if (useLowVoltageRowErase) { // use row erases displayStatusWindow.Text = "Erasing Part with Low Voltage Row Erase...\n"; this.Update(); Pk2.RowEraseDevice(); } else { // bulk erases Pk2.RunScript(KONST.PROG_ENTRY, 1); Pk2.RunScript(KONST.ERASE_PROGMEM, 1); Pk2.RunScript(KONST.PROG_EXIT, 1); } } else if (checkBoxEEMem.Checked &&(Pk2.DevFile.PartsList[Pk2.ActivePart].EEMemEraseScript != 0)) { // Some parts must have EE bulk erased before being re-written. Pk2.RunScript(KONST.PROG_ENTRY, 1); Pk2.RunScript(KONST.ERASE_EE, 1); Pk2.RunScript(KONST.PROG_EXIT, 1); } else if ((!checkBoxEEMem.Checked && checkBoxEEMem.Enabled) && Pk2.DevFile.PartsList[Pk2.ActivePart].ProgMemEraseScript == 0) {// Some parts cannot erase ProgMem, UserID, & config without erasing EE // so must read & re-write EE. displayStatusWindow.Text = "Reading device:\n"; this.Update(); readEEPROM(); updateGUI(true); if (useLowVoltageRowErase) { // use row erases displayStatusWindow.Text = "Erasing Part with Low Voltage Row Erase...\n"; this.Update(); Pk2.RowEraseDevice(); } else { // bulk erase // dsPIC30F5011, 5013 need configs cleared before erase // but don't run this script if a row erase is defined if ((Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigMemEraseScript > 0) && (Pk2.DevFile.PartsList[Pk2.ActivePart].DebugRowEraseSize == 0)) { Pk2.RunScript(KONST.PROG_ENTRY, 1); if (Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigWrPrepScript > 0) { Pk2.DownloadAddress3(0); Pk2.RunScript(KONST.CONFIG_WR_PREP, 1); } Pk2.ExecuteScript(Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigMemEraseScript); Pk2.RunScript(KONST.PROG_EXIT, 1); } Pk2.RunScript(KONST.PROG_ENTRY, 1); if (Pk2.DevFile.PartsList[Pk2.ActivePart].ChipErasePrepScript > 0) { Pk2.RunScript(KONST.ERASE_CHIP_PREP, 1); } Pk2.RunScript(KONST.ERASE_CHIP, 1); Pk2.RunScript(KONST.PROG_EXIT, 1); reWriteEE = true; } } displayStatusWindow.Text = "Writing device:\n"; //displayStatusWindow.Update(); this.Update(); bool configInProgramSpace = false; // compute configration information. int configLocation = (int)Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigAddr / Pk2.DevFile.Families[Pk2.GetActiveFamily()].ProgMemHexBytes; int configWords = Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigWords; int endOfBuffer = Pk2.DeviceBuffers.ProgramMemory.Length; uint[] configBackups = new uint[configWords]; // use because DevicBuffers are masked & won't verify later if ((configLocation < Pk2.DevFile.PartsList[Pk2.ActivePart].ProgramMem) && (configWords > 0)) {// if config in program memory, set them to clear. configInProgramSpace = true; for (int cfg = configWords; cfg > 0; cfg--) { configBackups[cfg - 1] = Pk2.DeviceBuffers.ProgramMemory[endOfBuffer - cfg]; Pk2.DeviceBuffers.ProgramMemory[endOfBuffer - cfg] = Pk2.DevFile.Families[Pk2.GetActiveFamily()].BlankValue; } } endOfBuffer--; // Write Program Memory if (checkBoxProgMemEnabled.Checked) { displayStatusWindow.Text += "Program Memory... "; //displayStatusWindow.Update(); this.Update(); progressBar1.Value = 0; // reset bar Pk2.RunScript(KONST.PROG_ENTRY, 1); if (Pk2.DevFile.PartsList[Pk2.ActivePart].ProgMemWrPrepScript != 0) { // if prog mem address set script exists for this part Pk2.DownloadAddress3(0); //Pk2.DownloadAddress3(0x200000); Pk2.RunScript(KONST.PROGMEM_WR_PREP, 1); } if (Pk2.FamilyIsKeeloq()) { Pk2.HCS360_361_VppSpecial(); } int wordsPerWrite = Pk2.DevFile.PartsList[Pk2.ActivePart].ProgMemWrWords; int bytesPerWord = Pk2.DevFile.Families[Pk2.GetActiveFamily()].BytesPerLocation; int scriptRunsToUseDownload = KONST.DownLoadBufferSize / (wordsPerWrite * bytesPerWord); int wordsPerLoop = scriptRunsToUseDownload * wordsPerWrite; int wordsWritten = 0; // Find end of used memory endOfBuffer = Pk2.FindLastUsedInBuffer(Pk2.DeviceBuffers.ProgramMemory, Pk2.DevFile.Families[Pk2.GetActiveFamily()].BlankValue, endOfBuffer); if (((wordsPerWrite == (endOfBuffer +1)) || (wordsPerLoop > (endOfBuffer +1))) && !Pk2.LearnMode) { // very small memory sizes (like HCS parts) scriptRunsToUseDownload = 1; wordsPerLoop = wordsPerWrite; } // align end on next loop boundary int writes = (endOfBuffer + 1) / wordsPerLoop; if (((endOfBuffer + 1) % wordsPerLoop) > 0) { writes++; } endOfBuffer = writes * wordsPerLoop; progressBar1.Maximum = (int)endOfBuffer / wordsPerLoop; if (useProgExec33()) { if (!PE33.PE33Write(endOfBuffer, displayStatusWindow.Text)) { statusWindowColor = Constants.StatusColor.red; conditionalVDDOff(); updateGUI(KONST.UpdateMemoryDisplays); return false; } } else if (useProgExec24F()) { if (!PE24.PE24FWrite(endOfBuffer, displayStatusWindow.Text, verifyOnWriteToolStripMenuItem.Checked)) { statusWindowColor = Constants.StatusColor.red; conditionalVDDOff(); updateGUI(KONST.UpdateMemoryDisplays); return false; } } else { byte[] downloadBuffer = new byte[KONST.DownLoadBufferSize]; do { int downloadIndex = 0; for (int word = 0; word < wordsPerLoop; word++) { if (wordsWritten == endOfBuffer) { break; // for cases where ProgramMemSize%WordsPerLoop != 0 } uint memWord = Pk2.DeviceBuffers.ProgramMemory[wordsWritten++]; if (Pk2.DevFile.Families[Pk2.GetActiveFamily()].ProgMemShift > 0) { memWord = memWord << 1; } downloadBuffer[downloadIndex++] = (byte) (memWord & 0xFF); checksumPk2Go += (byte) (memWord & 0xFF); for (int bite = 1; bite < bytesPerWord; bite++) { memWord >>= 8; downloadBuffer[downloadIndex++] = (byte) (memWord & 0xFF); checksumPk2Go += (byte)(memWord & 0xFF); } } // download data if (Pk2.FamilyIsKeeloq()) { processKeeloqData(ref downloadBuffer, wordsWritten); } int dataIndex = Pk2.DataClrAndDownload(downloadBuffer, 0); while (dataIndex < downloadIndex) { dataIndex = Pk2.DataDownload(downloadBuffer, dataIndex, downloadIndex); } Pk2.RunScript(KONST.PROGMEM_WR, scriptRunsToUseDownload); if (((wordsWritten % 0x8000) == 0) && (Pk2.DevFile.PartsList[Pk2.ActivePart].ProgMemWrPrepScript != 0)) { //PIC24 must update TBLPAG Pk2.DownloadAddress3(0x10000 * (wordsWritten / 0x8000)); Pk2.RunScript(KONST.PROGMEM_WR_PREP, 1); } progressBar1.PerformStep(); } while (wordsWritten < endOfBuffer); Pk2.RunScript(KONST.PROG_EXIT, 1); } } int verifyStop = endOfBuffer; if (configInProgramSpace) {// if config in program memory, restore prog memory to proper values. for (int cfg = configWords; cfg > 0; cfg--) { Pk2.DeviceBuffers.ProgramMemory[Pk2.DeviceBuffers.ProgramMemory.Length - cfg] = configBackups[cfg - 1]; } } // Write EEPROM if (((checkBoxEEMem.Checked) || reWriteEE) && (Pk2.DevFile.PartsList[Pk2.ActivePart].EEMem > 0)) { displayStatusWindow.Text += "EE... "; this.Update(); Pk2.RunScript(KONST.PROG_ENTRY, 1); if (Pk2.DevFile.PartsList[Pk2.ActivePart].EEWrPrepScript > 1) { if (Pk2.DevFile.Families[Pk2.GetActiveFamily()].EEMemHexBytes == 4) { // 16-bit parts Pk2.DownloadAddress3((int)(Pk2.DevFile.PartsList[Pk2.ActivePart].EEAddr / 2)); } else { Pk2.DownloadAddress3(0); } Pk2.RunScript(KONST.EE_WR_PREP, 1); } int bytesPerWord = Pk2.DevFile.Families[Pk2.GetActiveFamily()].EEMemBytesPerWord; uint eeBlank = getEEBlank(); // write at least 16 locations per loop int locationsPerLoop = Pk2.DevFile.PartsList[Pk2.ActivePart].EEWrLocations; if (locationsPerLoop < 16) { locationsPerLoop = 16; } // find end of used EE if (checkBoxProgMemEnabled.Checked && !useLowVoltageRowErase && !Pk2.LearnMode) { // we're writing all, so EE is erased first, we can skip blank locations at end // unless we're using LVRowErase in which we need to write all as EE isn't erased first. endOfBuffer = Pk2.FindLastUsedInBuffer(Pk2.DeviceBuffers.EEPromMemory, eeBlank, Pk2.DeviceBuffers.EEPromMemory.Length - 1); } else { // if we're only writing EE, must write blanks in case they aren't blank on device endOfBuffer = Pk2.DeviceBuffers.EEPromMemory.Length - 1; } // align end on next loop boundary int writes = (endOfBuffer + 1) / locationsPerLoop; if (((endOfBuffer + 1) % locationsPerLoop) > 0) { writes++; } endOfBuffer = writes * locationsPerLoop; byte[] downloadBuffer = new byte[(locationsPerLoop * bytesPerWord)]; int scriptRunsPerLoop = locationsPerLoop / Pk2.DevFile.PartsList[Pk2.ActivePart].EEWrLocations; int locationsWritten = 0; progressBar1.Value = 0; // reset bar progressBar1.Maximum = (int)endOfBuffer / locationsPerLoop; do { int downloadIndex = 0; for (int word = 0; word < locationsPerLoop; word++) { uint eeWord = Pk2.DeviceBuffers.EEPromMemory[locationsWritten++]; if (Pk2.DevFile.Families[Pk2.GetActiveFamily()].ProgMemShift > 0) { eeWord = eeWord << 1; } downloadBuffer[downloadIndex++] = (byte)(eeWord & 0xFF); checksumPk2Go += (byte)(eeWord & 0xFF); for (int bite = 1; bite < bytesPerWord; bite++) { eeWord >>= 8; downloadBuffer[downloadIndex++] = (byte)(eeWord & 0xFF); checksumPk2Go += (byte)(eeWord & 0xFF); } } // download data Pk2.DataClrAndDownload(downloadBuffer, 0); Pk2.RunScript(KONST.EE_WR, scriptRunsPerLoop); progressBar1.PerformStep(); } while (locationsWritten < endOfBuffer); Pk2.RunScript(KONST.PROG_EXIT, 1); } // Write UserIDs if (checkBoxProgMemEnabled.Checked && (Pk2.DevFile.PartsList[Pk2.ActivePart].UserIDWords > 0)) { // do not write if EE unselected as PIC18F cannot erase/write UserIDs except with ChipErase displayStatusWindow.Text += "UserIDs... "; this.Update(); Pk2.RunScript(KONST.PROG_ENTRY, 1); if (Pk2.DevFile.PartsList[Pk2.ActivePart].UserIDWrPrepScript > 0) { Pk2.RunScript(KONST.USERID_WR_PREP, 1); } int bytesPerID = Pk2.DevFile.Families[Pk2.GetActiveFamily()].UserIDBytes; byte[] downloadBuffer = new byte[Pk2.DevFile.PartsList[Pk2.ActivePart].UserIDWords * bytesPerID]; int downloadIndex = 0; int idWritten = 0; for (int word = 0; word < Pk2.DevFile.PartsList[Pk2.ActivePart].UserIDWords; word++) { uint memWord = Pk2.DeviceBuffers.UserIDs[idWritten++]; if (Pk2.DevFile.Families[Pk2.GetActiveFamily()].ProgMemShift > 0) { memWord = memWord << 1; } downloadBuffer[downloadIndex++] = (byte)(memWord & 0xFF); checksumPk2Go += (byte)(memWord & 0xFF); for (int bite = 1; bite < bytesPerID; bite++) { memWord >>= 8; downloadBuffer[downloadIndex++] = (byte)(memWord & 0xFF); checksumPk2Go += (byte)(memWord & 0xFF); } } // download data int dataIndex = Pk2.DataClrAndDownload(downloadBuffer, 0); while (dataIndex < downloadIndex) { dataIndex = Pk2.DataDownload(downloadBuffer, dataIndex, downloadIndex); } Pk2.RunScript(KONST.USERID_WR, 1); Pk2.RunScript(KONST.PROG_EXIT, 1); } bool verifySuccess = true; // Verify all but config (since hasn't been written as may contain code protection settings. if (configInProgramSpace) {// if config in program memory, don't verify configs. if (Pk2.LearnMode) { if (verifyStop == Pk2.DevFile.PartsList[Pk2.ActivePart].ProgramMem) { // last block of program memory where config words are if (Pk2.DevFile.Families[Pk2.GetActiveFamily()].BlankValue > 0xFFFF) { // 24FJ checksumPk2Go -= 0x80; // adjust for "7F" in blank configs. } else { // 18J checksumPk2Go -= 0x08; // adjust for "F7" in blank configs. } } } else { // normal verifyStop = verifyStop - configWords; } } if (verifyOnWriteToolStripMenuItem.Checked) { if (Pk2.LearnMode) { Pk2.MetaCmd_START_CHECKSUM(); } verifySuccess = deviceVerify(true, (verifyStop - 1), reWriteEE); if (Pk2.LearnMode) { Pk2.MetaCmd_VERIFY_CHECKSUM(checksumPk2Go); checksumPk2Go = 0; // clear for config check } } if (Pk2.LearnMode && Pk2.DevFile.PartsList[Pk2.ActivePart].OSSCALSave) { Pk2.MetaCmd_WRITE_OSCCAL(); // restore OSCCAL Pk2.DeviceBuffers.ProgramMemory[Pk2.DeviceBuffers.ProgramMemory.Length - 1] = Pk2.DeviceBuffers.OSCCAL; } // WRITE CONFIGURATION if (verifySuccess) { // if we've failed verification, don't try to finish write // Write Configuration if ((configWords > 0) && (!configInProgramSpace) && checkBoxProgMemEnabled.Checked) { // Write config words differently for any part where they are stored in program memory. if (!verifyOnWriteToolStripMenuItem.Checked) { displayStatusWindow.Text += "Config... "; //displayStatusWindow.Update(); this.Update(); } // 18F devices create a problem as the WRTC bit in the next to last config word // is effective immediately upon being written, which if asserted prevents the // last config word from being written. // To get around this, we're using a bit of hack. Detect PIC18F or PIC18F_K_parts, // and look for WRTC = 0. If found, write config words once with CONFIG6 = 0xFFFF // then re-write it with the correct value. if ((Pk2.DevFile.Families[Pk2.GetActiveFamily()].FamilyName == "PIC18F") || (Pk2.DevFile.Families[Pk2.GetActiveFamily()].FamilyName == "PIC18F_K_")) { if (Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigWords > 5) { // don't blow up if part doesn't have enough config words if ((Pk2.DeviceBuffers.ConfigWords[5] & ~0x2000) == Pk2.DeviceBuffers.ConfigWords[5]) { // if WRTC is asserted uint saveConfig6 = Pk2.DeviceBuffers.ConfigWords[5]; Pk2.DeviceBuffers.ConfigWords[5] = 0xFFFF; Pk2.WriteConfigOutsideProgMem(false, false); // no protects Pk2.DeviceBuffers.ConfigWords[5] = saveConfig6; } } } checksumPk2Go += Pk2.WriteConfigOutsideProgMem((enableCodeProtectToolStripMenuItem.Enabled && enableCodeProtectToolStripMenuItem.Checked), (enableDataProtectStripMenuItem.Enabled && enableDataProtectStripMenuItem.Checked)); //Adjust for some PIC18F masked config bits that remain set. if (Pk2.DevFile.Families[Pk2.GetActiveFamily()].BlankValue == 0xFFFF) { checksumPk2Go += Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigMasks[7]; } // Verify Configuration if (verifyOnWriteToolStripMenuItem.Checked) { if (!Pk2.LearnMode || (Pk2.DevFile.PartsList[Pk2.ActivePart].BandGapMask == 0)) { bool verifyGood = verifyConfig(configWords, configLocation); if (verifyGood) { statusWindowColor = Constants.StatusColor.green; displayStatusWindow.Text = "Programming Successful.\n"; } else if (!Pk2.LearnMode) { statusWindowColor = Constants.StatusColor.red; verifySuccess = false; } if (Pk2.LearnMode && (Pk2.DevFile.PartsList[Pk2.ActivePart].BandGapMask == 0)) { Pk2.MetaCmd_VERIFY_CHECKSUM(checksumPk2Go); } } } } else if ((configWords > 0) && checkBoxProgMemEnabled.Checked) { // for parts where config resides in program memory. // program last memory block. if (!verifyOnWriteToolStripMenuItem.Checked) { displayStatusWindow.Text += "Config... "; //displayStatusWindow.Update(); this.Update(); } for (int i = 0; i < configWords; i++) { // Set code protects & blank masks if (i == Pk2.DevFile.PartsList[Pk2.ActivePart].CPConfig - 1) { if (enableCodeProtectToolStripMenuItem.Enabled && enableCodeProtectToolStripMenuItem.Checked) { Pk2.DeviceBuffers.ProgramMemory[configLocation + i] &= (uint)~Pk2.DevFile.PartsList[Pk2.ActivePart].CPMask; } if (enableDataProtectStripMenuItem.Enabled && enableDataProtectStripMenuItem.Checked) { Pk2.DeviceBuffers.ProgramMemory[configLocation + i] &= (uint)~Pk2.DevFile.PartsList[Pk2.ActivePart].DPMask; } } } writeConfigInsideProgramMem(); if (verifyOnWriteToolStripMenuItem.Checked) { statusWindowColor = Constants.StatusColor.green; displayStatusWindow.Text = "Programming Successful.\n"; } else { verifySuccess = false; } } else if (!checkBoxProgMemEnabled.Checked) { statusWindowColor = Constants.StatusColor.green; displayStatusWindow.Text = "Programming Successful.\n"; } else { // HCS parts statusWindowColor = Constants.StatusColor.green; displayStatusWindow.Text = "Programming Successful.\n"; } // TEST for DEBUG vector write //Pk2.WriteDebugVector(0x12343ABC); conditionalVDDOff(); if (!verifyOnWriteToolStripMenuItem.Checked) { displayStatusWindow.Text += "Done."; } if (Pk2.LearnMode) { displayStatusWindow.Text = "Programmer-To-Go download complete."; } updateGUI(KONST.UpdateMemoryDisplays); return verifySuccess; } return false; // verifySuccess false }
private bool deviceVerify(bool writeVerify, int lastLocation, bool forceEEVerify) { if (!writeVerify) { // only check if "stand-alone" verify if (!preProgrammingCheck(Pk2.GetActiveFamily())) { return false; // abort } } if (Pk2.FamilyIsPIC32()) { if (P32.P32Verify(writeVerify, enableCodeProtectToolStripMenuItem.Checked)) { statusWindowColor = Constants.StatusColor.green; conditionalVDDOff(); updateGUI(KONST.UpdateMemoryDisplays); return true; } else { statusWindowColor = Constants.StatusColor.red; conditionalVDDOff(); updateGUI(KONST.UpdateMemoryDisplays); return true; } } displayStatusWindow.Text = "Verifying Device:\n"; //displayStatusWindow.Update(); this.Update(); if (!Pk2.FamilyIsKeeloq() && (!writeVerify || !Pk2.FamilyIsPIC24FJ())) { Pk2.SetMCLRTemp(true); // assert /MCLR to prevent code execution before programming mode entered. } Pk2.VddOn(); byte[] upload_buffer = new byte[KONST.UploadBufferSize]; // compute configration information. int configLocation = (int)Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigAddr / Pk2.DevFile.Families[Pk2.GetActiveFamily()].ProgMemHexBytes; int configWords = Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigWords; int endOfBuffer = Pk2.DeviceBuffers.ProgramMemory.Length - 1; if (writeVerify) { // unless it's a write-verify endOfBuffer = lastLocation; } //Verify Program Memory ---------------------------------------------------------------------------- if (checkBoxProgMemEnabled.Checked) { displayStatusWindow.Text += "Program Memory... "; //displayStatusWindow.Update(); this.Update(); if (useProgExec33()) { if (!PE33.PE33VerifyCRC(displayStatusWindow.Text)) { if (!writeVerify) { displayStatusWindow.Text = "Verification of Program Memory failed.\n"; } else { displayStatusWindow.Text = "Programming of Program Memory failed.\n"; } conditionalVDDOff(); statusWindowColor = Constants.StatusColor.red; updateGUI(KONST.UpdateMemoryDisplays); return false; } } else if (useProgExec24F()) { if (!PE24.PE24FVerify(displayStatusWindow.Text, writeVerify, lastLocation)) { conditionalVDDOff(); statusWindowColor = Constants.StatusColor.red; updateGUI(KONST.UpdateMemoryDisplays); return false; } } else { Pk2.RunScript(KONST.PROG_ENTRY, 1); if ((Pk2.DevFile.PartsList[Pk2.ActivePart].ProgMemAddrSetScript != 0) && (Pk2.DevFile.PartsList[Pk2.ActivePart].ProgMemAddrBytes != 0)) { // if prog mem address set script exists for this part if (Pk2.FamilyIsEEPROM()) { Pk2.DownloadAddress3MSBFirst(eeprom24BitAddress(0, KONST.WRITE_BIT)); Pk2.RunScript(KONST.PROGMEM_ADDRSET, 1); if (!writeVerify) { if (eeprom_CheckBusErrors()) return false; } } else { Pk2.DownloadAddress3(0); Pk2.RunScript(KONST.PROGMEM_ADDRSET, 1); } } int bytesPerWord = Pk2.DevFile.Families[Pk2.GetActiveFamily()].BytesPerLocation; int scriptRunsToFillUpload = KONST.UploadBufferSize / (Pk2.DevFile.PartsList[Pk2.ActivePart].ProgMemRdWords * bytesPerWord); int wordsPerLoop = scriptRunsToFillUpload * Pk2.DevFile.PartsList[Pk2.ActivePart].ProgMemRdWords; int wordsRead = 0; if (Pk2.DevFile.PartsList[Pk2.ActivePart].ProgMemRdWords == (endOfBuffer + 1)) { // very small memory sizes (like HCS parts) scriptRunsToFillUpload = 1; wordsPerLoop = endOfBuffer + 1; } progressBar1.Value = 0; // reset bar progressBar1.Maximum = (int)endOfBuffer / wordsPerLoop; do { if (Pk2.FamilyIsEEPROM()) { if ((Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigMasks[KONST.PROTOCOL_CFG] == KONST.I2C_BUS) && (wordsRead > Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigMasks[KONST.ADR_MASK_CFG]) && (wordsRead % (Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigMasks[KONST.ADR_MASK_CFG]+1) ==0)) { Pk2.DownloadAddress3MSBFirst(eeprom24BitAddress(wordsRead, KONST.WRITE_BIT)); Pk2.RunScript(KONST.PROGMEM_ADDRSET, 1); } Pk2.Download3Multiples(eeprom24BitAddress(wordsRead, KONST.READ_BIT), scriptRunsToFillUpload, Pk2.DevFile.PartsList[Pk2.ActivePart].ProgMemRdWords); } //Pk2.RunScriptUploadNoLen2(KONST.PROGMEM_RD, scriptRunsToFillUpload); Pk2.RunScriptUploadNoLen(KONST.PROGMEM_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; } // swap "Endian-ness" of 16 bit 93LC EEPROMs if ((bytesPerWord == 2) && (Pk2.FamilyIsEEPROM()) && (Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigMasks[KONST.PROTOCOL_CFG] == KONST.MICROWIRE_BUS)) { uint memTemp = 0; memTemp = (memWord << 8) & 0xFF00; memWord >>= 8; memWord |= memTemp; } if ((memWord != Pk2.DeviceBuffers.ProgramMemory[wordsRead++]) && !Pk2.LearnMode) { Pk2.RunScript(KONST.PROG_EXIT, 1); conditionalVDDOff(); if (!writeVerify) { if (Pk2.FamilyIsEEPROM()) { displayStatusWindow.Text = "Verification of EEPROM failed at address\n"; } else { displayStatusWindow.Text = "Verification of Program Memory failed at address\n"; } } else { if (Pk2.FamilyIsEEPROM()) { displayStatusWindow.Text = "Programming failed at EEPROM address\n"; } else { displayStatusWindow.Text = "Programming failed at Program Memory address\n"; } } displayStatusWindow.Text += string.Format("0x{0:X6}", (--wordsRead * Pk2.DevFile.Families[Pk2.GetActiveFamily()].AddressIncrement)); statusWindowColor = Constants.StatusColor.red; updateGUI(KONST.UpdateMemoryDisplays); return false; } if (((wordsRead % 0x8000) == 0) && (Pk2.DevFile.PartsList[Pk2.ActivePart].ProgMemAddrSetScript != 0) && (Pk2.DevFile.PartsList[Pk2.ActivePart].ProgMemAddrBytes != 0) && (Pk2.DevFile.Families[Pk2.GetActiveFamily()].BlankValue > 0xFFFF)) { //PIC24 must update TBLPAG Pk2.DownloadAddress3(0x10000 * (wordsRead / 0x8000)); Pk2.RunScript(KONST.PROGMEM_ADDRSET, 1); break; } if (wordsRead > endOfBuffer) { break; // for cases where ProgramMemSize%WordsPerLoop != 0 } } progressBar1.PerformStep(); } while (wordsRead < endOfBuffer); Pk2.RunScript(KONST.PROG_EXIT, 1); } } //Verify EEPROM ------------------------------------------------------------------------------------ if (((checkBoxEEMem.Checked) || forceEEVerify) && (Pk2.DevFile.PartsList[Pk2.ActivePart].EEMem > 0)) { if (Pk2.LearnMode && (Pk2.DevFile.Families[Pk2.GetActiveFamily()].ProgMemShift > 0)) { Pk2.MetaCmd_CHANGE_CHKSM_FRMT(2); } displayStatusWindow.Text += "EE... "; this.Update(); Pk2.RunScript(KONST.PROG_ENTRY, 1); if (Pk2.DevFile.PartsList[Pk2.ActivePart].EERdPrepScript > 0) { if (Pk2.DevFile.Families[Pk2.GetActiveFamily()].EEMemHexBytes == 4) { // 16-bit parts Pk2.DownloadAddress3((int)(Pk2.DevFile.PartsList[Pk2.ActivePart].EEAddr / 2)); } else { Pk2.DownloadAddress3(0); } Pk2.RunScript(KONST.EE_RD_PREP, 1); } int bytesPerLoc = Pk2.DevFile.Families[Pk2.GetActiveFamily()].EEMemBytesPerWord; int scriptRuns2FillUpload = KONST.UploadBufferSize / (Pk2.DevFile.PartsList[Pk2.ActivePart].EERdLocations * bytesPerLoc); int locPerLoop = scriptRuns2FillUpload * Pk2.DevFile.PartsList[Pk2.ActivePart].EERdLocations; int locsRead = 0; uint eeBlank = getEEBlank(); progressBar1.Value = 0; // reset bar progressBar1.Maximum = (int)Pk2.DevFile.PartsList[Pk2.ActivePart].EEMem / locPerLoop; do { //Pk2.RunScriptUploadNoLen2(KONST.EE_RD, scriptRuns2FillUpload); Pk2.RunScriptUploadNoLen(KONST.EE_RD, scriptRuns2FillUpload); 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 < locPerLoop; word++) { int bite = 0; uint memWord = (uint)upload_buffer[uploadIndex + bite++]; if (bite < bytesPerLoc) { memWord |= (uint)upload_buffer[uploadIndex + bite++] << 8; } uploadIndex += bite; // shift if necessary if (Pk2.DevFile.Families[Pk2.GetActiveFamily()].ProgMemShift > 0) { memWord = (memWord >> 1) & eeBlank; } if ((memWord != Pk2.DeviceBuffers.EEPromMemory[locsRead++]) && !Pk2.LearnMode) { Pk2.RunScript(KONST.PROG_EXIT, 1); conditionalVDDOff(); if (!writeVerify) { displayStatusWindow.Text = "Verification of EE Data Memory failed at address\n"; } else { displayStatusWindow.Text = "Programming failed at EE Data address\n"; } if (eeBlank == 0xFFFF) { displayStatusWindow.Text += string.Format("0x{0:X4}", (--locsRead * 2)); } else { displayStatusWindow.Text += string.Format("0x{0:X4}", --locsRead); } statusWindowColor = Constants.StatusColor.red; updateGUI(KONST.UpdateMemoryDisplays); return false; } if (locsRead >= Pk2.DevFile.PartsList[Pk2.ActivePart].EEMem) { break; // for cases where ProgramMemSize%WordsPerLoop != 0 } } progressBar1.PerformStep(); } while (locsRead < Pk2.DevFile.PartsList[Pk2.ActivePart].EEMem); Pk2.RunScript(KONST.PROG_EXIT, 1); if (Pk2.LearnMode && (Pk2.DevFile.Families[Pk2.GetActiveFamily()].ProgMemShift > 0)) { Pk2.MetaCmd_CHANGE_CHKSM_FRMT(1); } } //Verify User IDs ---------------------------------------------------------------------------------- if ((Pk2.DevFile.PartsList[Pk2.ActivePart].UserIDWords > 0) && checkBoxProgMemEnabled.Checked) { // When EE deselected, UserIDs are not programmed so don't try to verify. displayStatusWindow.Text += "UserIDs... "; this.Update(); Pk2.RunScript(KONST.PROG_ENTRY, 1); if (Pk2.DevFile.PartsList[Pk2.ActivePart].UserIDRdPrepScript > 0) { Pk2.RunScript(KONST.USERID_RD_PREP, 1); } int bytesPerWord = Pk2.DevFile.Families[Pk2.GetActiveFamily()].UserIDBytes; int wordsRead = 0; int bufferIndex = 0; Pk2.RunScriptUploadNoLen(KONST.USERID_RD, 1); Array.Copy(Pk2.Usb_read_array, 1, upload_buffer, 0, KONST.USB_REPORTLENGTH); if ((Pk2.DevFile.PartsList[Pk2.ActivePart].UserIDWords * bytesPerWord) > KONST.USB_REPORTLENGTH) { Pk2.UploadDataNoLen(); Array.Copy(Pk2.Usb_read_array, 1, upload_buffer, KONST.USB_REPORTLENGTH, KONST.USB_REPORTLENGTH); } Pk2.RunScript(KONST.PROG_EXIT, 1); do { int bite = 0; uint memWord = (uint)upload_buffer[bufferIndex + bite++]; if (bite < bytesPerWord) { memWord |= (uint)upload_buffer[bufferIndex + bite++] << 8; } if (bite < bytesPerWord) { memWord |= (uint)upload_buffer[bufferIndex + bite++] << 16; } if (bite < bytesPerWord) { memWord |= (uint)upload_buffer[bufferIndex + bite++] << 24; } bufferIndex += bite; // shift if necessary if (Pk2.DevFile.Families[Pk2.GetActiveFamily()].ProgMemShift > 0) { memWord = (memWord >> 1) & Pk2.DevFile.Families[Pk2.GetActiveFamily()].BlankValue; } if ((memWord != Pk2.DeviceBuffers.UserIDs[wordsRead++]) && !Pk2.LearnMode) { conditionalVDDOff(); if (!writeVerify) { displayStatusWindow.Text = "Verification of User IDs failed."; } else { displayStatusWindow.Text = "Programming failed at User IDs."; } statusWindowColor = Constants.StatusColor.red; updateGUI(KONST.UpdateMemoryDisplays); return false; } } while (wordsRead < Pk2.DevFile.PartsList[Pk2.ActivePart].UserIDWords); } if (!writeVerify) { // don't check config if write-verify: it isn't written yet as it may contain code protection if (!verifyConfig(configWords, configLocation)) { return false; } } Pk2.RunScript(KONST.PROG_EXIT, 1); if (!writeVerify) { statusWindowColor = Constants.StatusColor.green; displayStatusWindow.Text = "Verification Successful.\n"; conditionalVDDOff(); } updateGUI(KONST.UpdateMemoryDisplays); return true; }
private void deviceRead() { if (Pk2.FamilyIsKeeloq()) { displayStatusWindow.Text = "Read not supported for this device type."; statusWindowColor = Constants.StatusColor.yellow; updateGUI(KONST.DontUpdateMemDisplays); return; // abort } if (!preProgrammingCheck(Pk2.GetActiveFamily())) { return ; // abort } if (Pk2.FamilyIsPIC32()) { if (P32.PIC32Read()) { statusWindowColor = Constants.StatusColor.normal; } else { statusWindowColor = Constants.StatusColor.red; } conditionalVDDOff(); updateGUI(KONST.UpdateMemoryDisplays); return; } displayStatusWindow.Text = "Reading device:\n"; //displayStatusWindow.Update(); this.Update(); byte[] upload_buffer = new byte[KONST.UploadBufferSize]; Pk2.SetMCLRTemp(true); // assert /MCLR to prevent code execution before programming mode entered. Pk2.VddOn(); // Read Program Memory if (checkBoxProgMemEnabled.Checked) { displayStatusWindow.Text += "Program Memory... "; this.Update(); if (useProgExec33()) { if (!PE33.PE33Read(displayStatusWindow.Text)) { statusWindowColor = Constants.StatusColor.red; conditionalVDDOff(); updateGUI(KONST.UpdateMemoryDisplays); return; } } else if (useProgExec24F()) { if (!PE24.PE24FRead(displayStatusWindow.Text)) { statusWindowColor = Constants.StatusColor.red; conditionalVDDOff(); updateGUI(KONST.UpdateMemoryDisplays); return; } } else { Pk2.RunScript(KONST.PROG_ENTRY, 1); if ((Pk2.DevFile.PartsList[Pk2.ActivePart].ProgMemAddrSetScript != 0) && (Pk2.DevFile.PartsList[Pk2.ActivePart].ProgMemAddrBytes != 0)) { // if prog mem address set script exists for this part & # bytes is not zero // (MPLAB uses script on some parts when PICkit 2 does not) if (Pk2.FamilyIsEEPROM()) { Pk2.DownloadAddress3MSBFirst(eeprom24BitAddress(0, KONST.WRITE_BIT)); Pk2.RunScript(KONST.PROGMEM_ADDRSET, 1); if (eeprom_CheckBusErrors()) { return; } } else { Pk2.DownloadAddress3(0); //Pk2.DownloadAddress3(0x800000); Pk2.RunScript(KONST.PROGMEM_ADDRSET, 1); } } int bytesPerWord = Pk2.DevFile.Families[Pk2.GetActiveFamily()].BytesPerLocation; int scriptRunsToFillUpload = KONST.UploadBufferSize / (Pk2.DevFile.PartsList[Pk2.ActivePart].ProgMemRdWords * bytesPerWord); int wordsPerLoop = scriptRunsToFillUpload * Pk2.DevFile.PartsList[Pk2.ActivePart].ProgMemRdWords; int wordsRead = 0; progressBar1.Value = 0; // reset bar progressBar1.Maximum = (int)Pk2.DevFile.PartsList[Pk2.ActivePart].ProgramMem / wordsPerLoop; do { if (Pk2.FamilyIsEEPROM()) { if ((Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigMasks[KONST.PROTOCOL_CFG] == KONST.I2C_BUS) && (wordsRead > Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigMasks[KONST.ADR_MASK_CFG]) && (wordsRead % (Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigMasks[KONST.ADR_MASK_CFG] + 1) == 0)) { // must resend address to EE every time we cross a bank border. Pk2.DownloadAddress3MSBFirst(eeprom24BitAddress(wordsRead, KONST.WRITE_BIT)); Pk2.RunScript(KONST.PROGMEM_ADDRSET, 1); } Pk2.Download3Multiples(eeprom24BitAddress(wordsRead, KONST.READ_BIT), scriptRunsToFillUpload, Pk2.DevFile.PartsList[Pk2.ActivePart].ProgMemRdWords); } //Pk2.RunScriptUploadNoLen2(KONST.PROGMEM_RD, scriptRunsToFillUpload); Pk2.RunScriptUploadNoLen(KONST.PROGMEM_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; } Pk2.DeviceBuffers.ProgramMemory[wordsRead++] = memWord; if (wordsRead == Pk2.DevFile.PartsList[Pk2.ActivePart].ProgramMem) { break; // for cases where ProgramMemSize%WordsPerLoop != 0 } if (((wordsRead % 0x8000) == 0) && (Pk2.DevFile.PartsList[Pk2.ActivePart].ProgMemAddrSetScript != 0) && (Pk2.DevFile.PartsList[Pk2.ActivePart].ProgMemAddrBytes != 0) && (Pk2.DevFile.Families[Pk2.GetActiveFamily()].BlankValue > 0xFFFF)) { //PIC24 must update TBLPAG Pk2.DownloadAddress3(0x10000 * (wordsRead / 0x8000)); Pk2.RunScript(KONST.PROGMEM_ADDRSET, 1); break; } } progressBar1.PerformStep(); } while (wordsRead < Pk2.DevFile.PartsList[Pk2.ActivePart].ProgramMem); Pk2.RunScript(KONST.PROG_EXIT, 1); // swap "Endian-ness" of 16 bit 93LC EEPROMs if ((Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigMasks[KONST.PROTOCOL_CFG] == KONST.MICROWIRE_BUS) && (bytesPerWord == 2) && (Pk2.FamilyIsEEPROM())) { uint memTemp = 0; for (int x = 0; x < Pk2.DeviceBuffers.ProgramMemory.Length; x++) { memTemp = (Pk2.DeviceBuffers.ProgramMemory[x] << 8) & 0xFF00; Pk2.DeviceBuffers.ProgramMemory[x] >>= 8; Pk2.DeviceBuffers.ProgramMemory[x] |= memTemp; } } } } // Read EEPROM if ((checkBoxEEMem.Checked) && (Pk2.DevFile.PartsList[Pk2.ActivePart].EEMem > 0)) { readEEPROM(); } // Read UserIDs if ((Pk2.DevFile.PartsList[Pk2.ActivePart].UserIDWords > 0) && checkBoxProgMemEnabled.Checked) { displayStatusWindow.Text += "UserIDs... "; this.Update(); Pk2.RunScript(KONST.PROG_ENTRY, 1); if (Pk2.DevFile.PartsList[Pk2.ActivePart].UserIDRdPrepScript > 0) { Pk2.RunScript(KONST.USERID_RD_PREP, 1); } int bytesPerWord = Pk2.DevFile.Families[Pk2.GetActiveFamily()].UserIDBytes; int wordsRead = 0; int bufferIndex = 0; Pk2.RunScriptUploadNoLen(KONST.USERID_RD, 1); Array.Copy(Pk2.Usb_read_array, 1, upload_buffer, 0, KONST.USB_REPORTLENGTH); if ((Pk2.DevFile.PartsList[Pk2.ActivePart].UserIDWords * bytesPerWord) > KONST.USB_REPORTLENGTH) { Pk2.UploadDataNoLen(); Array.Copy(Pk2.Usb_read_array, 1, upload_buffer, KONST.USB_REPORTLENGTH, KONST.USB_REPORTLENGTH); } Pk2.RunScript(KONST.PROG_EXIT, 1); do { int bite = 0; uint memWord = (uint)upload_buffer[bufferIndex + bite++]; if (bite < bytesPerWord) { memWord |= (uint)upload_buffer[bufferIndex + bite++] << 8; } if (bite < bytesPerWord) { memWord |= (uint)upload_buffer[bufferIndex + bite++] << 16; } if (bite < bytesPerWord) { memWord |= (uint)upload_buffer[bufferIndex + bite++] << 24; } bufferIndex += bite; // shift if necessary if (Pk2.DevFile.Families[Pk2.GetActiveFamily()].ProgMemShift > 0) { memWord = (memWord >> 1) & Pk2.DevFile.Families[Pk2.GetActiveFamily()].BlankValue; } Pk2.DeviceBuffers.UserIDs[wordsRead++] = memWord; } while (wordsRead < Pk2.DevFile.PartsList[Pk2.ActivePart].UserIDWords); } // Read Configuration int configLocation = (int)Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigAddr / Pk2.DevFile.Families[Pk2.GetActiveFamily()].ProgMemHexBytes; int configWords = Pk2.DevFile.PartsList[Pk2.ActivePart].ConfigWords; if ((configWords > 0) && (configLocation >= Pk2.DevFile.PartsList[Pk2.ActivePart].ProgramMem ) && checkBoxProgMemEnabled.Checked) { // Don't read config words for any part where they are stored in program memory. displayStatusWindow.Text += "Config... "; //displayStatusWindow.Update(); this.Update(); Pk2.ReadConfigOutsideProgMem(); // save bandgap if necessary if (Pk2.DevFile.PartsList[Pk2.ActivePart].BandGapMask > 0) { Pk2.DeviceBuffers.BandGap = Pk2.DeviceBuffers.ConfigWords[0] & Pk2.DevFile.PartsList[Pk2.ActivePart].BandGapMask; } } else if ((configWords > 0) && checkBoxProgMemEnabled.Checked) { // pull them out of program memory. displayStatusWindow.Text += "Config... "; //displayStatusWindow.Update(); this.Update(); for (int word = 0; word < configWords; word++) { Pk2.DeviceBuffers.ConfigWords[word] = Pk2.DeviceBuffers.ProgramMemory[configLocation + word]; } } // Read OSCCAL if exists if (Pk2.DevFile.PartsList[Pk2.ActivePart].OSSCALSave) { Pk2.ReadOSSCAL(); } // Read Test Memory if open if (TestMemoryEnabled && TestMemoryOpen) { formTestMem.ReadTestMemory(); } // TESTING for DEBUG vector Read //labelBandGap.Text = string.Format("{0:X8}", Pk2.ReadDebugVector()); conditionalVDDOff(); displayStatusWindow.Text += "Done."; // update SOURCE box displayDataSource.Text = "Read from " + Pk2.DevFile.PartsList[Pk2.ActivePart].PartName; checkImportFile = false; updateGUI(KONST.UpdateMemoryDisplays); }
private void buttonReadExport(object sender, EventArgs e) { if (Pk2.FamilyIsKeeloq()) { displayStatusWindow.Text = "Read not supported for this device type."; statusWindowColor = Constants.StatusColor.yellow; updateGUI(KONST.DontUpdateMemDisplays); return; // abort } // read device deviceRead(); updateGUI(KONST.UpdateMemoryDisplays); this.Refresh(); // export hex saveHexFileDialog.ShowDialog(); }