private bool DecodeLine(StreamReader sr, ref int upper_address, ref bool eof, ref LineInfo li) // Desc: Decode one line in hex file // Input: sr: Stream for hex file // upper_address: Upper address // eof: End of file indicator // Output: li: Line inforamtion { byte[] data; int i; string s; string line; // Read one line line = sr.ReadLine(); // Get record type s = line.Substring(7, 2); li.recordType = int.Parse(s, NumberStyles.AllowHexSpecifier); // Deal with extended address while (li.recordType == 04) { s = line.Substring(9, 4); upper_address = int.Parse(s, NumberStyles.AllowHexSpecifier); line = sr.ReadLine(); s = line.Substring(7, 2); li.recordType = int.Parse(s, NumberStyles.AllowHexSpecifier); } // Check if last line if (li.recordType == 1) { eof = true; return(false); } // Get line inforamtion s = (line.Substring(1, 2)); li.byteLen = int.Parse(s, NumberStyles.AllowHexSpecifier); s = line.Substring(3, 4); li.address = (int.Parse(s, NumberStyles.AllowHexSpecifier) + (upper_address << 16)) / 2; // Store data in li.data if (li.byteLen >= 4) { data = new byte[li.byteLen * 3 / 4]; for (i = 0; i < li.byteLen; i = i + 4) { s = line.Substring(9 + 2 * i, 2); // LSB data[i * 3 / 4 + 0] = (byte)int.Parse(s, NumberStyles.AllowHexSpecifier); s = line.Substring(11 + 2 * i, 2); data[i * 3 / 4 + 1] = (byte)int.Parse(s, NumberStyles.AllowHexSpecifier); s = line.Substring(13 + 2 * i, 2); // MSB data[i * 3 / 4 + 2] = (byte)int.Parse(s, NumberStyles.AllowHexSpecifier); } li.data = data; } return(true); }
private bool CreateBuffers(int start_address, byte[] code_buf, byte[] config_buf) // Desc: Create buffer to send for programing with bootloader // Input: start_address: Start address of bootloader program (should not be overwritten) // Output: code_buf: Data for user application // config_buf: Data for configuration register // bool: true = success, false = fail { StreamReader sr; LineInfo li = new LineInfo(); int i; int upper_address = 0; bool eof = false; // Initialize code_buf for (i = 0; i < code_buf.Length; i++) { code_buf[i] = 0xff; } code_buf[0] = (byte)start_address; // LSB code_buf[1] = (byte)(start_address >> 8); code_buf[2] = (byte)(start_address >> 16); // MSB code_buf[3] = 0x00; code_buf[4] = 0x00; code_buf[5] = 0x00; code_buf[0x0C00 * 3 / 2] = 0x00; // Delay value for bootloader // Initialize config_buf for (i = 0; i < config_buf.Length; i++) { config_buf[i] = 0xff; } // Open hex file sr = File.OpenText(hexFileLocation); while (eof == false) { // Decode next line(s) if (DecodeLine(sr, ref upper_address, ref eof, ref li) == true) { // Add initialization location (start of user application) to code_buf if (li.address == 0) { for (i = 0; i < li.data.Length; i++) { code_buf[0x0C02 * 3 / 2 + i] = li.data[i]; } } // Add to code_buff else if (((0x4 <= li.address) && (li.address < 0x200) || (0xC00 <= li.address) && (li.address < 0x15800))) { for (i = 0; i < li.data.Length; i++) { code_buf[li.address * 3 / 2 + i] = li.data[i]; } } // Add to config_buf else if ((li.address >= 0xf80000) && (li.address < 0xf80010)) { config_buf[(li.address - 0xf80000) * 3 / 2] = 0x00; config_buf[(li.address - 0xf80000) * 3 / 2 + 1] = li.data[0]; // LSB config_buf[(li.address - 0xf80000) * 3 / 2 + 2] = li.data[1]; // MSB } else { MessageBox.Show("Invalid memory region to write"); return(false); } } } sr.Close(); return(true); }