C/C++教程

Windows10搭建STM32 GCC开源开发环境

本文主要是介绍Windows10搭建STM32 GCC开源开发环境,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

KeiluVision5 MDK快到期了吧(雾),心想算啥?网上再搜一搜相应的破解工具。但是始终不是长期解决方法。别以为用破解版没啥问题。但是某一天你收到来自Keil公司的到律师函,估计就噩耗了。估计会给你的公司或者学校带来不小的麻烦。使用开源才是至上哈。咱们来搭建一个不会受到律师函的免费开发环境。

必备工具

  • Git-2.33.0.2-64-bit.exe 最新的版本你可以到Git官网下载。因为简化的MINGW64。
  • xpack-arm-none-eabi-gcc-10.2.1 支持Cortex arm的gcc编译器。
  • make 因为Git中简化的MINGW64,不含有make工具。所以我们需要make工具解析Makefile文件,最终实现直接make一键编译。
  • stlink-utility 开源的ST-LINK下载与调试工具windows版本。如果你有翻墙工具,你可以直接登录https://github.com/stlink-org/stlink查看其具体实现。
  • xpack-openocd-0.11.0-1.zip 开源的GDB工具,支持很多调试工具(ST-LINK,JLINK,DAP-LINK等等)在线调试。
  • STM32F4-FreeRTOS 支持STM32F4XX的GCC源码。

1. 安装Git

首先根据安装教程Windows系统Git安装教程(详解Git安装过程),安装好Git。

在你的windows某个盘符下创建一个workspace的目录。点击右键 → \rightarrow → Git Bash Here进入到MINGW64命令行。
请添加图片描述
请添加图片描述

2.xpack-arm-none-eabi-gcc-10.2.1/stlink-utility/make/xpack-openocd-0.11.0-1

xpack-arm-none-eabi-gcc-10.2.1 下载

打开https://github.com/xpack-dev-tools/arm-none-eabi-gcc-xpack/releases/tag/v10.3.1-2.3/链接。下载适用于windows的版本。
请添加图片描述

stlink-utility 下载

打开https://github.com/stlink-org/stlink/releases/tag/v1.7.0链接。下载适用于windows的版本。
请添加图片描述

make 下载

打开https://cfhcable.dl.sourceforge.net/project/gnuwin32/make/3.81/make-3.81.exe链接。下载并安装。

请添加图片描述

xpack-openocd-0.11.0-1 下载

打开https://github.com/xpack-dev-tools/openocd-xpack/releases/tag/v0.11.0-3/链接。下载适用于windows的版本。
请添加图片描述
创建一个toolchain目录,把xpack-arm-none-eabi-gcc-10.2.1/stlink-utility/xpack-openocd-0.11.0-1拷贝到目录并解压。
请添加图片描述

3. STM32F4-FreeRTOS源码

在workspace目录中下载STM32F4-FreeRTOS源码。

克隆Gitee的STM32F4-FreeRTOS源码

#
Weston@HingeTech-SH109 MINGW64 /d/workspace
$ git clone https://gitee.com/msntec/stm32-f4-free-rtos.git
Cloning into 'stm32-f4-free-rtos'...
remote: Enumerating objects: 529, done.
remote: Counting objects: 100% (529/529), done.
remote: Compressing objects: 100% (265/265), done.
remote: Total 529 (delta 242), reused 519 (delta 237), pack-reused 0
Receiving objects: 100% (529/529), 1.61 MiB | 1.30 MiB/s, done.
Resolving deltas: 100% (242/242), done.

查看是否已经设置GCC STLINK OPENOCD工具环境变量

Weston@HingeTech-SH109 MINGW64 /d/workspace
$ cd stm32-f4-free-rtos/

Weston@HingeTech-SH109 MINGW64 /d/workspace/stm32-f4-free-rtos (master)
$ echo $PATH
/c/Users/Administrator/bin:/mingw64/bin:/usr/local/bin:/usr/bin:/bin:/mingw64/bin:/usr/bin:/c/Users/Administrator/bin:/c/ProgramData/Oracle/Java/javapath:/c/Windows/system32:/c/Windows:/c/Windows/System32/Wbem:/c/Windows/System32/WindowsPowerShell/v1.0:/c/Windows/System32/OpenSSH:/cmd:/c/HighTec/toolchains/tricore/v4.9.3.0-infineon-1.0/bin:/c/Users/Administrator/AppData/Local/Microsoft/WindowsApps:/c/Infineon/LauncherService:/d/toolchain/msys64/mingw32/gcc-arm-none-eabi-7-2017-q4-major-win32/bin:/usr/bin/vendor_perl:/usr/bin/core_perl

source GCC STLINK OPENOCD工具环境变量配置

Weston@HingeTech-SH109 MINGW64 /d/workspace/stm32-f4-free-rtos (master)
$ source env.config
There is no folder ""
Setting default toolchain path "../toolchain"

再次确认GCC STLINK OPENOCD工具环境变量配置

