bigfanofloT 发表于 2017-11-4 14:17:17

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

本帖最后由 bigfanofloT 于 2017-12-19 21:52 编辑

【一起来玩RTOS系列】
[*]在微信宠物屋中添加RT-Thread-完结http://club.gizwits.com/thread-8177-1-1.html
[*]在微信宠物屋中添加RT-Thread-裸机http://club.gizwits.com/thread-8176-1-1.html
[*]RT-Thread Nano添加shell功能http://club.gizwits.com/thread-8175-1-1.html
[*]RT-Thread 使用消息队列用于线程间通信http://club.gizwits.com/thread-8132-1-1.html
[*]RT-Thread 邮箱用于线程间通信http://club.gizwits.com/thread-8131-1-1.html
[*]RT-Thread 事件用于线程同步http://club.gizwits.com/thread-8130-1-1.html
[*]RT-Thread 互斥锁解决优先级反转http://club.gizwits.com/thread-8129-1-1.html
[*]RT-Thread 信号量优先级反转http://club.gizwits.com/thread-8128-1-1.html
[*]RT-Thread 信号量作为锁用于临界区访问http://club.gizwits.com/thread-8096-1-1.html
[*]RT-Thread 信号量用于中断与线程的同步http://club.gizwits.com/thread-8095-1-1.html
[*]RT-Thread 信号量用于资源计数http://club.gizwits.com/thread-8094-1-1.html
[*]RT-Thread 信号量用于线程同步http://club.gizwits.com/thread-8093-1-1.html
[*]RT-Thread 空闲任务idle钩子函数http://club.gizwits.com/thread-8044-1-1.html
[*]RT-Thread 空闲任务idle钩子函数 http://club.gizwits.com/thread-8044-1-1.html
[*]RT-Thread 通过yield切换线程 http://club.gizwits.com/thread-8042-1-1.html
[*]RT-Thread 通过关中断保护临界代码 http://club.gizwits.com/thread-8041-1-1.html
[*]RT-Thread 通过调度锁保护代码 http://club.gizwits.com/thread-8040-1-1.html
[*]RT-Thread 线程的挂起和恢复 http://club.gizwits.com/thread-8039-1-1.html
[*]RT-Thread 软件定时器-静态方法    http://club.gizwits.com/thread-7991-1-1.html
[*]RT-Thread 软件定时器-动态方法 http://club.gizwits.com/thread-7990-1-1.html
[*]RT-Thread Nano线程调度与管理    http://club.gizwits.com/thread-7944-1-1.html
[*]RT-Thread Nano添加串口打印驱动 http://club.gizwits.com/thread-7943-1-1.html
[*]RT-Thread Nano快速创建工程 http://club.gizwits.com/thread-7922-1-1.html



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界面:


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


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再安装它。


二、kernel加载与应用


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


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


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



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个函数:
void HardFault_Handler(void);
void PendSV_Handler(void);
void SysTick_Handler(void);2)修改RTOS分组下的board.c上文件:
修改第24行为:
#include "stm32f1xx_hal.h"修改第66行:取消注释,并加入2行代码如下。
void SysTick_Handler(void)
{
         /* enter interrupt */
         rt_interrupt_enter();
         
<font color="#ff00ff">      HAL_IncTick();
      HAL_SYSTICK_IRQHandler();</font>

         rt_tick_increase();
         
         /* leave interrupt */
         rt_interrupt_leave();
}3)在rtconfig.h使能动态内存管理:


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

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


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

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

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */
/* Private variables ---------------------------------------------------------*/

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);

/* USER CODE BEGIN PFP */
/* Private function prototypes -----------------------------------------------*/

/* USER CODE END PFP */

/* USER CODE BEGIN 0 */
//线程LED1
static void led1_thread_entry(void* parameter)
{
       while(1)
       {
             LED1_Toggle();
             rt_thread_delay(500);   //延时
       }
}
//线程LED2
static void led2_thread_entry(void* parameter)
{
       while(1)
       {
             LED2_Toggle();
             rt_thread_delay(100);   //延时
       }
}

/* USER CODE END 0 */

int main(void)
{

/* USER CODE BEGIN 1 */

/* USER CODE END 1 */

/* MCU Configuration----------------------------------------------------------*/

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

/* USER CODE BEGIN Init */

/* USER CODE END Init */

/* Configure the system clock */
SystemClock_Config();

/* USER CODE BEGIN SysInit */

/* USER CODE END SysInit */

/* Initialize all configured peripherals */
MX_GPIO_Init();

/* USER CODE BEGIN 2 */
      
      rt_thread_t tid1=RT_NULL;//线程句柄
      rt_thread_t tid2=RT_NULL;//线程句柄
      
      //创建动态线程
      tid1=rt_thread_create("led1",//线程名字
                           led1_thread_entry,//线程入口函数
                           RT_NULL,//线程参数
                           256,//线程栈大小
                           3,//线程优先级
                           20);//线程时间片
      //启动线程      
      rt_thread_startup(tid1);

      //创建动态线程
      tid2=rt_thread_create("led2",//线程名字
                           led2_thread_entry,//线程入口函数
                           RT_NULL,//线程参数
                           256,//线程栈大小
                           4,//线程优先级
                           20);//线程时间片
      //启动线程                                                                                                         
      rt_thread_startup(tid2);


/* USER CODE END 2 */

/* Infinite loop */
/* USER CODE BEGIN WHILE */
//while (1)
//{
///* USER CODE END WHILE */
///* USER CODE BEGIN 3 */
//               
//}
/* USER CODE END 3 */

}

/** System Clock Configuration
*/
void SystemClock_Config(void)
{

RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_ClkInitTypeDef RCC_ClkInitStruct;

    /**Initializes the CPU, AHB and APB busses clocks
    */
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
    _Error_Handler(__FILE__, __LINE__);
}

    /**Initializes the CPU, AHB and APB busses clocks
    */
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
    _Error_Handler(__FILE__, __LINE__);
}

    /**Configure the Systick interrupt time
    */
HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

    /**Configure the Systick
    */
HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

/* SysTick_IRQn interrupt configuration */
HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**
* @briefThis function is executed in case of error occurrence.
* @paramNone
* @retval None
*/
void _Error_Handler(char * file, int line)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
while(1)
{
}
/* USER CODE END Error_Handler_Debug */
}

#ifdef USE_FULL_ASSERT

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

}

#endif

/**
* @}
*/

/**
* @}
*/

/******** (C) COPYRIGHT STMicroelectronics **END OF FILE**/


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

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



源码下载:
















MCU记忆 发表于 2017-11-24 16:56:35

这个 系统关断时间有些长 。像国产的 RAW-OS 系统关断时间可以做0.8us 。现在就是阿里的AliOS 系统了。

bigfanofloT 发表于 2017-11-24 19:28:41

MCU记忆 发表于 2017-11-24 16:56
这个 系统关断时间有些长 。像国产的 RAW-OS 系统关断时间可以做0.8us 。现在就是阿里的AliOS 系统了。 ...

官方实测数据最低是0.6us吧,我记得,这个rtos好用在于它的组件丰富,有设备驱动框架,各种网络协议,文件系统,GUI,不仅仅是个内核

MCU记忆 发表于 2017-11-27 22:32:56

我学好我手上的RAW-OS 系统.之后就上阿里云AliOS-Things-master可以做好多

JohnDZhang 发表于 2020-1-5 23:19:32

简单实用,谢谢分享
页: [1]
查看完整版本: 【一起来玩RTOS系列】之RT-Thread Nano快速创建工程