我一直使用mbed在线编译器。该工具在mbed2时代一直表现很好,但是mbed5之后总会出现在线编译ARMCC和离线工具链MDK5之间的差异(包括库函数差异和运行时差异)。所以想要测试一下mbed CLI工具的表现。
安装工具
针对Windows 7以上用户,mbed提供了专门的软件包,大约180MB左右。愿意手动安装的同学可以自行安装:
- Python 2.7.X;
- mbed-cli工具,通过pip安装;
- git工具;
- hg工具。
在命令行中分别测试是否在系统路径中,以及版本是否合适。
构建测试环境
在工作盘中建立一个测试文件夹:
D:\MBED\MBED-GO-THROUGH
└─dev_directory
将dev_directory
作为工作目录,移动到该目录并输入:
allankliu@DELL-D520 D:\mbed\mbed-go-through\dev_directory
> mbed import mbed-os-example-blinky
[mbed] Importing program "mbed-os-example-blinky" from "https://github.com/ARMmbed/mbed-os-example-blinky" at latest revision in the current branch
[mbed] Adding library "mbed-os" from "https://github.com/ARMmbed/mbed-os" at rev #ca661f9d2852
mbed-os大约500MB,导入很花时间。真正瓶颈在网络连接,耗时而且没有进度条。请自行寻找科学上网方式,并确保足够带宽。mbed可以配置成SSL方式替代HTTPS方式,可以加快导入速度。
编译源码
- 目标板:
NUCLEO_F103RB
- 工具链:
GCC_ARM
首先将本工作目录作为root。
输入:
> mbed config root .
[mbed] . now set as default root in program "dev_directory"
> mbed deploy
[mbed] WARNING: Program "dev_directory" in "D:\mbed\mbed-go-through\dev_directory" does not use source control management.
[mbed] WARNING: To fix this you should use "mbed new ." in the root of your program.
---
[mbed] Updating library "mbed-os-example-blinky\mbed-os" to rev #ca661f9d2852
[mbed] Couldn't find build tools in your program. Downloading the mbed 2.0 SDK tools...
以上WARNING/ERROR,我尚有疑问。
输入:
> mbed compile -m NUCLEO_F103RB -t GCC_ARM
Building project dev_directory (NUCLEO_F103RB, GCC_ARM)
Scan: .
Scan: mbed
Scan: env
Compile [ 0.4%]: AnalogIn.cpp
Compile [ 0.7%]: main.cpp
Compile [ 1.1%]: BusIn.cpp
Compile [ 1.4%]: BusInOut.cpp
Compile [ 1.8%]: BusOut.cpp
Compile [ 2.2%]: CAN.cpp
.
.
.
Compile [ 97.8%]: serial_api.c
Compile [ 98.2%]: rtc_api.c
Compile [ 98.6%]: sleep.c
Compile [ 98.9%]: stm_spi_api.c
Compile [ 99.3%]: trng_api.c
Compile [ 99.6%]: us_ticker_16b.c
Compile [100.0%]: us_ticker_32b.c
Link: dev_directory
Elf2Bin: dev_directory
+--------------------+-------+-------+------+
| Module | .text | .data | .bss |
+--------------------+-------+-------+------+
| Fill | 60 | 8 | 14 |
| Misc | 22065 | 2492 | 112 |
| drivers | 118 | 4 | 100 |
| hal | 548 | 0 | 24 |
| platform | 1125 | 4 | 270 |
| rtos | 491 | 4 | 4360 |
| rtos/rtx5 | 7924 | 176 | 1368 |
| targets/TARGET_STM | 6377 | 4 | 432 |
| Subtotals | 38708 | 2692 | 6680 |
+--------------------+-------+-------+------+
Allocated Heap: unknown
Allocated Stack: unknown
Total Static RAM memory (data + bss): 9372 bytes
Total RAM memory (data + bss + heap + stack): 9372 bytes
Total Flash memory (text + data + misc): 41400 bytes
Image: .\BUILD\NUCLEO_F103RB\GCC_ARM\dev_directory.bin
把这个bin文件通过mbed MSD下载到NUCLEO中去,工作正常。
导出工程
输入:
> mbed export -m NUCLEO_F103RB -i UVISION5
Using targets from D:\mbed\mbed-go-through\dev_directory\mbed-os-example-blinky\mbed-os\targets\targets.json
Scan: .
工具链选项是-i
,不是-t
。
编译MDK5工程
工程导出后,可以正常打开,但是许多细节需要大家自行核实:
- NUCLEO_F103RB的Debugger选项是否切换到SWD方式;
- Flash下载算法是否指定;
- 工程选项是否使能HEX输出和浏览信息(调试信息)。
编译后进行下载和调试,工作正常。
帮助信息
mbed是个自解释的脚本,可以通过-h
选项来获取。
输入:
> mbed --help
usage: mbed [-h] [--version] ...
Command-line code management tool for ARM mbed OS - http://www.mbed.com
version 0.9.10
Use 'mbed -h|--help' for detailed help.
Online manual and guide available at https://github.com/ARMmbed/mbed-cli
optional arguments:
-h, --help show this help message and exit
--version print version number and exit
Commands:
new Create new mbed program or library
import Import program from URL
add Add library from URL
remove Remove library
deploy Find and add missing libraries
publish Publish program or library
update Update to branch, tag, revision or latest
sync Synchronize library references
ls View dependency tree
status Show version control status
compile Compile code using the mbed build tools
test Find, build and run tests
export Generate an IDE project
detect Detect connected mbed targets/boards
config Tool configuration
target Set or get default target
toolchain Set or get default toolchain
help This help screen
如果想知道命令的详细帮助,在命令后加上 "-h" 即可,输入:
> mbed config -h
usage: mbed config [-h] [-G] [-U] [-L] [-v] [-vv] [var] [value]
Gets, sets or unsets mbed tool configuration options.
Options can be global (via the --global switch) or local (per program)
Global options are always overridden by local/program options.
Currently supported options: target, toolchain, protocol, depth, cache
positional arguments:
var Variable name. E.g. "target", "toolchain", "protocol"
value Value. Will show the currently set default value for a
variable if not specified.
optional arguments:
-h, --help show this help message and exit
-G, --global Use global settings, not local
-U, --unset Unset the specified variable.
-L, --list List mbed tool configuration. Not to be confused with
compile configuration, e.g. "mbed compile --config".
-v, --verbose Verbose diagnostic output
-vv, --very_verbose Very verbose diagnostic output
仔细查看各个命令,可以有不少发现:
-
mbed new --mbedlib
可以只导入lib,而不导入源码;
总结
简单的教程总算是正常走过了,虽然还有些小问题没有解决。mbed CLI在简单例程中没有出现F103RB的CAN定义缺失和HardFault运行时问题,这是好事情。但是缺点是源码下载、编译泰国耗时,且占用太多硬盘空间。一个MCU工程500MB,而且只是切换LED就把整个OS包括mbed TLS都放在一起编译,太夸张了。与MDK5相比,使用GCC_ARM快许多,而且GCC_ARM在Linux下还可以支持并行编译。