收藏官网首页
查看: 17021|回复: 4

【一起来玩RTOS系列】之RT-Thread Nano快速创建工程

321

主题

1054

帖子

4501

积分

论坛元老

Rank: 8Rank: 8

积分
4501
QQ
发表于 2017-11-4 14:17:17 | 显示全部楼层 |阅读模式
注册成为机智云开发者,手机加虚拟设备快速开发
本帖最后由 bigfanofloT 于 2017-12-19 21:52 编辑

【一起来玩RTOS系列】


RT-Thread Nano是RT-Thread的精简版,只有内核、shell(msh)、设备驱动三大功能,以Keil5的pack形式发布。RT-Thread Nano在保证了具备完整功能的RTOS实时内核的前提下实现了极小的FLASH和RAM占用,默认配置下,FLASH可小至2.5KB, RAM可以小至1KB,对于当今主流32位MCU/SoC来说,跑起来毫无压力。

下面就跟随小编一起,看看如何在机智云gokit智能硬件开发板上将RT-Thread Nano跑起来吧~

一、RT-Thread Nano Pack安装

1.  使用STM32CubeMX创建一个可以点亮板载LED的基本工程,参考:http://club.gizwits.com/thread-3859-1-1.html
2.  在MDK5主界面上点击“Pack Install”按钮,进入Pack Install界面:
1.png

3.  在Pack Install界面下,RT-Thread Pack在右边栏中。点击“Install”可下载,点击“Update”可更新。
2.png

4.  如果在上图界面“Packs”栏中未发现“RT-Thread”,通过两种方法获取RT-Thread Pack。
第一种方法是直接从http://www.rt-thread.org/downloa ... rtthread.2.1.1.pack下载2.1.1版本的的RT-Thread Pack,然后双击完成安装。
第二种方法是在菜单“Packs”下点击“Check for Updates”,Update完成后,将可看到RT-Thread Pack,然后下载Pack再安装它。
3.png

二、kernel加载与应用


1.  在主界面点击“ManageRun-TimeEnvironment”进入加载页:
4.png

在“RTOS”一栏中选中“RT-Thread”,并在列表中选中“kernel”:
5.png

2.  确定后,RT-Thread的kernel文件会被自动添加进来:

6.png

Kernel文件包括:
clock.c
components.c
device.c
idle.c
ipc.c
irq.c
kservice.c
mem.c
object.c
scheduler.c
thread.c
timer.c
Cortex-M芯片内核移植代码:
cpuport.c
context_rvds.s
应用代码及配置文件:
board.c
rtconfig.h

三、修改源码适配机智云Gokit


1.需要做一些微小的修改才能在Gokit上跑起来:
1)修改Application/User分组下的stm32f1xx_it.c文件,删除如下3个函数:
  1. void HardFault_Handler(void);
  2. void PendSV_Handler(void);
  3. void SysTick_Handler(void);
复制代码
2)修改RTOS分组下的board.c上文件:
修改第24行为:
  1. #include "stm32f1xx_hal.h"
复制代码
修改第66行:取消注释,并加入2行代码如下。
  1. void SysTick_Handler(void)
  2. {
  3.          /* enter interrupt */
  4.          rt_interrupt_enter();
  5.          
  6. <font color="#ff00ff">        HAL_IncTick();
  7.         HAL_SYSTICK_IRQHandler();</font>

  8.          rt_tick_increase();
  9.          
  10.          /* leave interrupt */
  11.          rt_interrupt_leave();
  12. }
复制代码
3)在rtconfig.h使能动态内存管理:
搜狗截图20171104133420.png

此外,第15行,修改RT_TICK_PER_SECOND为1000。

2.  修改main.c文件,屏蔽掉while(1)死循环,加入测试代码:
8.png

  1. /**
  2.   ************************
  3.   * File Name          : main.c
  4.   * Description        : Main program body
  5.   ************************
  6.   ** This notice applies to any and all portions of this file
  7.   * that are not between comment pairs USER CODE BEGIN and
  8.   * USER CODE END. Other portions of this file, whether
  9.   * inserted by the user or by software development tools
  10.   * are owned by their respective copyright owners.
  11.   *
  12.   * COPYRIGHT(c) 2017 STMicroelectronics
  13.   *
  14.   * Redistribution and use in source and binary forms, with or without modification,
  15.   * are permitted provided that the following conditions are met:
  16.   *   1. Redistributions of source code must retain the above copyright notice,
  17.   *      this list of conditions and the following disclaimer.
  18.   *   2. Redistributions in binary form must reproduce the above copyright notice,
  19.   *      this list of conditions and the following disclaimer in the documentation
  20.   *      and/or other materials provided with the distribution.
  21.   *   3. Neither the name of STMicroelectronics nor the names of its contributors
  22.   *      may be used to endorse or promote products derived from this software
  23.   *      without specific prior written permission.
  24.   *
  25.   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  26.   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27.   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  28.   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  29.   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  30.   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  31.   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  32.   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  33.   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  34.   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35.   *
  36.   ************************
  37.   */
  38. /* Includes ------------------------------------------------------------------*/
  39. #include "main.h"
  40. #include "stm32f1xx_hal.h"
  41. #include "gpio.h"

  42. /* USER CODE BEGIN Includes */
  43. #include "rtthread.h"
  44. /* USER CODE END Includes */

  45. /* Private variables ---------------------------------------------------------*/

  46. /* USER CODE BEGIN PV */
  47. /* Private variables ---------------------------------------------------------*/

  48. /* USER CODE END PV */

  49. /* Private function prototypes -----------------------------------------------*/
  50. void SystemClock_Config(void);

  51. /* USER CODE BEGIN PFP */
  52. /* Private function prototypes -----------------------------------------------*/

  53. /* USER CODE END PFP */

  54. /* USER CODE BEGIN 0 */
  55. //线程LED1
  56. static void led1_thread_entry(void* parameter)
  57. {
  58.        while(1)
  59.        {
  60.              LED1_Toggle();
  61.              rt_thread_delay(500);   //延时
  62.        }
  63. }
  64. //线程LED2
  65. static void led2_thread_entry(void* parameter)
  66. {
  67.        while(1)
  68.        {
  69.              LED2_Toggle();
  70.              rt_thread_delay(100);   //延时
  71.        }
  72. }

  73. /* USER CODE END 0 */

  74. int main(void)
  75. {

  76.   /* USER CODE BEGIN 1 */

  77.   /* USER CODE END 1 */

  78.   /* MCU Configuration----------------------------------------------------------*/

  79.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  80.   HAL_Init();

  81.   /* USER CODE BEGIN Init */

  82.   /* USER CODE END Init */

  83.   /* Configure the system clock */
  84.   SystemClock_Config();

  85.   /* USER CODE BEGIN SysInit */

  86.   /* USER CODE END SysInit */

  87.   /* Initialize all configured peripherals */
  88.   MX_GPIO_Init();

  89.   /* USER CODE BEGIN 2 */
  90.         
  91.         rt_thread_t tid1=RT_NULL;//线程句柄
  92.         rt_thread_t tid2=RT_NULL;//线程句柄
  93.         
  94.         //创建动态线程
  95.         tid1=rt_thread_create("led1",//线程名字
  96.                              led1_thread_entry,//线程入口函数
  97.                              RT_NULL,//线程参数
  98.                              256,//线程栈大小
  99.                              3,//线程优先级
  100.                              20);//线程时间片
  101.         //启动线程        
  102.         rt_thread_startup(tid1);

  103.         //创建动态线程
  104.         tid2=rt_thread_create("led2",//线程名字
  105.                              led2_thread_entry,//线程入口函数
  106.                              RT_NULL,//线程参数
  107.                              256,//线程栈大小
  108.                              4,//线程优先级
  109.                              20);//线程时间片
  110.         //启动线程                                                                                                         
  111.         rt_thread_startup(tid2);


  112.   /* USER CODE END 2 */

  113.   /* Infinite loop */
  114.   /* USER CODE BEGIN WHILE */
  115. //  while (1)
  116. //  {
  117. //  /* USER CODE END WHILE */
  118. //  /* USER CODE BEGIN 3 */
  119. //               
  120. //  }
  121.   /* USER CODE END 3 */

  122. }

  123. /** System Clock Configuration
  124. */
  125. void SystemClock_Config(void)
  126. {

  127.   RCC_OscInitTypeDef RCC_OscInitStruct;
  128.   RCC_ClkInitTypeDef RCC_ClkInitStruct;

  129.     /**Initializes the CPU, AHB and APB busses clocks
  130.     */
  131.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  132.   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  133.   RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  134.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  135.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  136.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  137.   RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  138.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  139.   {
  140.     _Error_Handler(__FILE__, __LINE__);
  141.   }

  142.     /**Initializes the CPU, AHB and APB busses clocks
  143.     */
  144.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  145.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  146.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  147.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  148.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  149.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  150.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  151.   {
  152.     _Error_Handler(__FILE__, __LINE__);
  153.   }

  154.     /**Configure the Systick interrupt time
  155.     */
  156.   HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

  157.     /**Configure the Systick
  158.     */
  159.   HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

  160.   /* SysTick_IRQn interrupt configuration */
  161.   HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  162. }

  163. /* USER CODE BEGIN 4 */

  164. /* USER CODE END 4 */

  165. /**
  166.   * @brief  This function is executed in case of error occurrence.
  167.   * @param  None
  168.   * @retval None
  169.   */
  170. void _Error_Handler(char * file, int line)
  171. {
  172.   /* USER CODE BEGIN Error_Handler_Debug */
  173.   /* User can add his own implementation to report the HAL error return state */
  174.   while(1)
  175.   {
  176.   }
  177.   /* USER CODE END Error_Handler_Debug */
  178. }

  179. #ifdef USE_FULL_ASSERT

  180. /**
  181.    * @brief Reports the name of the source file and the source line number
  182.    * where the assert_param error has occurred.
  183.    * @param file: pointer to the source file name
  184.    * @param line: assert_param error line source number
  185.    * @retval None
  186.    */
  187. void assert_failed(uint8_t* file, uint32_t line)
  188. {
  189.   /* USER CODE BEGIN 6 */
  190.   /* User can add his own implementation to report the file name and line number,
  191.     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  192.   /* USER CODE END 6 */

  193. }

  194. #endif

  195. /**
  196.   * @}
  197.   */

  198. /**
  199.   * @}
  200. */

  201. /******** (C) COPYRIGHT STMicroelectronics **END OF FILE**/
