MCU燒錄文件生成(MCU的iap文件與app文件合併,生成燒錄用的bin文件)

做S32K144的一個案子,MDK只生成了一些bin文件,還要手動合成一個燒錄文件,麻煩。

在網上找了半天,居然沒找到一個我想要的bin文件合併小軟件,晚上加班寫了一個,寥寥一百行。

程序及用法示例上傳到了CSDN,下載鏈接:

https://download.csdn.net/download/timini/11780137

其中的批處理文件內容如下,這個案子iap代碼是從0x0000開始的,app代碼是從0x5000開始的:

del "bmcu_iap.bin"
del "bmcu_app.bin"
del "bmcu_program.bin"

bincombin.exe "bmcu_iap.bin" 0 "\iap\VECTOR_ROM"
bincombin.exe "bmcu_iap.bin" 0 "\iap\ER_m_flash_config"
bincombin.exe "bmcu_iap.bin" 0 "\iap\ER_m_text"

bincombin.exe "bmcu_app.bin" 5000 "VECTOR_ROM"
bincombin.exe "bmcu_app.bin" 5000 "ER_m_flash_config"
bincombin.exe "bmcu_app.bin" 5000 "ER_m_text"

bincombin.exe "bmcu_program.bin" 0 "bmcu_iap.bin"
bincombin.exe "bmcu_program.bin" 0 "bmcu_app.bin" 5000

pause

C#代碼如下:

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace bincombin
{
	class Program
	{
		static void show_help()
		{
			StringBuilder help_str = new StringBuilder();
			help_str.Append("\r\nError: Failed to Combin. Here is the help of this command:\r\n\r\n");
			help_str.Append("******************************************************************************\r\n\r\n");
			help_str.Append(" command   : bincombin  dst.bin  dst_start  src.bin  [src_start]\r\n\r\n");
			help_str.Append(" dst.bin   : 目標文件相對路徑(相對本程序而言)。如果不存在,則將創建該文件\r\n\r\n");
			help_str.Append(" dst_start : 目標文件起始地址(HEX格式)\r\n\r\n");
			help_str.Append(" src.bin   : 源文件相對路徑(相對本程序而言)。該文件將被追加到目標文件中\r\n\r\n");
			help_str.Append(" src_start : 源文件起始地址(HEX格式)。如不指定則合併時,源文件緊跟目標文件之後\r\n\r\n\r\n");
			help_str.Append(" 例如,有一個批處理文件內容如下:\r\n\r\n");
			help_str.Append("       bincombin “test\\all.bin\" 5000 \"test\\VECTOR_ROM\"\r\n");
			help_str.Append("       bincombin “test\\all.bin\" 5000 \"test\\ER_m_flash_config\"\r\n");
			help_str.Append("       bincombin “test\\all.bin\" 5000 \"test\\ER_m_text\" 6000\r\n\r\n");
			help_str.Append(" 批處理文件執行後,會在test文件夾下生成all.bin, 其內容如下:\r\n\r\n");
			help_str.Append("       文件開始爲VECTOR_ROM文件的內容。\r\n");
			help_str.Append("       之後就是ER_m_flash_config文件的內容。\r\n");
			help_str.Append("       在all.bin的1000(即6000-5000)處開始,是ER_m_text文件的內容。\r\n\r\n");
			help_str.Append("******************************************************************************\r\n");
			Console.Write(help_str.ToString());
		}
		static void Main(string[] args)
		{
			try
			{
				string file_dst = Directory.GetCurrentDirectory() + "\\" + args[0];
				string file_src = Directory.GetCurrentDirectory() + "\\" + args[2];

				//讀取 dst.bin文件
				byte[] dst_bin = read_bin(file_dst);
				UInt32 dst_start = UInt32.Parse(args[1], System.Globalization.NumberStyles.HexNumber);
				UInt32 dst_size = (UInt32)dst_bin.Length;

				//讀取 src.bin文件
				byte[] src_bin = read_bin(file_src);
				UInt32 src_size = (UInt32)src_bin.Length;

				//合併
				UInt32 src_start = dst_size;
				if (args.Length > 4)
				{
					src_start = UInt32.Parse(args[3], System.Globalization.NumberStyles.HexNumber);
					src_start -= dst_start;
				}
				UInt32 comb_size = src_start + src_size;
				comb_size = (comb_size > dst_size) ? comb_size : dst_size;
				byte[] combin = new byte[comb_size];
				for (UInt32 i = 0; i < dst_size; i++)
				{
					combin[i] = dst_bin[i];
				}
				for (UInt32 i = src_start; i < comb_size; i++)
				{
					combin[i] = src_bin[i - src_start];
				}
				write_bin(file_dst, combin); //合併結果覆蓋dst.bin
				Console.WriteLine("[OK]");
			}
			catch
			{
				show_help();
				Console.Read();
			}
		}

		static byte[] read_bin(string path)
		{
			FileStream rf = new FileStream(path, FileMode.OpenOrCreate);
			BinaryReader rb = new BinaryReader(rf);
			byte[] data = new byte[rf.Length];
			rb.Read(data, 0, data.Length);
			rb.Close();
			rf.Close();
			return data;
		}
		static void write_bin(string path, byte[] data)
		{
			FileStream wf = new FileStream(path, FileMode.Create);
			BinaryWriter wb = new BinaryWriter(wf);
			wb.Write(data, 0, data.Length);
			wb.Close();
			wf.Close();
		}
	}
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章