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("@@[email protected]@", cFileStr)

fileStr = fileStr.replace("@@[email protected]@", hPathStr)

fileStr = fileStr.replace("@@[email protected]@", sFileStr)

fileStr = fileStr.replace("@@[email protected]@", 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開發單片機程序》