macOS 下用 Clion和OpenOCD开发 STM32(st-link和STM32CubeMX)

macOS 开发 MCU,基本上就是 Ecllipse + GNU ARM GCC + OpenOCD这套(GNU MCU Eclipse)组合,虽然已经很不错了,但是用了 JetBrain 家的 IDE 后,发现 CLion 还是比 Eclipse 好用不少,,,
所以,如果你有STM32的评估板,然后加上CLion(及插件) + GNU ARM GCC + OpenOCD这套组合,至少在写代码层面上,是最爽的。
目前,还只支持st-link,相信后面JLink啥的也会很快跟上,到时候就可以扔掉 Keil了。
装完后,结合STM32CubeMX生成代码后,可以用 make 编译 和 st-flash 下载,也可以用 CLion(CMake) 编译和 OpenOCD下载,在线仿真。

  1. 安装编译器:arm-gcc-none-eabi-gcc
➜  ~ brew cask install gcc-arm-embedded

==> Satisfying dependencies
==> Downloading https://developer.arm.com/-/media/Files/downloads/gnu-rm/7-2017q4/gcc-arm-none-eabi-7-2017-q4-major-mac.tar.bz2
######################################################################## 100.0%
==> Verifying checksum for Cask gcc-arm-embedded
==> Installing Cask gcc-arm-embedded
==> Linking Binary 'arm-none-eabi-strip' to '/usr/local/bin/arm-none-eabi-strip'.
==> Linking Binary 'arm-none-eabi-ar' to '/usr/local/bin/arm-none-eabi-ar'.
==> Linking Binary 'arm-none-eabi-as' to '/usr/local/bin/arm-none-eabi-as'.
==> Linking Binary 'arm-none-eabi-c++' to '/usr/local/bin/arm-none-eabi-c++'.
==> Linking Binary 'arm-none-eabi-c++filt' to '/usr/local/bin/arm-none-eabi-c++filt'.
==> Linking Binary 'arm-none-eabi-cpp' to '/usr/local/bin/arm-none-eabi-cpp'.
==> Linking Binary 'arm-none-eabi-elfedit' to '/usr/local/bin/arm-none-eabi-elfedit'.
==> Linking Binary 'arm-none-eabi-g++' to '/usr/local/bin/arm-none-eabi-g++'.
==> Linking Binary 'arm-none-eabi-gcc' to '/usr/local/bin/arm-none-eabi-gcc'.
==> Linking Binary 'arm-none-eabi-gcc-ar' to '/usr/local/bin/arm-none-eabi-gcc-ar'.
==> Linking Binary 'arm-none-eabi-gcc-nm' to '/usr/local/bin/arm-none-eabi-gcc-nm'.
==> Linking Binary 'arm-none-eabi-gcc-ranlib' to '/usr/local/bin/arm-none-eabi-gcc-ranlib'.
==> Linking Binary 'arm-none-eabi-gcov' to '/usr/local/bin/arm-none-eabi-gcov'.
==> Linking Binary 'arm-none-eabi-gcov-tool' to '/usr/local/bin/arm-none-eabi-gcov-tool'.
==> Linking Binary 'arm-none-eabi-gdb' to '/usr/local/bin/arm-none-eabi-gdb'.
==> Linking Binary 'arm-none-eabi-gdb-py' to '/usr/local/bin/arm-none-eabi-gdb-py'.
==> Linking Binary 'arm-none-eabi-gprof' to '/usr/local/bin/arm-none-eabi-gprof'.
==> Linking Binary 'arm-none-eabi-ld' to '/usr/local/bin/arm-none-eabi-ld'.
==> Linking Binary 'arm-none-eabi-ld.bfd' to '/usr/local/bin/arm-none-eabi-ld.bfd'.
==> Linking Binary 'arm-none-eabi-nm' to '/usr/local/bin/arm-none-eabi-nm'.
==> Linking Binary 'arm-none-eabi-objcopy' to '/usr/local/bin/arm-none-eabi-objcopy'.
==> Linking Binary 'arm-none-eabi-objdump' to '/usr/local/bin/arm-none-eabi-objdump'.
==> Linking Binary 'arm-none-eabi-ranlib' to '/usr/local/bin/arm-none-eabi-ranlib'.
==> Linking Binary 'arm-none-eabi-readelf' to '/usr/local/bin/arm-none-eabi-readelf'.
==> Linking Binary 'arm-none-eabi-size' to '/usr/local/bin/arm-none-eabi-size'.
==> Linking Binary 'arm-none-eabi-strings' to '/usr/local/bin/arm-none-eabi-strings'.
==> Linking Binary 'arm-none-eabi-addr2line' to '/usr/local/bin/arm-none-eabi-addr2line'.
  gcc-arm-embedded was successfully installed!
