基于 STM32CubeMX 添加 RT-Thread 操作系统组件(十二)- 软件定时器

概述 

       本篇只要介绍这么使用STM32CubeMx工具添加RT-Thread操作系统组件,码代码的IDE是keil。介绍单线程SRAM静态内存使用。如果还不知道,这么使用STM32CubeMx工具添加RT-Thread操作系统组件,请移步到《基于 STM32CubeMX 添加 RT-Thread 操作系统组件(一)- 详细介绍操作步骤》文章阅读。好了,喝杯茶先^_^,继续前行。上一篇介绍关于《事件》

 

一、STM32CubeMx配置

 

二、KEIL IDE

  1. 在Application/User文件夹,新建app_rt_thread.c文件,并添加如下代码:
    #include "rtthread.h"
    #include "main.h"
    #include "stdio.h"
    #include "usart.h"
    
    
    
    
    /* 定义软件定时器控制块 */
    static rt_timer_t swtmr1 = RT_NULL;
    static rt_timer_t swtmr2 = RT_NULL;
    
    /* 全局变量声明 */
    static uint32_t TmrCb_Count1 = 0;
    static uint32_t TmrCb_Count2 = 0;
    
    /* 函数声明 */
    static void swtmr1_callback(void* parameter);
    static void swtmr2_callback(void* parameter);
    
    
    
    int MX_RT_Thread_Init(void)
    {
    	rt_kprintf("This is an RTT software timer experiment!\n");
    	rt_kprintf("The timer timeout function 1 executes only once and is destroyed\n");
    	rt_kprintf("The timer timeout function 2 is executed in a loop\n");
    	/* 创建一个软件定时器 */
    	swtmr1 = rt_timer_create("swtmr1_callback", /* 软件定时器的名称 */
    				 swtmr1_callback,/* 软件定时器的超时函数 */
    				 0, /* 定时器超时函数的入口参数 */
    				 5000, /* 软件定时器的超时时间(周期超时时间) */
    				 RT_TIMER_FLAG_ONE_SHOT | RT_TIMER_FLAG_SOFT_TIMER);
    	/* 软件定时器模式 一次模式 */
    	/* 启动定时器 */
    	if (swtmr1 != RT_NULL)
    		rt_timer_start(swtmr1);
    	/* 创建一个软件定时器 */
    	swtmr2 = rt_timer_create("swtmr2_callback", /* 软件定时器的名称 */
    				 swtmr2_callback,/* 软件定时器的超时函数 */
    				 0, /* 定时器超时函数的入口参数 */
    				 1000, /* 软件定时器的超时时间(周期超时时间) */
    				 RT_TIMER_FLAG_PERIODIC | RT_TIMER_FLAG_SOFT_TIMER);
    	 /* 软件定时器模式 周期模式 */
    	 /* 启动定时器 */
    	 if (swtmr2 != RT_NULL)
    		rt_timer_start(swtmr2);
    }
    
    /*
    ************************************************************
    * 线程定义
    *********************************************************
    */
    static void swtmr1_callback(void* parameter)
    {
    	uint32_t tick_num1;
    	TmrCb_Count1++; /* 每调用一次加一 */
    	tick_num1 = (uint32_t)rt_tick_get(); /* 获取滴答定时器的计数值 */
    	rt_kprintf("The SWtMR1 Callback function is executed %d times\n", TmrCb_Count1);
    	rt_kprintf("Tick timer value =%d\n", tick_num1);
    }
    static void swtmr2_callback(void* parameter)
    {
    	uint32_t tick_num2;
    	TmrCb_Count2++; /* 每调用一次加一 */
    	tick_num2 = (uint32_t)rt_tick_get(); /* 获取滴答定时器的计数值 */
    	rt_kprintf("The SWtMR2 Callback function is executed %d times\n", TmrCb_Count2);
    	rt_kprintf("Tick timer value =%d\n", tick_num2);
    }
    
    
    
  2. 在main.c文件添加如下代码:
    /* USER CODE END Header */
    
    /* Includes ------------------------------------------------------------------*/
    #include "main.h"
    #include "usart.h"
    #include "gpio.h"
    
    /* Private includes ----------------------------------------------------------*/
    /* USER CODE BEGIN Includes */
    
    /* USER CODE END Includes */
    
    /* Private typedef -----------------------------------------------------------*/
    /* USER CODE BEGIN PTD */
    extern int MX_RT_Thread_Init(void);
    /* USER CODE END PTD */
    
    /* Private define ------------------------------------------------------------*/
    /* USER CODE BEGIN PD */
    /* USER CODE END PD */
    
    /* Private macro -------------------------------------------------------------*/
    /* USER CODE BEGIN PM */
    
    /* USER CODE END PM */
    
    /* Private variables ---------------------------------------------------------*/
    
    /* USER CODE BEGIN PV */
    
    /* USER CODE END PV */
    
    /* Private function prototypes -----------------------------------------------*/
    void SystemClock_Config(void);
    /* USER CODE BEGIN PFP */
    
    /* USER CODE END PFP */
    
    /* Private user code ---------------------------------------------------------*/
    /* USER CODE BEGIN 0 */
    
    /* USER CODE END 0 */
    
    /**
      * @brief  The application entry point.
      * @retval int
      */
    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();
      MX_USART1_UART_Init();
      /* USER CODE BEGIN 2 */
      MX_RT_Thread_Init();
      /* USER CODE END 2 */
    
      /* Infinite loop */
      /* USER CODE BEGIN WHILE */
      while (1)
      {
        /* USER CODE END WHILE */
    
        /* USER CODE BEGIN 3 */
      }
      /* USER CODE END 3 */
    }

     

  3. 自定义rt_hw_console_output()函数,在kservice.c文件添加中(重映射串口控制台到 rt_kprintf 函数)代码:
    #include "usart.h"
    .
    .
    .
    
    RT_WEAK void rt_hw_console_output(const char *str)
    {
        /* empty console output */
    	/* 进入临界段 */
    	rt_enter_critical();
    	/* 直到字符串结束 */
    	while (*str!='\0') {
    	/* 换行 */
    	if (*str=='\n') {
    		HAL_UART_Transmit(&huart1,(uint8_t *)'\r',1,1000);
    	}
    		HAL_UART_Transmit(&huart1,(uint8_t *)(str++),1,1000);
    	}
    	/* 退出临界段 */
    	rt_exit_critical();
    }

  4. 运行结果
        

    源码:git​​​​​​​

猜你喜欢

转载自blog.csdn.net/qq_36075612/article/details/107629032