📄 dvflasher.cs
字号:
catch (ObjectDisposedException e) { Console.WriteLine(e.StackTrace); throw e; } } /// <summary> /// Waitforsequence with option for verbosity /// </summary> /// <param name="str">String to look for</param> /// <param name="altStr">String to look for but don't want</param> /// <param name="sp">SerialPort object.</param> /// <param name="verbose">Boolean to indicate verbosity.</param> /// <returns>Boolean to indicate if str or altStr was found.</returns> private static Boolean waitForSequence(String str,String altStr,SerialPort sp,Boolean verbose) { Boolean strFound = false, altStrFound = false; Byte[] input = new Byte[256]; String inputStr; Int32 i; while ((!strFound) && (!altStrFound)) { i = 0; do { input[i++] = (Byte)sp.ReadByte(); //Console.Write(input[i - 1] + " "); } while ( (input[i - 1] != 0) && (i < (input.Length - 1)) && (input[i - 1] != 0x0A) && (input[i - 1] != 0x0D) ); // Convert to string for comparison if ((input[i-1] == 0x0A) || (input[i-1] == 0x0D)) inputStr = (new ASCIIEncoding()).GetString(input, 0, i-1); else inputStr = (new ASCIIEncoding()).GetString(input, 0, i); if (inputStr.Length == 0) { continue; } // Compare Strings to see what came back if (verbose) Console.WriteLine("\tDVEVM:\t{0}", inputStr); if (inputStr.Contains(altStr)) { altStrFound = true; if (String.Equals(str, altStr)) { strFound = altStrFound; } } else if (inputStr.Contains(str)) { strFound = true; } else { strFound = false; } } return strFound; } /// <summary> /// Wait for null-terminated sequences from supplied serial port /// </summary> /// <param name="str">Expected character string.</param> /// <param name="altStr">Alternative string that if received, indicates failure /// This is "BOOTME\0" or "BOOTPSP\0"</param> /// <param name="sp">Serial port object to use to receive string.</param> /// <returns>Boolean ind4cating whether sequence was received.</returns> private static Boolean waitForSequence( String str,String altStr,SerialPort sp) { return waitForSequence(str, altStr, sp, cmdParams.Verbose); } #endregion //********************************************************************************** //********************************************************************************** #region Code to manipulate binary file to Motorola S-record /// <summary> /// Function to convert the input filestream into an byte array in S-record format, /// so that it can be downloaded to the EVM board. /// </summary> /// <param name="inputFileStream">The input filestream that encapsulates the /// input application file.</param> /// <param name="startAddr">The starting address of the RAM location where the binary data /// encapsulated by the S-record will be stored.</param> /// <returns>A byte array of the file data.</returns> public static Byte[] bin2srec(Stream inputStream, UInt32 startAddr, Boolean stripSelfCopyData) { Int64 totalLen; BinaryReader fileBR = new BinaryReader(inputStream); StringBuilder fileSB; String fileName; String shortFileName; Byte[] currChar = new Byte[1]; Byte[] currDataRecord; Int32 i, checksum8 = 0; Int32 recordSize = 16; UInt32 memAddr = startAddr; // Set the actual length if (stripSelfCopyData) totalLen = fileBR.BaseStream.Length - 256; else totalLen = fileBR.BaseStream.Length; fileSB = new StringBuilder(4 * (int)totalLen); // Set S-record filename (real name or fake) if (inputStream is FileStream) fileName = ((FileStream)inputStream).Name; else fileName = "ublDaVinci.bin"; // Make sure we are at the right place in the stream if (stripSelfCopyData) fileBR.BaseStream.Seek(0x100, SeekOrigin.Begin); else fileBR.BaseStream.Seek(0x0, SeekOrigin.Begin); // Get filename (this is S-record module name) if (Path.HasExtension(fileName)) shortFileName = Path.GetFileNameWithoutExtension(fileName) + ".srec"; else shortFileName = Path.GetFileName(fileName) + ".srec"; // Make sure S-record module name fits in 20 byte field if (shortFileName.Length > 20) shortFileName = shortFileName.Substring(0, 20); // Create first s-record (S0 record) fileSB.Append("S0"); // Write length field fileSB.AppendFormat("{0:X2}",shortFileName.Length+3); checksum8 += (Byte)(shortFileName.Length + 3); // Write address field fileSB.Append("0000"); // Write name field for (i = 0; i < shortFileName.Length; i++) { currChar = (new ASCIIEncoding()).GetBytes(shortFileName.Substring(i, 1)); checksum8 += currChar[0]; fileSB.AppendFormat("{0:X2}",currChar[0]); } // Write Checksum field fileSB.AppendFormat("{0:X2}\x0A", ((checksum8&0xFF)^0xFF)); // Write collection of S3 records (actual binary data) i = (Int32)totalLen; while (i >= recordSize) { checksum8 = 0; // Write S3 record label fileSB.Append("S3"); // Write length field (4 address bytes + 16 data bytes + 1 checksum byte) fileSB.AppendFormat("{0:X2}", recordSize + 5); checksum8 += (recordSize + 5); // Write address field and update it fileSB.AppendFormat("{0:X8}", memAddr); currDataRecord = System.BitConverter.GetBytes(memAddr); for (int j = 0; j < 4; j++) { checksum8 += currDataRecord[j]; } // Write out the bytes of data currDataRecord = fileBR.ReadBytes(recordSize); for (int j = 0; j < recordSize; j++) { fileSB.AppendFormat("{0:X2}", currDataRecord[j]); checksum8 += currDataRecord[j]; } //Write out checksum and linefeed character fileSB.AppendFormat("{0:X2}\x0A", ((checksum8&0xFF) ^ 0xFF)); memAddr += (UInt32)recordSize; i -= recordSize; } // Finish out the record if anything is left over if (i > 0) { checksum8 = 0; // Write S3 record label fileSB.Append("S3"); // Write length field (4 address bytes + 16 data bytes + 1 checksum byte) fileSB.AppendFormat("{0:X2}", i + 5); checksum8 += (i + 5); // Write address field and update it fileSB.AppendFormat("{0:X8}", memAddr); currDataRecord = System.BitConverter.GetBytes(memAddr); for (int j = 0; j < 4; j++) { checksum8 += currDataRecord[j]; } // Write out the bytes of data currDataRecord = fileBR.ReadBytes(i); for (int j = 0; j < i; j++) { fileSB.AppendFormat("{0:X2}", currDataRecord[j]); checksum8 += currDataRecord[j]; } //Write out checksum and linefeed character fileSB.AppendFormat("{0:X2}\x0A", ((checksum8 & 0xFF) ^ 0xFF)); memAddr += (UInt32)i; i = 0; } // Write out the final record (S7 record) checksum8 = 0; // Write S7 record label fileSB.Append("S7"); // Write length field (4 address bytes + 1 checksum byte) fileSB.AppendFormat("{0:X2}", 5); checksum8 += 5; // Write execution start address field and update it fileSB.AppendFormat("{0:X8}", startAddr); currDataRecord = System.BitConverter.GetBytes(startAddr); for (int j = 0; j < 4; j++) { checksum8 += currDataRecord[j]; } //Write out checksum and linefeed character fileSB.AppendFormat("{0:X2}\x0A", ((checksum8 & 0xFF) ^ 0xFF)); return (new ASCIIEncoding()).GetBytes(fileSB.ToString()); } /// <summary> /// Get the byte array if the file is already an S-record /// </summary> /// <param name="inputFileStream">The input filestream that encapsulates the /// input application file.</param> /// <returns>A byte array of the file data.</returns> public static Byte[] readSrec(FileStream inputFileStream) { inputFileStream.Position = 0; return (new BinaryReader(inputFileStream)).ReadBytes((Int32)inputFileStream.Length); } /// <summary> /// Check to see if filestream is an S-record by reading the first /// character of each line and seeing if it is 'S' /// </summary> /// <param name="inputFileStream">The input filestream that encapsulates the /// input application file.</param> /// <returns>a boolean representing whether the file is an s-record.</returns> public static Boolean isFileSrec(FileStream inputFileStream) { StreamReader sr = new StreamReader(inputFileStream, Encoding.ASCII); String text; inputFileStream.Position = 0; text = sr.ReadLine(); while (!sr.EndOfStream) { if (!text.StartsWith("S")) { return false; } text = sr.ReadLine(); } return true; } #endregion //********************************************************************************** } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -