超详细 STM32移植RT_thread 以及RT-Thread介绍

转载自:https://blog.csdn.net/qq_36958104/article/details/111604665

既然我们要移植Rt-thread   首先就要了解RT-thread

RT-Thread Nano 简介

RT-Thread Nano 是一个极简版的硬实时内核,它是由 C 语言开发,采用面向对象的编程思维,具有良好的代码风格,是一款可裁剪的、抢占式实时多任务的 RTOS。其内存资源占用极小,功能包括任务处理、软件定时器、信号量、邮箱和实时调度等相对完整的实时操作系统特性。适用于家电、消费电子、医疗设备、工控等领域大量使用的 32 位 ARM 入门级 MCU 的场合。

下图是 RT-Thread Nano 的软件框图,包含支持的 CPU 架构与内核源码,还有可拆卸的 FinSH 组件:

架构

支持架构:ARM:Cortex M0/ M3/ M4/ M7 等、RISC-V 及其他。

功能:线程管理、线程间同步与通信、时钟管理、中断管理、内存管理。

 

官网地址

https://www.rt-thread.org/document/site/

 

 源码下载地址

https://www.rt-thread.org/download/nano/rt-thread-3.1.3.zip

源码

 

1 从官网下载RT-Thread源码,里面包含stm32f1xx的例程。https://www.rt-thread.org/page/download.html

建议使用最新的源码。很多功能老版本的代码里面都没有,比如之前使用3.1.2的源码,想使用ADC功能,发现源码里没有这部分,更新到4.0.0就有了,并且4.0版本也是现在官方推荐使用的,配合ENV工具开发很方便,现在RTT的社区有很多软件包了,通过ENV就可以很轻松的使用这些功能。

BSP  文件夹内

 

 

 此版本是基于 HAL 的例程   大家如果熟悉使用HAL库 可直接在此使用

 

下面开始正式的介绍移植过程

本例程是基于标准库的 keil 移植的  

 

https://download.rt-thread.org/download/mdk/RealThread.RT-Thread.3.1.3.pack

Nano Pack 安装

Nano Pack 可以通过在 Keil MDK IDE 内进行安装,也可以手动安装。下面开始介绍两种安装方式。

方法一:在 IDE 内安装

打开 MDK 软件,点击工具栏的 Pack Installer 图标:

Packs 安装

点击右侧的 Pack,展开 Generic,可以找到 RealThread::RT-Thread,点击 Action 栏对应的 Install ,就可以在线安装 Nano Pack 了。另外,如果需要安装其他版本,则需要展开 RealThread::RT-Thread,进行选择。

Packs 管理

方法二:手动安装

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

Packs 手动安装

添加 RT-Thread Nano 到工程

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

MDK RTE

在 Manage Rum-Time Environment 里 "Software Component" 栏找到 RTOS,Variant 栏选择 RT-Thread,然后勾选 kernel,点击 "OK" 就添加 RT-Thread 内核到工程了。

添加 Nano 内核

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

添加了 RTOS 的工程

移植完  打开过程文件是错误的

适配 RT-Thread Nano

中断与异常处理

RT-Thread 会接管异常处理函数 HardFault_Handler() 和悬挂处理函数 PendSV_Handler(),这两个函数已由 RT-Thread 实现,所以需要删除工程里中断服务例程文件中的这两个函数,避免在编译时产生重复定义。如果此时对工程进行编译,没有出现函数重复定义的错误,则不用做修改。

系统时钟配置

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

如下代码所示, HAL_Init() 初始化 HAL 库, SystemClock_Config()配置了系统时钟, SystemCoreClockUpdate() 对系统时钟进行更新,_SysTick_Config() 配置了 OS Tick。此处 OS Tick 使用滴答定时器 systick 实现,需要用户在 board.c 中实现 SysTick_Handler() 中断服务例程,调用 RT-Thread 提供的 rt_tick_increase() ,如下图所示。


   
   
  1. /* board.c */
  2. void rt_hw_board_init()
  3. {
  4. HAL_Init();
  5. SystemClock_Config();
  6. /* System Clock Update */
  7. SystemCoreClockUpdate();
  8. /* System Tick Configuration */
  9. _SysTick_Config(SystemCoreClock / RT_TICK_PER_SECOND);
  10. /* Call components board initial (use INIT_BOARD_EXPORT()) */
  11. #ifdef RT_USING_COMPONENTS_INIT
  12. rt_components_board_init();
  13. #endif
  14. #if defined(RT_USING_USER_MAIN) & & defined(RT_USING_HEAP)
  15. rt_system_heap_init(rt_heap_begin_get(), rt_heap_end_get());
  16. #endif
  17. }

