[STM32] 在 Windows 下使用 VSCode 架設 STM32 開發環境

1. 所需安裝的軟體

  • GNU Arm Embedded Toolchain : 下載壓縮檔,解壓到你想擺放的目錄。例如:c:\stm32\gcc-arm\
  • GNU MCU Eclipse Windows Build Tools : 這是 make 和相關的軟體,也可以使用 cygwin 或 mingw 內的工具,但我發現這包最簡潔。下載壓縮檔後將其解開,把裡面 bin 目錄下的執行檔複製到你想擺放的目錄。例如:c:\stm32\build tools\
  • OpenOCD : 這是燒 code 和 debug 的工具。下載壓縮檔,解壓到你想擺放的目錄。例如:c:\stm32\openocd\
  • VSCode : 這次的主角,編輯器和整合的操作介面。下載安裝程式執行,依指示安裝。安裝過程中有選項可將開啟檔案和開啟目錄加入滑鼠右鍵選單,建議打勾,以後開啟專案比較方便。
  • ST-Link Utility : STM32 原廠開發版的驅動程式和燒錄工具
  • STM32CubeMX (非必備): ST 提供的程式碼生成工具,利用它產生 makfile 的專案比較快

2. VSCode 的設定

由於 VSCode 的 Build 和 Debug 設定是存放在專案目錄下 .vscode/ 裡的 tasks.json 和 launch.json ,以下的操作必需是以 Folder 的方式開啟專案目錄才能用。你可以在檔案總管中,在專案的目錄上按右鍵,選"Open with Code"(安裝時必需有打開滑鼠右鍵選單功能)。或是在 VSCode 內用 "Open Folder" 打專案的目錄。

2.1 安裝 Extensions

  • 第一次使用 VSCode 先從左側打開 Extensions 的側欄,安裝下列 Extensions。
    • C/C++ (Microsoft)
    • Cortex-Debug
  • 打開 settings,設定 cortex-debug 需要的路徑
        "cortex-debug.openocdPath": "c:/stm32/openocd/bin/openocd.exe",
        "cortex-debug.armToolchainPath": "c:/stm32/gcc-arm/bin/",
    

2.2 Build 設定

VSCode 本身並不具備編譯功能,Build 其實是透過 shell 呼叫外部的程式去執行,所以這裡其實是在設定外部程式的路徑和執行時的參數。
  • 從上方選單 Terminal -> Configure Default Build Task... 進入編輯 tasks.json 的視窗。第一次建立可以選 "Create tasks.json from template" 再選 "Others" 建立一個新的。
  • 首先要先設定的是執行外部程式時的環境變數,在 options 的 env 裡增加一個 path ,把前面下載的工具軟體的路徑都加進來。網路上很多教學會教你直接加到系統的環境變數內,但我比較喜歡把環境變數設在專案內。因為每個專案的開發環境不一定相同,全部都設在系統內會造成衝突。
  • 接著修改預設的第一個 task ,幾個重要的欄位如下:
    • label: 顯示的名稱
    • type: task 的類別,保持為 shell 即可
    • command: 執行的命令,就是 "make"
    • group: 這裡要設 {"kind": "build", "isDefault": true} ,讓快捷鍵 ctrl + shift + B 會執行這個 task
    • problemMatcher: 輸出訊息的分析器,因為這裡是用 gcc 在編譯,所以用 ["$gcc"] 。這樣編譯出錯時,可以直接點錯誤訊息跳到錯誤的程式碼
  • 如果想新增 task ,可以將建好 task 複製一份再進行修改。不過記得 "isDefault" 只能有一個 task
  • 下面是一個設定的例子:
    {
        // See https://go.microsoft.com/fwlink/?LinkId=733558
        // for the documentation about the tasks.json format
        "version": "2.0.0",
        "options": {
            "env": {
                "path": "c:/stm32/gcc-arm/bin/;c:/stm32/build tools/;c:/stm32/openocd/bin/;"
            }
        },
        "tasks": [
            {
                "label": "make",
                "type": "shell",
                "command": "make -j",
                "group": {
                    "kind": "build",
                    "isDefault": true
                },
                "presentation": {
                    "focus": true
                },
                "problemMatcher": [
                    "$gcc"
                ]
            },
            {
                "label": "clean",
                "type": "shell",
                "command": "make clean",
                "group": "build",
                "presentation": {
                    "focus": true
                },
                "problemMatcher": [
                    "$gcc"
                ]
            },
            {
                "label": "flash",
                "type": "shell",
                "command": "openocd -f board\\stm32f7discovery.cfg -c \"init\" -c \"reset halt\" -c \"flash write_image erase bin/stm32f746.elf\" -c \"reset run\" -c \"shutdown\"",
                "group": "build",
                "presentation": {
                    "focus": true
                },
                "problemMatcher": []
            }
        ]
    }
    

