VS Code 搭建stm32開發環境

MCU免費開發環境

通常芯片廠家會提供各類開發IDE方案,一般其中就包括其自家的集成IDE,如:html

意法半導體 STM32CubeIDEpython

NXP Codewarriorc++

TI CCSgit

另外也能夠用eclipse、VS studio、VS code等搭建開發環境shell

VS Code 搭建stm32開發環境

1.搭建準備

程序安裝

1.下載並安裝 vs code編程

2.下載並安裝 STM32CubeMXjson

支持最新的HAL庫,工程代碼配置與生成工具,支持生成IAR、Keil、STM32CubeIDE、Makefile等工程,這裏使用其生成的Makefile工程。windows

3.下載並安裝 Git for Windowsbash

該工具集成有精簡版的mingw,這裏咱們使用其bash終端和版本管理均很是有用。eclipse

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

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

安裝時勾選設置全局環境變量以便於配置;

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

5.下載並安裝 mingw

MinGW 的全稱是:Minimalist GNU on Windows 。它其實是將經典的開源 C語言 編譯器 GCC 移植到了 Windows 平臺下,而且包含了 Win32API 和 MSYS,所以能夠將源代碼編譯生成 Windows 下的可執行程序,又能如同在 Linux 平臺下時,使用一些 Windows 不具有的開發工具。

一句話來歸納:MinGW 就是 GCC 的 Windows 版本 。

其安裝通常爲在線安裝,按網上步驟便可。

這裏咱們主要須要使用其 mingw32-make 功能.

  • 離線安裝

若是因爲環境不能在線安裝,可安裝其離線安裝包

MinGW-W64 GCC-8.1.0 x86_64-win32-seh

下載壓縮文件並解壓到合適的位置,在系統環境變量添加\bin目錄,運行CMD或者Windows PowerShell,測試一下是否可用。命令:gcc -v

同時爲方便使用,複製 mingw32-make.exe 一份爲 make.exe,這樣後面編譯程序使用 make 便可。

6.安裝mysy2

shell 命令行開發環境,可用於替代 git-bash、cmd、power shell,功能相對更完善。

安裝以後,在vscode中配置 settings.json--"terminal.integrated.shell.windows": "C:\msys64\msys2_shell.cmd", 詳見下節。

7.下載並安裝(可選) OpenOCD for Windows

8.Jlink、ST-Link驅動

9.STM32CubeProg 用於stm32下載程序

VS Code插件搭建所需

安裝開發所需基礎插件(插件在 vs code 拓展欄搜索名稱便可)

  • C/C++(必要)

    增長了對C / C ++的語言支持,語法智能感知、加亮及調試功能

  • GBKtoUTF8

  • cortex-debug

2.工程示例

2.1 使用Cube-MX 生成Makefile工程

1.芯片選型、HAL版本、引腳配置、時鐘樹配置等,可參照下面博客

http://www.javashuo.com/article/p-hxpqfthi-ea.html

2.工程管理中,選擇生成makefile工程,而後點擊 generate code便可

在這裏插入圖片描述

2.2 vs code配置

默認狀況下,工程下是不含.vscode的文件夾的,修改編輯用戶或工程settings.json文件時會自動建立;

user--settings.json文件參考修改以下(包含配置終端和一些格式等設置):

{
    "C_Cpp.updateChannel": "Insiders",
    "http.proxySupport": "off",
    "workbench.iconTheme": "vscode-icons", //取消左側自動聚焦
    "explorer.autoReveal": false,
    "[c]": {
        "editor.defaultFormatter": "ms-vscode.cpptools" //默認格式化工具
    },
    "[h]": {
        "editor.defaultFormatter": "ms-vscode.cpptools" //默認格式化工具
    },
    "editor.formatOnSave": true, //文件保存時自動格式化
    "editor.formatOnPaste": true, //代碼粘貼時自動格式化
    "editor.formatOnType": true, //自動格式化鍵入行
    // "terminal.integrated.shell.windows": "D:\\Program Files\\Git\\git-bash.exe",
    "terminal.integrated.shell.windows": "C:\\msys64\\msys2_shell.cmd",
    "terminal.integrated.shellArgs.windows": [
        "-defterm",
        "-mingw64",
        "-no-start",
        "-here",
        "-use-full-path"    //使用系統環境變量
    ],
    "terminal.external.windowsExec": "D:\\Program Files\\Git\\git-bash.exe"
}

或者在菜單中設置 File--Preferences--Settings--Features--Terminal

2.3 工程makefile

1.實際開發須要熟悉 makefile

工程後面添加的文件程序須要由makefile來組織編譯;

打開工程makefile,編譯工具指定,默認 PREFIX = arm-none-eabi- ,若是已設置環境變量則無需修改,不然需添加(實際絕對路徑路徑) GCC_PATH = D:\gcc-arm-none-eabi-5_4-2016q3-20160926-win32\bin