Weston@HingeTech-SH109 MINGW64 /d/workspace/stm32-f4-free-rtos (master)
$ echo $PATH
/c/Users/Administrator/bin:/mingw64/bin:/usr/local/bin:/usr/bin:/bin:/mingw64/bin:/usr/bin:/c/Users/Administrator/bin:/c/ProgramData/Oracle/Java/javapath:/c/Windows/system32:/c/Windows:/c/Windows/System32/Wbem:/c/Windows/System32/WindowsPowerShell/v1.0:/c/Windows/System32/OpenSSH:/cmd:/c/HighTec/toolchains/tricore/v4.9.3.0-infineon-1.0/bin:/c/Users/Administrator/AppData/Local/Microsoft/WindowsApps:/c/Infineon/LauncherService:/d/toolchain/msys64/mingw32/gcc-arm-none-eabi-7-2017-q4-major-win32/bin:/usr/bin/vendor_perl:/usr/bin/core_perl:/d/workspace/toolchain/stlink-1.7.0-x86_64-w64-mingw32/bin:/d/workspace/toolchain/xpack-arm-none-eabi-gcc-10.3.1-2.3/bin:/d/workspace/toolchain/xpack-openocd-0.11.0-3/bin

编译STM32F4XX FreeRTOS工程文件

Weston@HingeTech-SH109 MINGW64 /d/workspace/stm32-f4-free-rtos (master)
$make
mkdir build
arm-none-eabi-gcc -c -mcpu=cortex-m4 -mthumb -mfpu=fpv4-sp-d16 -mfloat-abi=hard -DSTM32F4XX -DSTM32F40_41xxx -DUSE_STDPERIPH_DRIVER -DHSE_VALUE=8000000 -D__FPU_PRESENT=1 -D__FPU_USED=1 -DARM_MATH_CM4  -I CORE -I FreeRTOS/include -I FreeRTOS/portable/GCC/ARM_CM4F -I hardware -I Libraries/CMSIS/Device/ST/STM32F4xx/Include -I Libraries/CMSIS/Include -I Libraries/STM32F4xx_StdPeriph_Driver/inc -I config  -Og -Wall -fdata-sections -ffunction-sections -g -gdwarf-2 -MMD -MP -MF"build/croutine.d" -MT"build/croutine.d" -Wa,-a,-ad,-alms=build/croutine.lst FreeRTOS/croutine.c -o build/croutine.o
......
......
......
arm-none-eabi-gcc build/croutine.o build/event_groups.o build/list.o build/port.o build/heap_4.o build/queue.o build/tasks.o build/timers.o build/stm32f4xx_it.o build/system_stm32f4xx.o build/misc.o build/stm32f4xx_adc.o build/stm32f4xx_can.o build/stm32f4xx_cec.o build/stm32f4xx_crc.o build/stm32f4xx_dac.o build/stm32f4xx_dbgmcu.o build/stm32f4xx_dcmi.o build/stm32f4xx_dma.o build/stm32f4xx_dma2d.o build/stm32f4xx_dsi.o build/stm32f4xx_exti.o build/stm32f4xx_flash.o build/stm32f4xx_flash_ramfunc.o build/stm32f4xx_fmpi2c.o build/stm32f4xx_fsmc.o build/stm32f4xx_gpio.o build/stm32f4xx_i2c.o build/stm32f4xx_iwdg.o build/stm32f4xx_lptim.o build/stm32f4xx_ltdc.o build/stm32f4xx_pwr.o build/stm32f4xx_qspi.o build/stm32f4xx_rcc.o build/stm32f4xx_rng.o build/stm32f4xx_rtc.o build/stm32f4xx_sai.o build/stm32f4xx_sdio.o build/stm32f4xx_spdifrx.o build/stm32f4xx_spi.o build/stm32f4xx_syscfg.o build/stm32f4xx_tim.o build/stm32f4xx_usart.o build/stm32f4xx_wwdg.o build/syscalls.o build/main.o build/startup_stm32f4xx.o -mcpu=cortex-m4 -mthumb -mfpu=fpv4-sp-d16 -mfloat-abi=hard -specs=nano.specs -TUtilities/stm32_flash.ld  -lc -lm -lnosys  -Wl,-Map=build/FreeRTOS.map,--cref -Wl,--gc-sections -o build/FreeRTOS.elf
arm-none-eabi-size build/FreeRTOS.elf
   text    data     bss     dec     hex filename
  12312    1868   79000   93180   16bfc build/FreeRTOS.elf
mkdir binary
cp build/FreeRTOS.elf binary/FreeRTOS.elf
arm-none-eabi-objcopy -O ihex build/FreeRTOS.elf binary/FreeRTOS.hex
arm-none-eabi-objcopy -O binary -S build/FreeRTOS.elf binary/FreeRTOS.bin

st-flash flash擦除

Weston@HingeTech-SH109 MINGW64 /d/workspace/stm32-f4-free-rtos (master)
$ make erase
st-flash erase
st-flash 1.7.0
2022-01-06T17:10:17 INFO common.c: F4xx: 192 KiB SRAM, 1024 KiB flash in at least 16 KiB pages.
Mass erasing.....

