为MindSDK搭建`armgcc`编译环境(Windows版)

为MindSDK搭建armgcc编译环境(Windows版)

引言

Keil或者IAR为嵌入式开发者提供了非常方便的可视化开发环境,但Keil或者IAR是一个商业软件,需要购买并注册License才能正常使用(Arm提供的社区版License可以供用户免费使用1年)。另外,仍有用户希望使用命令行的方式编译微控制器工程,这些用户可能来自于Linux用户,也可能是某些定制工具开发包的用户,或者搭建自动化持续集成平台的系统维护工程师。此时,搭建开源的armgcc工具集,也是微控制器开发者常用的编译工具解决方案。命令行方式使用的工具,在Linux操作系统中较为常用,但在Windows操作系统中,可以基于MSYS2作为模拟命令行工作环境的虚拟终端,配合armgcc工具集,搭建微控制器开发环境。

本文讲述基于Windows操作系统,搭建armgcc编译环境,并支持编译灵动微电子官方软件开发平台的MindSDK样例工程。

安装MSYS2环境

MSYS2是一系列软件工具和软件库,可用于在Windows操作系统上模拟类似于Linux操作系统的命令行运行环境,并且可以使用部分Linux操作系统的部分命令,用以方便地构建、安装和运行本地的应用程序。

