lugl4313820 发表于 2022-5-23 19:26

【GD32F310G-START】移植RT_Thread

<p>【前言】移植RT_Thread是我的评测项目之一,具体RT_Thread的好处在这里不细说,这里详细讲述如何实现移植。</p>

<p>【移植】</p>

<p>1、将官方提供的例程GPIO_Ruing_LED文件夹复制一份备用,然后重命名01_GPIO_Runing_LED_RT</p>

<p>&nbsp;&nbsp; 2、打开例程,然后导出为MDK5.0的工程:</p>

<p> &nbsp;&nbsp; 3、再次打开例程,单击图示的地方,配置RTT</p>

<p>&nbsp; 4、钩选kernel、shell两个选项,如下图所示:&nbsp; 5、点击OK后,项目自动加入RTOS目录:</p>

<p>&nbsp; 6、编辑 board.c,修改 rt_hw_board_init 函数,新增 systick_config()。</p>

<p>&nbsp;修改为:</p>

<pre>
<code>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) &amp;&amp; defined(RT_USING_HEAP)
    rt_system_heap_init(rt_heap_begin_get(), rt_heap_end_get());
#endif
}
</code></pre>

<p>7、修改&nbsp;board.c 中的堆栈大小,避免链接时报错。原来的是:#define RT_HEAP_SIZE (15*1024),现在修改为:#define RT_HEAP_SIZE (4*1024)</p>

<p>8、在 board.c 中新增 SysTick_Handler()</p>

<p>&nbsp; 9、注释 gd32f3x0_it.c 中的 HardFault_Handler、PendSV_Handler 和 SysTick_Handler。</p>

<p>&nbsp; 10、修改 rtconfig.h,取消#include&quot;finsh_config.h&quot;前的注释</p>

<p> &nbsp;&nbsp; 11、修改finish_port.c中的uart0的功能函数:</p>

<p> &nbsp;&nbsp;具体代码下如:</p>

<pre>
<code>
#include &lt;rthw.h&gt;
#include &lt;rtconfig.h&gt;
#include "gd32f3x0.h"
#ifndef RT_USING_FINSH
#error Please uncomment the line &lt;#include "finsh_config.h"&gt; in the rtconfig.h
#endif

#ifdef RT_USING_FINSH

RT_WEAK char rt_hw_console_getchar(void)
{
    /* Note: the initial value of ch must &lt; 0 */
    int ch = -1;

    if(RESET != usart_flag_get(USART0, USART_FLAG_TC)&amp;&amp; RESET != usart_flag_get(USART0, USART_FLAG_RBNE))
      ch = usart_data_receive(USART0);

    return ch;
}

#endif /* RT_USING_FINSH */</code></pre>

<p>12、在rtconfig.h中打开RT串口打印</p>

<p>13、修改 board.c,新增 UART0的初始、发送函数、以及prinf的函数重定向:增加gd32f3x0.h以及stdio.h的头文件引用:</p>

<p>&nbsp;修改后代board.c代码如下:</p>

<pre>
<code>/*
* 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 &lt;rthw.h&gt;
#include &lt;rtthread.h&gt;
#include "gd32f3x0.h"
#include &lt;stdio.h&gt;

#if defined(RT_USING_USER_MAIN) &amp;&amp; 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_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) &amp;&amp; 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
    \paramnone
    \param 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

</code></pre>

<p>14、到这里基本代码修改完毕,编译通过:</p>

<p>&nbsp; 15、在main函数引用&ldquo;rtthread.h&quot;</p>

<p>将delay_1ms替换成rt_thread_mdelay:</p>

<p> &nbsp;</p>

<p>&nbsp; 16、编译后下载,led闪烁,串口打印如下:</p>

<p>&nbsp;输入free:</p>

<p>&nbsp; 到此,RT_Thread移植成功。</p>

okhxyyo 发表于 2022-6-16 22:36

<p>有个错别字,我直接给你更正了哈</p>

lugl4313820 发表于 2022-6-17 07:12

okhxyyo 发表于 2022-6-16 22:36
有个错别字,我直接给你更正了哈

<p>多谢多谢!难得老大亲自批阅,以后我会多检查检查!</p>

okhxyyo 发表于 2022-6-17 09:27

lugl4313820 发表于 2022-6-17 07:12
多谢多谢!难得老大亲自批阅,以后我会多检查检查!

<p><img height="48" src="https://bbs.eeworld.com.cn/static/editor/plugins/hkemoji/sticker/facebook/lol.gif" width="48" />打错字总有的,也是看帖子的时候碰巧发现了。</p>
页: [1]
查看完整版本: 【GD32F310G-START】移植RT_Thread