NXP

Python intelhex 包

2019-07-12 11:31发布

需求

拿到手一款NXP的LPC824-DIP开发板,好简单,与我之前的LPC812 Mini Kit一样简单:板载USB/UART转换IC,甚至使用了转换IC内部的LDO做供电,预留SWD引脚。 离开NXP之后,NXP/Freescale的工程和工具链就摸的少了。STM32F072的JLINK-OB不知道是否可以支持LPC824,或者拿个FRDM的板载CMSIS-DAP来试试看。

bin2hex转换

不过这么简单的MCU,可以采用编译下载和printf的方式。我目前用的最多的还是mbed在线编译器,它仅支持bin格式。而NXP推荐的FlashMagic只支持intel hex格式。而bin2hex的方法虽然有许多种,但是都嫌麻烦。Keil推荐的bin2hex早就落伍了,gcc的objcopy还需要记录一大堆参数。

Python intelhex包

忽然想起Python有一个intelhex包,重来没有用过,使用pip安装了一下: pip install intelhex

转换下载

安装完毕后,在PythonScripts下可以直接访问几个工具脚本: > dir c:Python27Scripts*hex*.py 驱动器 C 中的卷是 HP_HDD 卷的序列号是 48F7-515A c:Python27Scripts 的目录 2017/09/28 14:20 3,825 bin2hex.py 2017/09/28 14:20 4,684 hex2bin.py 2017/09/28 14:20 4,549 hex2dump.py 2017/09/28 14:20 2,857 hexdiff.py 2017/09/28 14:20 3,681 hexinfo.py 2017/09/28 14:20 6,214 hexmerge.py 6 个文件 25,810 字节 0 个目录 210,940,616,704 可用字节 我们把mbed产生的bin文件转换成hex格式。 allankliu@ALLANKLIU-HP C:UsersallankliuDownloads > bin2hex.py -h Bin2Hex convertor utility. Usage: python bin2hex.py [options] INFILE [OUTFILE] Arguments: INFILE name of bin file for processing. Use '-' for reading from stdin. OUTFILE name of output file. If omitted then output will be writing to stdout. Options: -h, --help this help message. -v, --version version info. --offset=N offset for loading bin file (default: 0). allankliu@ALLANKLIU-HP C:UsersallankliuDownloads > bin2hex.py mbed_blinky_LPC_LPC824.bin mbed_blinky_LPC_LPC824.hex allankliu@ALLANKLIU-HP C:UsersallankliuDownloads > hexinfo.py mbed_blinky_LPC_LPC824.hex - file: 'mbed_blinky_LPC_LPC824.hex' data: - { first: 0x00000000, last: 0x00002AFF, length: 0x00002B00 } 我们可以看一下转换后的hex文件。 allankliu@ALLANKLIU-HP C:UsersallankliuDownloads > hex2dump.py mbed_blinky_LPC_LPC824.hex 0000 00 20 00 10 DD 00 00 00 ED 00 00 00 E5 00 00 00 |. ..............| 0010 00 00 00 00 00 00 00 00 00 00 00 00 51 DD FF EF |............Q...| 0020 00 00 00 00 00 00 00 00 00 00 00 00 E7 00 00 00 |................| 0030 00 00 00 00 00 00 00 00 E9 00 00 00 EB 00 00 00 |................| 0040 ED 00 00 00 ED 00 00 00 00 00 00 00 ED 00 00 00 |................| 0050 ED 00 00 00 ED 00 00 00 00 00 00 00 ED 00 00 00 |................| 0060 ED 00 00 00 ED 00 00 00 ED 00 00 00 ED 00 00 00 |................| 0070 ED 00 00 00 ED 00 00 00 ED 00 00 00 ED 00 00 00 |................| 0080 ED 00 00 00 ED 00 00 00 ED 00 00 00 ED 00 00 00 |................| 0090 ED 00 00 00 ED 00 00 00 ED 00 00 00 00 00 00 00 |................| 00A0 ED 00 00 00 ED 00 00 00 ED 00 00 00 ED 00 00 00 |................| 00B0 ED 00 00 00 ED 00 00 00 ED 00 00 00 ED 00 00 00 |................| 00C0 05 48 85 46 00 F0 12 FB 00 F0 EE F8 00 F0 86 F8 |.H.F............| 00D0 00 48 00 47 E5 02 00 00 00 20 00 10 04 48 80 47 |.H.G..... ...H.G| 00E0 04 48 00 47 FE E7 FE E7 FE E7 FE E7 FE E7 00 00 |.H.G............| 00F0 39 08 00 00 C1 00 00 00 F8 B5 17 46 0E 46 05 46 |9..........F.F.F| 0100 00 2B 07 D0 10 46 58 43 44 19 E4 1B 20 46 B0 47 |.+...FXCD... F.G| 0110 A5 42 FA D1 28 46 08 38 F8 BD 38 B5 05 46 68 46 |.B..(F.8..8..FhF| 0120 05 70 0C 46 08 68 00 23 01 22 69 46 01 F0 92 FC |.p.F.h.#."iF....| 0130 00 28 02 D0 00 20 C0 43 38 BD E0 68 40 1C E0 60 |.(... .C8..h@..`| 0140 28 46 38 BD 30 B5 0B 46 01 46 00 20 20 22 01 24 |(F8.0..F.F. ".$| 0150 09 E0 0D 46 D5 40 9D 42 05 D3 1D 46 95 40 49 1B |...F.@.B...F.@I.| 0160 25 46 95 40 40 19 15 46 52 1E 00 2D F1 DC 30 BD |%F.@@..FR..-..0.| 0170 03 46 0B 43 9B 07 03 D0 09 E0 08 C9 12 1F 08 C0 |.F.C............| 0180 04 2A FA D2 03 E0 0B 78 03 70 40 1C 49 1C 52 1E |.*.....x.p@.I.R.| 0190 F9 D2 70 47 42 1C 01 78 40 1C 00 29 FB D1 80 1A |..pGB..x@..)....| 01A0 70 47 10 B5 00 22 00 E0 52 1C 83 5C 8C 5C A3 42 |pG..."..R....B| 01B0 01 D1 00 2B F8 D1 D8 B2 E1 B2 40 1A 10 BD 30 B5 |...+......@...0.| 01C0 05 46 00 20 03 46 00 E0 5B 1C 93 42 05 D2 EC 5C |.F. .F..[..B...| 01D0 C8 5C 20 1A 01 D1 00 2C F6 D1 30 BD 70 B5 05 4C |. ....,..0.p..L| 01E0 05 4D 03 E0 20 68 00 19 80 47 24 1D AC 42 F9 D1 |.M.. h...G$..B..| 01F0 70 BD 00 00 94 2A 00 00 98 2A 00 00 02 46 70 B5 |p....*...*...Fp.| 0200 4A 40 D3 0F DB 07 40 00 2D D0 4A 00 2A D0 01 0E |J@....@.-.J.*...| ... 2900 6F 72 6B 73 70 61 63 65 2F 62 6D 5F 77 72 61 70 |orkspace/bm_wrap| 2910 2F 31 33 38 34 2F 6D 62 65 64 2D 6F 73 2F 74 61 |/1384/mbed-os/ta| 2920 72 67 65 74 73 2F 54 41 52 47 45 54 5F 4E 58 50 |rgets/TARGET_NXP| 2930 2F 54 41 52 47 45 54 5F 4C 50 43 38 32 58 2F 67 |/TARGET_LPC82X/g| 2940 70 69 6F 5F 61 70 69 2E 63 00 00 00 2F 68 6F 6D |pio_api.c.../hom| 2950 65 2F 61 72 6D 2F 6D 62 65 64 5F 6A 65 6E 6B 69 |e/arm/mbed_jenki| 2960 6E 73 2F 77 6F 72 6B 73 70 61 63 65 2F 62 6D 5F |ns/workspace/bm_| 2970 77 72 61 70 2F 31 33 38 34 2F 6D 62 65 64 2D 6F |wrap/1384/mbed-o| 2980 73 2F 42 55 49 4C 44 2F 6D 62 65 64 2F 54 41 52 |s/BUILD/mbed/TAR| 2990 47 45 54 5F 4C 50 43 38 32 34 2F 54 41 52 47 45 |GET_LPC824/TARGE| 29A0 54 5F 4E 58 50 2F 54 41 52 47 45 54 5F 4C 50 43 |T_NXP/TARGET_LPC| 29B0 38 32 58 2F 67 70 69 6F 5F 6F 62 6A 65 63 74 2E |82X/gpio_object.| 29C0 68 00 00 00 2F 68 6F 6D 65 2F 61 72 6D 2F 6D 62 |h.../home/arm/mb| 29D0 65 64 5F 6A 65 6E 6B 69 6E 73 2F 77 6F 72 6B 73 |ed_jenkins/works| 29E0 70 61 63 65 2F 62 6D 5F 77 72 61 70 2F 31 33 38 |pace/bm_wrap/138| 29F0 34 2F 6D 62 65 64 2D 6F 73 2F 74 61 72 67 65 74 |4/mbed-os/target| 2A00 73 2F 54 41 52 47 45 54 5F 4E 58 50 2F 54 41 52 |s/TARGET_NXP/TAR| 2A10 47 45 54 5F 4C 50 43 38 32 58 2F 70 69 6E 6D 61 |GET_LPC82X/pinma| 2A20 70 2E 63 00 2F 68 6F 6D 65 2F 61 72 6D 2F 6D 62 |p.c./home/arm/mb| 2A30 65 64 5F 6A 65 6E 6B 69 6E 73 2F 77 6F 72 6B 73 |ed_jenkins/works| 2A40 70 61 63 65 2F 62 6D 5F 77 72 61 70 2F 31 33 38 |pace/bm_wrap/138| 2A50 34 2F 6D 62 65 64 2D 6F 73 2F 70 6C 61 74 66 6F |4/mbed-os/platfo| 2A60 72 6D 2F 6D 62 65 64 5F 63 72 69 74 69 63 61 6C |rm/mbed_critical| 2A70 2E 63 00 00 98 2A 00 00 C0 00 00 10 68 00 00 00 |.c...*......h...| 2A80 4C 0E 00 00 00 2B 00 00 28 01 00 10 E8 01 00 00 |L....+..(.......| 2A90 5C 0E 00 00 D9 E3 FF FF 00 00 00 00 00 00 00 00 |...............| 2AA0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 2AB0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 2AC0 00 00 00 00 80 C3 C9 01 00 87 93 03 00 00 00 00 |................| 2AD0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 2AE0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 2AF0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| 话说回来,觉得HEX中带有这么多的源码字符串有些怪异,可能与mbed平台内调试需求有关联。但是最好有个编译开关能够把这些没有必要的调试信息给关闭。这在小资源MCU还蛮重要的。

下载测试

把HEX代码下载后,一开始没有任何动静。想想可能是LED的GPIO配置不同,改动代码后,重复了整合流程,绿 {MOD}LED开始闪烁,UART也有输出了。好简单! #include "mbed.h" DigitalOut myled(LED2); // Change LED1 to LED2 on LPC824-DIP board Serial pc(D1, D2); int main() { while(1) { myled = 1; wait(0.2); myled = 0; wait(0.2); pc.printf("."); printf("#"); } }

改进余地

  • Python intelhex不仅提供了命令行工具软件,更重要的是可以实现批量的程序化定制,比如根据MCU的UID进行部分代码的加密。
  • LPC系列中,8051/ARM7/Cortex-M3/M4/M0/M0+,无论哪种内核,都可以直接使用串口的RTS等串口流控线来控制目标MCU的RST/ISP引脚,而且逻辑电平都是负电平。
  • Python pyserial可以很容易地操控流控引脚,继而操控RST/ISP来控制MCU的工作模式,可以用于自动读取UID,计算密钥,将密钥hex与原始hex进行合并,烧入固件,启动CRP代码保护,直至复位。
  • 基于这些设计,可以实现主机端(包括PC/平板和手机)动态更新MCU固件,在同一硬件上实现固件的动态功能变化。不过嵌入式系统往往不具备通用性,即时代码更新缺乏业务支点和市场需求。不过我个人觉得可以先拿通用遥控器来玩玩、试试。话说,好古老的需求。