主页 > 互联网 > 内容页
为MindSDK搭建armgcc编译环境

灵动MM32MCU   2023-04-13 15:10:07

引言


(相关资料图)

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

本文讲述基于Windows操作系统,搭建 armgcc 编译环境,并支持编译MindSDK样例工程。

安装 MSYS2 环境

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

在 MSYS2 的官网上,可下载当前最新的安装包。如图1所示。

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

如图1所示,下载 msys-x86_64-20220904.exe 安装文件后,即可启动安装过程。安装完成后,启动 MSYS2 程序,可以看到MSYS2的命令行交互终端界面,如图2所示。

图2 运行MSYS的终端界面

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

MindMotion@DESKTOP-MCR7G5AMSYS~$lsMindMotion@DESKTOP-MCR7G5AMSYS~$pwd/home/MindMotion

在 MSYS2 环境中安装工具

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

在 MSYS2 环境中安装 make 工具

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

MindMotion@DESKTOP-MCR7G5AMSYS~$pacman-Ssmake...msys/make4.3-3(base-devel)GNUmakeutilitytomaintaingroupsofprograms...

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

MindMotion@DESKTOP-MCR7G5AMSYS~$pacman-Smsys/makeresolvingdependencies...lookingforconflictingpackages...Packages(1)make-4.3-3TotalDownloadSize:0.46MiBTotalInstalledSize:1.48MiB::Proceedwithinstallation?[Y/n]Y::Retrievingpackages...make-4.3-3-x86_64466.2KiB538KiB/s00:01[###############################]100%(1/1)checkingkeysinkeyring[###############################]100%(1/1)checkingpackageintegrity[###############################]100%(1/1)loadingpackagefiles[###############################]100%(1/1)checkingforfileconflicts[###############################]100%(1/1)checkingavailablediskspace[###############################]100%::Processingpackagechanges...(1/1)installingmake[###############################]100%::Runningpost-transactionhooks...(1/1)Updatingtheinfodirectoryfile...

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

MindMotion@DESKTOP-MCR7G5AMSYS~$make--versionGNUMake4.3Builtforx86_64-pc-msysCopyright(C)1988-2020FreeSoftwareFoundation,Inc.LicenseGPLv3+:GNUGPLversion3orlaterThisisfreesoftware:youarefreetochangeandredistributeit.ThereisNOWARRANTY,totheextentpermittedbylaw.

在 MSYS2 中安装 CMake 工具

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

MindMotion@DESKTOP-MCR7G5AMSYS~$pacman-Sscmake...msys/cmake3.21.0-1Across-platformopen-sourcemakesystem...

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

MindMotion@DESKTOP-MCR7G5AMSYS~$pacman-Smsys/cmakeresolvingdependencies...lookingforconflictingpackages...Packages(6)jsoncpp-1.9.5-1libarchive-3.6.1-2librhash-1.4.2-1libuv-1.42.0-1pkgconf-1.8.0-1cmake-3.23.2-1TotalDownloadSize:7.80MiBTotalInstalledSize:41.22MiB::Proceedwithinstallation?[Y/n]Y::Retrievingpackages...jsoncpp-1.9.5-1-any101.2KiB116KiB/s00:01[###############################]100%libuv-1.42.0-1-x86_64113.1KiB79.1KiB/s00:01[###############################]100%pkgconf-1.8.0-1-x86_6458.4KiB83.0KiB/s00:01[###############################]100%librhash-1.4.2-1-x86_64116.5KiB47.8KiB/s00:02[###############################]100%libarchive-3.6.1-2-x86_641138.4KiB353KiB/s00:03[###############################]100%cmake-3.23.2-1-x86_646.3MiB1738KiB/s00:04[###############################]100%Total(6/6)7.8MiB1965KiB/s00:04[###############################]100%(6/6)checkingkeysinkeyring[###############################]100%(6/6)checkingpackageintegrity[###############################]100%(6/6)loadingpackagefiles[###############################]100%(6/6)checkingforfileconflicts[###############################]100%(6/6)checkingavailablediskspace[###############################]100%::Processingpackagechanges...(1/6)installingjsoncpp[###############################]100%JsonCppheadersandbuildlibrariesarenowinjsoncpp-devel.(2/6)installinglibarchive[###############################]100%(3/6)installinglibrhash[###############################]100%(4/6)installinglibuv[###############################]100%(5/6)installingpkgconf[###############################]100%(6/6)installingcmake[###############################]100%

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

MindMotion@DESKTOP-MCR7G5AMSYS~$cmake--versioncmakeversion3.23.2CMakesuitemaintainedandsupportedbyKitware(kitware.com/cmake).

在 MSYS2 环境中安装 armgcc 工具集

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

MindMotion@DESKTOP-MCR7G5AMSYS~$pacman-Ssarm-none-eabi-gccmingw32/mingw-w64-i686-arm-none-eabi-gcc10.1.0-2(mingw-w64-i686-arm-none-eabi-toolchain)GNUToolsforARMEmbeddedProcessors-GCC(mingw-w64)mingw64/mingw-w64-x86_64-arm-none-eabi-gcc10.1.0-2(mingw-w64-x86_64-arm-none-eabi-toolchain)GNUToolsforARMEmbeddedProcessors-GCC(mingw-w64)

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

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

在Arm的官网上,可下载Windows操作系统版本对应的armgcc编译器工具包。如图4所示。

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

如图4所示页面中,下载 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"#CAUTION#"echo"##"echo"#ThisisfirststartofMSYS2.#"echo"#YouMUSTrestartshelltoapplynecessaryactions.#"echo"##"echo"##"echo"###################################################################"echoechofiunsetMAYBE_FIRST_STARTexportPATH="/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-MCR7G5AMSYS~$source/etc/profile

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

MindMotion@DESKTOP-MCR7G5AMSYS~$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-MCR7G5AMSYS~$arm-none-eabi-gcc--versionarm-none-eabi-gcc.exe(GNUArmEmbeddedToolchain10.3-2021.10)10.3.120210824(release)Copyright(C)2020FreeSoftwareFoundation,Inc.Thisisfreesoftware;seethesourceforcopyingconditions.ThereisNOwarranty;notevenforMERCHANTABILITYorFITNESSFORAPARTICULARPURPOSE.

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

编译MindSDK样例工程

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

MindMotion@PF2LD92HMSYS/d/_git_repos/mm32-mcu-sdk/boards/plus-f5270/demo_apps/basic/hello_world/build/armgcc$lsCMakeLists.txtarmgcc.cmakebuild.shclean.shMindMotion@PF2LD92HMSYS/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--TheCcompileridentificationisGNU10.2.1--TheCXXcompileridentificationisGNU10.2.1--DetectingCcompilerABIinfo--DetectingCcompilerABIinfo-done--CheckforworkingCcompiler:/usr/gcc-arm-none-eabi-10-2020-q4-major/bin/arm-none-eabi-gcc-skipped--DetectingCcompilefeatures--DetectingCcompilefeatures-done--DetectingCXXcompilerABIinfo--DetectingCXXcompilerABIinfo-done--CheckforworkingCXXcompiler:/usr/gcc-arm-none-eabi-10-2020-q4-major/bin/arm-none-eabi-g++-skipped--DetectingCXXcompilefeatures--DetectingCXXcompilefeatures-done--TheASMcompileridentificationisGNU--Foundassembler:/usr/gcc-arm-none-eabi-10-2020-q4-major/bin/arm-none-eabi-gcc--Configuringdone--Generatingdone--Buildfileshavebeenwrittento:/d/_git_repos/mm32-mcu-sdk/boards/plus-f5270/demo_apps/basic/hello_world/build/armgccScanningdependenciesoftargetmy_project.elf[2%]BuildingCobjectCMakeFiles/my_project.elf.dir/d/_git_repos/mm32-mcu-sdk/applications/demo_apps/basic/hello_world/main.c.obj[5%]BuildingCobjectCMakeFiles/my_project.elf.dir/d/_git_repos/mm32-mcu-sdk/boards/plus-f5270/demo_apps/basic/hello_world/board_init.c.obj[8%]BuildingCobjectCMakeFiles/my_project.elf.dir/d/_git_repos/mm32-mcu-sdk/boards/plus-f5270/demo_apps/basic/hello_world/clock_init.c.obj[11%]BuildingCobjectCMakeFiles/my_project.elf.dir/d/_git_repos/mm32-mcu-sdk/boards/plus-f5270/demo_apps/basic/hello_world/pin_init.c.obj[14%]BuildingCobjectCMakeFiles/my_project.elf.dir/d/_git_repos/mm32-mcu-sdk/devices/MM32F5277E/drivers/hal_power.c.obj[17%]BuildingCobjectCMakeFiles/my_project.elf.dir/d/_git_repos/mm32-mcu-sdk/devices/MM32F5277E/drivers/hal_pwr.c.obj[20%]BuildingCobjectCMakeFiles/my_project.elf.dir/d/_git_repos/mm32-mcu-sdk/devices/MM32F5277E/drivers/hal_rcc.c.obj[23%]BuildingCobjectCMakeFiles/my_project.elf.dir/d/_git_repos/mm32-mcu-sdk/devices/MM32F5277E/drivers/hal_syscfg.c.obj[26%]BuildingCobjectCMakeFiles/my_project.elf.dir/d/_git_repos/mm32-mcu-sdk/drivers/adc_1/hal_adc.c.obj[29%]BuildingCobjectCMakeFiles/my_project.elf.dir/d/_git_repos/mm32-mcu-sdk/drivers/comp_1/hal_comp.c.obj[32%]BuildingCobjectCMakeFiles/my_project.elf.dir/d/_git_repos/mm32-mcu-sdk/drivers/cordic_0/hal_cordic.c.obj[35%]BuildingCobjectCMakeFiles/my_project.elf.dir/d/_git_repos/mm32-mcu-sdk/drivers/crc_0/hal_crc.c.obj[38%]BuildingCobjectCMakeFiles/my_project.elf.dir/d/_git_repos/mm32-mcu-sdk/drivers/dac_1/hal_dac.c.obj[41%]BuildingCobjectCMakeFiles/my_project.elf.dir/d/_git_repos/mm32-mcu-sdk/drivers/dma_1/hal_dma.c.obj[44%]BuildingCobjectCMakeFiles/my_project.elf.dir/d/_git_repos/mm32-mcu-sdk/drivers/exti_0/hal_exti.c.obj[47%]BuildingCobjectCMakeFiles/my_project.elf.dir/d/_git_repos/mm32-mcu-sdk/drivers/flexcan_0/hal_flexcan.c.obj[50%]BuildingCobjectCMakeFiles/my_project.elf.dir/d/_git_repos/mm32-mcu-sdk/drivers/fsmc_0/hal_fsmc.c.obj[52%]BuildingCobjectCMakeFiles/my_project.elf.dir/d/_git_repos/mm32-mcu-sdk/drivers/gpio_0/hal_gpio.c.obj[55%]BuildingCobjectCMakeFiles/my_project.elf.dir/d/_git_repos/mm32-mcu-sdk/drivers/i2c_0/hal_i2c.c.obj[58%]BuildingCobjectCMakeFiles/my_project.elf.dir/d/_git_repos/mm32-mcu-sdk/drivers/i2s_0/hal_i2s.c.obj[61%]BuildingCobjectCMakeFiles/my_project.elf.dir/d/_git_repos/mm32-mcu-sdk/drivers/iwdg_0/hal_iwdg.c.obj[64%]BuildingCobjectCMakeFiles/my_project.elf.dir/d/_git_repos/mm32-mcu-sdk/drivers/lptim_0/hal_lptim.c.obj[67%]BuildingCobjectCMakeFiles/my_project.elf.dir/d/_git_repos/mm32-mcu-sdk/drivers/lpuart_0/hal_lpuart.c.obj[70%]BuildingCobjectCMakeFiles/my_project.elf.dir/d/_git_repos/mm32-mcu-sdk/drivers/mds_0/hal_mds.c.obj[73%]BuildingCobjectCMakeFiles/my_project.elf.dir/d/_git_repos/mm32-mcu-sdk/drivers/rtc_0/hal_rtc.c.obj[76%]BuildingCobjectCMakeFiles/my_project.elf.dir/d/_git_repos/mm32-mcu-sdk/drivers/qspi_0/hal_qspi.c.obj[79%]BuildingCobjectCMakeFiles/my_project.elf.dir/d/_git_repos/mm32-mcu-sdk/drivers/spi_0/hal_spi.c.obj[82%]BuildingCobjectCMakeFiles/my_project.elf.dir/d/_git_repos/mm32-mcu-sdk/drivers/tim_1/hal_tim.c.obj[85%]BuildingCobjectCMakeFiles/my_project.elf.dir/d/_git_repos/mm32-mcu-sdk/drivers/uart_0/hal_uart.c.obj[88%]BuildingCobjectCMakeFiles/my_project.elf.dir/d/_git_repos/mm32-mcu-sdk/drivers/usb_0/hal_usb.c.obj[91%]BuildingCobjectCMakeFiles/my_project.elf.dir/d/_git_repos/mm32-mcu-sdk/drivers/wwdg_0/hal_wwdg.c.obj[94%]BuildingCobjectCMakeFiles/my_project.elf.dir/d/_git_repos/mm32-mcu-sdk/devices/MM32F5277E/system_mm32f5277e.c.obj[97%]BuildingASMobjectCMakeFiles/my_project.elf.dir/d/_git_repos/mm32-mcu-sdk/devices/MM32F5277E/armgcc/startup_mm32f5277e.S.obj[100%]LinkingCexecutabledebug/my_project.elf[100%]Builttargetmy_project.elf

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

审核编辑:刘清