➜  ~ arm-none-eabi-gcc --version
arm-none-eabi-gcc (GNU Tools for Arm Embedded Processors 7-2017-q4-major) 7.2.1 20170904 (release) [ARM/embedded-7-branch revision 255204]
Copyright (C) 2017 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.
➜  ~ which arm-none-eabi-gcc 
/usr/local/bin/arm-none-eabi-gcc

How I installed GCC ARM on my Mac 10.9 Mac Book Pro

  1. 安装下载调试器:st-link
➜  u-boot-2016.05 brew install stlink
Updating Homebrew...
==> Auto-updated Homebrew!
==> Downloading https://homebrew.bintray.com/bottles/stlink-1.4.0.el_capitan.bottle.tar.gz
######################################################################## 100.0%
==> Pouring stlink-1.4.0.el_capitan.bottle.tar.gz
  /usr/local/Cellar/stlink/1.4.0: 27 files, 702KB
➜  u-boot-2016.05 st-info --version
v1.4.0
➜  ~ st-info --flash
0x80000
➜  ~ st-info --version 
v1.4.0
➜  ~ st-info --flash  
➜  ~ st-info --chipid
0x0433
➜  ~ st-info --serial
303636454646343934393531373835
➜  ~ st-flash 
invalid command line
stlinkv1 command line: ./st-flash [--debug] [--reset] [--format <format>] [--flash=<fsize>] {read|write} /dev/sgX <path> <addr> <size>
stlinkv1 command line: ./st-flash [--debug] /dev/sgX erase
stlinkv2 command line: ./st-flash [--debug] [--reset] [--serial <serial>] [--format <format>] [--flash=<fsize>] {read|write} <path> <addr> <size>
stlinkv2 command line: ./st-flash [--debug] [--serial <serial>] erase
stlinkv2 command line: ./st-flash [--debug] [--serial <serial>] reset
                       Use hex format for addr, <serial> and <size>.
                       fsize: Use decimal, octal or hex by prefix 0xXXX for hex, optionally followed by k=KB, or m=MB (eg. --flash=128k)
                       Format may be 'binary' (default) or 'ihex', although <addr> must be specified for binary format only.
                       ./st-flash [--version]
➜  ~ st-flash erase 
st-flash 1.4.0
2017-12-31T17:30:22 INFO src/common.c: Loading device parameters....
2017-12-31T17:30:22 INFO src/common.c: Device connected is: F4 device (Dynamic Efficency), id 0x10006433
2017-12-31T17:30:22 INFO src/common.c: SRAM size: 0x18000 bytes (96 KiB), Flash: 0x80000 bytes (512 KiB) in pages of 16384 bytes
Mass erasing......
  1. 新建跑马灯例程:STM32CubeMX

3.1 下载安装 STM32CubeMX 后,New Project新建 NUCLEO-F401RE 工程,选择板子或者芯片型号:

选择板子或者芯片型号

3.2 Boards List中选中的板子,双击打开配置界面,根据 UM1724 文档 的 6.4 小节可以看到,LED2为 PA5。如果之前是选择的板子,那么Cube就已经帮你设定好了;如果选择的时芯片,那么自己标记一下就可以了。

LD2

3.2 保存配置,并生成工程和代码


选择 Makefile
工程文件结构

3.3 进入工程所在根目录,make工程,并修复 Cube 的自动生成的一些 Makefile 的错误。

错误一:没有指定 arm-none-eabi-gcc完整路径,提示找不到编译器

➜  0_blinking make
mkdir build
/arm-none-eabi-gcc -c -mcpu=cortex-m4 -mthumb -mfpu=fpv4-sp-d16 -mfloat-abi=hard -DUSE_HAL_DRIVER -DSTM32F401xE -IInc -IDrivers/STM32F4xx_HAL_Driver/Inc -IDrivers/STM32F4xx_HAL_Driver/Inc/Legacy -IDrivers/CMSIS/Device/ST/STM32F4xx/Include -IDrivers/CMSIS/Include -Og -Wall -fdata-sections -ffunction-sections -g -gdwarf-2 -MMD -MP -MF"build/stm32f4xx_hal_gpio.d" -MT"build/stm32f4xx_hal_gpio.d" -Wa,-a,-ad,-alms=build/stm32f4xx_hal_gpio.lst Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_gpio.c -o build/stm32f4xx_hal_gpio.o
/bin/sh: /arm-none-eabi-gcc: No such file or directory
make: *** [build/stm32f4xx_hal_gpio.o] Error 127

修复一:添加 BINPATH路径,可以用which arm-none-eabi-gcc 查看确认


