摘要
在macOS上搭建gd32f103cbt6的PlatformIO的開發環境,編譯點燈例程.
關鍵資訊
- macOS 14.4.1(Apple Silicon M2)
- 宿主平臺:VSCODE
- PIO Core v6.1.7
- 晶片:GD32F103CBT6
原理簡介
PlatformIO簡介
[https://docs.platformio.org/en/latest/what-is-platformio.html]
A place where Developers and Teams have true Freedom! No more vendor lock-in!
PlatformIO is a cross-platform, cross-architecture, multiple framework, professional tool for embedded systems engineers and for software developers who write applications for embedded products.
PlatformIO’s unique philosophy in the embedded market provides developers with a modern integrated development environment (Cloud & Desktop IDEs) that works cross-platform, supports many different software development kits (SDKs) or Frameworks, and includes sophisticated debugging (Debugging), unit testing (Unit Testing), automated code analysis (Static Code Analysis), and remote management (Remote Development). It is architected to maximize flexibility and choice by developers, who can use either graphical or command line editors (PlatformIO Core (CLI)), or both.
PlatformIO is a must-have tool for professional embedded systems engineers who develop solutions on more than one specific platform. In addition, by having a decentralized architecture, PlatformIO offers both new and existing developers a quick integration path for developing commercial-ready products, and reduces the overall time-to-market.
And it runs on any one of your favorite modern operating systems (macOS, MS Windows, Linux, FreeBSD).
一個開發者和團隊能夠享有真正自由的地方!不再有供應商鎖定!
PlatformIO 是一個跨平臺、跨架構、多框架的專業工具,適用於嵌入式系統工程師,以及為嵌入式產品編寫應用程式的軟體開發人員。
PlatformIO 在嵌入式市場的獨特理念為開發者提供了一個現代化的整合開發環境(雲和桌面 IDE),它能夠跨平臺工作,支援許多不同的軟體開發套件(SDK)或框架,幷包括複雜的除錯(除錯)、單元測試(單元測試)、自動程式碼分析(靜態程式碼分析)和遠端管理(遠端開發)。它的架構旨在最大化開發者的靈活性和選擇權,開發者可以使用圖形介面或命令列編輯器(PlatformIO Core (CLI)),或兩者兼用。
PlatformIO 是專業嵌入式系統工程師必不可少的工具,他們開發的產品不僅僅針對一個特定的平臺。此外,透過擁有去中心化的架構,PlatformIO 為新晉和現有開發者提供了一個快速整合的路徑,用於開發商業級產品,並減少了上市總時間。
它能夠在您喜歡的任何現代作業系統上執行(macOS、MS Windows、Linux、FreeBSD)。
GD32F103CBT6簡介
[https://blog.csdn.net/a1058191679/article/details/120954423]
[https://www.gigadevice.com/product/mcu/arm-cortex-m3/gd32f103cbt6]
GD32F1 is a new series product of 32-bit general-purpose, high-performance, and low-power MCU powered by the ARM® Cortex®-M3 core, suitable for a wide range of application scenarios. The GD32F1 product series integrates rich features to simplify system design and provide customers with a wide range of superior cost-effective MCU portfolios with proven technology and great innovations. Building on GigaDevice advanced gFlash technology and mass production experiences, it provides a new form of MCUs which offer customers high functionality and quality. The GD32F1 series is available in a lineup of basic, mainstream, connectivity, value, and performance.
The GD32F1 series of MCUs incorporates the 32-bit ARM® Cortex®-M3 core operating at 108MHz maximum frequency with Flash accesses zero wait states to obtain maximum efficiency. Meanwhile, it provides up to 3024KB on-chip Flash memory and up to 96KB SRAM. An extensive range of enhanced I/O ports and on-chip peripherals are connected to two APB buses. The MCU series provides up to three 12-bit 1MSPS ADCs and ten general-purpose 16-bit timers plus one PWM advanced timer, as well as standard and advanced communication interfaces.
The GD32F1 series of MCUs also brings many advantages to end-users. The maximum speed of GD32F1 series MCUs has been increased 50% than market's products. The code execution efficiency has been enhanced by 30% to 40% and power consumption had reduced by 20% to 30% in the same frequency. These performances provide maximum capability and bandwidth options for various market requirements.
The GD32F1 series of MCUs is widely suitable for fields of industrial control, human-machine interface, motor control, power monitor and alarm systems, consumer and handheld equipment, solar photovoltaic control, touch panel, and more.
GD32F1 是一款由 ARM® Cortex®-M3 核心提供支援的全新 32 位通用、高效能、低功耗微控制器系列,適用於廣泛的應用場景。GD32F1 產品系列整合了豐富的特性,以簡化系統設計,為客戶提供具有經過驗證技術和重大創新的高價效比 MCU 組合。基於 GigaDevice 先進的 gFlash 技術和大規模生產經驗,它為客戶提供了一種提供高功能和質量的新型 MCU。GD32F1 系列包括基礎型、主流型、連線型、價值型和效能型等多種型號。
GD32F1 系列的微控制器內建了 32 位 ARM® Cortex®-M3 核心,最高執行頻率可達 108MHz,並且 Flash 儲存器訪問零等待狀態,以獲得最大效率。同時,它提供高達 3024KB 的片上 Flash 儲存器和高達 96KB 的 SRAM。一系列增強的 I/O 埠和片上外設連線到兩個 APB 匯流排上。該 MCU 系列提供多達三個 12 位 1MSPS ADC 和十個通用 16 位定時器以及一個 PWM 高階定時器,以及標準和高階通訊介面。
GD32F1 系列的微控制器也為終端使用者帶來了許多優勢。GD32F1 系列微控制器的最大速度比市場產品提高了 50%。程式碼執行效率提高了 30% 到 40%,並且在相同頻率下功耗降低了 20% 到 30%。這些效能為滿足各種市場需求提供了最大的能力和頻寬選項。
GD32F1 系列的微控制器廣泛應用於工業控制、人機介面、電機控制、電源監控和報警系統、消費類和手持裝置、太陽能光伏控制、觸控皮膚等領域。
GD32的SPL庫簡介
[https://bbs.21ic.com/icview-3381260-1-1.html]
GD32系列提供了一些庫函式,與STM32的標準外設庫(SPL)類似,用於配置和控制硬體資源。這些庫函式可以幫助簡化程式碼編寫和硬體操作。儘管它們與STM32的庫函式類似,但並不完全相容。因此,在從STM32遷移到GD32時,可能需要進行一些適配和調整以確保程式碼的正確性和可靠性。
CommunityGD32Cores/platform-gd32簡介
[https://gitcode.com/CommunityGD32Cores/platform-gd32/overview]
這是專為 GD32F、GD32E、GD32W 和 GD32C 系列晶片設計的 PlatformIO 平臺實現。目前仍在開發中。
支援的板卡
請查閱 boards 資料夾。
- GD32L23x(Cortex-M23)
- GD32C10x(Cortex-M4)
- GD32W51x(Cortex-M33)
- GD32E10x(Cortex-M4)
- GD32E23x(Cortex-M23)
- GD32E50x(Cortex-M33)
- GD32F10x(Cortex-M4)
- GD32F1x0(Cortex-M3)
- GD32F20x(Cortex-M3)
- GD32F30x(Cortex-M4)
- GD32F3x0(Cortex-M4)
- GD32F4xx(Cortex-M4)
- GD32F403(Cortex-M4)
所有基於 ARM 架構的 GD32 微控制器都可透過 genericGD32... 板卡定義支援。此外,還支援由 Gigadevice 及其他廠商製造的許多 GD32 開發板。
請參考示例專案:https://github.com/CommunityGD32Cores/gd32-pio-projects。
該平臺可以透過命令列工具手動安裝:pio platform install https://github.com/CommunityGD32Cores/platform-gd32.git(參見 PlatformIO Core CLI 文件)。當你編譯其中一個示例專案時,這會自動完成。
實現
安裝PlatformIO
- VSCODE安裝PlatformIO外掛
啟用外掛會顯示初始化中,如果有魔法可以跳過如下步驟. - 使用國內源進行初始化
conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/pro
conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
conda create -n platformio python=3.9 -y
# 啟用新建立的環境
conda init bash
conda activate platformio
python -m pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pqi
python -m PQI.pqi use ustc
wget https://raw.gitcode.com/platformio/platformio-core-installer/raw/master/get-platformio.py
python get-platformio.py
echo 'export PATH=$PATH:/Users/workspace/.platformio/penv/bin' >> ~/.zshrc
source ~/.zshrc
pio boards
然後重啟vscode;
初始化GD32支援&新建工程
#安裝gd32 sdk和toolchain等
export https_proxy=http://127.0.0.1:7890 http_proxy=http://127.0.0.1:7890 all_proxy=socks5://127.0.0.1:7890
pio pkg install -l https://gitcode.com/QS2002/platform-gd32.git
# 新建一個專案資料夾
mkdir nebula-gd32f103cbt6-blink-platformio && cd nebula-gd32f103cbt6-blink-platformio
# 初始化一個genericGD32F103CB board專案
pio project init --board genericGD32F103CB
編譯
工程目錄下新建src/main.c
#if defined(GD32F10x)
#include "gd32f10x.h"
#endif
/* define blinky LED pin here, board specific, otherwise default PC13 */
#define LEDPORT GPIOC
#define LEDPIN GPIO_PIN_13
#define LED_CLOCK RCU_GPIOC
void systick_config(void);
void delay_1ms(uint32_t count);
int main(void)
{
systick_config();
rcu_periph_clock_enable(LED_CLOCK);
/* set output as output */
#if defined(GD32F3x0) || defined(GD32F1x0) || defined(GD32F4xx) || defined(GD32E23x) || defined(GD32L23x) || defined(GD32W51x)
gpio_mode_set(LEDPORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, LEDPIN);
gpio_output_options_set(LEDPORT, GPIO_OTYPE_PP, GPIO_OSPEED_2MHZ, LEDPIN);
#else /* valid for GD32F10x, GD32E20x, GD32F30x, GD32F403, GD32E10X, GD32C10X */
gpio_init(LEDPORT, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, LEDPIN);
#endif
while (1)
{
gpio_bit_set(LEDPORT, LEDPIN);
delay_1ms(100);
gpio_bit_reset(LEDPORT, LEDPIN);
delay_1ms(100);
}
}
volatile static uint32_t delay;
void systick_config(void)
{
/* setup systick timer for 1000Hz interrupts */
if (SysTick_Config(SystemCoreClock / 1000U))
{
/* capture error */
while (1)
{
}
}
/* configure the systick handler priority */
NVIC_SetPriority(SysTick_IRQn, 0x00U);
}
void delay_1ms(uint32_t count)
{
delay = count;
while (0U != delay)
{
}
}
void delay_decrement(void)
{
if (0U != delay)
{
delay--;
}
}
void NMI_Handler(void) {}
void HardFault_Handler(void)
{
while (1)
;
}
void MemManage_Handler(void)
{
while (1)
;
}
void BusFault_Handler(void)
{
while (1)
;
}
void UsageFault_Handler(void)
{
while (1)
;
}
void SVC_Handler(void)
{
}
void DebugMon_Handler(void)
{
}
void PendSV_Handler(void)
{
}
void SysTick_Handler(void)
{
delay_decrement();
}
然後編譯:
pio run -e genericGD32F103CB
效果
h:1492:13: warning: 'enet_delay' declared 'static' but never defined [-Wunused-function]
1492 | static void enet_delay(uint32_t ncount);
| ^~~~~~~~~~
Archiving .pio/build/genericGD32F103CB/libFrameworkSPL.a
Indexing .pio/build/genericGD32F103CB/libFrameworkCMSISVariant.a
Indexing .pio/build/genericGD32F103CB/libFrameworkSPL.a
Indexing .pio/build/genericGD32F103CB/lib81a/libplatform-gd32.a
Linking .pio/build/genericGD32F103CB/firmware.elf
Checking size .pio/build/genericGD32F103CB/firmware.elf
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
RAM: [ ] 0.2% (used 36 bytes from 20480 bytes)
Flash: [ ] 0.7% (used 920 bytes from 131072 bytes)
Building .pio/build/genericGD32F103CB/firmware.bin
================ [SUCCESS] Took 10.40 seconds ================