STM32-Dev-Mac

stm32开发环境搭建(mac)

环境工具

  • CLion
  • STM32CubeMX 记录见:mac系统安装STM32CubeMX
  • Segger 下载 Ozone - The J-Link Debugger
  • 安装编译器:arm-gcc-none-eabi-gcc brew cask install gcc-arm-embedded
  • 安装stlinkbrew install stlink
  • 安装openocd 执行brew install openocd

运行测试第一个跑马灯

环境准备

  • mac os系统

  • 开发板NUCLEO-F401RE 其中mcu型号stm32f401RETx

  • 安装项目初始化软件STM32CubeMX 步骤见mac系统安装STM32CubeMX

  • 安装编译器arm-gcc-none-eabi-gcc 执行brew cask install gcc-arm-embedded

  • 安装stlink下载器brew install stlink

  • 开发板NUCLEO-F401RE跳线如图

    62fMm8.png

项目搭建步骤:

  1. 初始化项目工程见STM32CubeMX使用之初始化项目

  2. 进入生成的工作目录执行make

  3. 提示错误/bin/sh: /arm-none-eabi-gcc: No such file or directory解决,修改Makefile

    1
    2
    # BINPATH是指arm-none-eabi-gcc的路径,可以通过执行which arm-none-eabi-gcc得到路径
    BINPATH = /usr/local/bin/
  4. 提示错误

    1
    2
    build/main.o: In function `main':
    /Users/xuanleung/IdeaProjects/f401demo/Src/main.c:75: multiple definition of `main'

    解决修改Makefile,删除里面重复的,#标注为有重复,删除即可

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    C_SOURCES =  \
    Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pwr_ex.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_ramfunc.c \
    #Src/stm32f4xx_it.c \
    Src/stm32f4xx_it.c \
    Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash_ex.c \
    Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dma.c \
    /Src/system_stm32f4xx.c \
    #Src/stm32f4xx_hal_msp.c \
    Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_tim.c \
    Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_uart.c \
    Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc_ex.c \
    Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash.c \
    Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pwr.c \
    Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc.c \
    #Src/main.c \
    Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_tim_ex.c \
    Src/stm32f4xx_hal_msp.c \
    Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_gpio.c \
    Src/main.c
  5. 再重新执行make,得到编译成功的文件了

    1
    2
    3
    4
    5
    /usr/local/bin//arm-none-eabi-size build/f401demo.elf
    text data bss dec hex filename
    5460 20 1636 7116 1bcc build/f401demo.elf
    /usr/local/bin//arm-none-eabi-objcopy -O ihex build/f401demo.elf build/f401demo.hex
    /usr/local/bin//arm-none-eabi-objcopy -O binary -S build/f401demo.elf build/f401demo.bin
  6. 下载bin文件到开发板NUCLEO-F401RE ,执行st-flash write ./build/xxxxx.bin 0x8000000,如果下载失败重启单片机和检查跳线帽

  7. 修改src下面的main.c添加跑马灯代码

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    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);
    }
  8. 重新编译make和下载st-flash write ./build/f401demo.bin 0x8000000

  9. 下载完成后ld2就会闪烁

使用CLion进行项目编译运行

操作记录:mac下stm32CubeMX+CLion+openocd开发环境

  1. 使用STM32CubeMX重新初始化项目,选择SW4STM32

问题

debug提示如下错误:

1
Cannot load symbol file: "/Users/xuanleung/IdeaProjects/f401re_demo/cmake-build-debug/f401re_demo.elf": not in executable format: File format not recognized

解决:取消勾选

62fsh9.png

参考

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

CLion for embedded development

CLion for Embedded Development Part II

http://blog.meekdai.com/MacOS-Eclipse-ARM-GCC-STM32.html

STM32 Nucle o-64 boards文档

Nucleo-64板载ST-LINK/ V2-1调试器 之对外界编程