第一步打开任意能够正常编译下载实现功能的LED灯闪烁的工程模板。这里我用的是江科大的LED灯闪烁的工程模板。
第二步根据这个链接下载RT-Thread压缩包,解压之后双击安装。
https://www.rt-thread.org/download/mdk/RealThread.RT-Thread.3.1.5.pack
第三步查看Keil软件中这个位置和我的是否一致,不一致就是未安装成功,一般不会安装失败。
第四步将你Keil的安装路径下\ARM\PACK\RealThread处的文件夹整体复制并粘贴到你之前找好的工程中。
第五步:将你的工程路径\Fire_RT-Thread\RealThread\3.1.5\bsp\_template下的rtconfig.h与
board.c文件拷贝一份,粘贴在你的工程中的User文件夹下。
第六步://参考野火的放置方式
在开发环境里面新建
rtt/source
和
rtt/ports
两个组文件夹,其中
rtt/source
用
于存放RealThread\3.1.5\
src 文件夹的内容,
rtt/ports 用于存放 RealThread\3.1.5\
libcpu/arm/cortex-m?文件夹的内容,“?”表 示 3
、
4
或者
7
,
如图

第七步:将上述RealThread\3.1.5\bsp\_template、RealThread\3.1.5\
src、RealThread\3.1.5\
libcpu/arm/cortex-m?、RealThread\3.1.5\
libcpu、RealThread\3.1.5\
inlcude、RealThread\3.1.5\components\finsh等在开发环境中指定一下路径

第八步:创建board.h文件
#ifndef __BOARD_H__
#define __BOARD_H__
/* STM32 固件库头文件 */
#include "stm32f10x.h"
/* 开发板硬件 bsp 头文件 */
#include "bsp_led.h"
void rt_hw_board_init(void);
void SysTick_Handler(void);
#endif /* __BOARD_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 "board.h"
#include <rthw.h>
#include <rtthread.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 (15*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();
}
/**
* This function will initial your board.
*/
void rt_hw_board_init(void)
{
//#error "TODO 1: OS Tick Configuration."
// /*
// * TODO 1: OS Tick Configuration
// * Enable the hardware timer and call the rt_os_tick_callback function
// * periodically with the frequency RT_TICK_PER_SECOND.
// */
// /* Call components board initial (use INIT_BOARD_EXPORT()) */
/* 初始化 SysTick *///这一句是加入的
SysTick_Config( SystemCoreClock / RT_TICK_PER_SECOND );
//下面放置硬件初始化
bsp_led_Init();
#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
static int uart_init(void)
{
#error "TODO 2: Enable the hardware uart and config baudrate."
return 0;
}
INIT_BOARD_EXPORT(uart_init);
void rt_hw_console_output(const char *str)
{
#error "TODO 3: Output the string 'str' through the uart."
}
#endif
//这是加入的
/**
* @brief SysTick中断服务函数
* @param 无
* @retval 无
*
* @attention
* SysTick中断服务函数在固件库文件stm32f10x_it.c中也定义了,而现在
* 在board.c中又定义一次,那么编译的时候会出现重复定义的错误,解决
* 方法是可以把stm32f10x_it.c中的注释或者删除即可。
*/
void SysTick_Handler(void)
{
/* 进入中断 */
rt_interrupt_enter();
/* 更新时基 */
rt_tick_increase();
/* 离开中断 */
rt_interrupt_leave();
}
下面就是主函数的测试程序LED灯闪烁
#include "board.h"
#include "rtthread.h"
/*定义线程控制块*/
static struct rt_thread led1_thread;
/* 定义线程控栈时要求 RT_ALIGN_SIZE 个字节对齐 */
ALIGN(RT_ALIGN_SIZE)
/* 定义线程栈 */
static rt_uint8_t rt_led1_thread_stack[1024];
//声明线程函数
static void led1_thread_entry(void* parameter);
int main()
{
//硬件初始化不需要在主函数中显式调用
/*
* 开发板硬件初始化,RTT 系统初始化已经在 main 函数之前完成,
* 即在 component.c 文件中的 rtthread_startup()函数中完成了。
* 所以在 main 函数中,只需要创建线程和启动线程即可。
*/
//初始化线程
rt_thread_init(&led1_thread, /*线程控制块*/
"led1", /*线程名字*/
led1_thread_entry, /*线程入口函数*/
RT_NULL, /*线程入口函数参数*/
&rt_led1_thread_stack[0],/*线程栈起始地址*/
sizeof(rt_led1_thread_stack),/*线程栈大小*/
3, /*线程的优先级*/
20); /*线程时间片*/
//启动线程
rt_thread_startup(&led1_thread);
}
//定义线程函数
static void led1_thread_entry(void* parameter)
{
while(1)
{
bsp_led_move(0);
rt_thread_delay(500);/* 延时 500 个 tick 操作系统中的阻塞延时*/
bsp_led_move(1);
rt_thread_delay(500);/* 延时 500 个 tick */
}
}