[英]Creating different versions of .bin file in stm32cubeide
Context语境
I'm currently working on a firmware for a STM32F411CEU6, using STM32CubeIDE, I'm going to be programming several UC's, everyone of them is going to have an ID (a 32 bit unsigned number), this number is static and it will never be change in his lifespan, we are a small team but maybe we will have to program a few hundred of these devices, so changing the value associated whit that ID in the code manually will be kinda exhausting, and time consuming, so, my question is:我目前正在使用 STM32CubeIDE 为 STM32F411CEU6 开发固件,我将对多个 UC 进行编程,每个 UC 都会有一个 ID(一个 32 位无符号数),这个数字是 static,它永远不会改变他的寿命,我们是一个小团队,但也许我们必须对数百个这样的设备进行编程,因此手动更改代码中与该 ID 相关的值会有点累人,而且很耗时,所以,我的问题是:
¿Is there a way to compile different versions of firmware so it generate several.bin files, each one whit the only difference that this single constant change? ¿有没有一种方法可以编译不同版本的固件,以便生成多个 .bin 文件,每个文件的唯一区别是这个单一的常量变化?
¿Is there a way to automate this process? ¿有没有办法使这个过程自动化?
What have I thought我怎么想的
I have thought on defining this constant (and other constants if I have to) on a header file, then use something like Python to make different versions of the code, but then I would have to open every project or workspace and still have to compile and produce every.binfile manually, ¿Is there a way to produce the.bin file from python (using the STM32CubeIDE), or something like that?我考虑过在 header 文件上定义这个常量(如果需要的话,还有其他常量),然后使用类似 Python 的东西来制作不同版本的代码,但是我必须打开每个项目或工作区并且仍然必须编译并手动生成每个 .bin 文件,有没有办法从 python(使用 STM32CubeIDE)或类似的东西生成 .bin 文件?
Additional information附加信息
Thanks in advance!提前致谢! Any help would be very much appreciated任何帮助将不胜感激
If using a hex
file is an option, you could avoiding the need for re-compilation like so:如果可以选择使用hex
文件,则可以避免像这样重新编译的需要:
To generate the hex data, you can use the intelhex package. For example:要生成十六进制数据,可以使用intelhex package。例如:
import struct
from intelhex import IntelHex
from io import StringIO
ID_FLASH_ADDRESS = 0x8020000
hex_data = StringIO()
ih = IntelHex()
ih.puts(ID_FLASH_ADDRESS, struct.pack('<I', chip_id))
# Output data to variable
ih.write_hex_file(hex_data)
# Get the data
hex_data.getvalue().encode('utf-8')
Notes:笔记:
'<I'
.请参阅结构文档了解'<I'
的含义。Here are a few ideas.这里有一些想法。
The STM32F411 chip is pre-programmed (by STMicro at the factory) with a 96-bit unique device ID. STM32F411 芯片预编程(由 STMicro 在出厂时)具有 96 位唯一设备 ID。 Perhaps you can use the device's unique ID for your purposes rather than creating and assigning your own ID value.也许您可以将设备的唯一 ID 用于您的目的,而不是创建和分配您自己的 ID 值。 See Section 24.1 of the reference manual .请参阅参考手册的第 24.1 节。 This seems much safer than trying to create and manage a different bin file for each ID value.这似乎比尝试为每个 ID 值创建和管理不同的 bin 文件安全得多。
If you really want your own custom ID value, then program the ID value separately from the firmware bin file so that you don't need to create/manage different bin files for each unit.如果你真的想要你自己的自定义 ID 值,那么将 ID 值与固件 bin 文件分开编程,这样你就不需要为每个单元创建/管理不同的 bin 文件。 Write the program so that the ID value is at a know fixed address in ROM.编写程序,使 ID 值位于 ROM 中已知的固定地址。 Use the linker scatter file to reserve that address for the ID value.使用 linker 分散文件为 ID 值保留该地址。 Program the ROM of each unit in two steps, the bin file and the ID value.分两步对每个单元的ROM进行编程,bin文件和ID值。
If you really want to incorporate the ID value into the bin file then you can use a tool such as srec_cat.exe to concatenate bin (also hex or srec) files.如果您真的想将 ID 值合并到 bin 文件中,那么您可以使用诸如srec_cat.exe 之类的工具来连接 bin(也可以是 hex 或 srec)文件。 It's very versatile and you should study the man page.它用途广泛,您应该研究手册页。 One example of how you could use this tool is this: In the source code for your program, declare your unique ID value a constant pointer to a constant value located at a fixed address in ROM beyond the end of the ROM consumed by the bin file.您可以如何使用此工具的一个示例是:在您的程序的源代码中,将您的唯一 ID 值声明为一个常量指针,指向一个常量值,该常量值位于 ROM 中超出 bin 文件使用的 ROM 末尾的固定地址. Build the bin file like normal.像往常一样构建 bin 文件。 Then run srec_cat.exe to concatenate the unique ID value to the bin file with the appropriate offset.然后运行 srec_cat.exe 以将唯一 ID 值连接到具有适当偏移量的 bin 文件。 You could write a script to do this repeatedly for each unique ID value.您可以编写脚本为每个唯一 ID 值重复执行此操作。 Perhaps this script runs as a post-build action from the IDE. This solution could work but it seems like a maintenance nightmare to ensure the right bin file gets programmed onto the right device.也许此脚本作为构建后操作从 IDE 运行。此解决方案可能有效,但确保将正确的 bin 文件编程到正确的设备上似乎是一场维护噩梦。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.