參考這個博主https://blog.csdn.net/qq_34245464/article/details/100769643的博文,
使用VS Code再重新打造一次完美的嵌入式IDE平台。
前言
在我剛開始接觸STM32的時候,使用的keil作為IDE,由於在這之前,我使用過VS, 使用過eclipse, 因而在我使用keil之后,實在難以忍受keil編輯器簡陋的功能,可以說是極其糟糕的寫代碼體驗
之后,我嘗試過各種IDE,使用eclipse + keil,結果發現eclipse對C語言的支持也是雞肋,使用emBits + gcc,需要和其他人協同的話就比較麻煩,之后發現了platformIO,也是使用gcc作為編譯器,不過只支持HAL庫,而且還有一個重要的原因,同事都是用的keil,如果我使用gcc,就不能協同工作了
最后,通過使用VS Code + keil的方式,完美解決了寫代碼的體驗問題,以及工程協作問題
其實網上使用VS Code作為編輯器,keil作為編譯器的教程很多,不過基本都是需要在VS Code中編輯,然后在keil中編譯,下載,調試,本文就要實現編輯,編譯,下載,調試,全部使用VS Code
環境
VS Code
keil
python
GNU Arm Embedded Toolchain(arm gcc工具鏈,這一部分原博主沒有講的很細,只是提了一下)
C/C++(VS Code 插件)
Cortex-Debug(VS Code 插件)
其他VS Code插件(提升體驗)
前提
正式寫代碼之前,首先需要建立好一個工程,這個需要使用keil完成,包括工程配置,文件添加…
編輯
在安裝好VS Code插件之后,VS Code編寫C代碼本身體驗就已經很好了,但是,因為我們使用的是keil環境,所以需要配置頭文件包含,宏定義等
在工程路徑的.vscode文件夾下打開c_cpp_properties.json文件,沒有自己新建一個,內容配置如下:
1 { 2 "configurations": [ 3 { 4 "name": "STM32", 5 "includePath": [ 6 "D:/Program Files/MDK5/ARM/ARMCC/**", 7 "${workspaceFolder}/**", 8 "" 9 ], 10 "browse": { 11 "limitSymbolsToIncludedHeaders": true, 12 "databaseFilename": "${workspaceRoot}/.vscode/.browse.c_cpp.db", 13 "path": [ 14 "D:/Program Files/MDK5/ARM/ARMCC/**", 15 "${workspaceFolder}/**", 16 "" 17 ] 18 }, 19 "defines": [ 20 "_DEBUG", 21 "UNICODE", 22 "_UNICODE", 23 "__CC_ARM", 24 "USE_STDPERIPH_DRIVER", 25 "STM32F10X_MD" 26 ], 27 "intelliSenseMode": "msvc-x64" 28 } 29 ], 30 "version": 4 31 }
其中,需要在includePath和path中添加頭文件路徑,${workspaceFolder}/**是工程路徑,不用改動,額外需要添加的是keil的頭文件路徑
然后在defines中添加宏,也就是在keil的Options for Target的C++選項卡中配置的宏
然后就可以體驗VS Code強大的代碼提示,函數跳轉等功能了(甩keil的編輯器一整個時代)
編譯、燒錄
編譯和燒錄通過VS Code的Task功能實現,通過Task,使用命令行的方式調用keil進行編譯和燒錄
keil本身就支持命令行調用,具體可以參考keil的手冊,這里就不多說了,但是問題在於,使用命令行調用keil,不管是什么操作,他的輸出都不會輸出到控制台上!!!(要你這命令行支持有何用)
不過好在,keil支持輸出到文件中,那我們就只能利用這個做點騷操作了————一邊執行命令,一邊讀取文件內容並打印到控制台,從而就實現了輸出在控制台上,我們就能直接在VS Code中看到編譯過程了
為此,我編寫了一個Python腳本,實現keil的命令行調用並同時讀取文件輸出到控制台
1 #!/usr/bin/python 2 # -*- coding:UTF-8 -*- 3 4 import os 5 import threading 6 import sys 7 8 runing = True 9 10 def readfile(logfile): 11 with open(logfile, 'w') as f: 12 pass 13 with open(logfile, 'r') as f: 14 while runing: 15 line = f.readline(1000) 16 if line != '': 17 line = line.replace('\\', '/') 18 print(line, end = '') 19 20 if __name__ == '__main__': 21 modulePath = os.path.abspath(os.curdir) 22 logfile = modulePath + '/build.log' 23 cmd = '\"D:/Program Files/MDK5/UV4/UV4.exe\" ' 24 for i in range(1, len(sys.argv)): 25 cmd += sys.argv[i] + ' ' 26 cmd += '-j0 -o ' + logfile 27 thread = threading.Thread(target=readfile, args=(logfile,)) 28 thread.start() 29 code = os.system(cmd) 30 runing = False 31 thread.join() 32 sys.exit(code)
此腳本需要結合VS Code的Task運行,通過配置Task,我們還需要匹配輸出中的錯誤信息(編譯錯誤),實現在keil中,點擊錯誤直接跳轉到錯誤代碼處,具體如何配置請參考VS Code的文檔,這里給出我的Task
1 { 2 // See https://go.microsoft.com/fwlink/?LinkId=733558 3 // for the documentation about the tasks.json format 4 "version": "2.0.0", 5 "tasks": [ 6 { 7 "label": "build", 8 "type": "shell", 9 "command": "py", 10 "args": [ 11 "-3", 12 "${workspaceFolder}/scripts/build.py", 13 "-b", 14 "${config:uvprojxPath}" 15 ], 16 "group": { 17 "kind": "build", 18 "isDefault": true 19 }, 20 "problemMatcher": [ 21 { 22 "owner": "c", 23 "fileLocation": [ 24 "relative", 25 "${workspaceFolder}/Project" 26 ], 27 "pattern": { 28 "regexp": "^(.*)\\((\\d+)\\):\\s+(warning|error):\\s+(.*):\\s+(.*)$", 29 "file": 1, 30 "line": 2, 31 "severity": 3, 32 "code": 4, 33 "message": 5 34 } 35 } 36 ] 37 }, 38 { 39 "label": "rebuild", 40 "type": "shell", 41 "command": "py", 42 "args": [ 43 "-3", 44 "${workspaceFolder}/scripts/build.py", 45 "-r", 46 "${config:uvprojxPath}" 47 ], 48 "group": "build", 49 "problemMatcher": [ 50 { 51 "owner": "c", 52 "fileLocation": [ 53 "relative", 54 "${workspaceFolder}/Project" 55 ], 56 "pattern": { 57 "regexp": "^(.*)\\((\\d+)\\):\\s+(warning|error):\\s+(.*):\\s+(.*)$", 58 "file": 1, 59 "line": 2, 60 "severity": 3, 61 "code": 4, 62 "message": 5 63 } 64 } 65 ] 66 }, 67 { 68 "label": "download", 69 "type": "shell", 70 "command": "py", 71 "args": [ 72 "-3", 73 "E:\\Work\\Store\\MyWork\\STM32F1\\FreeModbus_M3\\scripts\\build.py", 74 "-f", 75 "${config:uvprojxPath}" 76 ], 77 "group": "test" 78 }, 79 { 80 "label": "open in keil", 81 "type": "process", 82 "command": "${config:uvPath}", 83 "args": [ 84 "${config:uvprojxPath}" 85 ], 86 "group": "test" 87 } 88 ] 89 }
對於使用ARM Compiler 6編譯的工程,build和rebuild中的problemMatcher應該配置為
1 "problemMatcher": [ 2 { 3 "owner": "c", 4 "fileLocation": ["relative", "${workspaceFolder}/MDK-ARM"], 5 "pattern": { 6 "regexp": "^(.*)\\((\\d+)\\):\\s+(warning|error):\\s+(.*)$", 7 "file": 1, 8 "line": 2, 9 "severity": 3, 10 "message": 4, 11 } 12 } 13 ]
文件中的config:uvPath和config:uvprojxPath分別為keil的UV4.exe文件路徑和工程路徑(.uvprojx),可以直接修改為具體路徑,或者在VS Code的setting.json中增加對應的項
至此,我們已經完美實現了在VS Code中編輯,編譯,下載了
編譯輸出:

有錯誤時輸出:

錯誤匹配:

調試
調試需要使用到Cortex-Debug插件,以及arm gcc工具鏈,這部分可以參考Cortex-Debug的文檔,說的比較詳細
首先安裝Cortex-Debug插件和arm gcc工具鏈,然后配置好環境路徑,如果使用Jlink調試,需要下載Jlink套件,安轉好之后,找到JLinkGDBServerCL.exe這個程序,在VS Code的設置中添加"cortex-debug.JLinkGDBServerPath": "C:/Program Files (x86)/SEGGER/JLink/JLinkGDBServerCL.exe",后面的路徑是你自己的路徑。
這里補充一下arm gcc工具鏈的配置:
如果使用STLink調試,需要下載stutil工具,在GitHub上搜索即可找到,同樣配置好路徑即可。
以上步驟弄好之后,可以直接點擊VS Code的調試按鈕,此時會新建luanch.json文件,這個文件就是VS Code的調試配置文件,可參考我的文件進行配置
1 { 2 // 使用 IntelliSense 了解相關屬性。 3 // 懸停以查看現有屬性的描述。 4 // 欲了解更多信息,請訪問: https://go.microsoft.com/fwlink/?linkid=830387 5 "version": "0.2.0", 6 "configurations": [ 7 8 { 9 "name": "Cortex Debug(JLINK)", 10 "cwd": "${workspaceRoot}", 11 "executable": "${workspaceRoot}/Project/Objects/Demo.axf", 12 "request": "attach", 13 "type": "cortex-debug", 14 "servertype": "jlink", 15 "device": "STM32F103C8", 16 "svdFile": "D:\\Program Files\\ARM\\Packs\\Keil\\STM32F1xx_DFP\\2.3.0\\SVD\\STM32F103xx.svd", 17 "interface": "swd", 18 "ipAddress": null, 19 "serialNumber": null 20 }, 21 { 22 "name": "Cortex Debug(ST-LINK)", 23 "cwd": "${workspaceRoot}", 24 "executable": "${workspaceRoot}/Project/Objects/Demo.axf", 25 "request": "attach", 26 "type": "cortex-debug", 27 "servertype": "stutil", 28 "svdFile": "D:\\Program Files\\ARM\\Packs\\Keil\\STM32F1xx_DFP\\2.3.0\\SVD\\STM32F103xx.svd", 29 "device": "STM32F103C8", 30 "v1": false 31 } 32 ] 33 }
注意其中幾個需要修改的地方,executable修改為你的工程生成的目標文件,也就是工程的.axf文件,svdFile用於對MCU外設的監控,該文件可以在keil的安裝路徑中找到,可以參考我的路徑去找
配置完成后,再次點擊調試按鈕即可進行調試

相比keil自己的調試功能,VS Code還支持條件斷點,可以設置命中條件,次數等,可以極大的方便調試
總結
通過以上的配置,我們基本上,除了建立工程和往工程中添加文件,其他完全不需要打開keil,所以也無妨說一句,再見,智障keil!