st-flash firmware烧写后复位

Weston@HingeTech-SH109 MINGW64 /d/workspace/stm32-f4-free-rtos (master)
$ make flash
st-flash --connect-under-reset write binary/FreeRTOS.bin 0x08000000
st-flash 1.7.0
2022-01-06T17:11:19 WARN common.c: NRST is not connected
2022-01-06T17:11:19 INFO common.c: F4xx: 192 KiB SRAM, 1024 KiB flash in at least 16 KiB pages.
file binary/FreeRTOS.bin md5 checksum: b03f8cbd7b4a854e473f33359dff2a, stlink checksum: 0x0013d60f
2022-01-06T17:11:19 INFO common.c: Attempting to write 14188 (0x376c) bytes to stm32 address: 134217728 (0x8000000)
EraseFlash - Sector:0x0 Size:0x4000 2022-01-06T17:11:19 INFO common.c: Flash page at addr: 0x08000000 erased
2022-01-06T17:11:19 INFO common.c: Finished erasing 1 pages of 16384 (0x4000) bytes
2022-01-06T17:11:19 INFO common.c: Starting Flash write for F2/F4/F7/L4
2022-01-06T17:11:19 INFO flash_loader.c: Successfully loaded flash loader in sram
2022-01-06T17:11:19 INFO flash_loader.c: Clear DFSR
2022-01-06T17:11:19 INFO common.c: enabling 32-bit flash writes
2022-01-06T17:11:19 INFO common.c: Starting verification of write complete
2022-01-06T17:11:20 INFO common.c: Flash written and verified! jolly good!

st-util 在线调试

Weston@HingeTech-SH109 MINGW64 /d/workspace/stm32-f4-free-rtos (master)
$ st-util.exe
st-util
2022-01-06T17:19:20 WARN common.c: NRST is not connected
2022-01-06T17:19:20 INFO common.c: F4xx: 192 KiB SRAM, 1024 KiB flash in at least 16 KiB pages.
2022-01-06T17:19:20 INFO gdb-server.c: Listening at *:4242...
2022-01-06T17:20:31 WARN common.c: NRST is not connected
2022-01-06T17:20:31 INFO common.c: F4xx: 192 KiB SRAM, 1024 KiB flash in at least 16 KiB pages.
2022-01-06T17:20:31 INFO gdb-server.c: Found 6 hw breakpoint registers
2022-01-06T17:20:31 INFO gdb-server.c: GDB connected.
2022-01-06T17:21:05 ERROR gdb-server.c: cannot recv: -2
2022-01-06T17:21:05 INFO gdb-server.c: Listening at *:4242...
2022-01-06T17:21:14 WARN common.c: NRST is not connected
2022-01-06T17:21:14 INFO common.c: F4xx: 192 KiB SRAM, 1024 KiB flash in at least 16 KiB pages.
2022-01-06T17:21:14 INFO gdb-server.c: Found 6 hw breakpoint registers
2022-01-06T17:21:14 INFO gdb-server.c: GDB connected.

arm-none-eabi-gdb.exe 加载调试ELF文件, target extended localhost:4242命令远程登录调试

Weston@HingeTech-SH109 MINGW64 /d/workspace/stm32-f4-free-rtos (master)
$ arm-none-eabi-gdb.exe binary/FreeRTOS.elf
GNU gdb (xPack GNU Arm Embedded GCC x86_64) 10.2.90.20210621-git
Copyright (C) 2021 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.
Type "show copying" and "show warranty" for details.
This GDB was configured as "--host=x86_64-w64-mingw32 --target=arm-none-eabi".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from binary/FreeRTOS.elf...
(gdb) target extended localhost:4242
Remote debugging using localhost:4242
Reset_Handler () at hardware/startup_stm32f4xx.s:94
94        movs  r3, #0
(gdb) b main
Breakpoint 1 at 0x8002408: file main.c, line 29.
Note: automatically using hardware breakpoints for read-only addresses.
(gdb) l
89        bcc  CopyDataInit
90        ldr  r2, =_sbss
91        b  LoopFillZerobss
92      /* Zero fill the bss segment. */
93      FillZerobss:
94        movs  r3, #0
95        str  r3, [r2], #4
96
97      LoopFillZerobss:
98        ldr  r3, = _ebss
(gdb) c
Continuing.

Breakpoint 1, main () at main.c:29
29      int main(void) {
(gdb) l
24              }
25      }
26
27
28
29      int main(void) {
30        GPIO_InitTypeDef GPIO_InitStruct;
31
32        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
33
(gdb)

GDB的命令大家可以看宋宝华的Linux gdb调试器用法全面解析和GDB GUI 工具Eclipse使用方法在如何结合Eclipse+OpenOCD+arm-none-eabi-gdb实现可视化在线调试ARM Cortex M7内核?文章中介绍。

由于大部分资源是gitub资源。国内访问github需要翻墙才能访问。大家可以直接加我的公众号“背包旅行码农”,输入关键字“STM32 GCC”获取。
请添加图片描述

这篇关于Windows10搭建STM32 GCC开源开发环境的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!