【灵动微】FTHR-G0140开发板移植RT-Thread驱动

描述

#申请原创# #有奖活动#

 

【目的】移植RT-Thread nano到FTHR-G0140开发板上,并实现任务的创建与运行。

【开发环境】

MDK5.28

【移植步骤】

1、打开一个可以亮灯的基础例程,这里打开示例的GPIO工程。

2、Nano Pack 安装:我们从官网下载安装文件,RT-Thread Nano 离线安装包下载,下载结束后双击文件进行安装:

 

RT-Thread

3、添加 RT-Thread Nano 到工程,打开已经准备好的可以运行的裸机程序,将 RT-Thread 添加到工程。如下图,点击 Manage Run-Time Environment。

RT-Thread

4、现在可以在 Project 看到 RT-Thread RTOS 已经添加进来了,展开 RTOS,可以看到添加到工程的文件:

RT-Thread

5、适配 RT-Thread Nano

中断与异常处理

RT-Thread 会接管异常处理函数 HardFault_Handler() 和悬挂处理函数 PendSV_Handler(),这两个函数已由 RT-Thread 实现,所以需要删除工程里中断服务例程文件中的这两个函数,避免在编译时产生重复定义。

RT-Thread

系统时钟配置

需要在 board.c 中实现 系统时钟配置(为 MCU、外设提供工作时钟)与 os tick  的配置 (为操作系统提供心跳 / 节拍)。

如下代码所示,用户需要在 board.c 文件中系统初始化和 OS Tick 的配置,cortex-m0 架构使用 SysTick_Handler()

我们修改函数内容如下:

#define SYSCLK_HSI_XXMHz 72000000
void rt_os_tick_callback(void)
{
rt_interrupt_enter();

rt_tick_increase();

rt_interrupt_leave();
}

void SysTick_Handler(void)
{
rt_os_tick_callback();
}
/**
* This function will initial your board.
*/
void rt_hw_board_init(void)
{
SysTick_Config(SYSCLK_HSI_XXMHz/1000);
/*
* 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()) */
#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
}

RT-Thread

同时我们打开rttconfig.h,在Memory Management Configuation中关闭动态内存池管理

RT-Thread

然后我们就可以编译工程了:

Program Size: Code=6560 RO-data=556 RW-data=148 ZI-data=3172
FromELF: creating hex file...
".ObjectsGPIO_LED_Toggle.axf" - 0 Error(s), 0 Warning(s).
Build Time Elapsed: 00:00:02

6、创建两个任务,并启动:

struct rt_thread thread1;
struct rt_thread thread2;

char thread1_stack[512];
char thread2_stack[512];

void thread1_entry(void*param)
{

while (1)
{
printf("thread1 is runningrn");
rt_thread_mdelay(200);
}

}

void thread2_entry(void*param)
{

while (1)
{
printf("thread2is runningrn");
rt_thread_mdelay(400);
}

}

void thread1_init(void)
{

rt_err_t fd=rt_thread_init(&thread1,"thread1",&thread1_entry,0,&thread1_stack[0],sizeof(thread1_stack),10,10);
if(fd < 0)
{
printf("thread1 init is fail rn");
}
else
{
printf("thread1init is success rn");
}
rt_thread_startup(&thread1);
}
void thread2_init(void)
{

rt_err_t fd=rt_thread_init(&thread2,"thread2",&thread2_entry,0,&thread2_stack[0],sizeof(thread2_stack),10,10);
if(fd < 0)
{
printf("thread2 init is fail rn");
}
else
{
printf("thread2init is success rn");
}
rt_thread_startup(&thread2);
}
/***********************************************************************************************************************
* @brief This function is main entrance
* @note main
* @param none
* @retval none
*********************************************************************************************************************/
int main(void)
{
PLATFORM_Init();
thread1_init();
thread2_init();

while (1)
{
}
}

7、实验效果:

编译后下载到开发板,我们就可以看到RT-Thread成功启动了两个任,打印效果如下:

RT-Thread

【总结】作为这款芯片是基于Cortex-M0核,厂家采用了标准的CMSIS结构,使得移植RTT比较成功。

审核编辑 黄宇

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分