MSYS2的官网(http://www.msys2.org/)上,可下载当前最新的安装包。如图x所示。
在这里插入图片描述

图x 在MSYS2官网下载MSYS2安装包

如图x所示,下载msys-x86_64-20220904.exe安装文件后,即可启动安装过程。安装完成后,启动MSYS2程序,可以看到MSYS2的命令行交互终端界面,如图x所示。
在这里插入图片描述

图x 运行MSYS的终端界面

安装完之后,可在MSYS2环境的终端下,试用Linux下的文件夹查看命令"ls"和显示工作目录命令"pwd",确认MSYS2环境可以正常工作。

MindMotion@DESKTOP-MCR7G5A MSYS ~
$ ls
MindMotion@DESKTOP-MCR7G5A MSYS ~
$ pwd
/home/MindMotion

MSYS2环境中安装工具

这里使用make作为基本的构建系统工具,但为了便于用户编写构建工程文件makefile,选用cmake作为创建makefile文件的工具。MSYS2环境中的的包管理器已经提供了可用的makecmake工具,可用于安装。

MSYS2环境中安装make工具

使用pacman -Ss命令,在MSYS2中查找可用的make工具的安装包:

MindMotion@DESKTOP-MCR7G5A MSYS ~
$ pacman -Ss make
...
msys/make 4.3-3 (base-devel)
    GNU make utility to maintain groups of programs
...

使用pacman -S命令,在MSYS2环境中安装make工具:

MindMotion@DESKTOP-MCR7G5A MSYS ~
$ pacman -S msys/make
resolving dependencies...
looking for conflicting packages...

Packages (1) make-4.3-3

Total Download Size:   0.46 MiB
Total Installed Size:  1.48 MiB

:: Proceed with installation? [Y/n] Y
:: Retrieving packages...
 make-4.3-3-x86_64               466.2 KiB   538 KiB/s 00:01 [###############################] 100%
(1/1) checking keys in keyring                               [###############################] 100%
(1/1) checking package integrity                             [###############################] 100%
(1/1) loading package files                                  [###############################] 100%
(1/1) checking for file conflicts                            [###############################] 100%
(1/1) checking available disk space                          [###############################] 100%
:: Processing package changes...
(1/1) installing make                                        [###############################] 100%
:: Running post-transaction hooks...
(1/1) Updating the info directory file...

运行命令查看版本号,可以确认make工具已经安装完成,可以正常使用。

MindMotion@DESKTOP-MCR7G5A MSYS ~
$ make --version
GNU Make 4.3
Built for x86_64-pc-msys
Copyright (C) 1988-2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

MSYS2中安装CMake工具

使用pacman -Ss命令,在MSYS2环境中查找cmake工具的安装包。

MindMotion@DESKTOP-MCR7G5A MSYS ~
$ pacman -Ss cmake
...
msys/cmake 3.21.0-1
    A cross-platform open-source make system
...

使用pacman -S命令,在MSYS2环境中安装cmake工具

MindMotion@DESKTOP-MCR7G5A MSYS ~
$ pacman -S msys/cmake
resolving dependencies...
looking for conflicting packages...

Packages (6) jsoncpp-1.9.5-1  libarchive-3.6.1-2  librhash-1.4.2-1  libuv-1.42.0-1  pkgconf-1.8.0-1
             cmake-3.23.2-1

Total Download Size:    7.80 MiB
Total Installed Size:  41.22 MiB

:: Proceed with installation? [Y/n] Y
:: Retrieving packages...
 jsoncpp-1.9.5-1-any             101.2 KiB   116 KiB/s 00:01 [###############################] 100%
 libuv-1.42.0-1-x86_64           113.1 KiB  79.1 KiB/s 00:01 [###############################] 100%
 pkgconf-1.8.0-1-x86_64           58.4 KiB  83.0 KiB/s 00:01 [###############################] 100%
 librhash-1.4.2-1-x86_64         116.5 KiB  47.8 KiB/s 00:02 [###############################] 100%
 libarchive-3.6.1-2-x86_64      1138.4 KiB   353 KiB/s 00:03 [###############################] 100%
 cmake-3.23.2-1-x86_64             6.3 MiB  1738 KiB/s 00:04 [###############################] 100%
 Total (6/6)                       7.8 MiB  1965 KiB/s 00:04 [###############################] 100%
(6/6) checking keys in keyring                               [###############################] 100%
(6/6) checking package integrity                             [###############################] 100%
(6/6) loading package files                                  [###############################] 100%
(6/6) checking for file conflicts                            [###############################] 100%
(6/6) checking available disk space                          [###############################] 100%
:: Processing package changes...
(1/6) installing jsoncpp                                     [###############################] 100%
JsonCpp headers and build libraries are now in jsoncpp-devel.
(2/6) installing libarchive                                  [###############################] 100%
(3/6) installing librhash                                    [###############################] 100%
(4/6) installing libuv                                       [###############################] 100%
(5/6) installing pkgconf                                     [###############################] 100%
(6/6) installing cmake                                       [###############################] 100%

尝试查看版本号,可以确认CMake工具已经安装完成并可正常使用。

MindMotion@DESKTOP-MCR7G5A MSYS ~
$ cmake --version
cmake version 3.23.2

CMake suite maintained and supported by Kitware (kitware.com/cmake).

MSYS2环境中安装armgcc工具集

MSYS2的包管理器中也包含了armgcc的工具集,可以使用pacman搜索arm-none-eabi-gcc,找到对应的软件包。

MindMotion@DESKTOP-MCR7G5A MSYS ~
$ pacman -Ss arm-none-eabi-gcc
mingw32/mingw-w64-i686-arm-none-eabi-gcc 10.1.0-2 (mingw-w64-i686-arm-none-eabi-toolchain)
    GNU Tools for ARM Embedded Processors - GCC (mingw-w64)
mingw64/mingw-w64-x86_64-arm-none-eabi-gcc 10.1.0-2 (mingw-w64-x86_64-arm-none-eabi-toolchain)
    GNU Tools for ARM Embedded Processors - GCC (mingw-w64)

在这里插入图片描述

图x 在SYS2环境中使用pacman检索`armgcc`工具集

但实际上,armgcc工具集也提供了Windows操作系统下的可执行文件,因此,这里推荐的做法是,直接下载并解压Windows操作系统的armgcc工具集的压缩包,然后在MSYS2中引用。这个过程展现了另一种在MSYS2环境中集成工具软件的方法,但更重要的是,在Windows操作系统中安装的armgcc工具集,同时还可以被其它可用的集成开发环境引用,例如VS Code,这样可以避免重复安装同一个软件包,确保在同一台开发主机上编译器行为的一致性。

在Arm的官网(https://developer.arm.com/downloads/-/gnu-rm)上,可下载Windows操作系统版本对应的armgcc编译器工具包。如图x所示。
在这里插入图片描述

图x 在MSYS2官网下载MSYS2安装包

如图x所示页面中,下载gcc-arm-none-eabi-10.3-2021.10-win32.zip文件,解压至Windows文件系统的目录中,例如C:\gcc-arm-none-eabi-xxxx目录。然后,在MSYS2的安装目录下的etc目录下,例如C:/msys64/etc,在其中profile文件末尾处添加对gcc-arm-none-eabi工具集的引用路径。另外,可在MSYS2运行环境中创建armgcc`_DIR`环境变量,后续编译MindSDK的`armgcc`工程时,将会使用armgcc_DIR环境变量检索armgcc编译工具集的路径。

...
  echo "###################################################################"
  echo "#                                                                 #"
  echo "#                                                                 #"
  echo "#                   C   A   U   T   I   O   N                     #"
  echo "#                                                                 #"
  echo "#                  This is first start of MSYS2.                  #"
  echo "#       You MUST restart shell to apply necessary actions.        #"
  echo "#                                                                 #"
  echo "#                                                                 #"
  echo "###################################################################"
  echo
  echo
fi
unset MAYBE_FIRST_START
export PATH="/c/gcc-arm-none-eabi-10.3-2021.10/bin:$PATH"
export `armgcc`_DIR="/c/gcc-arm-none-eabi-10.3-2021.10"

保存更改过的文件后,使用source命令,激活对profile文件的更新,让更新的内容在MSYS2环境中生效。

MindMotion@DESKTOP-MCR7G5A MSYS ~
$ source /etc/profile

可查看PATH变量的最新值,若添加成功,则可观察到已包含arm-gcc-none-eabi的引用路径。

MindMotion@DESKTOP-MCR7G5A MSYS ~
$ echo $PATH
/c/gcc-arm-none-eabi-10.3-2021.10/bin:/usr/local/bin:/usr/bin:/bin:/opt/bin:/c/Windows/System32:/c/Windows:/c/Windows/System32/Wbem:/c/Windows/System32/WindowsPowerShell/v1.0/:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl

可试着查看arm-gcc-none-eabi工具的版本号,验证armgcc可在MSYS2中被正常调用。

MindMotion@DESKTOP-MCR7G5A MSYS ~
$ arm-none-eabi-gcc --version
arm-none-eabi-gcc.exe (GNU Arm Embedded Toolchain 10.3-2021.10) 10.3.1 20210824 (release)
Copyright (C) 2020 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

至此,基于Windows操作系统,在MSYS2环境中安装armgcc工具链完成。

编译MindSDK样例工程

用户可以在MindSDK的网站上(https://mindsdk.mindmotion.com.cn)下载适用于armgcc工具链创建的工程,即可在MSYS2环境中执行编译创建可执行文件。这里以plus-f5270开发板上的hello_world工程为例。

MindMotion@PF2LD92H MSYS /d/_git_repos/mm32-mcu-sdk/boards/plus-f5270/demo_apps/basic/hello_world/build/armgcc
$ ls
CMakeLists.txt  armgcc.cmake  build.sh  clean.sh

MindMotion@PF2LD92H MSYS /d/_git_repos/mm32-mcu-sdk/boards/plus-f5270/demo_apps/basic/hello_world/build/armgcc
$ ./build.sh
-- TOOLCHAIN_DIR: /usr/gcc-arm-none-eabi-10-2020-q4-major
-- BUILD_TYPE: debug
-- TOOLCHAIN_DIR: /usr/gcc-arm-none-eabi-10-2020-q4-major
-- BUILD_TYPE: debug
-- The C compiler identification is GNU 10.2.1
-- The CXX compiler identification is GNU 10.2.1
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/gcc-arm-none-eabi-10-2020-q4-major/bin/arm-none-eabi-gcc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/gcc-arm-none-eabi-10-2020-q4-major/bin/arm-none-eabi-g++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- The ASM compiler identification is GNU
-- Found assembler: /usr/gcc-arm-none-eabi-10-2020-q4-major/bin/arm-none-eabi-gcc
-- Configuring done
-- Generating done
-- Build files have been written to: /d/_git_repos/mm32-mcu-sdk/boards/plus-f5270/demo_apps/basic/hello_world/build/armgcc
Scanning dependencies of target my_project.elf
[  2%] Building C object CMakeFiles/my_project.elf.dir/d/_git_repos/mm32-mcu-sdk/applications/demo_apps/basic/hello_world/main.c.obj
[  5%] Building C object CMakeFiles/my_project.elf.dir/d/_git_repos/mm32-mcu-sdk/boards/plus-f5270/demo_apps/basic/hello_world/board_init.c.obj
[  8%] Building C object CMakeFiles/my_project.elf.dir/d/_git_repos/mm32-mcu-sdk/boards/plus-f5270/demo_apps/basic/hello_world/clock_init.c.obj
[ 11%] Building C object CMakeFiles/my_project.elf.dir/d/_git_repos/mm32-mcu-sdk/boards/plus-f5270/demo_apps/basic/hello_world/pin_init.c.obj
[ 14%] Building C object CMakeFiles/my_project.elf.dir/d/_git_repos/mm32-mcu-sdk/devices/MM32F5277E/drivers/hal_power.c.obj
[ 17%] Building C object CMakeFiles/my_project.elf.dir/d/_git_repos/mm32-mcu-sdk/devices/MM32F5277E/drivers/hal_pwr.c.obj
[ 20%] Building C object CMakeFiles/my_project.elf.dir/d/_git_repos/mm32-mcu-sdk/devices/MM32F5277E/drivers/hal_rcc.c.obj
[ 23%] Building C object CMakeFiles/my_project.elf.dir/d/_git_repos/mm32-mcu-sdk/devices/MM32F5277E/drivers/hal_syscfg.c.obj
[ 26%] Building C object CMakeFiles/my_project.elf.dir/d/_git_repos/mm32-mcu-sdk/drivers/adc_1/hal_adc.c.obj
[ 29%] Building C object CMakeFiles/my_project.elf.dir/d/_git_repos/mm32-mcu-sdk/drivers/comp_1/hal_comp.c.obj
[ 32%] Building C object CMakeFiles/my_project.elf.dir/d/_git_repos/mm32-mcu-sdk/drivers/cordic_0/hal_cordic.c.obj
[ 35%] Building C object CMakeFiles/my_project.elf.dir/d/_git_repos/mm32-mcu-sdk/drivers/crc_0/hal_crc.c.obj
[ 38%] Building C object CMakeFiles/my_project.elf.dir/d/_git_repos/mm32-mcu-sdk/drivers/dac_1/hal_dac.c.obj
[ 41%] Building C object CMakeFiles/my_project.elf.dir/d/_git_repos/mm32-mcu-sdk/drivers/dma_1/hal_dma.c.obj
[ 44%] Building C object CMakeFiles/my_project.elf.dir/d/_git_repos/mm32-mcu-sdk/drivers/exti_0/hal_exti.c.obj
[ 47%] Building C object CMakeFiles/my_project.elf.dir/d/_git_repos/mm32-mcu-sdk/drivers/flexcan_0/hal_flexcan.c.obj
[ 50%] Building C object CMakeFiles/my_project.elf.dir/d/_git_repos/mm32-mcu-sdk/drivers/fsmc_0/hal_fsmc.c.obj
[ 52%] Building C object CMakeFiles/my_project.elf.dir/d/_git_repos/mm32-mcu-sdk/drivers/gpio_0/hal_gpio.c.obj
[ 55%] Building C object CMakeFiles/my_project.elf.dir/d/_git_repos/mm32-mcu-sdk/drivers/i2c_0/hal_i2c.c.obj
[ 58%] Building C object CMakeFiles/my_project.elf.dir/d/_git_repos/mm32-mcu-sdk/drivers/i2s_0/hal_i2s.c.obj
[ 61%] Building C object CMakeFiles/my_project.elf.dir/d/_git_repos/mm32-mcu-sdk/drivers/iwdg_0/hal_iwdg.c.obj
[ 64%] Building C object CMakeFiles/my_project.elf.dir/d/_git_repos/mm32-mcu-sdk/drivers/lptim_0/hal_lptim.c.obj
[ 67%] Building C object CMakeFiles/my_project.elf.dir/d/_git_repos/mm32-mcu-sdk/drivers/lpuart_0/hal_lpuart.c.obj
[ 70%] Building C object CMakeFiles/my_project.elf.dir/d/_git_repos/mm32-mcu-sdk/drivers/mds_0/hal_mds.c.obj
[ 73%] Building C object CMakeFiles/my_project.elf.dir/d/_git_repos/mm32-mcu-sdk/drivers/rtc_0/hal_rtc.c.obj
[ 76%] Building C object CMakeFiles/my_project.elf.dir/d/_git_repos/mm32-mcu-sdk/drivers/qspi_0/hal_qspi.c.obj
[ 79%] Building C object CMakeFiles/my_project.elf.dir/d/_git_repos/mm32-mcu-sdk/drivers/spi_0/hal_spi.c.obj
[ 82%] Building C object CMakeFiles/my_project.elf.dir/d/_git_repos/mm32-mcu-sdk/drivers/tim_1/hal_tim.c.obj
[ 85%] Building C object CMakeFiles/my_project.elf.dir/d/_git_repos/mm32-mcu-sdk/drivers/uart_0/hal_uart.c.obj
[ 88%] Building C object CMakeFiles/my_project.elf.dir/d/_git_repos/mm32-mcu-sdk/drivers/usb_0/hal_usb.c.obj
[ 91%] Building C object CMakeFiles/my_project.elf.dir/d/_git_repos/mm32-mcu-sdk/drivers/wwdg_0/hal_wwdg.c.obj
[ 94%] Building C object CMakeFiles/my_project.elf.dir/d/_git_repos/mm32-mcu-sdk/devices/MM32F5277E/system_mm32f5277e.c.obj
[ 97%] Building ASM object CMakeFiles/my_project.elf.dir/d/_git_repos/mm32-mcu-sdk/devices/MM32F5277E/armgcc/startup_mm32f5277e.S.obj
[100%] Linking C executable debug/my_project.elf
[100%] Built target my_project.elf

编译成功,创建了my_project.elf文件。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值