当前位置:网站首页>单片机 MCU 固件打包脚本软件
单片机 MCU 固件打包脚本软件
2022-07-01 02:36:00 【大橙子疯】
1 前言
开发完 MCU 软件后,通常都会生成 hex 文件或者 bin 文件,用来做固件烧录或者升级,如果用来做产品开发,就涉及到固件版本的问题,初学者通常采用固件文件重命名来区分版本。
如果需要将版本写入固件中,就需要通过一定的方式去实现,实现的方式有很多。
2 介绍
下面介绍一个自动打包固件的脚本软件,主要实现以下功能:
- 基于 Windows 平台的单片机 MCU 固件脚本打包工具
- 支持 hex 文件的裁剪和 hex 文件的合并
- 可以为 hex 固件添加版本信息、Git Commit 分支和提交记录等
- 按照版本信息命名hex固件,可生成 bin 文件等
- 以上均可通过 ini 配置文件设置参数对 hex 文件进行操作
3 实现步骤
下面以 MDK + STM32 开发为例介绍。
3.1 __attribute__机制
首先了解一下__attribute__机制,它是个编译器指令,告诉编译器声明的特性,或者让编译器进行更多的错误检查和高级优化。
GUN C中可以使用__attribute__()给变量、函数和类型设置各种属性,而__attribute__的section选项可以改变段的特性;
其中__attribute__((section("section_name")))的作用是将该定义的函数或数据变量放入指定名为”section_name”段中。
无论是 GNU 还是 ARM 的编译器, 都支持
__attribute__所指定的编译属性。
打开keil的options…,取消勾选下图所示,然后点击“Edit…”。

自动弹出“*.sct”文件(先编译通过再操作),下面就是 Keil 中 STM32 的链接文件,编译器会根据链接文件和__attribute__的section选项(可以自己添加一个段,分配地址和大小)等分配函数和数据变量在程序固件中的地址。
; *************************************************************
; *** Scatter-Loading Description File generated by uVision ***
; *************************************************************
LR_IROM1 0x08000000 0x00010000 { ; load region size_region
ER_IROM1 0x08000000 0x00010000 { ; load address = execution address
*.o (RESET, +First)
*(InRoot$$Sections)
.ANY (+RO)
.ANY (+XO)
}
RW_IRAM1 0x20000000 0x00005000 { ; RW data
.ANY (+RW +ZI)
}
} 这里不做过多介绍了,下面介绍的方式不需要自己修改“*.sct”文件,还是采用__attribute__的section选项,只不过在section选项中指定位置即可。
__attribute__ ((section(".ARM.__at_0x08000020")))3.2 代码实现
1. 定义一个结构体,里面定义一些软件版本相关的信息
typedef struct
{
char szVersion[32]; // 软件版本
char szBuildDate[32]; // 程序编译日期
char szBuildTime[32]; // 程序编译时间
char szCommitId[32]; // git commit id
}AppInfo_t;2. 通过__attribute__定义一个只读结构体变量(只读的目的:防止程序改变、节约RAM),赋初值(其中__DATE_ 和__TIME__是C语言中的内置宏,分别是当前的编译日期和编译时间)。
const AppInfo_t __attribute__ ((section(".ARM.__at_0x08002000"))) sg_tAppInfo =
{
"STM32_TEST",
__DATE__,
__TIME__,
""
};注:STM32的代码起始地址是从0x08000000开始的,且存储中断向量表信息,因此在选择程序地址的时候一定要绕开,也不能太靠后,不然生成的bin文件超出了实际的代码固件大小,在实现bin文件升级的时候就会耗时太长。
3. 通过串口打印出来
int main(void)
{
FML_USART_Init();
USART_Printf(0, "Version : %s\r\n", sg_tAppInfo.szVersion);
USART_Printf(0, "buildTime: %s\r\n", sg_tAppInfo.szBuildDate);
USART_Printf(0, "buildTime: %s\r\n", sg_tAppInfo.szBuildTime);
USART_Printf(0, "commitId: %s\r\n\r\n", sg_tAppInfo.szCommitId);
while(1);
}4. 提交git编译后,可以看的 git commit id 值(通过 git commit 可以迅速定位是什么时候的源码进行编译的)

