1# GD32 ARM系列 BSP 制作教程 2 3## 1. BSP 框架介绍 4 5BSP 框架结构如下图所示: 6 7 8 9GD32 ARM系列BSP架构主要分为三个部分:libraries、tools和具体的Boards,其中libraries包含了GD32的通用库,包括每个系列的Firmware Library以及适配RT-Thread的drivers;tools是生成工程的Python脚本工具;另外就是Boards文件,当然这里的Boards有很多,我这里值列举了GD32407V-START。 10 11## 2. 知识准备 12 13制作一个 BSP 的过程就是构建一个新系统的过程,因此想要制作出好用的 BSP,要对 RT-Thread 系统的构建过程有一定了解,需要的知识准备如下所示: 14 15- 掌握 GD32 ARM系列 BSP 的使用方法 16 17 了解 BSP 的使用方法,可以阅读 [BSP 说明文档](../README.md) 中使用教程表格内的文档。 18 19- 了解 Scons 工程构建方法 20 21 RT-Thread 使用 Scons 作为系统的构建工具,因此了解 Scons 的常用命令对制作新 BSP 是基本要求。 22 23- 了解设备驱动框架 24 25 在 RT-Thread 系统中,应用程序通过设备驱动框架来操作硬件,因此了解设备驱动框架,对添加 BSP 驱动是很重要的。 26 27- 了解 Kconfig 语法 28 29 RT-Thread 系统通过 menuconfig 的方式进行配置,而 menuconfig 中的选项是由 Kconfig 文件决定的,因此想要对 RT-Thread 系统进行配置,需要对 kconfig 语法有一定了解。 30 31## 3. BSP移植 32 33### 3.1 Keil环境准备 34 35目前市面通用的MDK for ARM版本有Keil 4和Keil 5:使用Keil 4建议安装4.74及以上;使用Keil 5建议安装5.20以上版本。本文的MDK是5.30。 36 37从MDK的官网可以下载得到MDK的安装包,然后安装即可。 38 39[MDK下载地址](https://www.keil.com/download/product/) 40 41 42 43安装完成后会自动打开,我们将其关闭。 44 45接下来我们下载GD32F4xx的软件支持包。 46 47[下载地址](http://www.gd32mcu.com/cn/download) 48 49  50 51下载好后双击GigaDevice.GD32F4xx_DFP.2.1.0.pack运行即可: 52 53  54 55点击[Next]即可安装完成。 56 57  58 59安装成功后,重新打开Keil,则可以在File->Device Database中出现Gigadevice的下拉选项,点击可以查看到相应的型号。 60 61  62 63### 3.2 BSP工程制作 64 65**1.构建基础工程** 66 67首先看看RT-Thread代码仓库中已有很多BSP,而我要移植的是Cortex-M4内核。这里我找了一个相似的内核,把它复制一份,并修改文件名为:gd32407v-start。这样就有一个基础的工程。然后就开始增删改查,完成最终的BSP,几乎所有的BSP的制作都是如此。 68 69**2.修改BSP构建脚本** 70 71bsp/gd32/arm/gd32407v-start/SConstruct修改后的内容如下: 72 73```python 74import os 75import sys 76import rtconfig 77 78if os.getenv('RTT_ROOT'): 79 RTT_ROOT = os.getenv('RTT_ROOT') 80else: 81 RTT_ROOT = os.path.normpath(os.getcwd() + '/../../../..') 82 83sys.path = sys.path + [os.path.join(RTT_ROOT, 'tools')] 84try: 85 from building import * 86except: 87 print('Cannot found RT-Thread root directory, please check RTT_ROOT') 88 print(RTT_ROOT) 89 exit(-1) 90 91TARGET = 'rtthread.' + rtconfig.TARGET_EXT 92 93DefaultEnvironment(tools=[]) 94env = Environment(tools = ['mingw'], 95 AS = rtconfig.AS, ASFLAGS = rtconfig.AFLAGS, 96 CC = rtconfig.CC, CFLAGS = rtconfig.CFLAGS, 97 AR = rtconfig.AR, ARFLAGS = '-rc', 98 CXX = rtconfig.CXX, CXXFLAGS = rtconfig.CXXFLAGS, 99 LINK = rtconfig.LINK, LINKFLAGS = rtconfig.LFLAGS) 100env.PrependENVPath('PATH', rtconfig.EXEC_PATH) 101 102if rtconfig.PLATFORM in ['iccarm']: 103 env.Replace(CCCOM = ['$CC $CFLAGS $CPPFLAGS $_CPPDEFFLAGS $_CPPINCFLAGS -o $TARGET $SOURCES']) 104 env.Replace(ARFLAGS = ['']) 105 env.Replace(LINKCOM = env["LINKCOM"] + ' --map rtthread.map') 106 107Export('RTT_ROOT') 108Export('rtconfig') 109 110SDK_ROOT = os.path.abspath('./') 111 112if os.path.exists(SDK_ROOT + '/libraries'): 113 libraries_path_prefix = SDK_ROOT + '/libraries' 114else: 115 libraries_path_prefix = os.path.dirname(SDK_ROOT) + '/libraries' 116 117SDK_LIB = libraries_path_prefix 118Export('SDK_LIB') 119 120# prepare building environment 121objs = PrepareBuilding(env, RTT_ROOT, has_libcpu=False) 122 123gd32_library = 'GD32F4xx_Firmware_Library' 124rtconfig.BSP_LIBRARY_TYPE = gd32_library 125 126# include libraries 127objs.extend(SConscript(os.path.join(libraries_path_prefix, gd32_library, 'SConscript'))) 128 129# include drivers 130objs.extend(SConscript(os.path.join(libraries_path_prefix, 'Drivers', 'SConscript'))) 131 132# make a building 133DoBuilding(TARGET, objs) 134``` 135 136该文件用于链接所有的依赖文件,主要修改固件库路径,并调用make进行编译。 137 138**3.修改KEIL的模板工程** 139 140双击:template.uvprojx即可修改模板工程。 141 142修改为对应芯片设备: 143 144  145 146修改FLASH和RAM的配置: 147 148  149 150修改可执行文件名字: 151 152 153 154修改默认调试工具:CMSIS-DAP Debugger。 155 156 157 158修改编程算法:GD32F4xx FMC。 159 160 161 162**4.修改board文件夹** 163 164(1) 修改bsp/gd32/arm/gd32407v-start/board/linker_scripts/link.icf 165 166修改后的内容如下: 167 168``` 169/*###ICF### Section handled by ICF editor, don't touch! / 170/*-Editor annotation file-*/ 171/* IcfEditorFile="$TOOLKIT_DIR$\config\ide\IcfEditor\cortex_v1_0.xml" */ 172/*-Specials-*/ 173define symbol __ICFEDIT_intvec_start__ = 0x08000000; 174/*-Memory Regions-*/ 175define symbol __ICFEDIT_region_ROM_start__ = 0x08000000; 176define symbol __ICFEDIT_region_ROM_end__ = 0x082FFFFF; 177define symbol __ICFEDIT_region_RAM_start__ = 0x20000000; 178define symbol __ICFEDIT_region_RAM_end__ = 0x2002FFFF; 179/*-Sizes-*/ 180define symbol __ICFEDIT_size_cstack__ = 0x2000; 181define symbol __ICFEDIT_size_heap__ = 0x2000; 182/ End of ICF editor section. ###ICF###*/ 183 184export symbol __ICFEDIT_region_RAM_end__; 185 186define symbol __region_RAM1_start__ = 0x10000000; 187define symbol __region_RAM1_end__ = 0x1000FFFF; 188 189define memory mem with size = 4G; 190define region ROM_region = mem:[from __ICFEDIT_region_ROM_start__ to __ICFEDIT_region_ROM_end__]; 191define region RAM_region = mem:[from __ICFEDIT_region_RAM_start__ to __ICFEDIT_region_RAM_end__]; 192define region RAM1_region = mem:[from __region_RAM1_start__ to __region_RAM1_end__]; 193 194define block CSTACK with alignment = 8, size = __ICFEDIT_size_cstack__ { }; 195define block HEAP with alignment = 8, size = __ICFEDIT_size_heap__ { }; 196 197initialize by copy { readwrite }; 198do not initialize { section .noinit }; 199 200keep { section FSymTab }; 201keep { section VSymTab }; 202keep { section .rti_fn* }; 203place at address mem:__ICFEDIT_intvec_start__ { readonly section .intvec }; 204 205place in ROM_region { readonly }; 206place in RAM_region { readwrite, 207 block CSTACK, block HEAP }; 208place in RAM1_region { section .sram }; 209``` 210 211该文件是IAR编译的链接脚本,根据《GD32F407xx_Datasheet_Rev2.1》可知,GD32F407VKT6的flash大小为3072KB,SRAM大小为192KB,因此需要设置ROM和RAM的起始地址和堆栈大小等。 212 213(2) 修改bsp/gd32/arm/gd32407v-start/board/linker_scripts/link.ld 214 215修改后的内容如下: 216 217``` 218/* Program Entry, set to mark it as "used" and avoid gc */ 219MEMORY 220{ 221 CODE (rx) : ORIGIN = 0x08000000, LENGTH = 3072k /* 3072KB flash */ 222 DATA (rw) : ORIGIN = 0x20000000, LENGTH = 192k /* 192KB sram */ 223} 224ENTRY(Reset_Handler) 225_system_stack_size = 0x200; 226 227SECTIONS 228{ 229 .text : 230 { 231 . = ALIGN(4); 232 _stext = .; 233 KEEP(*(.isr_vector)) /* Startup code */ 234 . = ALIGN(4); 235 *(.text) /* remaining code */ 236 *(.text.*) /* remaining code */ 237 *(.rodata) /* read-only data (constants) */ 238 *(.rodata*) 239 *(.glue_7) 240 *(.glue_7t) 241 *(.gnu.linkonce.t*) 242 243 /* section information for finsh shell */ 244 . = ALIGN(4); 245 __fsymtab_start = .; 246 KEEP(*(FSymTab)) 247 __fsymtab_end = .; 248 . = ALIGN(4); 249 __vsymtab_start = .; 250 KEEP(*(VSymTab)) 251 __vsymtab_end = .; 252 . = ALIGN(4); 253 254 /* section information for initial. */ 255 . = ALIGN(4); 256 __rt_init_start = .; 257 KEEP(*(SORT(.rti_fn*))) 258 __rt_init_end = .; 259 . = ALIGN(4); 260 261 . = ALIGN(4); 262 _etext = .; 263 } > CODE = 0 264 265 /* .ARM.exidx is sorted, so has to go in its own output section. */ 266 __exidx_start = .; 267 .ARM.exidx : 268 { 269 *(.ARM.exidx* .gnu.linkonce.armexidx.*) 270 271 /* This is used by the startup in order to initialize the .data secion */ 272 _sidata = .; 273 } > CODE 274 __exidx_end = .; 275 276 /* .data section which is used for initialized data */ 277 278 .data : AT (_sidata) 279 { 280 . = ALIGN(4); 281 /* This is used by the startup in order to initialize the .data secion */ 282 _sdata = . ; 283 284 *(.data) 285 *(.data.*) 286 *(.gnu.linkonce.d*) 287 288 . = ALIGN(4); 289 /* This is used by the startup in order to initialize the .data secion */ 290 _edata = . ; 291 } >DATA 292 293 .stack : 294 { 295 . = . + _system_stack_size; 296 . = ALIGN(4); 297 _estack = .; 298 } >DATA 299 300 __bss_start = .; 301 .bss : 302 { 303 . = ALIGN(4); 304 /* This is used by the startup in order to initialize the .bss secion */ 305 _sbss = .; 306 307 *(.bss) 308 *(.bss.*) 309 *(COMMON) 310 311 . = ALIGN(4); 312 /* This is used by the startup in order to initialize the .bss secion */ 313 _ebss = . ; 314 315 *(.bss.init) 316 } > DATA 317 __bss_end = .; 318 319 _end = .; 320 321 /* Stabs debugging sections. */ 322 .stab 0 : { *(.stab) } 323 .stabstr 0 : { *(.stabstr) } 324 .stab.excl 0 : { *(.stab.excl) } 325 .stab.exclstr 0 : { *(.stab.exclstr) } 326 .stab.index 0 : { *(.stab.index) } 327 .stab.indexstr 0 : { *(.stab.indexstr) } 328 .comment 0 : { *(.comment) } 329 /* DWARF debug sections. 330 * Symbols in the DWARF debugging sections are relative to the beginning 331 * of the section so we begin them at 0. */ 332 /* DWARF 1 */ 333 .debug 0 : { *(.debug) } 334 .line 0 : { *(.line) } 335 /* GNU DWARF 1 extensions */ 336 .debug_srcinfo 0 : { *(.debug_srcinfo) } 337 .debug_sfnames 0 : { *(.debug_sfnames) } 338 /* DWARF 1.1 and DWARF 2 */ 339 .debug_aranges 0 : { *(.debug_aranges) } 340 .debug_pubnames 0 : { *(.debug_pubnames) } 341 /* DWARF 2 */ 342 .debug_info 0 : { *(.debug_info .gnu.linkonce.wi.*) } 343 .debug_abbrev 0 : { *(.debug_abbrev) } 344 .debug_line 0 : { *(.debug_line) } 345 .debug_frame 0 : { *(.debug_frame) } 346 .debug_str 0 : { *(.debug_str) } 347 .debug_loc 0 : { *(.debug_loc) } 348 .debug_macinfo 0 : { *(.debug_macinfo) } 349 /* SGI/MIPS DWARF 2 extensions */ 350 .debug_weaknames 0 : { *(.debug_weaknames) } 351 .debug_funcnames 0 : { *(.debug_funcnames) } 352 .debug_typenames 0 : { *(.debug_typenames) } 353 .debug_varnames 0 : { *(.debug_varnames) } 354} 355``` 356 357该文件是GCC编译的链接脚本,根据《GD32F407xx_Datasheet_Rev2.1》可知,GD32F407VKT6的flash大小为3072KB,SRAM大小为192KB,因此CODE和DATA 的LENGTH分别设置为3072KB和192KB,其他芯片类似,但其实地址都是一样的。 358 359(3) 修改bsp/gd32/arm/gd32407v-start/board/linker_scripts/link.sct 360修改后的内容如下: 361 362``` 363; ************************************************************* 364; *** Scatter-Loading Description File generated by uVision *** 365; ************************************************************* 366 367LR_IROM1 0x08000000 0x00300000 { ; load region size_region 368 ER_IROM1 0x08000000 0x00300000 { ; load address = execution address 369 *.o (RESET, +First) 370 *(InRoot$$Sections) 371 .ANY (+RO) 372 } 373 RW_IRAM1 0x20000000 0x00030000 { ; RW data 374 .ANY (+RW +ZI) 375 } 376} 377``` 378 379该文件是MDK的连接脚本,根据《GD32F407xx_Datasheet_Rev2.1》手册,因此需要将 LR_IROM1 和 ER_IROM1 的参数设置为 0x00300000;RAM 的大小为192k,因此需要将 RW_IRAM1 的参数设置为 0x00030000。 380 381(4) 修改bsp/gd32/arm/gd32407v-start/board/board.h文件 382 383修改后内容如下: 384 385```c 386#ifndef __BOARD_H__ 387#define __BOARD_H__ 388 389#include "gd32f4xx.h" 390#include "drv_usart.h" 391#include "drv_gpio.h" 392 393#include "gd32f4xx_exti.h" 394 395#define EXT_SDRAM_BEGIN (0xC0000000U) /* the begining address of external SDRAM */ 396#define EXT_SDRAM_END (EXT_SDRAM_BEGIN + (32U * 1024 * 1024)) /* the end address of external SDRAM */ 397 398// <o> Internal SRAM memory size[Kbytes] <8-64> 399// <i>Default: 64 400#ifdef __ICCARM__ 401// Use *.icf ram symbal, to avoid hardcode. 402extern char __ICFEDIT_region_RAM_end__; 403#define GD32_SRAM_END &__ICFEDIT_region_RAM_end__ 404#else 405#define GD32_SRAM_SIZE 192 406#define GD32_SRAM_END (0x20000000 + GD32_SRAM_SIZE * 1024) 407#endif 408 409#ifdef __CC_ARM 410extern int Image$$RW_IRAM1$$ZI$$Limit; 411#define HEAP_BEGIN (&Image$$RW_IRAM1$$ZI$$Limit) 412#elif __ICCARM__ 413#pragma section="HEAP" 414#define HEAP_BEGIN (__segment_end("HEAP")) 415#else 416extern int __bss_end; 417#define HEAP_BEGIN (&__bss_end) 418#endif 419 420#define HEAP_END GD32_SRAM_END 421 422#endif 423``` 424 425值得注意的是,不同的编译器规定的堆栈内存的起始地址 HEAP_BEGIN 和结束地址 HEAP_END。这里 HEAP_BEGIN 和 HEAP_END 的值需要和前面的链接脚本是一致的,需要结合实际去修改。 426 427(5) 修改bsp/gd32/arm/gd32407v-start/board/board.c文件 428 429修改后的文件如下: 430 431```c 432#include <stdint.h> 433#include <rthw.h> 434#include <rtthread.h> 435#include <board.h> 436 437/** 438 * @brief This function is executed in case of error occurrence. 439 * @param None 440 * @retval None 441 */ 442 void Error_Handler(void) 443 { 444 /* USER CODE BEGIN Error_Handler */ 445 /* User can add his own implementation to report the HAL error return state */ 446 while (1) 447 { 448 } 449 /* USER CODE END Error_Handler */ 450 } 451 452/** System Clock Configuration 453*/ 454void SystemClock_Config(void) 455{ 456 SysTick_Config(SystemCoreClock / RT_TICK_PER_SECOND); 457 NVIC_SetPriority(SysTick_IRQn, 0); 458} 459 460/** 461 * This is the timer interrupt service routine. 462 * 463 */ 464 void SysTick_Handler(void) 465 { 466 /* enter interrupt */ 467 rt_interrupt_enter(); 468 469 rt_tick_increase(); 470 471 /* leave interrupt */ 472 rt_interrupt_leave(); 473 } 474 475/** 476 * This function will initial GD32 board. 477 */ 478 void rt_hw_board_init() 479 { 480 /* NVIC Configuration */ 481 #define NVIC_VTOR_MASK 0x3FFFFF80 482 #ifdef VECT_TAB_RAM 483 /* Set the Vector Table base location at 0x10000000 */ 484 SCB->VTOR = (0x10000000 & NVIC_VTOR_MASK); 485 #else /* VECT_TAB_FLASH */ 486 /* Set the Vector Table base location at 0x08000000 */ 487 SCB->VTOR = (0x08000000 & NVIC_VTOR_MASK); 488 #endif 489 490 SystemClock_Config(); 491 492#ifdef RT_USING_COMPONENTS_INIT 493 rt_components_board_init(); 494#endif 495 496#ifdef RT_USING_CONSOLE 497 rt_console_set_device(RT_CONSOLE_DEVICE_NAME); 498#endif 499 500#ifdef BSP_USING_SDRAM 501 rt_system_heap_init((void *)EXT_SDRAM_BEGIN, (void *)EXT_SDRAM_END); 502#else 503 rt_system_heap_init((void *)HEAP_BEGIN, (void *)HEAP_END); 504#endif 505} 506``` 507 508该文件重点关注的就是SystemClock_Config配置,SystemCoreClock的定义在system_gd32f4xx.c中定义的。 509 510(6) 修改bsp/gd32/arm/gd32407v-start/board/Kconfig文件 511修改后内容如下: 512 513```config 514menu "Hardware Drivers Config" 515config SOC_SERIES_GD32F4xx 516 default y 517 518config SOC_GD32407V 519 bool 520 select SOC_SERIES_GD32F4xx 521 select RT_USING_COMPONENTS_INIT 522 select RT_USING_USER_MAIN 523 default y 524 525menu "Onboard Peripheral Drivers" 526 527endmenu 528 529menu "On-chip Peripheral Drivers" 530 531 config BSP_USING_GPIO 532 bool "Enable GPIO" 533 select RT_USING_PIN 534 default y 535 536 menuconfig BSP_USING_UART 537 bool "Enable UART" 538 default y 539 select RT_USING_SERIAL 540 if BSP_USING_UART 541 config BSP_USING_UART1 542 bool "Enable UART1" 543 default y 544 545 config BSP_UART1_RX_USING_DMA 546 bool "Enable UART1 RX DMA" 547 depends on BSP_USING_UART1 && RT_SERIAL_USING_DMA 548 default n 549 endif 550 551 menuconfig BSP_USING_SPI 552 bool "Enable SPI BUS" 553 default n 554 select RT_USING_SPI 555 if BSP_USING_SPI 556 config BSP_USING_SPI1 557 bool "Enable SPI1 BUS" 558 default n 559 560 config BSP_SPI1_TX_USING_DMA 561 bool "Enable SPI1 TX DMA" 562 depends on BSP_USING_SPI1 563 default n 564 565 config BSP_SPI1_RX_USING_DMA 566 bool "Enable SPI1 RX DMA" 567 depends on BSP_USING_SPI1 568 select BSP_SPI1_TX_USING_DMA 569 default n 570 endif 571 572 menuconfig BSP_USING_I2C1 573 bool "Enable I2C1 BUS (software simulation)" 574 default n 575 select RT_USING_I2C 576 select RT_USING_I2C_BITOPS 577 select RT_USING_PIN 578 if BSP_USING_I2C1 579 config BSP_I2C1_SCL_PIN 580 int "i2c1 scl pin number" 581 range 1 216 582 default 24 583 config BSP_I2C1_SDA_PIN 584 int "I2C1 sda pin number" 585 range 1 216 586 default 25 587 endif 588 source "../libraries/gd32_drivers/Kconfig" 589 590endmenu 591 592menu "Board extended module Drivers" 593 594endmenu 595 596endmenu 597``` 598 599这个文件就是配置板子驱动的,这里可根据实际需求添加。 600 601(7) 修改bsp/gd32/arm/gd32407v-start/board/SConscript文件 602 603修改后内容如下: 604 605```python 606import os 607import rtconfig 608from building import * 609 610Import('SDK_LIB') 611 612cwd = GetCurrentDir() 613 614# add general drivers 615src = Split(''' 616board.c 617''') 618 619path = [cwd] 620 621startup_path_prefix = SDK_LIB 622 623if rtconfig.PLATFORM in ['gcc']: 624 src += [startup_path_prefix + '/GD32F4xx_Firmware_Library/CMSIS/GD/GD32F4xx/Source/GCC/startup_gd32f4xx.s'] 625elif rtconfig.PLATFORM in ['armcc', 'armclang']: 626 src += [startup_path_prefix + '/GD32F4xx_Firmware_Library/CMSIS/GD/GD32F4xx/Source/ARM/startup_gd32f4xx.s'] 627elif rtconfig.PLATFORM in ['iccarm']: 628 src += [startup_path_prefix + '/GD32F4xx_Firmware_Library/CMSIS/GD/GD32F4xx/Source/IAR/startup_gd32f4xx.s'] 629 630CPPDEFINES = ['GD32F407'] 631group = DefineGroup('Drivers', src, depend = [''], CPPPATH = path, CPPDEFINES = CPPDEFINES) 632 633Return('group') 634``` 635 636该文件主要添加board文件夹的.c文件和头文件路径。另外根据开发环境选择相应的汇编文件,和前面的libraries的SConscript语法是一样,文件的结构都是类似的,这里就没有注释了。 637 638到这里,基本所有的依赖脚本都配置完成了,接下来将通过menuconfig配置工程。 639 640**5.menuconfig配置** 641关闭套接字抽象层。 642 643 644 645关闭网络设备接口。 646 647 648 649关闭LWIP协议栈。 650 651 652 653GD32407V-START板载没有以太网,因此这里主要是关闭网络相关的内容,当然GD32407V-START的资源丰富,不关这些其实也不影响,如果是其他MCU,根据实际需求自行修改吧。 654 655**6.驱动修改** 656一个基本的BSP中,串口是必不可少的,所以还需要编写串口驱动,这里使用的串口2作为调试串口。 657板子上还有LED灯,主要要编写GPIO驱动即可。 658 659关于串口和LED的驱动可以查看源码,这里就不贴出来了。 660 661**7.应用开发** 662 663笔者在applications的main.c中添加LED的应用代码, 664 665```c 666#include <stdio.h> 667#include <rtthread.h> 668#include <rtdevice.h> 669#include <board.h> 670 671/* defined the LED2 pin: PC6 */ 672#define LED2_PIN GET_PIN(C, 6) 673 674int main(void) 675{ 676 int count = 1; 677 678 /* set LED2 pin mode to output */ 679 rt_pin_mode(LED2_PIN, PIN_MODE_OUTPUT); 680 681 while (count++) 682 { 683 rt_pin_write(LED2_PIN, PIN_HIGH); 684 rt_thread_mdelay(500); 685 rt_pin_write(LED2_PIN, PIN_LOW); 686 rt_thread_mdelay(500); 687 } 688 689 return RT_EOK; 690} 691``` 692 693当然,这需要GPIO驱动的支持。 694 695**8.使用ENV编译工程** 696在env中执行:scons 697 698 699 700编译成功打印信息如下: 701 702 703 704**9.使用env生成MDK工程** 705在env中执行:scons --target=mdk5 706 707 708 709生成MDK工程后,打开MDK工程进行编译 710 711 712 713成功编译打印信息如下: 714 715 716 717### 3.3 使用GD-Link 下载调试GD32 718 719前面使用ENV和MDK成功编译可BSP,那么接下来就是下载调试环节,下载需要下载器,而GD32部分开发板自带GD-link,可以用开发板上自带的GD-link调试仿真代码,不带的可外接GD-link模块,还是很方便的。具体操作方法如下。 720 7211.第一次使用GD-link插入电脑后,会自动安装驱动。 722 723在Options for Target -> Debug 中选择“CMSIS-DAP Debugger”,值得注意的是,只有Keil4.74以上的版本和Keil5才支持CMSIS-DAP Debugger选项。 724 725  726 7272.在Options for Target -> Debug ->Settings勾选SWJ、 Port选择 SW。右框IDcode会出现”0xXBAXXXXX”。 728 729  730 7313.在Options for Target -> Debug ->Settings -> Flash Download中添加GD32的flash算法。 732 733  734 7354.单击下图的快捷方式“debug”, 即可使用GD-Link进行仿真。 736 737  738 739当然啦,也可使用GD-Link下载程序。 740 741  742 743下载程序成功后,打印信息如下: 744 745 746 747接上串口,打印信息如下: 748 749 750 751同时LED会不断闪烁。 752 753### 3.4 RT-Thread studio开发 754 755当然,该工程也可导出使用rt-thread studio开发。 756 757先使用scons --dist导出工程。 758 759 760 761再将工程导入rt-thread studio中 762 763  764 765最后,就可在rt-thread studio就可进行开发工作了。 766 767 768 769## 4. 规范 770 771本章节介绍 RT-Thread GD32 系列 BSP 制作与提交时应当遵守的规范 。开发人员在 BSP 制作完成后,可以根据本规范提出的检查点对制作的 BSP 进行检查,确保 BSP 在提交前有较高的质量 。 772 773### 4.1 BSP 制作规范 774 775GD32 BSP 的制作规范主要分为 3 个方面:工程配置,ENV 配置和 IDE 配置。在已有的 GD32 系列 BSP 的模板中,已经根据下列规范对模板进行配置。在制作新 BSP 的过程中,拷贝模板进行修改时,需要注意的是不要修改这些默认的配置。BSP 制作完成后,需要对新制作的 BSP 进行功能测试,功能正常后再进行代码提交。 776 777下面将详细介绍 BSP 的制作规范。 778 779#### 4.1.1 工程配置 780 781- 遵从RT-Thread 编码规范,代码注释风格统一 782- main 函数功能保持一致 783 - 如果有 LED 的话,main 函数里**只放一个** LED 1HZ 闪烁的程序 784- 在 `rt_hw_board_init` 中需要完成堆的初始化:调用 `rt_system_heap_init` 785- 默认只初始化 GPIO 驱动和 FinSH 对应的串口驱动,不使用 DMA 786- 当使能板载外设驱动时,应做到不需要修改代码就能编译下载使用 787- 提交前应检查 GCC/MDK/IAR 三种编译器直接编译或者重新生成后编译是否成功 788- 使用 `dist` 命令对 BSP 进行发布,检查使用 `dist` 命令生成的工程是否可以正常使用 789 790#### 4.1.2 ENV 配置 791 792- 系统心跳统一设置为 1000(宏:RT_TICK_PER_SECOND) 793- BSP 中需要打开调试选项中的断言(宏:RT_USING_DEBUG) 794- 系统空闲线程栈大小统一设置为 256(宏:IDLE_THREAD_STACK_SIZE) 795- 开启组件自动初始化(宏:RT_USING_COMPONENTS_INIT) 796- 需要开启 user main 选项(宏:RT_USING_USER_MAIN) 797- 默认关闭 libc(宏:RT_USING_LIBC) 798- FinSH 默认只使用 MSH 模式(宏:FINSH_USING_MSH_ONLY) 799 800#### 4.1.3 IDE 配置 801 802- 使能下载代码后自动运行 803- 使能 C99 支持 804- 使能 One ELF Section per Function(MDK) 805- MDK/IAR 生成的临时文件分别放到build下的 MDK/IAR 文件夹下 806- MDK/GCC/IAR 生成 bin 文件名字统一成 rtthread.bin 807 808### 4.2 BSP 提交规范 809 810- 提交前请认真修改 BSP 的 README.md 文件,README.md 文件的外设支持表单只填写 BSP 支持的外设,可参考其他 BSP 填写。查看文档[《GD32 ARM系列驱动介绍》](./GD32 ARM系列驱动介绍.md)了解驱动分类。 811- 提交 BSP 分为 2 个阶段提交: 812 - 第一阶段:基础 BSP 包括串口驱动和 GPIO 驱动,能运行 FinSH 控制台。完成 MDK4、MDK5 、IAR 和 GCC 编译器支持,如果芯片不支持某款编译器(比如MDK4)可以不用做。 BSP 的 README.md 文件需要填写第二阶段要完成的驱动。 813 - 第二阶段:完成板载外设驱动支持,所有板载外设使用 menuconfig 配置后就能直接使用。若开发板没有板载外设,则此阶段可以不用完成。不同的驱动要分开提交,方便 review 和合并。 814- 只提交 BSP 必要的文件,删除无关的中间文件,能够提交的文件请对照其他 BSP。 815- 提交 GD32 不同系列的 Library 库时,请参考 f1/f4 系列的 HAL 库,删除多余库文件 816- 提交前要对 BSP 进行编译测试,确保在不同编译器下编译正常 817- 提交前要对 BSP 进行功能测试,确保 BSP 的在提交前符合工程配置章节中的要求