#######################################
# binaries
#######################################
BINPATH = /usr/local/bin/
PREFIX = arm-none-eabi-
CC = $(BINPATH)/$(PREFIX)gcc
AS = $(BINPATH)/$(PREFIX)gcc -x assembler-with-cpp
CP = $(BINPATH)/$(PREFIX)objcopy
AR = $(BINPATH)/$(PREFIX)ar
SZ = $(BINPATH)/$(PREFIX)size
HEX = $(CP) -O ihex
BIN = $(CP) -O binary -S

错误二:多次引用源代码,导致链接时重复

➜  0_blinking make
/usr/local/bin//arm-none-eabi-gcc -c -mcpu=cortex-m4 -mthumb -mfpu=fpv4-sp-d16 -mfloat-abi=hard -DUSE_HAL_DRIVER -DSTM32F401xE -IInc -IDrivers/STM32F4xx_HAL_Driver/Inc -IDrivers/STM32F4xx_HAL_Driver/Inc/Legacy -IDrivers/CMSIS/Device/ST/STM32F4xx/Include -IDrivers/CMSIS/Include -Og -Wall -fdata-sections -ffunction-sections -g -gdwarf-2 -MMD -MP -MF"build/stm32f4xx_hal_gpio.d" -MT"build/stm32f4xx_hal_gpio.d" -Wa,-a,-ad,-alms=build/stm32f4xx_hal_gpio.lst Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_gpio.c -o build/stm32f4xx_hal_gpio.o
············
build/stm32f4xx_hal_msp.o: In function `HAL_MspInit':
/Users/linjinhui/workplace/stm32f401re/0_blinking/Src/stm32f4xx_hal_msp.c:50: multiple definition of `HAL_MspInit'
build/stm32f4xx_hal_msp.o:/Users/linjinhui/workplace/stm32f401re/0_blinking/Src/stm32f4xx_hal_msp.c:50: first defined here
build/main.o: In function `_Error_Handler':
/Users/linjinhui/workplace/stm32f401re/0_blinking/Src/main.c:222: multiple definition of `_Error_Handler'
build/main.o:/Users/linjinhui/workplace/stm32f401re/0_blinking/Src/main.c:222: first defined here
build/main.o: In function `SystemClock_Config':
/Users/linjinhui/workplace/stm32f401re/0_blinking/Src/main.c:113: multiple definition of `SystemClock_Config'
build/main.o:/Users/linjinhui/workplace/stm32f401re/0_blinking/Src/main.c:113: first defined here
build/main.o: In function `main':
/Users/linjinhui/workplace/stm32f401re/0_blinking/Src/main.c:68: multiple definition of `main'
build/main.o:/Users/linjinhui/workplace/stm32f401re/0_blinking/Src/main.c:68: first defined here
build/stm32f4xx_it.o: In function `SysTick_Handler':
/Users/linjinhui/workplace/stm32f401re/0_blinking/Src/stm32f4xx_it.c:52: multiple definition of `SysTick_Handler'
build/stm32f4xx_it.o:/Users/linjinhui/workplace/stm32f401re/0_blinking/Src/stm32f4xx_it.c:52: first defined here
collect2: error: ld returned 1 exit status
make: *** [build/0_blinking.elf] Error 1

修复二:去除C_SOURCES中重复的源文件(注:去除后面那个多出来的,具体是stm32f4xx_it.cstm32f4xx_hal_msp.cmain.c),修改完如下

######################################
# source
######################################
# C sources
C_SOURCES =  \
Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_gpio.c \
Src/stm32f4xx_it.c \
Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc_ex.c \
Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_tim.c \
Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc.c \
Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_cortex.c \
Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal.c \
Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dma_ex.c \
Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash.c \
Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_tim_ex.c \
Src/stm32f4xx_hal_msp.c \
Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dma.c \
Src/main.c \
Src/system_stm32f4xx.c \
Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash_ramfunc.c \
Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pwr.c \
Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pwr_ex.c \
Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash_ex.c

再次make,成功在build子目录下编译出文件显示如下:

➜  0_blinking make
/usr/local/bin//arm-none-eabi-gcc -c -mcpu=cortex-m4 -mthumb -mfpu=fpv4-sp-d16 -mfloat-abi=hard -DUSE_HAL_DRIVER -DSTM32F401xE -IInc -IDrivers/STM32F4xx_HAL_Driver/Inc -IDrivers/STM32F4xx_HAL_Driver/Inc/Legacy -IDrivers/CMSIS/Device/ST/STM32F4xx/Include -IDrivers/CMSIS/Include -Og -Wall -fdata-sections -ffunction-sections -g -gdwarf-2 -MMD -MP -MF"build/stm32f4xx_hal_gpio.d" -MT"build/stm32f4xx_hal_gpio.d" -Wa,-a,-ad,-alms=build/stm32f4xx_hal_gpio.lst Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_gpio.c -o build/stm32f4xx_hal_gpio.o
.............
/usr/local/bin//arm-none-eabi-size build/0_blinking.elf
   text    data     bss     dec     hex filename
   4280      12    1572    5864    16e8 build/0_blinking.elf
