[MM32生态] 【FTHR-G0140】移植RT-Thread

[复制链接]
 楼主| lulugl 发表于 2023-6-5 13:44 | 显示全部楼层 |阅读模式
#申请原创# #有奖活动#
【目的】移植RT-Thread nano到FTHR-G0140开发板上,并实现任务的创建与运行。
【开发环境】
MDK5.28
【移植步骤】
1、打开一个可以亮灯的基础例程,这里打开示例的GPIO工程。
2、Nano Pack 安装:我们从官网下载安装文件,[color=var(--theme-color, #42b983)]RT-Thread Nano 离线安装包下载,下载结束后双击文件进行安装:
packs-install-man.png
3、添加 RT-Thread Nano 到工程,打开已经准备好的可以运行的裸机程序,将 RT-Thread 添加到工程。如下图,点击 Manage Run-Time Environment。
添加nano到工程.png
4、现在可以在 Project 看到 RT-Thread RTOS 已经添加进来了,展开 RTOS,可以看到添加到工程的文件:
添加nano到工程-2.png
5、适配 RT-Thread Nano
中断与异常处理
RT-Thread 会接管异常处理函数 HardFault_Handler() 和悬挂处理函数 PendSV_Handler(),这两个函数已由 RT-Thread 实现,所以需要删除工程里中断服务例程文件中的这两个函数,避免在编译时产生重复定义。

中断处理函数的屏蔽.png
系统时钟配置
需要在 board.c 中实现 系统时钟配置(为 MCU、外设提供工作时钟)与 os tick  的配置 (为操作系统提供心跳 / 节拍)。
如下代码所示,用户需要在 board.c 文件中系统初始化和 OS Tick 的配置,cortex-m0 架构使用 SysTick_Handler()

我们修改函数内容如下:
  1. #define SYSCLK_HSI_XXMHz  72000000
  2. void rt_os_tick_callback(void)
  3. {
  4.     rt_interrupt_enter();
  5.    
  6.     rt_tick_increase();

  7.     rt_interrupt_leave();
  8. }

  9. void SysTick_Handler(void)
  10. {
  11.     rt_os_tick_callback();
  12. }
  13. /**
  14. * This function will initial your board.
  15. */
  16. void rt_hw_board_init(void)
  17. {
  18.     SysTick_Config(SYSCLK_HSI_XXMHz/1000);
  19.     /*
  20.      * TODO 1: OS Tick Configuration
  21.      * Enable the hardware timer and call the rt_os_tick_callback function
  22.      * periodically with the frequency RT_TICK_PER_SECOND.
  23.      */

  24.     /* Call components board initial (use INIT_BOARD_EXPORT()) */
  25. #ifdef RT_USING_COMPONENTS_INIT
  26.     rt_components_board_init();
  27. #endif

  28. #if defined(RT_USING_USER_MAIN) && defined(RT_USING_HEAP)
  29.     rt_system_heap_init(rt_heap_begin_get(), rt_heap_end_get());
  30. #endif
  31. }
修改board.c.png
同时我们打开rttconfig.h,在Memory Management Configuation中关闭动态内存池管理
内存堆取消.png
然后我们就可以编译工程了:
  1. Program Size: Code=6560 RO-data=556 RW-data=148 ZI-data=3172  
  2. FromELF: creating hex file...
  3. ".\Objects\GPIO_LED_Toggle.axf" - 0 Error(s), 0 Warning(s).
  4. Build Time Elapsed:  00:00:02
6、创建两个任务,并启动:
  1. struct rt_thread thread1;
  2. struct rt_thread thread2;

  3. char thread1_stack[512];
  4. char thread2_stack[512];

  5. void thread1_entry(void*param)
  6. {

  7.         while (1)
  8.         {
  9.            printf("thread1 is running\r\n");
  10.            rt_thread_mdelay(200);
  11.         }

  12. }

  13.   void thread2_entry(void*param)
  14. {

  15.         while (1)
  16.         {
  17.               printf("thread2is running\r\n");
  18.               rt_thread_mdelay(400);
  19.         }

  20. }

  21.   void thread1_init(void)
  22. {

  23.        rt_err_t fd=rt_thread_init(&thread1,"thread1",&thread1_entry,0,&thread1_stack[0],sizeof(thread1_stack),10,10);
  24.         if(fd < 0)
  25.         {
  26.                printf("thread1 init is fail \r\n");
  27.         }
  28.         else
  29.         {
  30.               printf("thread1init is success \r\n");
  31.         }
  32.         rt_thread_startup(&thread1);
  33. }
  34. void thread2_init(void)
  35. {

  36.        rt_err_t fd=rt_thread_init(&thread2,"thread2",&thread2_entry,0,&thread2_stack[0],sizeof(thread2_stack),10,10);
  37.         if(fd < 0)
  38.         {
  39.                printf("thread2 init is fail \r\n");
  40.         }
  41.         else
  42.         {
  43.               printf("thread2init is success \r\n");
  44.         }
  45.         rt_thread_startup(&thread2);
  46. }
  47. /***********************************************************************************************************************
  48.   * [url=home.php?mod=space&uid=247401]@brief[/url]  This function is main entrance
  49.   * [url=home.php?mod=space&uid=536309]@NOTE[/url]   main
  50.   * @param  none
  51.   * @retval none
  52.   *********************************************************************************************************************/
  53. int main(void)
  54. {
  55.     PLATFORM_Init();
  56.                 thread1_init();
  57.     thread2_init();
  58.                
  59.     while (1)
  60.     {
  61.     }
  62. }
7、实验效果:
编译后下载到开发板,我们就可以看到RT-Thread成功启动了两个任,打印效果如下:
任务效果.png
【总结】作为这款芯片是基于Cortex-M0核,厂家采用了标准的CMSIS结构,使得移植RTT比较成功。
 楼主| lulugl 发表于 2023-6-5 17:12 | 显示全部楼层
您需要登录后才可以回帖 登录 | 注册

本版积分规则

180

主题

830

帖子

12

粉丝
快速回复 在线客服 返回列表 返回顶部

180

主题

830

帖子

12

粉丝
快速回复 在线客服 返回列表 返回顶部