OS Tick 的实现

由于 SysTick_Handler() 中断服务例程由用户在 board.c 中重新实现,做了系统 OS Tick,所以还需要删除工程里中原本已经实现的 SysTick_Handler() ,避免在编译时产生重复定义。如果此时对工程进行编译,没有出现函数重复定义的错误,则不用做修改。

内存堆初始化

系统内存堆的初始化在 board.c 中的 rt_hw_board_init() 函数中完成,内存堆功能是否使用取决于宏 RT_USING_HEAP 是否开启,RT-Thread Nano 默认不开启内存堆功能,这样可以保持一个较小的体积,不用为内存堆开辟空间。

开启系统 heap 将可以使用动态内存功能,如使用 rt_malloc、rt_free 以及各种系统动态创建对象的 API。若需要使用系统内存堆功能,则打开 RT_USING_HEAP 宏定义即可,此时内存堆初始化函数 rt_system_heap_init() 将被调用,如下所示:

系统 heap 初始化

初始化内存堆需要堆的起始地址与结束地址这两个参数,系统中默认使用数组作为 heap,并获取了 heap 的起始地址与结束地址,该数组大小可手动更改,如下所示:

默认 heap 的实现

注意:开启 heap 动态内存功能后,heap 默认值较小,在使用的时候需要改大,否则可能会有申请内存失败或者创建线程失败的情况,修改方法有以下两种:

  • 可以直接修改数组中定义的 RT_HEAP_SIZE 的大小,至少大于各个动态申请内存大小之和,但要小于芯片 RAM 总大小。
  • 也可以参考《RT-Thread Nano 移植原理》——实现动态内存堆 章节进行修改,使用 RAM ZI 段结尾处作为 HEAP 的起始地址,使用 RAM 的结尾地址作为 HEAP 的结尾地址,这是 heap 能设置的最大值的方法。

编写第一个应用

移植好 RT-Thread Nano 之后,则可以开始编写第一个应用代码验证移植结果。此时 main() 函数就转变成 RT-Thread 操作系统的一个线程,现在可以在 main() 函数中实现第一个应用:板载 LED 指示灯闪烁,这里直接基于裸机 LED 指示灯进行修改。

  1. 首先在文件首部增加 RT-Thread 的相关头文件 <rtthread.h> 。
  2. 在 main() 函数中(也就是在 main 线程中)实现 LED 闪烁代码:初始化 LED 引脚、在循环中点亮 / 熄灭 LED。
  3. 将延时函数替换为 RT-Thread 提供的延时函数 rt_thread_mdelay()。该函数会引起系统调度,切换到其他线程运行,体现了线程实时性的特点。

RT-THREAD main

编译程序之后下载到芯片就可以看到基于 RT-Thread 的程序运行起来了,LED 正常闪烁。

注意事项:当添加 RT-Thread 之后,裸机中的 main() 函数会自动变成 RT-Thread 系统中 main 线程 的入口函数。由于线程不能一直独占 CPU,所以此时在 main() 中使用 while(1) 时,需要有让出 CPU 的动作,比如使用 rt_thread_mdelay() 系列的函数让出 CPU。

与裸机 LED 闪烁应用代码的不同

1). 延时函数不同: RT-Thread 提供的 rt_thread_mdelay() 函数可以引起操作系统进行调度,当调用该函数进行延时时,本线程将不占用 CPU,调度器切换到系统的其他线程开始运行。而裸机的 delay 函数是一直占用 CPU 运行的。

