RTT-移植Nano
一、准备工作
STM32F103模板工程 RTT-nano源码 https://www.rt-thread.org/document/site/#/rt-thread-version/rt-thread-nano/an0038-nano-introduction
二、增减文件
rt-thread\bsp里只保留board.c
和rtconfig.h
rt-thread\libcpu里只保留cortex-m3
在MDK的工程文件路径里加入:
rt-thread\bsp
rt-thread\components\finsh
rt-thread\include
rt-thread\include\libc
在MDK的工程文件Groups中添加RTT/src
,加入rt-thread\src
文件夹中的所有*.c文件 在MDK的工程文件Groups中添加RTT/port
,加入rt-thread\libcpu\cortex-m3
文件夹中context_rvds.S
和cpuport.c
以及rt-thread\bsp
中的rtconfig.h
三、修改文件
注释rtconfig.h
中的#include “RTE_Components.h” 编译会发现报错,解决方法是将stm32f10x_it.c
文件中的这个三个中断函数注释掉
linking...
.\Output\F103xE.axf: Error: L6200E: Symbol HardFault_Handler multiply defined (by context_rvds.o and stm32f10x_it.o).
.\Output\F103xE.axf: Error: L6200E: Symbol PendSV_Handler multiply defined (by context_rvds.o and stm32f10x_it.o).
.\Output\F103xE.axf: Error: L6200E: Symbol SysTick_Handler multiply defined (by board.o and bsp_systick.o).
Not enough information to list image symbols.
Not enough information to list load addresses in the image map.
Finished: 2 information, 0 warning and 3 error messages.
".\Output\F103xE.axf" - 3 Error(s), 0 Warning(s).
修改rtconfig.h
,取消#define RT_USING_HEAP
这一行的注释,开启动态堆栈 新建board.h,修改board.c
# ifndef __BOARD_H__
# define __BOARD_H__
# include "stm32f10x.h"
# include "bsp_uart.h"
void rt_hw_board_init ( void ) ;
void SysTick_Handler ( void ) ;
# endif
# include "board.h"
# include <rthw.h>
# include <rtthread.h>
# if defined ( RT_USING_USER_MAIN) && defined ( RT_USING_HEAP)
# define RT_HEAP_SIZE 2048
static uint32_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_hw_board_init ( )
{
SysTick_Config ( SystemCoreClock / RT_TICK_PER_SECOND ) ;
# ifdef RT_USING_COMPONENTS_INIT
rt_components_board_init ( ) ;
# endif
# if defined ( RT_USING_CONSOLE) && defined ( RT_USING_DEVICE)
rt_console_set_device ( RT_CONSOLE_DEVICE_NAME) ;
# endif
# if defined ( RT_USING_USER_MAIN) && defined ( RT_USING_HEAP)
rt_system_heap_init ( rt_heap_begin_get ( ) , rt_heap_end_get ( ) ) ;
# endif
}
void SysTick_Handler ( void )
{
rt_interrupt_enter ( ) ;
rt_tick_increase ( ) ;
rt_interrupt_leave ( ) ;
}
四、适配rt_printf
在void rt_hw_board_init()
中加入相应串口初始化 在board.c
中适配void rt_hw_console_output(const char *str)
void rt_hw_console_output ( const char * str)
{
rt_enter_critical ( ) ;
while ( * str!= '\0' )
{
USART_SendData ( DEBUG_USARTx, * str++ ) ;
while ( USART_GetFlagStatus ( DEBUG_USARTx, USART_FLAG_TXE) == RESET) ;
}
rt_exit_critical ( ) ;
}
编写测试线程
# include "board.h"
# include "rtthread.h"
static rt_thread_t test_thread = RT_NULL;
static void test_thread_entry ( void * parameter)
{
while ( 1 )
{
rt_kprintf ( "test code!\n" ) ;
rt_thread_delay ( 1000 ) ;
}
}
int main ( void )
{
test_thread =
rt_thread_create ( "test" ,
test_thread_entry,
RT_NULL,
512 ,
3 ,
20 ) ;
if ( test_thread != RT_NULL)
rt_thread_startup ( test_thread) ;
else
return - 1 ;
}
五、 适配Finsh
在MDK的工程文件Groups中添加RTT/components
,加入rt-thread\components\finsh
文件夹中的 cmd.c
、msh.c
、shell.c
、 finsh_port.c
适配finsh_port.c
中的char rt_hw_console_getchar(void)
char rt_hw_console_getchar ( void )
{
int ch = - 1 ;
if ( USART_GetFlagStatus ( DEBUG_USARTx, USART_FLAG_RXNE) != RESET)
{
ch = ( int ) USART_ReceiveData ( DEBUG_USARTx) ;
USART_ClearFlag ( DEBUG_USARTx, USART_FLAG_RXNE) ;
}
else
{
if ( USART_GetFlagStatus ( DEBUG_USARTx, USART_FLAG_ORE) != RESET)
{
USART_ClearFlag ( DEBUG_USARTx, USART_FLAG_ORE) ;
}
rt_thread_mdelay ( 10 ) ;
}
return ch;
}
在rtconfig.h
中加入#include "finsh_config.h"