【GD32F310G-START】移植RT_Thread
[复制链接]
【前言】移植RT_Thread是我的评测项目之一,具体RT_Thread的好处在这里不细说,这里详细讲述如何实现移植。
【移植】
1、将官方提供的例程GPIO_Ruing_LED文件夹复制一份备用,然后重命名01_GPIO_Runing_LED_RT
2、打开例程,然后导出为MDK5.0的工程:
3、再次打开例程,单击图示的地方,配置RTT
4、钩选kernel、shell两个选项,如下图所示:
5、点击OK后,项目自动加入RTOS目录:
6、编辑 board.c,修改 rt_hw_board_init 函数,新增 systick_config()。
修改为:
void rt_hw_board_init(void)
{
#include "systick.h"
systick_config();
#ifdef RT_USING_COMPONENTS_INIT
rt_components_board_init();
#endif
#if defined(RT_USING_USER_MAIN) && defined(RT_USING_HEAP)
rt_system_heap_init(rt_heap_begin_get(), rt_heap_end_get());
#endif
}
7、修改 board.c 中的堆栈大小,避免链接时报错。原来的是:#define RT_HEAP_SIZE (15*1024),现在修改为:#define RT_HEAP_SIZE (4*1024)
8、在 board.c 中新增 SysTick_Handler()
9、注释 gd32f3x0_it.c 中的 HardFault_Handler、PendSV_Handler 和 SysTick_Handler。
10、修改 rtconfig.h,取消#include"finsh_config.h"前的注释
11、修改finish_port.c中的uart0的功能函数:
具体代码下如:
#include <rthw.h>
#include <rtconfig.h>
#include "gd32f3x0.h"
#ifndef RT_USING_FINSH
#error Please uncomment the line <#include "finsh_config.h"> in the rtconfig.h
#endif
#ifdef RT_USING_FINSH
RT_WEAK char rt_hw_console_getchar(void)
{
/* Note: the initial value of ch must < 0 */
int ch = -1;
if(RESET != usart_flag_get(USART0, USART_FLAG_TC) && RESET != usart_flag_get(USART0, USART_FLAG_RBNE))
ch = usart_data_receive(USART0);
return ch;
}
#endif /* RT_USING_FINSH */
12、在rtconfig.h中打开RT串口打印
13、修改 board.c,新增 UART0的初始、发送函数、以及prinf的函数重定向:增加gd32f3x0.h以及stdio.h的头文件引用:
修改后代board.c代码如下:
/*
* Copyright (c) 2006-2019, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2021-05-24 the first version
*/
#include <rthw.h>
#include <rtthread.h>
#include "gd32f3x0.h"
#include <stdio.h>
#if defined(RT_USING_USER_MAIN) && defined(RT_USING_HEAP)
/*
* Please modify RT_HEAP_SIZE if you enable RT_USING_HEAP
* the RT_HEAP_SIZE max value = (sram size - ZI size), 1024 means 1024 bytes
*/
#define RT_HEAP_SIZE (4*1024)
static rt_uint8_t rt_heap[RT_HEAP_SIZE];
RT_WEAK void *rt_heap_begin_get(void)
{
return rt_heap;
}
RT_WEAK void *rt_heap_end_get(void)
{
return rt_heap + RT_HEAP_SIZE;
}
#endif
void rt_os_tick_callback(void)
{
rt_interrupt_enter();
rt_tick_increase();
rt_interrupt_leave();
}
void SysTick_Handler()
{
rt_os_tick_callback();
}
/**
* This function will initial your board.
*/
void rt_hw_board_init(void)
{
#include "systick.h"
systick_config();
#ifdef RT_USING_COMPONENTS_INIT
rt_components_board_init();
#endif
#if defined(RT_USING_USER_MAIN) && defined(RT_USING_HEAP)
rt_system_heap_init(rt_heap_begin_get(), rt_heap_end_get());
#endif
}
#ifdef RT_USING_CONSOLE
void usart0_gpio_config(void)
{
/* enable COM GPIO clock */
rcu_periph_clock_enable(RCU_GPIOA);
/* connect port to USARTx_Tx */
gpio_af_set(GPIOA, GPIO_AF_1, GPIO_PIN_9);
/* connect port to USARTx_Rx */
gpio_af_set(GPIOA, GPIO_AF_1, GPIO_PIN_10);
/* configure USART Tx as alternate function push-pull */
gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_9);
gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, GPIO_PIN_9);
/* configure USART Rx as alternate function push-pull */
gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_10);
gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, GPIO_PIN_10);
}
/*!
\brief configure the USART0
\param[in] none
\param[out] none
\retval none
*/
void usart0_config(void)
{
/* enable USART clock */
rcu_periph_clock_enable(RCU_USART0);
/* USART configure */
usart_deinit(USART0);
usart_word_length_set(USART0, USART_WL_8BIT);
usart_stop_bit_set(USART0, USART_STB_1BIT);
usart_parity_config(USART0, USART_PM_NONE);
usart_baudrate_set(USART0, 115200U);
usart_receive_config(USART0, USART_RECEIVE_ENABLE);
usart_transmit_config(USART0, USART_TRANSMIT_ENABLE);
usart_enable(USART0);
}
/* retarget the C library printf function to the USART */
int fputc(int ch, FILE *f)
{
usart_data_transmit(USART0, (uint8_t) ch);
while(RESET == usart_flag_get(USART0, USART_FLAG_TBE));
return ch;
}
static int uart_init(void)
{
usart0_gpio_config();
usart0_config();
return 0;
}
INIT_BOARD_EXPORT(uart_init);
void rt_hw_console_output(const char *str)
{
printf("%s",str);
}
#endif
14、到这里基本代码修改完毕,编译通过:
15、在main函数引用“rtthread.h"
将delay_1ms替换成rt_thread_mdelay:
16、编译后下载,led闪烁,串口打印如下:
输入free:
到此,RT_Thread移植成功。
|