前言:
无意在微信看到了GD做活动,想到了第一时间体验一下,搭配RT-THREAD,也很方便使用。
主要:本文只使用keil进行测试验证,gcc和iar需自行修改
硬件准备:
GD32F427 start开发板+TTL(方便查看shell数据)
移植篇:
我们不需要从头移植,因为rt-thread已经有GD的bsp了,我们将gd32450z-eval复制改名成gd32427v-start
随后将最新的标准库替换掉
因为GD的库API是一样的,我们只需要把芯片差异处修改一下:
Libraries\SConscript:
import rtconfig
from building import *
# get current directory
cwd = GetCurrentDir()
# The set of source files associated with this SConscript file.
src = Glob('GD32F4xx_standard_peripheral/Source/*.c')
src += [cwd + '/CMSIS/GD/GD32F4xx/Source/system_gd32f4xx.c']
#add for startup script
if rtconfig.PLATFORM in ['gcc']:
src += [cwd + '/CMSIS/GD/GD32F4xx/Source/GCC/startup_gd32f407_427.S']
elif rtconfig.PLATFORM in ['armcc', 'armclang']:
src += [cwd + '/CMSIS/GD/GD32F4xx/Source/ARM/startup_gd32f407_427.s']
elif rtconfig.PLATFORM in ['iccarm']:
src += [cwd + '/CMSIS/GD/GD32F4xx/Source/IAR/startup_gd32f407_427.s']
path = [
cwd + '/CMSIS/GD/GD32F4xx/Include',
cwd + '/CMSIS',
cwd + '/GD32F4xx_standard_peripheral/Include',]
if GetDepend(['RT_USING_BSP_USB']):
path += [cwd + '/GD32F4xx_usb_driver/Include']
src += [cwd + '/GD32F4xx_usb_driver/Source']
CPPDEFINES = ['USE_STDPERIPH_DRIVER']
CPPDEFINES += ['GD32F427']
group = DefineGroup('Libraries', src, depend = [''], CPPPATH = path, CPPDEFINES = CPPDEFINES)
Return('group')
board.h:
#ifndef __BOARD_H__
#define __BOARD_H__
#include <gd32f4xx.h>
#define EXT_SDRAM_BEGIN (0xC0000000U) /* the begining address of external SDRAM */
#define EXT_SDRAM_END (EXT_SDRAM_BEGIN + (32U * 1024 * 1024)) /* the end address of external SDRAM */
// <o> Internal SRAM memory size[Kbytes] <8-64>
// <i>Default: 64
#ifdef __ICCARM__
// Use *.icf ram symbal, to avoid hardcode.
extern char __ICFEDIT_region_RAM_end__;
#define GD32_SRAM_END &__ICFEDIT_region_RAM_end__
#else
#define GD32_SRAM_SIZE 192
#define GD32_SRAM_END (0x20000000 + GD32_SRAM_SIZE * 1024)
#endif
#ifdef __CC_ARM
extern int Image$$RW_IRAM1$$ZI$$Limit;
#define HEAP_BEGIN (&Image$$RW_IRAM1$$ZI$$Limit)
#elif __ICCARM__
#pragma section="HEAP"
#define HEAP_BEGIN (__segment_end("HEAP"))
#else
extern int __bss_end;
#define HEAP_BEGIN (&__bss_end)
#endif
#define HEAP_END GD32_SRAM_END
#endif
//*** <<< end of configuration section >>> ***
因为PA9开发板接到了USB_VBUS,所以我们uart0使用PB6 PB7
#ifdef BSP_USING_UART0
{
USART0, // uart peripheral index
USART0_IRQn, // uart iqrn
RCU_USART0, RCU_GPIOB, RCU_GPIOB, // periph clock, tx gpio clock, rt gpio clock
GPIOB, GPIO_AF_7, GPIO_PIN_6, // tx port, tx alternate, tx pin
GPIOB, GPIO_AF_7, GPIO_PIN_7, // rx port, rx alternate, rx pin
&serial0,
"uart0",
},
#endif
之后我们写测试代码:
#include <stdio.h>
#include <rtthread.h>
#include "drv_gpio.h"
#define LED2_PIN GET_PIN(C, 6)
int main(void)
{
rt_pin_mode(LED2_PIN, PIN_MODE_OUTPUT);
while (1)
{
rt_pin_write(LED2_PIN, PIN_LOW);
rt_thread_mdelay(500);
rt_pin_write(LED2_PIN, PIN_HIGH);
rt_thread_mdelay(500);
}
}
编译下载即可
测试结果:
我们可以看到LED0循环闪烁,并且串口0输出控制台