/usr/local/bin//arm-none-eabi-objcopy -O ihex build/0_blinking.elf build/0_blinking.hex
/usr/local/bin//arm-none-eabi-objcopy -O binary -S build/0_blinking.elf build/0_blinking.bin

st-flash下载跑马灯:

➜  0_blinking st-flash write ./build/0_blinking.bin 0x8000000
st-flash 1.4.0
2018-01-07T22:49:52 INFO src/common.c: Loading device parameters....
2018-01-07T22:49:52 INFO src/common.c: Device connected is: F4 device (Dynamic Efficency), id 0x10006433
2018-01-07T22:49:52 INFO src/common.c: SRAM size: 0x18000 bytes (96 KiB), Flash: 0x80000 bytes (512 KiB) in pages of 16384 bytes
2018-01-07T22:49:52 INFO src/common.c: Attempting to write 4204 (0x106c) bytes to stm32 address: 134217728 (0x8000000)
Flash page at addr: 0x08000000 erased
2018-01-07T22:49:52 INFO src/common.c: Finished erasing 1 pages of 16384 (0x4000) bytes
2018-01-07T22:49:52 INFO src/common.c: Starting Flash write for F2/F4/L4
2018-01-07T22:49:52 INFO src/flash_loader.c: Successfully loaded flash loader in sram
enabling 32-bit flash writes
size: 4204
2018-01-07T22:49:52 INFO src/common.c: Starting verification of write complete
2018-01-07T22:49:52 INFO src/common.c: Flash written and verified! jolly good!

哦,没反应,忘了翻转了,还有,另外不要忘了跳冒要接好,,,
修改main函数中while(1),翻转起来:

    /* Infinite loop */
    /* USER CODE BEGIN WHILE */
    while (1)
    {
        /* USER CODE END WHILE */

        /* USER CODE BEGIN 3 */
        HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_RESET);
        HAL_Delay(500);
        HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_SET);
        HAL_Delay(500);

    }

再次编译和下载,闪起来了,,,

  1. 在线仿真调试 IDE:CLion+OpenOCD

5.1 安装 OpenOCD:brew install openocd

➜  ~ brew install openocd --enable_ft2232_libftdi --enable_stlink
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 4 taps (caskroom/cask, caskroom/versions, homebrew/core, homebrew/science).
==> New Formulae
opencascade
Warning: open-ocd: this formula has no --enable_ft2232_libftdi option so it will be ignored!
Warning: open-ocd: this formula has no --enable_stlink option so it will be ignored!
==> Downloading https://homebrew.bintray.com/bottles/open-ocd-0.10.0.el_capitan.bottle.1.tar.gz
######################################################################## 100.0%
==> Pouring open-ocd-0.10.0.el_capitan.bottle.1.tar.gz
  /usr/local/Cellar/open-ocd/0.10.0: 632 files, 4.7MB
➜  ~ which openocd  
/usr/local/bin/openocd
➜  ~ openocd        
Open On-Chip Debugger 0.10.0
Licensed under GNU GPL v2
For bug reports, read
    http://openocd.org/doc/doxygen/bugs.html
embedded:startup.tcl:60: Error: Can't find openocd.cfg
in procedure 'script' 
at file "embedded:startup.tcl", line 60
Error: Debug Adapter has to be specified, see "interface" command
embedded:startup.tcl:60: Error: 
in procedure 'script' 
at file "embedded:startup.tcl", line 60

5.2 CLion 及其插件

如果要Debug,则需要重新用Cube生成SW4STM32,如下:

SW4STM32

安装 CLion 及其插件clion-embedded-arm后,导入刚才 Cube 生成的工程:

导入工程

勾选文件

CLion 会自动生成 CMakeLists.txt

image.png

用插件生成CMakeLists.txt来替换原来的CMakeLists.txt

image.png

image.png

此时,选择Run->Build显示编译成功如下:

image.png

配置 OpenOCD 如下:


image.png

注:

  1. OpenOCD目前一定要符合完整结构,见 Issue 那么之前安装的不就没啥用了,汗,不过作者也在重新考虑是否需要完整的目录结构,因为好多人碰到这个问题了。
  2. 去掉自带的 GDB,否则提示错误格式,见Issue
  3. macOS和Linux下,Toolchains选项和CMake选项默认不用修改,Windows需要安装 MinGW 或者 Cygwin。

然后,就可以愉快的 Debug 在线仿真了,,,


Debug

参考链接

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 211,884评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,347评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,435评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,509评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,611评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,837评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,987评论 3 408
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,730评论 0 267
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,194评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,525评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,664评论 1 340
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,334评论 4 330
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,944评论 3 313
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,764评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,997评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,389评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,554评论 2 349

推荐阅读更多精彩内容