PREFIX = arm-none-eabi-
# The gcc compiler bin path can be either defined in make command via GCC_PATH variable (> make GCC_PATH=xxx)
# either it can be added to the PATH environment variable.
ifdef GCC_PATH
CC = $(GCC_PATH)/$(PREFIX)gcc
AS = $(GCC_PATH)/$(PREFIX)gcc -x assembler-with-cpp
CP = $(GCC_PATH)/$(PREFIX)objcopy
SZ = $(GCC_PATH)/$(PREFIX)size
else
CC = $(PREFIX)gcc
AS = $(PREFIX)gcc -x assembler-with-cpp
CP = $(PREFIX)objcopy
SZ = $(PREFIX)size
endif
HEX = $(CP) -O ihex
BIN = $(CP) -O binary -S

2.4 工程 .vscode json配置(4個)

1.c_cpp_properties.json

主要有添加include路徑,編譯器路徑,宏定義等,設置好後索引、編譯就跟keil同樣方便;

打開工程 .vscode 下面的 c_cpp_properties.json 配置腳本,這個json不容許有註釋,若是你本身編寫了頭文件又不在workspaceFolder下,路徑也要加到includePath和browse裏。設置以下:

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**",
                "${workspaceFolder}Drivers/STM32F4xx_HAL_Driver/Inc",
                "${workspaceFolder}Drivers/STM32F4xx_HAL_Driver/Inc/Legacy",
                "${workspaceFolder}Drivers/CMSIS/Device/ST/STM32F4xx/Include",
                "${workspaceFolder}Drivers/CMSIS/Include",
                "${workspaceFolder}Drivers/CMSIS/Include",
                "D:/gcc-arm-none-eabi-5_4-2016q3-20160926-win32/arm-none-eabi/include"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE",
                "USE_HAL_DRIVER",
                "STM32F407xx"
            ],
            "compilerPath": "D:\\gcc-arm-none-eabi-5_4-2016q3-20160926-win32\\bin\\arm-none-eabi-gcc.exe",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "gcc-x86"
        }
    ],
    "version": 4
}

2.launch.json

所須要調試的文件的路徑、調試時的CWD(工做路徑)、調試器的路徑及一些調試參數(程序啓動參數等);

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "cwd": "${workspaceRoot}",
            "executable": "./bin/HAL_Test.elf",
            "name": "stm32 Debug",
            "request": "launch",
            "type": "cortex-debug",
            "servertype": "stutil",
            "device": "STM32F407ZG",
            "preLaunchTask": "編譯並下載",
            "postDebugTask": "復位設備"
        }
    ]
}

3.tasks.json

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "編譯",
            "type": "shell",
            "command": "make -j6",
            "problemMatcher": [],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        },
        {
            "label": "編譯並下載",
            "type": "shell",
            "command": "make -j6 && make update",
            "problemMatcher": []
        },
        {
            "label": "從新編譯",
            "type": "shell",
            "command": "make clean && make -j6",
            "problemMatcher": []
        },
        {
            "label": "復位設備",
            "type": "shell",
            "command": "STM32_Programmer_CLI -c port=SWD -hardRst",
            "problemMatcher": []
        }
    ]
}

4.工程下打開終端,輸入 make

工程編程會生成 .bin 文件,這個就是咱們要燒錄的目標文件。

5.使用 Jlink 的 Jflash 工具燒錄

6.使用 JLink GDB server 調試,調試方法如同Linux下面的GDB,主要使用命令行

3. vs code 配置

1.取消文件自動定位到側邊欄

當我在右側點擊某個文件時,左側會自動定位到該文件所在位置,這點特別煩,尤爲在項目目錄很長的時候。

在用戶 settings.json 中修改

"explorer.autoReveal": false

2.設置默認終端

File--Preferences--Settings-- 中打開用戶 setting.json文件,修改以下:

"terminal.integrated.shell.windows": "D:\\Program Files\\Git\\bin\\bash.exe",
"terminal.external.windowsExec": "D:\\Program Files\\Git\\bin\\bash.exe",

2.使用插件推薦(根據需求選擇)

插件 功能
C/C++ C / C ++的語言支持,語法智能感知、加亮及調試功能,固然須要系統安裝arm-none-eabi-gcc編譯器
Include Autocomplete 頭文件自動匹配
Code Runner 代碼一鍵運行
Cortex Debug 提供jlink、stlink等調試接口功能
filesize 顯示文件大小
Python Python的語言支持,語法智能感知、加亮及調試功能,須要系統安裝python
Git History 查看版本歷史及比較
GitLens 代碼中顯示提交信息、日誌查看方便,同時提供操做圖標
GBKtoUTF8
ARM arm彙編語言支持
Bracket Pair Colorizer 彩虹花括號,程序邏輯範圍查看方便
DeviceTree 設備樹語法支持
vscode-icons 文件圖標,可快速查看文件類型
PlatformIO IDE
相關文章
相關標籤/搜索