2). 初始化系统时钟的位置不同:移植好 RT-Thread Nano 之后,不需要再在 main() 中做相应的系统配置(如 hal 初始化、时钟初始化等),这是因为 RT-Thread 在系统启动时,已经做好了系统时钟初始化等的配置,这在上一小节 “系统时钟配置” 中有讲解。

配置 RT-Thread Nano

用户可以根据自己的需要通过修改 rtconfig.h 文件里面的宏定义配置相应功能。

RT-Thread Nano 默认未开启宏 RT_USING_HEAP,故只支持静态方式创建任务及信号量。若要通过动态方式创建对象则需要在 rtconfig.h 文件里开启 RT_USING_HEAP 宏定义。

MDK 的配置向导 configuration Wizard 可以很方便的对工程进行配置,Value 一栏可以选中对应功能及修改相关值,等同于直接修改配置文件 rtconfig.h。更多细节配置详见 《 RT-Thread Nano 配置》

 

实现动态内存堆

RT-Thread Nano 默认不开启动态内存堆功能,开启 RT_USING_HEAP 将可以使用动态内存功能,即可以使用 rt_malloc、rt_free 以及各种系统动态创建对象的 API。动态内存堆管理功能的初始化是通过 rt_system_heap_init() 函数完成的,动态内存堆的初始化需要指定堆内存的起始地址和结束地址,函数原型如下:

void rt_system_heap_init(void *begin_addr, void *end_addr)
   
   

开启 RT_USING_HEAP 后,系统默认使用数组作为 heap,heap 的起始地址与结束地址作为参数传入 heap 初始化函数,heap 初始化函数 rt_system_heap_init() 将在 rt_hw_board_init() 中被调用。

开启 heap 后,系统中默认使用数组作为 heap(heap 默认较小,实际使用时请根据芯片 RAM 情况改大),获得的 heap 的起始地址与结束地址,作为参数传入 heap 初始化函数:


   
   
  1. #define RT_HEAP_SIZE 1024
  2. static uint32_t rt_heap[RT_HEAP_SIZE];
  3. RT_WEAK void *rt_heap_begin_get(void)
  4. {
  5. return rt_heap;
  6. }
  7. RT_WEAK void *rt_heap_end_get(void)
  8. {
  9. return rt_heap + RT_HEAP_SIZE;
  10. }
  11. void rt_hw_board_init(void)
  12. {
  13. ....
  14. #if defined(RT_USING_USER_MAIN) && defined(RT_USING_HEAP)
  15. rt_system_heap_init(rt_heap_begin_get(), rt_heap_end_get()); //传入 heap 的起始地址与结束地址
  16. #endif
  17. ....
  18. }

如果不想使用数组作为动态内存堆,则可以重新指定系统 HEAP 的大小,例如使用 RAM ZI 段结尾处作为 HEAP 的起始地址(这里需检查与链接脚本是否对应),使用 RAM 的结尾地址作为 HEAP 的结尾地址,这样可以将空余RAM 全部作为动态内存 heap 使用。如下示例重新定义了 HEAP 的起始地址与结尾地址,并作为初始化参数进行系统 HEAP 初始化。


   
   
  1. #define STM32_SRAM1_START (0x20000000)
  2. #define STM32_SRAM1_END (STM32_SRAM1_START + 20 * 1024) // 结束地址 = 0x20000000(基址) + 20K(RAM大小)
  3. #if defined(__CC_ARM) || defined(__CLANG_ARM)
  4. extern int Image$$RW_IRAM1$$ZI$$Limit; // RW_IRAM1,需与链接脚本中运行时域名相对应
  5. #define HEAP_BEGIN ((void *)&Image$$RW_IRAM1$$ZI$$Limit)
  6. #endif
  7. #define HEAP_END STM32_SRAM1_END

   
   
  1. void rt_hw_board_init(void)
  2. {
  3. ....
  4. #if defined(RT_USING_USER_MAIN) && defined(RT_USING_HEAP)
  5. rt_system_heap_init(( void *)HEAP_BEGIN, ( void *)HEAP_END);
  6. #endif
  7. ....
  8. }

 