2.3 Debug 設定

  • 從上方選單 Debug -> Open Configurations 打開 launch.json
  • 用右下方 Add Configuration... 新增一個 Cortex-Debug: openocd
  • 設定工作目錄的路徑,.elf 檔、.svd 檔和 openocd 的 .cfg 檔的位置。.svd 檔是非必要的,如果有的話可以顯示 register 的名稱。檔案可以根據你的 chip 到 stm32 原廠網頁下載
  • 下面是一個設定的例子:
    {
        // 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": [
            {
                "name": "Debug (OpenOCD)",
                "type": "cortex-debug",
                "request": "launch",
                "servertype": "openocd",
                "cwd": "${workspaceRoot}",
                "executable": "${workspaceRoot}/bin/stm32f746.elf",
                "device": "STM32F746ZG",
                "svdFile": "STM32F7x6.svd",
                "configFiles": [
                    "stm32f7discovery.cfg"
                ]
            }
        ]
    }
    
  • 設定完成後就能用 F5 開始進行 debug
  • 完整的使用方式可參考官方說明 Cortex-Debug Launch Configurations

3. openocd 支援 FreeRTOS 的設定

如果你有使用 FreeRTOS 想在 Debug 時讓 gdb 看到 multithread ,就必需開啟 openocd rtos 的設定。
  • 要讓 openocd 支援 FreeRTOS,需要增加一個全域變數。這個變數在舊版的 FreeRTOS 是有的,但新版拿掉了,所以必需手動補上去。在 main.c 或任意 .c 檔中增加下面這行。
        const int __attribute__((used)) uxTopUsedPriority = configMAX_PRIORITIES - 1;
    
  • makefile 中 gcc 的 compile option 加上 -Wl,--undefined=uxTopUsedPriority ( 通常是加在 CFLAG 裡 )
  • 接著要修改 openocd config 檔。為了避免動到 openocd 原始的檔案,將你使用的 config 檔拷備一份到你的專案目錄下再進行修改。增加下面一行
    xxxxxxxx.cpu configure -rtos FreeRTOS
    
    xxxxxxxx 是你的 chip 在 openocd 裡的名字,可以從 openocd 的 target config 檔裡找到
  • 以下是一個修改範例
    # This is an STM32F7 discovery board with a single STM32F756NGH6 chip.
    # http://www.st.com/web/catalog/tools/FM116/SC959/SS1532/LN1848/PF261641
    
    # This is for using the onboard STLINK/V2-1
    source [find interface/stlink-v2-1.cfg]
    
    transport select hla_swd
    
    # increase working area to 256KB
    set WORKAREASIZE 0x40000
    
    source [find target/stm32f7x.cfg]
    
    stm32f7x.cpu configure -rtos FreeRTOS
    

留言

張貼留言

這個網誌中的熱門文章

[STM32] 3. USART 的發送與接收

[STM32] 1. 在 Windows 下架設 gcc 開發環境