3.3 固件打包
下载固件打包脚本,根据配置设置后,双击 bat 即可完成固件打包,然后点击下载验证即可。
需要通过 J-LINK 工具包或者 ST-Link 工具打开生成的固件进行烧录(通过Keil编译直接下载的没有用,我这里用的是 ST-Link 工具)。

4 配置文件内容
下面列举配置文件中的选项
; 文中的路径可采用绝对路径或者相对路径(相对于固件打包bat文件而言)
; 版本信息 Flash 起始地址 预留大小 前缀字符串
[version]
addr=0x08002000
size=32
strPrefix=
; Git 信息 Flash 起始地址 预留大小
[git_commit]
addr=0x08002060
size=32
[boot_file]
; Boot Hex 文件路径 文件名称
hexFilePath=.\
hexFileName=test_boot
[file]
; Hex 文件路径 文件名称
hexFilePath=.\
hexFileName=test
; 裁剪起始地址 保留大小
hexFileAddr=0x08000000
hexFileSize=0xFFFF
; 打包文件的输出路径
outputPath=.\output
[option]
; 是否合并boot固件
isMergeBootHexFile=0
; 是否生成 Bin 文件
isGenerateBin=1
; 是否裁剪 Hex 文件,根据(hexFileAddr hexFileSize)
isCropHexFile=1
; 是否添加 Git Commit 信息
isAddGitCommit=0
; 打包成功后是否清除临时文件
isClearTmpFile=15 下载地址
边栏推荐
- SWT / anr problem - SWT caused by too long dump time
- Evaluation of the entry-level models of 5 mainstream smart speakers: apple, Xiaomi, Huawei, tmall, Xiaodu, who is better?
- Alphabet rearrange inator 3000 (dictionary tree custom sorting)
- Is there any discount for opening an account now? In addition, is it safe to open a mobile account?
- How to buy Hong Kong shares in China? What platform is safer?
- Pytorch —— 基礎指北_貳 高中生都能看懂的[反向傳播和梯度下降]
- UE4渲染管线学习笔记
- [JS] [Nuggets] get people who are not followers
- The image variables in the Halcon variable window are not displayed, and it is useless to restart the software and the computer
- robots.txt限制搜索引擎收录
猜你喜欢

pycharm 软件deployment 灰色 无法点

园区运营效率提升,小程序容器技术加速应用平台化管理

js中的图片预加载

halcon变量窗口的图像变量不显示,重启软件和电脑都没用

My PMP learning test experience

产业互联网中,「小」程序有「大」作为

kubernetes资源对象介绍及常用命令(二)
![How to add a condition for an associated table in an SQL statement [null value required or not required]](/img/91/0efbc13597be4dba5b9cf4e8644e35.png)
How to add a condition for an associated table in an SQL statement [null value required or not required]

小程序自定义顶部导航栏,uni-app微信小程序自定义顶部导航栏

nacos配置中心使用教程
随机推荐
Pytorch - - Basic Reference North Deux élèves du secondaire peuvent comprendre [Rétropropagation et Gradient descendant]
SWT / anr problem - deadlock
[punch in questions] integrated daily 5 questions sharing (phase I)
@ConfigurationProperties和@Value的区别
SWT / anr problem - SWT caused by too long dump time
The mobile edge browser cannot open the third-party application
鼠标悬停效果四
鼠标悬停效果十
Thread Detach
Find the length of the common part of two line segments
522. Longest special sequence II
Pulsar geo replication/ disaster recovery / regional replication
Sampling Area Lights
AI edge computing platform - beaglebone AI 64 introduction
Leetcode interview question 17.10 Main elements
halcon变量窗口的图像变量不显示,重启软件和电脑都没用
十大券商有哪些?另外想问,现在在线开户安全么?
SAP ALV summary is inconsistent with exported excel summary data
Objects and object variables
[graduation season · advanced technology Er] - summary from graduation to work
https://gitee.com/const-zpc/mcu-pack-script