链接脚本

链接脚本,也称分散加载文件,决定在生成 image 文件时如何来分配相关数据的存放基址,如果不指定特定的链接脚本,连接器就会自动采用默认的链接脚本来生成镜像。

举例 stm32 在 KEIL MDK 开发环境下的链接脚本文件 xxx.sct:


   
   
  1. LR_IROM1 0x08000000 0x00020000 { ; load region size_region
  2. ER_IROM1 0x08000000 0x00020000 { ; load address = execution address
  3. *.o (RESET, +First)
  4. *(InRoot$$Sections)
  5. .ANY (+RO)
  6. }
  7. RW_IRAM1 0x20000000 0x00005000 { ; RW data
  8. .ANY (+RW +ZI)
  9. }
  10. }

其中 RW_IRAM1 0x20000000 0x00005000 表示定义一个运行时域 RW_IRAM1(默认域名),域基址为 0x20000000,域大小为 0x00005000(即 20K ),对应实际 RAM 大小。.ANY (+RW +ZI) 表示加载所有匹配目标文件的可读写数据 RW-Data、清零数据 ZI-Data。所以运行时所占内存的结尾处就是 ZI 段结尾处,可以将 ZI 结尾处之后的内存空间作为系统动态内存堆使用。

 

 

Nano 配置

获取示例代码

Keil MDK 中集成的 RT-Thread Nano 软件包附带示例代码,如果需要参照示例代码,则可以在 Keil 中打开相应的示例代码工程。

首先点击 Pack Installer,进入下图所示界面:

在 keil 中打开示例代码

右侧界面切换到 Examples,然后在左侧界面搜索 Device 或者 Boards,点击搜索出的芯片或者开发板,会显示与其相关的所有示例代码,同时可以看到 RT-Thread 的示例代码也在其中,点击 Copy,选择一个路径,然后点击 OK 即可打开示例代码工程

 

打开 keil  的安装路径 将  RT-Thread Package 到裸机工程根目录

 

1、拷贝 rtconfig.h 文件到 user 文件夹
将 RT-Thread/3.0.3/bsp 文件夹下面的 rtconfig.h 文件拷贝到工程根目录下面的 user文件夹, 可以通过修改这个 RT-Thread 内核的配置头文件来裁剪 RT-Thread 的功能

2、拷贝 board.c 文件到 user 文件夹下(新建RTE )
将 RT-Thread/3.0.3/bsp 文件夹下面的 board.c 文件拷贝到工程根目录下面的 user 文件夹, 等下我们需要对这个 board.c 进行修改。

 

 

 

 

3、添加 RT-Thread 源码到工程组文件夹
新建 rtt/source 和 rtt/cpu 两个组文件夹,其中 rtt/source 用于存放 src 文件夹的内容, rtt/cpu用于存放 libcpu/arm/cortex-m? 文件夹的内容,“?”表示0  3、 4 或者 7。内核文件   我们移植的为stm32f103  内核选择  Cortex-M3

指定 RT-Thread 头文件的路径
RT-Thread 的 源 码 里 面 只 有

RT-Thread\3.1.3\include\libcpu

RTThread\3.1.3\include

RTThread\3.1.3\src

RTThread\3.1.3\components\finsh

和 user 文件夹下(RTE) rtconfig.h 有头文件,只需要将这头文件的路径在开发环境里面指定即可。


 

这些都做完之后   编译还是有两个错误 

因为还没有配置 RT-Thread Nano

参考上面讲述的配置 步骤

 

 

 

