CubeMX + VS Code配置環境代替Keil編寫燒錄除錯程式

“逛丟一隻鞋”發表於2020-10-06

VS Code 搭建stm32開發環境

搭建準備

程式安裝

1.下載並安裝 vs code

https://code.visualstudio.com/

2.下載並安裝 STM32CubeMX

https://www.st.com/content/st_com/zh/products/development-tools/software-development-tools/stm32-software-development-tools/stm32-configurators-and-code-generators/stm32cubemx.html

支援最新的HAL庫,工程程式碼配置與生成工具,支援生成IAR、Keil、STM32CubeIDE、Makefile等工程,這裡使用其生成的Makefile工程。

3.下載並安裝 Git for Windows

https://gitforwindows.org/

該工具整合有精簡版的mingw,這裡我們使用其bash終端和版本管理均非常有用。

4.下載並安裝 arm-none-eabi-gcc

https://launchpad.net/gcc-arm-embedded/+download

編譯器,GUN的arm的通用交叉編譯鏈工具,基本上常用的arm處理器均支援;

安裝時勾選設定全域性環境變數以便於配置;

使用離線免安裝包時,解壓到合適的位置,在系統環境變數新增\bin目錄,執行CMD或者Windows PowerShell,測試一下是否可用。命令:arm-none-eabi-gcc -v

5.下載並安裝 mingw

https://osdn.net/projects/mingw/downloads/68260/mingw-get-setup.exe/

MinGW 的全稱是:Minimalist GNU on Windows 。它實際上是將經典的開源 C語言 編譯器 GCC 移植到了 Windows 平臺下,並且包含了 Win32API 和 MSYS,因此可以將原始碼編譯生成 Windows 下的可執行程式,又能如同在 Linux 平臺下時,使用一些 Windows 不具備的開發工具。

一句話來概括:MinGW 就是 GCC 的 Windows 版本 。

其安裝一般為線上安裝,按網上步驟即可。

這裡我們主要需要使用其 mingw32-make 功能.

VS Code外掛搭建所需

安裝開發所需基礎外掛(外掛在 vs code 擴充欄搜尋名稱即可)

C/C++(必要)

增加了對C / C ++的語言支援,語法智慧感知、加亮及除錯功能

GBKtoUTF8

cortex-debug

工程示例

使用Cube-MX 生成Makefile工程

晶片選型、HAL版本、引腳配置、時鐘樹配置等,可參照下面部落格,這個是用Keil來進行編譯,所以只需要在最後一步修改成makefile就可以了

STM32CubeMX讓我們的藍色LED閃亮起來(胎教級包看包會不用懂原理)

工程管理中,選擇生成makefile工程,然後點選 generate code即可

在這裡插入圖片描述

vs code配置

工程 .vscode json配置(3個)

在這裡插入圖片描述

c_cpp_properties.json

主要有新增include路徑,編譯器路徑,巨集定義等,設定好後索引、編譯就跟keil一樣方便;

開啟工程 .vscode 下面的 c_cpp_properties.json 配置指令碼,這個json不允許有註釋,如果你自己編寫了標頭檔案又不在workspaceFolder下,路徑也要加到includePath和browse裡。設定如下:

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "C:/Program Files (x86)/GNU Arm Embedded Toolchain/9 2020-q2-update/arm-none-eabi/include",
                "${workspaceFolder}/Core/Inc",
                "${workspaceFolder}/LWIP/App",
                "${workspaceFolder}/LWIP/Target",
                "${workspaceFolder}/Middlewares/Third_Party/LwIP/src/include",
                "${workspaceFolder}/Middlewares/Third_Party/LwIP/system",
                "${workspaceFolder}/Drivers/STM32F4xx_HAL_Driver/Inc",
                "${workspaceFolder}/Drivers/STM32F4xx_HAL_Driver/Inc/Legacy",
                "${workspaceFolder}/Middlewares/Third_Party/FreeRTOS/Source/include",
                "${workspaceFolder}/Middlewares/Third_Party/FreeRTOS/Source/CMSIS_RTOS_V2",
                "${workspaceFolder}/Middlewares/Third_Party/FreeRTOS/Source/portable/GCC/ARM_CM4F",
                "${workspaceFolder}/Middlewares/Third_Party/LwIP/src/include/netif/ppp",
                "${workspaceFolder}/Drivers/CMSIS/Device/ST/STM32F4xx/Include",
                "${workspaceFolder}/Middlewares/Third_Party/LwIP/src/include/lwip",
                "${workspaceFolder}/Middlewares/Third_Party/LwIP/src/include/lwip/apps",
                "${workspaceFolder}/Middlewares/Third_Party/LwIP/src/include/lwip/priv",
                "${workspaceFolder}/Middlewares/Third_Party/LwIP/src/include/lwip/prot",
                "${workspaceFolder}/Middlewares/Third_Party/LwIP/src/include/netif",
                "${workspaceFolder}/Middlewares/Third_Party/LwIP/src/include/compat/posix",
                "${workspaceFolder}/Middlewares/Third_Party/LwIP/src/include/compat/posix/arpa",
                "${workspaceFolder}/Middlewares/Third_Party/LwIP/src/include/compat/posix/net",
                "${workspaceFolder}/Middlewares/Third_Party/LwIP/src/include/compat/posix/sys",
                "${workspaceFolder}/Middlewares/Third_Party/LwIP/src/include/compat/stdc",
                "${workspaceFolder}/Middlewares/Third_Party/LwIP/system/arch",
                "${workspaceFolder}/Drivers/CMSIS/Include"
            ],
            "defines": [
                "USE_HAL_DRIVER",
                "STM32F407xx",
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "intelliSenseMode": "gcc-x64"
        }
    ],
    "version": 4
}

launch.json

所需要除錯的檔案的路徑、除錯時的CWD(工作路徑)、偵錯程式的路徑及一些除錯引數(程式啟動引數等);

{
    // 使用 IntelliSense 瞭解相關屬性。 
    // 懸停以檢視現有屬性的描述。
    // 欲瞭解更多資訊,請訪問: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Cortex Debug",
            "cwd": "${workspaceRoot}",
            "executable": "./build/Control.elf",
            "request": "launch",
            "type": "cortex-debug",
            "servertype": "jlink",
            "device": "STM32F407ZG",
            "interface": "swd",
            "armToolchainPath": "C:/Program Files (x86)/GNU Arm Embedded Toolchain/9 2020-q2-update/bin/",
            "preLaunchTask": "build",
            "rtos": "FreeRTOS"
        }
    ]
}

tasks.json

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
      {
        "label": "build",
        "type": "shell",
        "command": "make -j8",
        "problemMatcher": [],
        "group": {
          "kind": "build",
          "isDefault": true
        }
      }
    ]
  }

工程下開啟終端,輸入 make

工程程式設計會生成 .bin 檔案,這個就是我們要燒錄的目標檔案。

相關文章