1-VSCode搭建GD32開發環境

語言: CN / TW / HK

一、使用VSCode開發GD32的原因

1-微控制器開發用的最多的IDE為Keil,而Keil為商用軟體,並非開源,而且只支援windows環境,介於當前關係,有斷供的風險在。

2-其他IDE類似第1條。

3-VSCode為開源的編輯器,不存在授權一說,介面比較簡約和優雅。可擴充套件性強。

二、VSCode開發環境搭建

下面用GD32F303介紹下環境的搭建。

1-下載VSCode: 參考網址http://code.visualstudio.com/docs/setup/setup-overview

2-下載編譯工具arm-none-eabi-gcc: 點選http://developer.arm.com/downloads/-/gnu-rm

3-安裝工具pyocd: pip3 install pyocd: 安裝完畢後檢查pyocd版本:pyocd --version,如果遇到PyYAML問題可 pip install --ignore-installed PyYAML

4-安裝VSCode擴充套件Cortex-Debug: 開啟VSCode並搜尋Cortex-Debug

5-安裝c、c++支援: 開啟VSCode並搜尋c/c++

5-下載例項程式: 開啟兆易官網http://www.gd32mcu.com,點選資料下載-開發板資料,點選左邊GD32F3 MCU,選擇GD32F30x Demo Suites

6-開啟例項程式: 解壓下載好的GD32F30x Demo Suites。點選GD32303B_START_Demo_Suites,找到 Projects資料夾裡的示例01_GPIO_Running_LED。將整個資料夾拖入VSCode.

7-匯入GD3230系列庫函式: 點選GD32303B_START_Demo_Suites, 把GD32F30x_Firmware_Library裡的CMSIS和GD32F30x_standard_peripheral拖入VSCode

三、生產Makefile檔案

在Linux環境或mac環境下,使用GNU make來構建和管理自己的工程。整個工程的編譯只需要一個命令就可以完成編譯、連線以至於最後的執行

1-在本案例裡,使用python指令碼toMakefile.py來產生makefile檔案

toMakefile.py指令碼如下:

import os

def main():

cFileList = []

hPathList = []

sFileList = []

ldFileList = []

for root,dirs,files in os.walk("./"):

for file in files:

if file.endswith(".c"):

cFile = os.path.join(root,file)#.decode('gbk').encode('utf-8')

cFileList.append(cFile)

if file.endswith(".h"):

hPath = os.path.join(root)#.decode('gbk').encode('utf-8')

if hPathList.count(hPath) == 0:

hPathList.append(hPath)

if file.endswith(".s"):

sFile = os.path.join(root,file)#.decode('gbk').encode('utf-8')

sFileList.append(sFile)

if file.endswith(".ld"):

ldFile = os.path.join(root,file)#.decode('gbk').encode('utf-8')

ldFileList.append(ldFile)

print(cFileList)

print(hPathList)

print(sFileList)

print(ldFileList)

cFileStr = ""

hPathStr = ""

sFileStr = ""

ldFileStr = ""

for listStr in cFileList:

# cFileStr += " \\\n" + listStr

cFileStr += "C_SOURCES += " + listStr + "\n"

for listStr in hPathList:

# hPathStr += " \\\n-I" + listStr

hPathStr += "C_INCLUDES += -I" + listStr + "\n"

for listStr in sFileList:

# sFileStr += " \\\n" + listStr

sFileStr += "ASM_SOURCES += " + listStr + "\n"

for listStr in ldFileList:

# ldFileStr += " \\\n" + listStr

ldFileStr += "LDSCRIPT += " + listStr + "\n"

try:

f = open("./Makefile.template", "r")

fileStr = f.read()

f.close()

fileStr = fileStr.replace("@@C_SOURCES@@", cFileStr)

fileStr = fileStr.replace("@@C_INCLUDES@@", hPathStr)

fileStr = fileStr.replace("@@ASM_SOURCES@@", sFileStr)

fileStr = fileStr.replace("@@LDSCRIPT@@", ldFileStr)

f = open("./Makefile", "w")

f.write(fileStr)#.decode('gbk').encode('utf-8'))

f.close()

finally:

f.close()

if __name__ == '__main__':

main()

2-程式碼編譯

在專案終端下輸入make,再回車,之後會看到專案目錄出現build資料夾,即編譯完成

四、程式碼除錯

使用USB線連線上開發板,點選VSCode上的執行和除錯,再點選開始除錯, 即可進入除錯狀態

五、參考文獻

1-極術社群 LJL 《【GD32F310開發板試用】MAC開發&除錯環境搭建》

2-簡書 _空格鍵_  《使用VS Code開發微控制器程式》