public DialogUnitSelect() { InitializeComponent(); this.Size = new Size(this.Size.Width, (int)(FormPICkit2.ScalefactH * this.Size.Height)); // Find up to 8 PICkit 2 Units. for (ushort i = 0; i < 8; i++) { KONST.PICkit2USB detRes = Pk2.DetectPICkit2Device(i, false); if (detRes != KONST.PICkit2USB.notFound) { // found something /*if (detRes == KONST.PICkit2USB.bootloader) * { * listBoxUnits.Items.Add(" " + i.ToString() + " <Bootloader>"); * } * else if (detRes == Constants.PICkit2USB.firmwareInvalid) * { // min FW for UnitID is 2.10 * if ((Pk2.FirmwareVersion[0] == '2') && (ushort.Parse(Pk2.FirmwareVersion.Substring(2,2)) >= 10)) * { * string unitID = Pk2.UnitIDRead(); * if (unitID == "") * unitID = "-"; * listBoxUnits.Items.Add(" " + i.ToString() + " " + unitID); * } * else * { * listBoxUnits.Items.Add(" " + i.ToString() + " <FW v" + Pk2.FirmwareVersion + ">"); * } * * } * else * { * string unitID = Pk2.UnitIDRead(); * if (unitID == "") * unitID = "-"; * listBoxUnits.Items.Add(" " + i.ToString() + " " + unitID); * }*/ string unitID = Pk2.GetSerialUnitID(); if (unitID == "PIC18F2550") { unitID = "<bootloader>"; } listBoxUnits.Items.Add(" " + i.ToString() + " " + unitID); } else { break; } } }
static public KONST.PICkit2USB WriteProgrammerOs(string ApHexFile, uint Command) { //TODO There is a "Magic Key" in APs and RS that we should check. uint StartAddr = 0; int NumBytes = 0; byte[] Ap; NumBytes = ImportHexFileAsBytes(out Ap, ApHexFile, out StartAddr); NumBytes = AdjustHexFileDataForSend(ref Ap); // Adjust the start address for the programmer StartAddr = StartAddr / 2; // dsPIC has 2 bytes per program word byte[] memObj = new byte[8]; // MemObj start address memObj[0] = Convert.ToByte(StartAddr & 0xFF); memObj[1] = Convert.ToByte((StartAddr >> 8) & 0xFF); memObj[2] = Convert.ToByte((StartAddr >> 16) & 0xFF); memObj[3] = Convert.ToByte((StartAddr >> 24) & 0xFF); // MemObj length in bytes uint RangeInBytes = (uint)Ap.Length; memObj[4] = Convert.ToByte(RangeInBytes & 0xFF); memObj[5] = Convert.ToByte((RangeInBytes >> 8) & 0xFF); memObj[6] = Convert.ToByte((RangeInBytes >> 16) & 0xFF); memObj[7] = Convert.ToByte((RangeInBytes >> 24) & 0xFF); SendCommandWithData(Command, memObj); SendBulkData(Ap); // The programmer will restart now, so we need to reinit comms Thread.Sleep(2000); KONST.PICkit2USB res = KONST.PICkit2USB.notFound; while (res == KONST.PICkit2USB.notFound) { res = Pk3.DetectPICkit2Device(FormPICkit2.pk2number, true); Thread.Sleep(500); // Delay a bit so we don't hammer the programmer } //Initialize(); return(res); }
public static bool ReadHexAndDownload(string fileName, ref ushort pk2num) { try { FileInfo hexFile = new FileInfo(fileName); TextReader hexRead = hexFile.OpenText(); byte[] flashWriteData = new byte[3 + 32]; // 3 address bytes plus 32 data bytes. string fileLine = hexRead.ReadLine(); if (fileLine != null) { Pk2.EnterBootloader(); Pk2.ResetPk2Number(); Thread.Sleep(3000); int i; pk2num = 0; for (i = 0; i < 10; i++) { if (Pk2.DetectPICkit2Device(pk2num, true) == Constants.PICkit2USB.bootloader) { if (Pk2.VerifyBootloaderMode()) { break; } } else { pk2num++; // look for PK2 with bootloader. } Thread.Sleep(500); } if (i == 10) { hexRead.Close(); return(false); } } // erase PICkit 2 firmware flash Pk2.BL_EraseFlash(); bool second16 = false; while (fileLine != null) { if ((fileLine[0] == ':') && (fileLine.Length >= 11)) { // skip line if not hex line entry,or not minimum length ":BBAAAATTCC" int byteCount = Int32.Parse(fileLine.Substring(1, 2), System.Globalization.NumberStyles.HexNumber); int fileAddress = Int32.Parse(fileLine.Substring(3, 4), System.Globalization.NumberStyles.HexNumber); int recordType = Int32.Parse(fileLine.Substring(7, 2), System.Globalization.NumberStyles.HexNumber); if ((second16) && ((fileAddress & 0x00000010) == 0)) {// if just moved to new 32-byte boundary. Pk2.BL_WriteFlash(flashWriteData); for (int x = 0; x < flashWriteData.Length; x++) { // clear array for skipped bytes in hex file flashWriteData[x] = 0xFF; } } second16 = ((fileAddress & 0x00000010) == 0x10); if (recordType == 0) { // Data Record} if ((fileAddress >= 0x2000) && (fileAddress < 0x7FE0)) { // don't program 5555 key at last address until after verification. if (!second16) { int rowAddress = fileAddress & 0xFFE0; flashWriteData[0] = (byte)(rowAddress & 0xFF); flashWriteData[1] = (byte)((rowAddress >> 8) & 0xFF); flashWriteData[2] = 0x00; // address upper } if (fileLine.Length >= (11 + (2 * byteCount))) { // skip if line isn't long enough for bytecount. int startByte = fileAddress & 0x000F; int endByte = startByte + byteCount; int offset = 3; if (second16) { offset = 19; } for (int rowByte = 0; rowByte < 16; rowByte++) { if ((rowByte >= startByte) && (rowByte < endByte)) { // get the byte value from hex file uint wordByte = UInt32.Parse(fileLine.Substring((9 + (2 * (rowByte - startByte))), 2), System.Globalization.NumberStyles.HexNumber); flashWriteData[offset + rowByte] = (byte)(wordByte & 0xFF); } } } } } // end if (recordType == 0) if (recordType == 1) { // end of record break; } } fileLine = hexRead.ReadLine(); } Pk2.BL_WriteFlash(flashWriteData); // write last row hexRead.Close(); return(true); } catch { return(false); } }