private static void parseHex(string file, out byte[] code, out byte[] uid, out byte[] data, out byte[] conf) { var f = File.Open( file, FileMode.Open, FileAccess.Read); var h = new HexParser(f); code = new byte[0x1000]; uid = new byte[8]; data = new byte[0x80]; conf = new byte[2]; long addr = -1; while (h.Address <= code.Length * 2) { addr = h.Address; code[addr] = h.Value; h.nextAddress(); } Array.Resize<byte>(ref code, (int)addr + 1); addr = -1; while (h.Address <= 0x2004 * 2) { addr = h.Address - 0x2000 * 2; uid[addr] = h.Value; h.nextAddress(); } Array.Resize<byte>(ref uid, (int)addr + 1); addr = -1; while (h.Address <= (0x2007 * 2) + 1) { addr = h.Address - 0x2007 * 2; conf[addr] = h.Value; h.nextAddress(); } Array.Resize<byte>(ref conf, (int)addr + 1); addr = -1; while (h.Address < (0x2100 + 0x80 * 2) * 2) { addr = (h.Address - 0x2100 * 2) / 2; data[addr] = h.Value; h.nextAddress(); h.nextAddress(); } Array.Resize<byte>(ref data, (int)addr + 1); f.Close(); }
private void button1_Click_1(object sender, EventArgs e) { var bgw = new BackgroundWorker { WorkerReportsProgress = true, WorkerSupportsCancellation = true }; var sb = new StringBuilder(); this.Enabled = false; bgw.DoWork += (obj, param) => { var file = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Code\microchip\testCprog.X\dist\default\production\testCprog.X.production.hex"; var f = new FileStream(file, FileMode.Open); var h = new HexParser(f); long lastAddr = 0; long startAddr = 0; Queue<Tuple<int, byte[]>> blocks = new Queue<Tuple<int, byte[]>>(); Queue<byte> block = new Queue<byte>(); block.Enqueue(h.Value); while (h.nextAddress() != long.MaxValue) { if (h.Address > lastAddr + 1) { blocks.Enqueue(new Tuple<int, byte[]>((int)startAddr, block.ToArray())); block = new Queue<byte>(); startAddr = h.Address; } block.Enqueue(h.Value); lastAddr = h.Address; } blocks.Enqueue(new Tuple<int, byte[]>((int)startAddr, block.ToArray())); f.Close(); var myset = from x in blocks select x.Item1; pp.bulkErase(); sb.AppendLine("erased"); //foreach (var st in myset) //{ // readCodeToSB(bgw, sb, st, 0x10); //} //sb.AppendLine(); var blankData = new byte[64]; Random r = new Random(); r.NextBytes(blankData); foreach (var b in blocks) { var address = b.Item1; var data = b.Item2; if (address < 0x300000) { sb.AppendLine(string.Format("Writing code @ {0:X6} - {1:X6}", address, address + data.Length)); pp.writeCode(address, data, 0, data.Length, bgw.ReportProgress); //pp.writeCode(address, blankData, 0, blankData.Length, bgw.ReportProgress); } else { sb.AppendLine(string.Format("Writing conf @ {0:X6} - {1:X6}", address, address + data.Length)); pp.writeConfig(address, data, 0, data.Length, bgw.ReportProgress); //pp.writeConfig(address, blankData, 0, blankData.Length, bgw.ReportProgress); } } //foreach (var st in myset) //{ // readCodeToSB(bgw, sb, st, 0x10); //} sb.AppendLine(); }; bgw.ProgressChanged += (s, ev) => { progressBar1.Value = ev.ProgressPercentage; }; bgw.RunWorkerCompleted += (s, ev) => { txtOut.Text = sb.ToString(); this.Enabled = true; }; bgw.RunWorkerAsync(); }
private void button2_Click(object sender, EventArgs e) { var bgw = new BackgroundWorker { WorkerReportsProgress = true, WorkerSupportsCancellation = true }; var sb = new StringBuilder(); this.Enabled = false; bgw.DoWork += (obj, param) => { var file = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\Code\microchip\testCprog.X\dist\default\production\testCprog.X.production.hex"; var f = new FileStream(file, FileMode.Open); var h = new HexParser(f); long lastAddr = 0; long startAddr = 0; Queue<Tuple<int, byte[]>> blocks = new Queue<Tuple<int, byte[]>>(); Queue<byte> block = new Queue<byte>(); block.Enqueue(h.Value); while (h.nextAddress() != long.MaxValue) { if (h.Address > lastAddr + 1) { blocks.Enqueue(new Tuple<int, byte[]>((int)startAddr, block.ToArray())); block = new Queue<byte>(); startAddr = h.Address; } block.Enqueue(h.Value); lastAddr = h.Address; } blocks.Enqueue(new Tuple<int, byte[]>((int)startAddr, block.ToArray())); f.Close(); foreach (var st in blocks) { readCodeToSB(bgw, sb, st.Item1, st.Item2.Length); } sb.AppendLine(); }; bgw.ProgressChanged += (s, ev) => { progressBar1.Value = ev.ProgressPercentage; }; bgw.RunWorkerCompleted += (s, ev) => { txtOut.Text = sb.ToString(); this.Enabled = true; }; bgw.RunWorkerAsync(); }
private void btnReadHex_Click(object sender, EventArgs e) { var f = File.Open( @"C:\microchip\testStepper\output\testStepper.hex", FileMode.Open, FileAccess.Read); var h = new HexParser(f); byte[] code = new byte[0x1000]; byte[] uid = new byte[8]; byte[] data = new byte[0x80]; byte[] conf = new byte[2]; long addr = -1; while (h.Address <= code.Length * 2) { addr = h.Address; code[addr] = h.Value; h.nextAddress(); } Array.Resize<byte>(ref code, (int)addr + 1); addr = -1; while (h.Address <= 0x2004 * 2) { addr = h.Address - 0x2000 * 2; uid[addr] = h.Value; h.nextAddress(); } Array.Resize<byte>(ref uid, (int)addr + 1); addr = -1; while (h.Address <= (0x2007 * 2) + 1 ) { addr = h.Address - 0x2007 * 2; conf[addr] = h.Value; h.nextAddress(); } Array.Resize<byte>(ref conf, (int)addr + 1); addr = -1; while (h.Address < (0x2100 + 0x80) * 2) { addr = h.Address - 0x2100 * 2; data[addr] = h.Value; h.nextAddress(); } Array.Resize<byte>(ref data, (int)addr + 1); pp.bulkErase(); pp.writeConfig(0x2000, uid, 0, uid.Length); if (conf.Length < 2) { conf = new byte[] { 0x10, 0x3f }; } pp.writeConfig(0x2007, conf, 0, conf.Length); pp.writeCode(0x0000, code, 0, code.Length); pp.writeData(0x0000, data, 0, data.Length); var confRead = new byte[16]; pp.readCode(0x2000, confRead, 0, confRead.Length); txtOut.AppendText("\r\n"); for (int i = 0; i < confRead.Length; i += 2) { txtOut.AppendText(String.Format("{0} {1}\r\n" , (i / 2 + 0x2000).ToString("X4") , (confRead[i] | (confRead[i + 1] << 8)).ToString("X4"))); } f.Close(); }