赞
踩
搞嵌入式的为啥要一定要学习RTOS? - 知乎 (zhihu.com)
2、FreeRTOS处理多任务的原理
严格来说 FreeRTOS 并不是实时操作系统,因为它是分时复用的。
系统将时间分割成很多时间片,然后轮流执行各个任务。
每个任务都是独立运行的,互不影响,由于切换的频率很快,就感觉像是同时运行的一样。
任务可以理解为进程/线程,创建一个任务,就会在内存开辟一个空间。
比如: 玩游戏、陪女朋友,都可以视为任务 Windows 系统中的 MarkText 、谷歌浏览器、记事本,都是任务。 任务通常都含有 while(1) 死循环。
动态创建任务的堆栈由系统分配,而静态创建任务的堆栈由用户自己传递。 通常情况下使用动态方式创建任务。
函数原型:
1. pvTaskCode:指向任务函数的指针,任务必须实现为永不返回(即连续循环);
2. pcName:任务的名字,主要是用来调试,默认情况下最大长度是16;
3. pvParameters:指定的任务栈的大小;
4. uxPriority:任务优先级,数值越大,优先级越大;
5. pxCreatedTask:用于返回已创建任务的句柄可以被引用。
函数原型:
- void vTaskDelete(TaskHandle_t xTaskToDelete);
- /* xTaskToDelete为要删除的任务句柄 */
只需将待删除的任务句柄传入该函数,即可将该任务删除。
当传入的参数为NULL,则代表删除任务自身(当前正在运行的任务)。
- /* Task to be created. */
- void vTaskCode( void * pvParameters ) /*任务函数*/
- {
- /* The parameter value is expected to be 1 as 1 is passed in the
- pvParameters value in the call to xTaskCreate() below.
- configASSERT( ( ( uint32_t ) pvParameters ) == 1 );
-
- for( ;; )
- {
- /* Task code goes here. */
- }
- }
- /* Function that creates a task. */
- void vOtherFunction( void )
- {
- BaseType_t xReturned; /*任务创建的返回值*/
- TaskHandle_t xHandle = NULL; /*定义一个任务句柄*/
-
- /* Create the task, storing the handle. */
- xReturned = xTaskCreate( /*动态创建一个任务,创建成功返回pdPASS*/
- vTaskCode, /* Function that implements the task. */
- "NAME", /* Text name for the task. */
- STACK_SIZE, /* Stack size in words, not bytes. */
- ( void * ) 1, /* Parameter passed into the task. */
- tskIDLE_PRIORITY,/* Priority at which the task is created. */
- &xHandle ); /* Used to pass out the created task's handle.*/
-
- if( xReturned == pdPASS )
- {
- /* The task was created. Use the task's handle to delete the task. */
- vTaskDelete( xHandle ); /*删除任务*/
- }
- }
创建两个任务,实现两个LED灯不同频率闪烁。
freertos.c
- /* USER CODE BEGIN Header */
- /**
- ******************************************************************************
- * File Name : freertos.c
- * Description : Code for freertos applications
- ******************************************************************************
- * @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 "FreeRTOS.h"
- #include "task.h"
- #include "main.h"
- #include "cmsis_os.h"
-
- /* Private includes ----------------------------------------------------------*/
- /* USER CODE BEGIN Includes */
-
- /* 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 Variables */
-
- /* USER CODE END Variables */
- osThreadId TaskLED1Handle; /* 创建任务句柄 */
- osThreadId TaskLED2Handle;
-
- /* Private function prototypes -----------------------------------------------*/
- /* USER CODE BEGIN FunctionPrototypes */
-
- /* USER CODE END FunctionPrototypes */
-
- void StartTaskLED1(void const * argument);
- void StartTaskLED2(void const * argument);
-
- void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */
-
- /* GetIdleTaskMemory prototype (linked to static allocation support) */
- void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize );
-
- /* USER CODE BEGIN GET_IDLE_TASK_MEMORY */
- static StaticTask_t xIdleTaskTCBBuffer;
- static StackType_t xIdleStack[configMINIMAL_STACK_SIZE];
-
- void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize )
- {
- *ppxIdleTaskTCBBuffer = &xIdleTaskTCBBuffer;
- *ppxIdleTaskStackBuffer = &xIdleStack[0];
- *pulIdleTaskStackSize = configMINIMAL_STACK_SIZE;
- /* place for user code */
- }
- /* USER CODE END GET_IDLE_TASK_MEMORY */
-
- /**
- * @brief FreeRTOS initialization
- * @param None
- * @retval None
- */
- void MX_FREERTOS_Init(void) {
- /* USER CODE BEGIN Init */
-
- /* USER CODE END Init */
-
- /* USER CODE BEGIN RTOS_MUTEX */
- /* add mutexes, ... */
- /* USER CODE END RTOS_MUTEX */
-
- /* USER CODE BEGIN RTOS_SEMAPHORES */
- /* add semaphores, ... */
- /* USER CODE END RTOS_SEMAPHORES */
-
- /* USER CODE BEGIN RTOS_TIMERS */
- /* start timers, add new ones, ... */
- /* USER CODE END RTOS_TIMERS */
-
- /* USER CODE BEGIN RTOS_QUEUES */
- /* add queues, ... */
- /* USER CODE END RTOS_QUEUES */
-
- /* Create the thread(s) */
- /* definition and creation of TaskLED1 */
- osThreadDef(TaskLED1, StartTaskLED1, osPriorityNormal, 0, 128);
- TaskLED1Handle = osThreadCreate(osThread(TaskLED1), NULL); /*创建任务,返回任务句柄*/
-
- /* definition and creation of TaskLED2 */
- osThreadDef(TaskLED2, StartTaskLED2, osPriorityNormal, 0, 128);
- TaskLED2Handle = osThreadCreate(osThread(TaskLED2), NULL);
-
- /* USER CODE BEGIN RTOS_THREADS */
- /* add threads, ... */
- /* USER CODE END RTOS_THREADS */
-
- }
-
- /* USER CODE BEGIN Header_StartTaskLED1 */
- /**
- * @brief Function implementing the TaskLED1 thread.
- * @param argument: Not used
- * @retval None
- */
- /* USER CODE END Header_StartTaskLED1 */
- void StartTaskLED1(void const * argument)
- {
- /* USER CODE BEGIN StartTaskLED1 */
- /* Infinite loop */
- for(;;)
- {
- HAL_GPIO_TogglePin(GPIOB ,GPIO_PIN_8);
- osDelay(500);
- }
- /* USER CODE END StartTaskLED1 */
- }
-
- /* USER CODE BEGIN Header_StartTaskLED2 */
- /**
- * @brief Function implementing the TaskLED2 thread.
- * @param argument: Not used
- * @retval None
- */
- /* USER CODE END Header_StartTaskLED2 */
- void StartTaskLED2(void const * argument)
- {
- /* USER CODE BEGIN StartTaskLED2 */
- /* Infinite loop */
- for(;;)
- {
- HAL_GPIO_TogglePin(GPIOB ,GPIO_PIN_9);
- osDelay(1000);
- }
- /* USER CODE END StartTaskLED2 */
- }
-
- /* Private application code --------------------------------------------------*/
- /* USER CODE BEGIN Application */
-
- /* USER CODE END Application */
-
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。