rtconfig.H

 

 


   
   
  1. /* RT-Thread config file */
  2. #ifndef __RTTHREAD_CFG_H__
  3. #define __RTTHREAD_CFG_H__
  4. #include "RTE_Components.h"
  5. // <<< Use Configuration Wizard in Context Menu >>>
  6. // <h>Basic Configuration
  7. // <o>Maximal level of thread priority <8-256>
  8. // <i>Default: 32
  9. #define RT_THREAD_PRIORITY_MAX 32
  10. // <o>OS tick per second
  11. // <i>Default: 1000 (1ms)
  12. #define RT_TICK_PER_SECOND 1000
  13. // <o>Alignment size for CPU architecture data access
  14. // <i>Default: 4
  15. #define RT_ALIGN_SIZE 4
  16. // <o>the max length of object name<2-16>
  17. // <i>Default: 8
  18. #define RT_NAME_MAX 8
  19. // <c1>Using RT-Thread components initialization
  20. // <i>Using RT-Thread components initialization
  21. #define RT_USING_COMPONENTS_INIT
  22. // </c>
  23. // <c1>Using user main
  24. // <i>Using user main
  25. #define RT_USING_USER_MAIN
  26. // </c>
  27. // <o>the size of main thread<1-4086>
  28. // <i>Default: 512
  29. #define RT_MAIN_THREAD_STACK_SIZE 256
  30. // </h>
  31. // <h>Debug Configuration
  32. // <c1>enable kernel debug configuration
  33. // <i>Default: enable kernel debug configuration
  34. //#define RT_DEBUG
  35. // </c>
  36. // <o>enable components initialization debug configuration<0-1>
  37. // <i>Default: 0
  38. #define RT_DEBUG_INIT 0
  39. // <c1>thread stack over flow detect
  40. // <i> Diable Thread stack over flow detect
  41. //#define RT_USING_OVERFLOW_CHECK
  42. // </c>
  43. // </h>
  44. // <h>Hook Configuration
  45. // <c1>using hook
  46. // <i>using hook
  47. //#define RT_USING_HOOK
  48. // </c>
  49. // <c1>using idle hook
  50. // <i>using idle hook
  51. //#define RT_USING_IDLE_HOOK
  52. // </c>
  53. // </h>
  54. // <e>Software timers Configuration
  55. // <i> Enables user timers
  56. #define RT_USING_TIMER_SOFT 0
  57. #if RT_USING_TIMER_SOFT == 0
  58. #undef RT_USING_TIMER_SOFT
  59. #endif
  60. // <o>The priority level of timer thread <0-31>
  61. // <i>Default: 4
  62. #define RT_TIMER_THREAD_PRIO 4
  63. // <o>The stack size of timer thread <0-8192>
  64. // <i>Default: 512
  65. #define RT_TIMER_THREAD_STACK_SIZE 512
  66. // <o>The soft-timer tick per second <0-1000>
  67. // <i>Default: 100
  68. #define RT_TIMER_TICK_PER_SECOND 1000
  69. // </e>
  70. // <h>IPC(Inter-process communication) Configuration
  71. // <c1>Using Semaphore
  72. // <i>Using Semaphore
  73. #define RT_USING_SEMAPHORE
  74. // </c>
  75. // <c1>Using Mutex
  76. // <i>Using Mutex
  77. //#define RT_USING_MUTEX
  78. // </c>
  79. // <c1>Using Event
  80. // <i>Using Event
  81. //#define RT_USING_EVENT
  82. // </c>
  83. // <c1>Using MailBox
  84. // <i>Using MailBox
  85. //#define RT_USING_MAILBOX
  86. // </c>
  87. // <c1>Using Message Queue
  88. // <i>Using Message Queue
  89. //#define RT_USING_MESSAGEQUEUE
  90. // </c>
  91. // </h>
  92. // <h>Memory Management Configuration
  93. // <c1>Using Memory Pool Management
  94. // <i>Using Memory Pool Management
  95. //#define RT_USING_MEMPOOL
  96. // </c>
  97. // <c1>Dynamic Heap Management
  98. // <i>Dynamic Heap Management
  99. #define RT_USING_HEAP
  100. // </c>
  101. // <c1>using small memory
  102. // <i>using small memory
  103. #define RT_USING_SMALL_MEM
  104. // </c>
  105. // <c1>using tiny size of memory
  106. // <i>using tiny size of memory
  107. //#define RT_USING_TINY_SIZE
  108. // </c>
  109. // </h>
  110. // <h>Device System Configuration
  111. // <c1>Using Device System
  112. // <i>Using Device System
  113. //#define RT_USING_DEVICE
  114. // </c>
  115. // <c1>Using device communication
  116. // <i>Using device communication
  117. //#define RT_USING_DEVICE_IPC
  118. // </c>
  119. // <c1>Using Serial
  120. // <i>Using Serial
  121. //#define RT_USING_SERIAL
  122. // </c>
  123. // </h>
  124. // <h>Console Configuration
  125. // <c1>Using console
  126. // <i>Using console
  127. #define RT_USING_CONSOLE
  128. // </c>
  129. // <o>the buffer size of console <1-1024>
  130. // <i>the buffer size of console
  131. // <i>Default: 128 (128Byte)
  132. #define RT_CONSOLEBUF_SIZE 128
  133. // <s>The device name for console
  134. // <i>The device name for console
  135. // <i>Default: uart1
  136. #define RT_CONSOLE_DEVICE_NAME "uart1"
  137. // </h>
  138. #if defined(RTE_FINSH_USING_FINSH) || defined(RTE_FINSH_USING_MSH)
  139. #define RT_USING_FINSH
  140. // <h>Finsh Configuration
  141. // <o>the priority of finsh thread <1-7>
  142. // <i>the priority of finsh thread
  143. // <i>Default: 6
  144. #define __FINSH_THREAD_PRIORITY 5
  145. #define FINSH_THREAD_PRIORITY (RT_THREAD_PRIORITY_MAX / 8 * __FINSH_THREAD_PRIORITY + 1)
  146. // <o>the stack of finsh thread <1-4096>
  147. // <i>the stack of finsh thread
  148. // <i>Default: 4096 (4096Byte)
  149. #define FINSH_THREAD_STACK_SIZE 512
  150. // <o>the history lines of finsh thread <1-32>
  151. // <i>the history lines of finsh thread
  152. // <i>Default: 5
  153. #define FINSH_HISTORY_LINES 1
  154. // <c1>Using symbol table in finsh shell
  155. // <i>Using symbol table in finsh shell
  156. #define FINSH_USING_SYMTAB
  157. // </c>
  158. // </h>
  159. #endif
  160. #if defined(RTE_FINSH_USING_MSH)
  161. #define FINSH_USING_MSH
  162. #endif
  163. #if !defined(RTE_FINSH_USING_FINSH) && defined(RTE_FINSH_USING_MSH)
  164. #define FINSH_USING_MSH_ONLY
  165. #endif
  166. // <<< end of configuration section >>>
  167. #define RT_USING_UART
  168. #define RT_USING_UART1
  169. #endif

