0、前言
之前写过一个小专题是基于STM32有关IAP的应用的专题,现在新增一个番外篇是关于如何将IAP与APP合并为一个烧录文件及相关实例。
第一章:浅析ICP与ISP、及IAP三种单片机烧录方式
第二章:STM32应用IAP进行程序更新详解及实例
第三章:Ymodem协议解析-基于STM32的IAP实现
番外篇:IAP与APP合并为一个烧写文件-STM32实测
之前的IAP和APP在最开始的时候是通过相关烧录工具“STM32CubeProgrammer”来进行分别烧录的,之后在有了IAP的基础上才是通过IAP来更新APP的firmware。如此一来在第一次进行Chip的烧录的时候就必须分开两次来进行烧录,个人做demo玩玩的话倒是也没什么,但是假如说是导入工厂进行量产的产品还要分开两次来烧录的话那就是大大的浪费时间。
好,那么现在就来介绍如何合并两个bin文件,本次测试是基于第二章:STM32应用IAP进行程序更新详解及实例中的实例进行测试,Flash分区如下图所示:
1、方法一:使用J-Flash
使用过J-Link的想必都了解J-Flash,不清楚的可详见J-Flash的官方介绍。J-Flash 是运行在 Windows(Windows 2000 及更高版本)系统、Linux 或 macOS 上的 PC 软件,它使您能够通过 J-Link 或 Flasher 对微控制器的内部和外部闪存进行编程。其功能详情如下:
- Supports most Cortex A-R-M, RX and Power PC devices/cores
- Supports the following internal and external flash devices:
- Internal flash of most popular microcontrollers
- CFI-compliant NOR flash (the combinations 1x8, 2x8, 1x16, 2x16 are supported)
- Most non-CFI compliant NOR flash devices (the combinations 1x8, 2x8, 1x16, 2x16 are supported)
- SPI NOR-flash
- NAND flash
1)加载IAP文件到地址0x08000000:
2)加载APP文件到地址0x08004000:
加载APP后地址0x08004000起下图所示:
3)文件合并File–>Save data file as…,将加载了IAP和APP文件的数据文件另存为一个文件:
2、使用脚本进行合并
# -*- coding: utf_8 -*-
#from __future__ import print_function
import os
import sys
import time
import argparse
import shutil
import struct
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="bin merge demo")
parser.add_argument('--iap', dest='iap', type=str, default='', action='store', help='iap bin file path')
parser.add_argument('--app', dest='app', type=str, default='', action='store', help='app bin file path')
parser.add_argument('--target', dest='target', type=str, default='./merge.bin', action='store', help='merge bin file path')
args = parser.parse_args()
if args.iap == '' or args.app == '':
print('Pls input the iap and app path')
sys.exit()
iap_path = args.iap
app_path = args.app
merge_path = args.target
offset1 = 0x00000000
offset2 = 0x00004000
shutil.copyfile(iap_path, merge_path)
iap_bin = open(iap_path,'rb')
app_bin = open(app_path,'rb')
bin_merge = open(merge_path, 'ab')
app_size = os.path.getsize(app_path)
bin_merge_size = os.path.getsize(merge_path)
final_size = offset2 + app_size
offset = bin_merge_size
value_default = struct.pack('B', 0xff)
while offset < final_size:
if offset == offset2:
data = app_bin.read()
bin_merge.write(data)
offset = bin_merge.tell()
else:
bin_merge.write(value_default)
offset = bin_merge.tell()
iap_bin.close()
app_bin.close()
bin_merge.close()
print('iap and app merge finish!\n')
sys.exit()
以上介绍了两种合并IAP和APP的bin文件的方式,之后有其他的话还会继续更新,如有错误欢迎指出,谢谢!