以下部分内容,由AI给出的解答:
- RTOS是一种专门设计用于实时系统的操作系统,它可以有效地管理多个任务,提供任务调度、同步和通信等功能。
- STM32可以使用多种RTOS,例如FreeRTOS、ChibiOS等,它们都提供了任务管理、信号量、消息队列等功能。
- 在RTOS中,每个任务都有自己的优先级,并且RTOS会根据任务的优先级进行调度。高优先级的任务将获得更多的CPU时间。
- RTOS提供了更结构化的任务管理,使得编写和维护多任务应用程序变得更容易。
- 定时器时间片轮询是一种基于定时器中断的任务调度方法,而不涉及RTOS的复杂性。
- 在这种方法中,任务的执行由定时器中断触发,每个任务都有一个预定的时间片来执行。
- 当定时器触发时,控制权将转移到下一个任务,如果当前任务没有执行完,它将在下一个时间片继续执行。
- 这种方式的调度对于简单的应用来说可能足够,但在复杂的多任务系统中,可能会导致任务之间的优先级管理和调度变得复杂。
void HAL_SYSTICK_Callback(void)
{
Sys_Tick_Count();
}
#include "sys_time.h"
static unsigned short int sys_tick = 0;
/**
* @brief 系统时基
*
*/
void Sys_Tick_Count(void)
{
sys_tick += 1;
}
/**
* @description: 获取系统滴答计时
* @param {*}
* @return {*}
*/
unsigned short int Get_Sys_Tick()
{
return sys_tick;
}
/**
* @description: 判断是否超时
* @param {unsigned long int} start 计算开始的时间
* @param {unsigned long int} timeout 超时时长
* @return {*}
*/
unsigned short int Is_Timeout(unsigned short int start, unsigned short int timeout)
{
return ((unsigned short int)(Get_Sys_Tick() - start)) > timeout ? 1: 0;
}
#include "sys_task.h"
#include "sys_time.h"
#include "string.h"
sys_task_t *sys_task_head = NULL;
/**
* @brief 系统任务
*
* @param task 任务
* @param handler 任务轮询函数
* @param interval 轮询间隔
*/
void sys_task_create(sys_task_t *task, void (*handler)(void), unsigned int interval)
{
sys_task_t *sys_task_tail = NULL;
memset(task, 0, sizeof(sys_task_t));
task->enable = 0;
task->interval = interval;
task->tick_cnt = Get_Sys_Tick();
task->task_handler = handler;
task->sys_task_next = NULL;
if (sys_task_head == NULL)
{
sys_task_head = task;
return ;
}
sys_task_tail = sys_task_head;
while (sys_task_tail->sys_task_next != NULL)
{
sys_task_tail = sys_task_tail->sys_task_next;
}
sys_task_tail->sys_task_next = task;
}
/**
* @brief 启动任务
*
* @param task 任务句柄
*/
void sys_task_start(sys_task_t *task)
{
task->enable = 1;
}
/**
* @brief 停止任务
*
* @param task 任务句柄
*/
void sys_task_stop(sys_task_t *task)
{
task->enable = 0;
}
/**
* @brief 系统任务轮询
*
*/
void sys_task_process()
{
sys_task_t *task = NULL;
for (task = sys_task_head; task != NULL; task = task->sys_task_next)
{
if (task->enable && Is_Timeout(task->tick_cnt, task->interval))
{
task->task_handler(); // 运行
task->tick_cnt = Get_Sys_Tick();
}
}
}
#include "Blink_TASK.h"
#include "usb_printf.h"
void Blink_Task1(void){
HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin);
usb_printf("This Blink_Task1\r\n");
HAL_Delay(1000);
}
void Blink_Task2(void){
HAL_GPIO_TogglePin(LED2_GPIO_Port, LED2_Pin);
usb_printf("This Blink_Task2\r\n");
HAL_Delay(800);
}
void Blink_Task3(void){
HAL_GPIO_TogglePin(LED3_GPIO_Port, LED3_Pin);
usb_printf("This Blink_Task3\r\n");
HAL_Delay(600);
}
- 添加所需运行的任务句柄,需要运行多少个任务就创建多少个对象。
// 控制LED1任务
sys_task_t Task1_Blink;
// 控制LED2任务
sys_task_t Task2_Blink;
// 控制LED3任务
sys_task_t Task3_Blink;
- 创建任务对象
void task_start()
{
sys_task_create(&Task1_Blink, Blink_Task1, 50);
sys_task_start(&Task1_Blink);
sys_task_create(&Task2_Blink, Blink_Task2, 20);
sys_task_start(&Task2_Blink);
sys_task_create(&Task3_Blink, Blink_Task3, 30);
sys_task_start(&Task3_Blink);
}
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2024 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "usart.h"
#include "usb_device.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "usb_printf.h"
//Timer Task Manage
#include "sys_time.h"
#include "sys_task.h"
#include "Blink_TASK.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* 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 */
// 控制LED1任务
sys_task_t Task1_Blink;
// 控制LED2任务
sys_task_t Task2_Blink;
// 控制LED3任务
sys_task_t Task3_Blink;
/* 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 */
// 任务创建
void task_start()
{
sys_task_create(&Task1_Blink, Blink_Task1, 50);
sys_task_start(&Task1_Blink);
sys_task_create(&Task2_Blink, Blink_Task2, 20);
sys_task_start(&Task2_Blink);
sys_task_create(&Task3_Blink, Blink_Task3, 30);
sys_task_start(&Task3_Blink);
}
/* 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();
MX_USB_DEVICE_Init();
/* USER CODE BEGIN 2 */
uint32_t Main_Fosc = HAL_RCC_GetSysClockFreq();
// HAL_GetTick();
usb_printf("Main_Fosc:%d \r\n", Main_Fosc);
task_start();
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while(1) {
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
sys_task_process();
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 4;
RCC_OscInitStruct.PLL.PLLN = 168;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
RCC_OscInitStruct.PLL.PLLQ = 7;
if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses 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();
}
}
/* USER CODE BEGIN 4 */
void HAL_SYSTICK_Callback(void)
{
Sys_Tick_Count();
}
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
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 /* USE_FULL_ASSERT */
usb_printf.c、h
代码:#include "usb_printf.h"
void usb_printf(const char *fmt, ...) {
char buf[128];//自定义缓冲区大小
va_list args;
va_start(args, fmt);
vsnprintf(buf, sizeof(buf), fmt, args);
va_end(args);
CDC_Transmit_FS((uint8_t *)buf, strlen(buf));
}
#ifndef _USB_PRINTF_H
#define _USB_PRINTF_H
#include "stdio.h"
#include
#include "usbd_cdc_if.h"
void usb_printf(const char *fmt, ...);
#endif
STM32Cube FW_F4 V1.28.0
链接:https://pan.baidu.com/s/1ouPVpfv9E_2paunmgrOMRg?pwd=r3xk
提取码:r3xk