【首发】从搭建环境开始,教你如何在小熊派上跑起来ThreadX操作系统!
![](https://i-blog.csdnimg.cn/blog_migrate/74f87026e29f431da716fb4307ab823d.png)
![](https://i-blog.csdnimg.cn/blog_migrate/91b19fe6a85cef08d9c1a2daabc423ad.png)
![](https://i-blog.csdnimg.cn/blog_migrate/13fe6059a18e0838393d1a0c72e0b4df.png)
![](https://i-blog.csdnimg.cn/blog_migrate/90ec421c5f9cb3a0c70857b98bd49de9.png)
1. 前言
微软于最近开源了 ThreadX 操作系统,关于这个RTOS有多牛逼,请看硬汉哥的这篇文章:
本文中使用的开发板为小熊派IoT开发板,主控为STM32L431RCT6:
由于 ThreadX 仅提供了 Cortex-M0 到 Cortex-M7 的 GCC版本的移植文件,所以本文中我们使用的工具链为:
- 操作系统:Window10
- 生成初始化工程:STM32CubeMX
- 阅读代码:VSCode
- 编译器:arm-none-eabi-gcc工具链
- 构建工具:make
- 下载工具:OpenOCD
- 串口工具
以上工具除了 STM32CubeMX 和 VScode 之外,华为最近出的一款VScode扩展中可以打包安装完成。
2. 环境搭建
2.1. 安装VScode扩展
这个扩展是华为LiteOS提供的,此处不需要了解过多,我们仅仅是使用此扩展完成以下事情:
- 自动安装arm-none-eabi-gccG工具链
- 自动安装make构建工具
- 自动安装openocd下载工具
- 可以在VScode中一键编译、下载、调试
- 可以在VScode中查看串口输出
接下来开始安装:
安装完成之后重启VScode即可。
iot-link扩展只支持小熊派开发板的一键编译、下载,如果是其它开发板:
- ① 将arm-none-eabi-gcc和make工具添加到环境变量,在命令行编译;
- ② 使用ST-Link的下载软件或者STM32cubeProg下载程序;
- ③ 串口助手可以正常使用;
2.2. 创建STM32CubeMX工程
使用STM32CubeMX创建一个基于小熊派开发板的裸机工程,只需要配置一个打印串口和正确的时钟频率(图省略)即可:
工程设置中选择Makefile,这样cubemx可以自动生成makefile:
生成工程即可:
2.3. 导入工程到VScode
打开VScode,点击下方的Home(第2节扩展安装成功才会有),选择导入GCC工程:
工程目录选择刚刚生成的目录,Makefile默认是此工程中的makefile,芯片选择STM32L431RCT6:
导入成功之后可以看到下方的操作按钮:
2.4. 测试裸机工程是否正常
打开usart.c
,添加将printf重定向到串口1的代码:
<span style="color:#000000"><code class="language-c"><span style="color:#708090">/* USER CODE BEGIN 1 */</span>
<span style="color:#986801">#<span style="color:#0077aa">if</span> 1</span>
<span style="color:#986801">#<span style="color:#0077aa">include</span> <span style="color:#50a14f"><stdio.h></span></span>
<span style="color:#0077aa">int</span> <span style="color:#dd4a68">_write</span><span style="color:#999999">(</span><span style="color:#0077aa">int</span> fd<span style="color:#999999">,</span> <span style="color:#0077aa">char</span> <span style="color:#a67f59">*</span>ptr<span style="color:#999999">,</span> <span style="color:#0077aa">int</span> len<span style="color:#999999">)</span>
<span style="color:#999999">{</span>
<span style="color:#dd4a68">HAL_UART_Transmit</span><span style="color:#999999">(</span><span style="color:#a67f59">&</span>huart1<span style="color:#999999">,</span> <span style="color:#999999">(</span>uint8_t<span style="color:#a67f59">*</span><span style="color:#999999">)</span>ptr<span style="color:#999999">,</span> len<span style="color:#999999">,</span> <span style="color:#986801">0xFFFF</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#0077aa">return</span> len<span style="color:#999999">;</span>
<span style="color:#999999">}</span>
<span style="color:#986801">#<span style="color:#0077aa">endif</span></span>
<span style="color:#708090">/* USER CODE END 1 */</span>
</code></span>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
接着在main.c
中添加头文件<stdio.h>
,然后在main函数中加入打印代码进行测试:
<span style="color:#000000"><code class="language-c"><span style="color:#708090">/* USER CODE BEGIN 2 */</span>
<span style="color:#dd4a68">printf</span><span style="color:#999999">(</span><span style="color:#50a14f">"ThreadX RTOS Port by Mculover666\r\n"</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#708090">/* USER CODE END 2 */</span>
</code></span>
- 1
- 2
- 3
- 4
- 5
点击下方的Build按钮开始编译,编译成功之后如图所示:
如果编译失败,请重复之前的导入工程步骤。
接下来连接小熊派开发板到电脑,点击下载按钮:
下载成功之后,点击下方的Serial按钮,选择小熊派开发板的串口,在VSCode中打开串口终端:
按下小熊派复位按钮,即可看到正常打印的数据:
printf测试可以正常使用之后,接下来开始移植今天的主角——threadX操作系统。
3. 移植threadX操作系统
3.1. 下载内核源码
内核源码可以在官方的GIthub下载:
如果下载较慢,可以拉取我的Gitee:
将源码中的common和ports文件夹复制到工程中:
3.2. 修改makefile
makefile文件是make工具使用的文件,描述了整个工程的编译构建关系。
修改makefile,将threadX的相关文件加入到makefile里。
① 添加common/src
下的所有C文件到C_SOURCES
变量中:
② 小熊派的内核是Cortex-M4,所以添加ports/cortex_m4/gnu/src
下的所有.S文件:
因为.s
文件是直接汇编文件,.S
文件需要进行预处理之后才能汇编,两者编译时有区别,所以使用两个变量进行区分。
③ 将common/inc
和ports/cortex_m4/gnu/inc
两个头文件路径添加:
④ 编写.S
文件的编译规则:
至此,makefile修改完成,但是还不能编译。
3.3. 适配小熊派开发板
在threadX底层初始化汇编文件中有两个全局变量:
这两个值需要我们根据不同的平台来自己定义。
① 修改stm32启动文件startup_stm32l431xx.s
,声明中断向量表_vectors标号是全局的:
将此标号位置添加到中断向量表处:
② 修改stm32链接文件STM32L431RCTx_FLASH.ld
,添加此标号所表示的位置:
3.4. 修改时钟频率
找到threadX的底层初始化汇编文件tx_initialize_low_level_sample.S
,修改系统主频为80Mhz,修改系统tick为1000个tick:
此时点击下方Build按钮开始编译:
编译成功,证明移植没有问题。
4. 编写threadX应用代码
在main.c
中编写创建两个不同优先级任务运行的应用代码,观察是否可以正常切换任务、演示。
① 引入头文件:
<span style="color:#000000"><code class="language-c"><span style="color:#708090">/* Private includes ----------------------------------------------------------*/</span>
<span style="color:#708090">/* USER CODE BEGIN Includes */</span>
<span style="color:#986801">#<span style="color:#0077aa">include</span> <span style="color:#50a14f"><stdio.h></span></span>
<span style="color:#986801">#<span style="color:#0077aa">include</span> <span style="color:#50a14f">"tx_api.h"</span></span>
<span style="color:#708090">/* USER CODE END Includes */</span>
</code></span>
- 1
- 2
- 3
- 4
- 5
② 创建两个任务控制块,两个任务入口函数,并创建两个任务:
<span style="color:#000000"><code class="language-c"><span style="color:#708090">/* Private user code ---------------------------------------------------------*/</span>
<span style="color:#708090">/* USER CODE BEGIN 0 */</span>
TX_THREAD my_thread1<span style="color:#999999">;</span>
TX_THREAD my_thread2<span style="color:#999999">;</span>
<span style="color:#0077aa">void</span> <span style="color:#dd4a68">my_thread1_entry</span><span style="color:#999999">(</span>ULONG thread_input<span style="color:#999999">)</span>
<span style="color:#999999">{</span>
<span style="color:#708090">/* Enter into a forever loop. */</span>
<span style="color:#0077aa">while</span><span style="color:#999999">(</span><span style="color:#986801">1</span><span style="color:#999999">)</span>
<span style="color:#999999">{</span>
<span style="color:#dd4a68">printf</span><span style="color:#999999">(</span><span style="color:#50a14f">"threadx 1 application running...\r\n"</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#708090">/* Sleep for 1 tick. */</span>
<span style="color:#dd4a68">tx_thread_sleep</span><span style="color:#999999">(</span><span style="color:#986801">1000</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#999999">}</span>
<span style="color:#999999">}</span>
<span style="color:#0077aa">void</span> <span style="color:#dd4a68">my_thread2_entry</span><span style="color:#999999">(</span>ULONG thread_input<span style="color:#999999">)</span>
<span style="color:#999999">{</span>
<span style="color:#708090">/* Enter into a forever loop. */</span>
<span style="color:#0077aa">while</span><span style="color:#999999">(</span><span style="color:#986801">1</span><span style="color:#999999">)</span>
<span style="color:#999999">{</span>
<span style="color:#dd4a68">printf</span><span style="color:#999999">(</span><span style="color:#50a14f">"threadx 2 application running...\r\n"</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#708090">/* Sleep for 1 tick. */</span>
<span style="color:#dd4a68">tx_thread_sleep</span><span style="color:#999999">(</span><span style="color:#986801">1000</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#999999">}</span>
<span style="color:#999999">}</span>
<span style="color:#0077aa">void</span> <span style="color:#dd4a68">tx_application_define</span><span style="color:#999999">(</span><span style="color:#0077aa">void</span> <span style="color:#a67f59">*</span>first_unused_memory<span style="color:#999999">)</span>
<span style="color:#999999">{</span>
<span style="color:#708090">/* Create my_thread! */</span>
<span style="color:#dd4a68">tx_thread_create</span><span style="color:#999999">(</span><span style="color:#a67f59">&</span>my_thread1<span style="color:#999999">,</span> <span style="color:#50a14f">"My Thread 1"</span><span style="color:#999999">,</span>
my_thread1_entry<span style="color:#999999">,</span> <span style="color:#986801">0x1234</span><span style="color:#999999">,</span> first_unused_memory<span style="color:#999999">,</span> <span style="color:#986801">1024</span><span style="color:#999999">,</span> <span style="color:#986801">3</span><span style="color:#999999">,</span> <span style="color:#986801">3</span><span style="color:#999999">,</span> TX_NO_TIME_SLICE<span style="color:#999999">,</span> TX_AUTO_START<span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#dd4a68">tx_thread_create</span><span style="color:#999999">(</span><span style="color:#a67f59">&</span>my_thread2<span style="color:#999999">,</span> <span style="color:#50a14f">"My Thread 2"</span><span style="color:#999999">,</span>
my_thread2_entry<span style="color:#999999">,</span> <span style="color:#986801">0x1234</span><span style="color:#999999">,</span> first_unused_memory<span style="color:#a67f59">+</span><span style="color:#986801">1024</span><span style="color:#999999">,</span> <span style="color:#986801">1024</span><span style="color:#999999">,</span> <span style="color:#986801">1</span><span style="color:#999999">,</span> <span style="color:#986801">1</span><span style="color:#999999">,</span> TX_NO_TIME_SLICE<span style="color:#999999">,</span> TX_AUTO_START<span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#999999">}</span>
</code></span>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
③ 在main函数初始化完毕之后启动内核:
<span style="color:#000000"><code class="language-c"><span style="color:#708090">/* USER CODE BEGIN 2 */</span>
<span style="color:#dd4a68">printf</span><span style="color:#999999">(</span><span style="color:#50a14f">"ThreadX RTOS Port By Mculover666\r\n"</span><span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#708090">/* Enter the ThreadX kernel. */</span>
<span style="color:#dd4a68">tx_kernel_enter</span><span style="color:#999999">(</span> <span style="color:#999999">)</span><span style="color:#999999">;</span>
<span style="color:#708090">/* USER CODE END 2 */</span>
</code></span>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
再次点击Build按钮编译,编译成功:
接上小熊派开发板,点击下方的Download按钮,烧录成功:
点击下方Serial,在VScode打开串口终端,查看串口输出:
1s打印一次,并且两个任务切换运行,任务2的优先级高于任务1,实现现象和预期一样,至此,threadX移植成功,赶快上手试试吧~
如果需要此工程源码,请在公众号 Mculover666 后台回复关键词 threadX 获取,拿到源码后在VScode的iotlink中导入之后编译即可。
接收更多精彩文章及资源推送,欢迎订阅我的微信公众号:『mculover666』。