复制代码


测试代码使用RT-Thread的动态线程创建函数rt_thread_create()创建了2个线程,在线程里面间隔不同的时间翻转LED。

将程序下载到开发板,可以看到LED1和LED2不同频率闪烁:

eg.gif

源码下载:
Gokit_RT-Thread.zip (8.02 MB, 下载次数: 12, 售价: 1 金钱)
1、机智云QQ群:G1群:104975951 G2群:491509598 G3群:287087942
机智云爱好者-APP开发群: 599735135
QQ群目前非常活跃,欢迎大家参与进来,交流,讨论,答疑,解惑~~
2、机智云微信公众号: 机智云 gizwits、 机智云智能宠物屋go-iot
关注机智云Gizwits官方公众号随时掌握最新资讯和活动信息

1

主题

30

帖子

374

积分

中级会员

Rank: 3Rank: 3

积分
374
发表于 2017-11-24 16:56:35 | 显示全部楼层
这个 系统关断时间有些长 。像国产的 RAW-OS 系统关断时间可以做0.8us 。现在就是阿里的AliOS 系统了。

点评

官方实测数据最低是0.6us吧,我记得,这个rtos好用在于它的组件丰富,有设备驱动框架,各种网络协议,文件系统,GUI,不仅仅是个内核  详情 回复 发表于 2017-11-24 19:28

321

主题

1054

帖子

4501

积分

论坛元老

Rank: 8Rank: 8

积分
4501
QQ
 楼主| 发表于 2017-11-24 19:28:41 | 显示全部楼层
MCU记忆 发表于 2017-11-24 16:56
这个 系统关断时间有些长 。像国产的 RAW-OS 系统关断时间可以做0.8us 。现在就是阿里的AliOS 系统了。 ...

官方实测数据最低是0.6us吧,我记得,这个rtos好用在于它的组件丰富,有设备驱动框架,各种网络协议,文件系统,GUI,不仅仅是个内核
1、机智云QQ群:G1群:104975951 G2群:491509598 G3群:287087942
机智云爱好者-APP开发群: 599735135
QQ群目前非常活跃,欢迎大家参与进来,交流,讨论,答疑,解惑~~
2、机智云微信公众号: 机智云 gizwits、 机智云智能宠物屋go-iot
关注机智云Gizwits官方公众号随时掌握最新资讯和活动信息

1

主题

30

帖子

374

积分

中级会员

Rank: 3Rank: 3

积分
374
发表于 2017-11-27 22:32:56 | 显示全部楼层
我学好我手上的RAW-OS 系统.  之后就上阿里云AliOS-Things-master  可以做好多

0

主题

4

帖子

31

积分

新手上路

Rank: 1

积分
31
发表于 2020-1-5 23:19:32 | 显示全部楼层
教您5分钟接入机智云,实现傻瓜式开发
简单实用,谢谢分享
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

加入Q群 返回顶部

版权与免责声明 © 2006-2024 Gizwits IoT Technology Co., Ltd. ( 粤ICP备11090211号 )

快速回复 返回顶部 返回列表