board.c


   
   
  1. /*
  2. * File : application.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2017-07-24 Tanek the first version
  13. */
  14. #include <rthw.h>
  15. #include <rtthread.h>
  16. #include "usart.h"
  17. #include "delay.h"
  18. #include "led.h"
  19. // rtthread tick configuration
  20. // 1. include header files
  21. // 2. configure rtos tick and interrupt
  22. // 3. add tick interrupt handler
  23. // rtthread tick configuration
  24. // 1. include some header file as need
  25. #include <stm32f10x.h>
  26. #ifdef __CC_ARM
  27. extern int Image$$RW_IRAM1$$ZI$$Limit;
  28. #define HEAP_BEGIN (&Image$$RW_IRAM1$$ZI$$Limit)
  29. #elif __ICCARM__
  30. #pragma section="HEAP"
  31. #define HEAP_BEGIN (__segment_end("HEAP"))
  32. #else
  33. extern int __bss_end;
  34. #define HEAP_BEGIN (&__bss_end)
  35. #endif
  36. #define SRAM_SIZE 8
  37. #define SRAM_END (0x20000000 + SRAM_SIZE * 1024)
  38. extern uint8_t OSRunning;
  39. /**
  40. * This function will initial STM32 board.
  41. */
  42. void rt_hw_board_init()
  43. {
  44. // rtthread tick configuration
  45. // 2. Configure rtos tick and interrupt
  46. SysTick_Config(SystemCoreClock / RT_TICK_PER_SECOND);
  47. //串口初始化
  48. uart_init( 115200);
  49. delay_init( 72);
  50. //初始化LED
  51. LED_Init();
  52. //tips:把硬件初始化放上面
  53. OSRunning= 1;
  54. /* Call components board initial (use INIT_BOARD_EXPORT()) */
  55. #ifdef RT_USING_COMPONENTS_INIT
  56. rt_components_board_init();
  57. #endif
  58. #if defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE)
  59. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  60. #endif
  61. #if defined(RT_USING_USER_MAIN) && defined(RT_USING_HEAP)
  62. rt_system_heap_init(( void*)HEAP_BEGIN, ( void*)SRAM_END);
  63. #endif
  64. }
  65. // rtthread tick configuration
  66. // 3. add tick interrupt handler
  67. void SysTick_Handler( void)
  68. {
  69. /* enter interrupt */
  70. rt_interrupt_enter();
  71. rt_tick_increase();
  72. /* leave interrupt */
  73. rt_interrupt_leave();
  74. }

 

