背景
只要設計了IAP功能,一般就需要有2段程序。手動燒寫2段程序到不同的分區是一件比較麻煩的事情。
當然,也可以使用分散加載文件來把2段程序寫為一個工程,但是工作量更大。
方法
記得在MDK中
Options
-Output
勾選Create HEX File
。
先寫再讀寫FALSH
IAP 先燒寫進flash 的 0x0800 0000 開始位置, APP燒寫到 flash 的0x 0800 3000開始的地方; 之后通過我上一篇博文的 IAP程序的文件讀出功能讀取flash 上的數據讀到一個.bin文件上。然后通過程序刷寫工具刷入起始flash地址為 0x0800 0000中。
手動編輯
手動合並iap 和app 的.hex 文件(麻煩,出錯率高)
(1)設置IAP程序下載到flash 的開頭地址為0x0800 0000,然后編譯程序生成hex文件。
(2)設置APP程序下載到flash 的開頭地址(地址依據芯片和程序大小而定),然后編譯程序生成hex文件。
(3)用 notepad++ 打開 IAP 的hex文件和APP的hex 文件
把IAP的.hex 最后一句結束語句去掉(即:刪除:00000001FF)
把APP的.hex 全部內容拷貝復制到 剛才刪掉結束語句的 IAP的.hex后面
(4)把兩個hex合成的hex文件重新命名為XXX.hex,然后通過燒寫工具燒寫到0x0800 0000 開始位置的地址即可。
這里有python3的實現
# -*- coding: utf-8 -*-
import sys,os
import intelhex
def createHelpDialog():
#print("軟件實現的是將兩個Hex文件同時轉為Bin文件並且合並為一個Bin文件輸出的功能。")
print("將兩個Hex文件合並為一個Hex文件。")
def mergeHex(inhex1, inhex2, outhex):
# 兩個 hex
#inHex1 = 'bootloader.hex'
#inHex2 = 'app.hex'
#outHex = 'python.hex'
inHex1 = inhex1
inHex2 = inhex2
outHex = outhex
#get file name
hex_boot_len = os.path.getsize(inHex1)
hex_app_len = os.path.getsize(inHex2)
print("First %s size is %d" %(inHex1, hex_boot_len))
print("Second %s size is %d"%(inHex2, hex_boot_len))
## 刪除 之前的結果
if(os.path.isfile(outHex)):
os.remove(outHex)
#先讀寫hex,再合並成bin
print("Merge Hex file....")
hex_boot_file = open(inHex1, 'rb')
hex_app_file = open(inHex2, 'rb')
hex_file = open(outHex, 'ab')
for h in hex_boot_file.readlines():
if h == b':00000001FF\r\n' :
print(h)
break
else :
hex_file.write(h)
continue
for h in hex_app_file.readlines():
hex_file.write(h)
hex_file.write(b'\r\n')
hex_boot_file.close()
hex_app_file.close()
hex_file.close()
def hex2bin(mergeHex, outBin) :
## 刪除 之前的結果
if(os.path.isfile(outBin)):
os.remove(outBin)
print("Hex File To Bin File...")
# BUG
intelhex.hex2bin(mergeHex, outBin, None, None, None, 00)
if(os.path.isfile(outBin)):
bin_file_len = os.path.getsize(outBin)
print("Bin File is [%s] "%(outBin))
print("Bin File size is [%d] "%(bin_file_len))
print("Hex File To Bin File completly")
if __name__ == "__main__":
#mergeHex('bootloader.hex', 'app.hex', 'python2.hex')
#hex2bin('python.hex', 'python.bin')
hex2bin('merge.hex', 'python.bin')
使用現成的工具
我在網上找到了一個開源的項目:STM32-IAP-HEX-Merge。
語言為c#
,運行環境為 vs