RT-thread(1)cubemx +keil5环境,使用RT-Thread nano

系列链接:

RT-thread(1)cubemx +keil5环境,使用RT-Thread nano                            https://www.cnblogs.com/excellentHellen/articles/16951617.html

RT-thread(2)RT-Thread 控制台与重载void rt_hw_console_output()函数   https://www.cnblogs.com/excellentHellen/articles/16963025.html

RT-thread(3)RT-Thread的定时器 简单例程(基于HAL库,在keil中使用程序框架) https://www.cnblogs.com/excellentHellen/articles/16963097.html

RT-thread(4)RT-Thread的信号 简单例程(基于HAL库,在keil中使用程序框架)https://www.cnblogs.com/excellentHellen/articles/16960216.html

RT-thread(5)RT-Thread的互斥信号 简单例程(keil5 +cubeMX)      https://www.cnblogs.com/excellentHellen/articles/16963577.html

RT-thread(6)RT-Thread的邮箱 简单例程(keil5 +cubeMX)             https://www.cnblogs.com/excellentHellen/articles/16963142.html

RT-thread(7)RT-Thread的事件 简单例程(keil5 +cubeMX)             https://www.cnblogs.com/excellentHellen/articles/16963544.html

 

内容:

1)cubemx 正常生成文件

2)在keil5中打开cubemx生成的工程文件,并按图选者RT-thread nano。

如果没有RT-thread,参照 6)中RT-Thread官网 链接文档进行安装。

 

 

3)在RTOS的文件夹中board.c文件中参考下图,填写系统时钟代码。

  在board.c 中加入如下#include "stm32g0xx_hal.h"

复制代码
void rt_os_tick_callback(void)
{
    rt_interrupt_enter();
    
    rt_tick_increase();

    rt_interrupt_leave();
}
/* cortex-m 架构使用 SysTick_Handler() */
void SysTick_Handler()
{
    rt_os_tick_callback();
}

/**
 * This function will initial your board.
 */
void rt_hw_board_init(void)
{
//#error "TODO 1: OS Tick Configuration."
    /* 
     * TODO 1: OS Tick Configuration
     * Enable the hardware timer and call the rt_os_tick_callback function
     * periodically with the frequency RT_TICK_PER_SECOND. 
     */
HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq() / RT_TICK_PER_SECOND);//设置系统时钟 OS Tick
    /* Call components board initial (use INIT_BOARD_EXPORT()) */
#ifdef RT_USING_COMPONENTS_INIT
    rt_components_board_init();
#endif

#if defined(RT_USING_USER_MAIN) && defined(RT_USING_HEAP)
    rt_system_heap_init(rt_heap_begin_get(), rt_heap_end_get());
#endif
}
复制代码

 

同时注释掉 cubeMX生成的stm32 ??xx_it.c文件中的相关中断代码,否者出现中断处理函数重定义错误。

复制代码
/**
  * @brief This function handles Hard fault interrupt.
  */
//void HardFault_Handler(void)
//{
//  /* USER CODE BEGIN HardFault_IRQn 0 */

//  /* USER CODE END HardFault_IRQn 0 */
//  while (1)
//  {
//    /* USER CODE BEGIN W1_HardFault_IRQn 0 */
//    /* USER CODE END W1_HardFault_IRQn 0 */
//  }
//}


/**
  * @brief This function handles Pendable request for system service.
  */
//void PendSV_Handler(void)
//{
//  /* USER CODE BEGIN PendSV_IRQn 0 */

//  /* USER CODE END PendSV_IRQn 0 */
//  /* USER CODE BEGIN PendSV_IRQn 1 */

//  /* USER CODE END PendSV_IRQn 1 */
//}

/**
  * @brief This function handles System tick timer.
  */
//void SysTick_Handler(void)
//{
//  /* USER CODE BEGIN SysTick_IRQn 0 */

//  /* USER CODE END SysTick_IRQn 0 */
//  //HAL_IncTick();
//  /* USER CODE BEGIN SysTick_IRQn 1 */

//  /* USER CODE END SysTick_IRQn 1 */
//}
复制代码

 

 

4)在main.c中填写代码,测试RTOS是否工作。

复制代码
/* main.c */

/* USER CODE BEGIN Includes */
#include "stdio.h"
#include "string.h"
#include <rtthread.h>
/* USER CODE END Includes */


/* USER CODE BEGIN PV */

/* RT-Thread 定义线程控制块指针 */
static rt_thread_t led1_thread = RT_NULL;
static rt_thread_t led2_thread = RT_NULL;

/* USER CODE END PV */

/* USER CODE BEGIN PFP */
/* RT-Thread 线程入口函数声明 */
static void led1_thread_entry(void* parameter);
static void led2_thread_entry(void* parameter);
/* USER CODE END PFP */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
led1_thread = /* 线程控制块指针 */
rt_thread_create( "led1", /* 线程名字 */
                  led1_thread_entry, /* 线程入口函数 */
                                    RT_NULL, /* 线程入口函数参数 */
                                    512, /* 线程栈大小 */
                    3, /* 线程的优先级 */
                                    20); /* 线程时间片 */
/* 启动线程,开启调度 */
if (led1_thread != RT_NULL)
   rt_thread_startup(led1_thread);
else
   return -1;                                    
                                    
led2_thread = /* 线程控制块指针 */                                    
rt_thread_create( "led2", /* 线程名字 */
                  led2_thread_entry, /* 线程入口函数 */
                                    RT_NULL, /* 线程入口函数参数 */
                                    512, /* 线程栈大小 */
                    3, /* 线程的优先级 */
                                    20); /* 线程时间片 */                
/* 启动线程,开启调度 */
if (led2_thread != RT_NULL)
   rt_thread_startup(led2_thread);
else
   return -1;        

  while (1)
  {
    /* USER CODE END WHILE */



/* USER CODE BEGIN 4 */
static void led1_thread_entry(void* parameter)
{
    while(1)
        {
     HAL_GPIO_WritePin(GPIOC, GPIO_PIN_9, GPIO_PIN_RESET);
     rt_thread_delay(500); /* 延时 500 个 tick, 让出CPU */            
        }
}
static void led2_thread_entry(void* parameter)
{
    while(1)
        {
     HAL_GPIO_WritePin(GPIOC, GPIO_PIN_9, GPIO_PIN_SET);        
         rt_thread_delay(500); /* 延时 500 个 tick, 让出CPU */
        }
}
/* USER CODE END 4 */
复制代码

5)测试效果:

     控制1个LED灯 亮-暗-亮-暗。。。。 交替。

    可以改为每个线程各控制1个LED灯,效果更好。

如果出现 空间不足问题,请修改 board.c如下代码,并注意修改 各线程堆栈大小。

/* board.c*/
/*
* Please modify RT_HEAP_SIZE if you enable RT_USING_HEAP * the RT_HEAP_SIZE max value = (sram size - ZI size), 1024 means 1024 bytes */ #define RT_HEAP_SIZE (2*1024)

 

 6)参考链接:

https://www.rt-thread.org/document/site/#/rt-thread-version/rt-thread-nano/nano-port-keil/an0039-nano-port-keil

 7)进一步学习

RT-Thread API 参考手册:

https://www.rt-thread.org/document/api/group__mailbox.html

 

电子书  RT-Thread内核实现与应用开发实战指南:

下载网站:https://www.rt-thread.org/document/site/

 

 

 

posted @   辛河  阅读(259)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示