修改完rtconfig.h 和board.c  编译通过

 

 

 

移植好的工程文件

链接:https://pan.baidu.com/s/1Jrxe9AtDGs9q2ZIF-mXd9A
提取码:lchq
复制这段内容后打开百度网盘手机App,操作更方便哦

 

 

 

  • 9
    点赞
  • 76
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
以下是基于STM32RT-Thread Nano系统的多线程流水灯项目参考代码: ```c #include "rtthread.h" #include "stm32f10x.h" #define LED1_PIN GPIO_Pin_0 #define LED2_PIN GPIO_Pin_1 #define LED3_PIN GPIO_Pin_2 #define LED4_PIN GPIO_Pin_3 static rt_thread_t led1_thread = RT_NULL; static rt_thread_t led2_thread = RT_NULL; static rt_thread_t led3_thread = RT_NULL; static rt_thread_t led4_thread = RT_NULL; static void led1_thread_entry(void *parameter) { while(1) { GPIO_ResetBits(GPIOC, LED1_PIN); rt_thread_delay(100); GPIO_SetBits(GPIOC, LED1_PIN); rt_thread_delay(100); } } static void led2_thread_entry(void *parameter) { while(1) { GPIO_ResetBits(GPIOC, LED2_PIN); rt_thread_delay(200); GPIO_SetBits(GPIOC, LED2_PIN); rt_thread_delay(200); } } static void led3_thread_entry(void *parameter) { while(1) { GPIO_ResetBits(GPIOC, LED3_PIN); rt_thread_delay(300); GPIO_SetBits(GPIOC, LED3_PIN); rt_thread_delay(300); } } static void led4_thread_entry(void *parameter) { while(1) { GPIO_ResetBits(GPIOC, LED4_PIN); rt_thread_delay(400); GPIO_SetBits(GPIOC, LED4_PIN); rt_thread_delay(400); } } void rt_init_thread_entry(void* parameter) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin = LED1_PIN | LED2_PIN | LED3_PIN | LED4_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOC, &GPIO_InitStructure); led1_thread = rt_thread_create("led1", led1_thread_entry, RT_NULL, 512, 20, 5); if(led1_thread != RT_NULL) { rt_thread_startup(led1_thread); } led2_thread = rt_thread_create("led2", led2_thread_entry, RT_NULL, 512, 20, 5); if(led2_thread != RT_NULL) { rt_thread_startup(led2_thread); } led3_thread = rt_thread_create("led3", led3_thread_entry, RT_NULL, 512, 20, 5); if(led3_thread != RT_NULL) { rt_thread_startup(led3_thread); } led4_thread = rt_thread_create("led4", led4_thread_entry, RT_NULL, 512, 20, 5); if(led4_thread != RT_NULL) { rt_thread_startup(led4_thread); } while(1) { rt_thread_delay(1000); } } ``` 这段代码实现了四个LED灯的流水效果,每个LED灯的流水速度不同,通过四个线程来控制每个LED灯的流水效果,每个线程通过调用rt_thread_delay函数来控制流水速度。主线程只是用来启动四个LED灯的线程,然后进入一个无限循环等待。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值