【嵌入式开源库】timeslice的使用,完全解耦的时间片轮询框架构(二)https://developer.aliyun.com/article/1472622
实验
/* USER CODE BEGIN Header */ /** ****************************************************************************** * @file : main.c * @brief : Main program body ****************************************************************************** * @attention * * Copyright (c) 2023 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 "tim.h" #include "gpio.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ #include "timeslice.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 */ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) { if(htim->Instance == TIM3) { timeslice_tick(); } } // 创建3个任务对象 TimesilceTaskObj task_1, task_2, task_3; // 具体的任务函数 void task1_hdl() { HAL_GPIO_TogglePin(GPIOE, GPIO_PIN_13); } void task2_hdl() { HAL_GPIO_TogglePin(GPIOE, GPIO_PIN_14 ); } void task3_hdl() { HAL_GPIO_TogglePin(GPIOE, GPIO_PIN_15); } // 初始化任务对象,并且将任务添加到时间片轮询调度中 void task_init() { timeslice_task_init(&task_1, task1_hdl, 1, 1); timeslice_task_init(&task_2, task2_hdl, 2, 1); timeslice_task_init(&task_3, task3_hdl, 3, 1); timeslice_task_add(&task_1); timeslice_task_add(&task_2); timeslice_task_add(&task_3); } /* 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_TIM3_Init(); /* USER CODE BEGIN 2 */ HAL_TIM_Base_Start_IT(&htim3); task_init(); /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ timeslice_exec(); } /* 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_SCALE1); /** 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 = 25; RCC_OscInitStruct.PLL.PLLN = 336; RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; RCC_OscInitStruct.PLL.PLLQ = 4; 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_DIV4; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2; if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK) { Error_Handler(); } } /* USER CODE BEGIN 4 */ /* 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 */
核心部分
// 创建3个任务对象 TimesilceTaskObj task_1, task_2, task_3; // 具体的任务函数 void task1_hdl() { HAL_GPIO_TogglePin(GPIOE, GPIO_PIN_13); } void task2_hdl() { HAL_GPIO_TogglePin(GPIOE, GPIO_PIN_14 ); } void task3_hdl() { HAL_GPIO_TogglePin(GPIOE, GPIO_PIN_15); } // 初始化任务对象,并且将任务添加到时间片轮询调度中 void task_init() { timeslice_task_init(&task_1, task1_hdl, 1, 1); timeslice_task_init(&task_2, task2_hdl, 2, 2); timeslice_task_init(&task_3, task3_hdl, 3, 3); timeslice_task_add(&task_1); timeslice_task_add(&task_2); timeslice_task_add(&task_3); }
需要注意的是必须要有一个任务是需要在exec前创建,这样才能保证运行,其他的任务可以在这个任务中再创建,上面的实验是实现三个任务,三个任务分别为一个中断触发一次,第二个任务是每隔两个中断触发一次,第三个任务是每隔三个中断触发一次任务;
函数说明
timeslice_task_init
初始化任务函数
void timeslice_task_init(TimesilceTaskObj* obj, void (*task_hdl)(void), unsigned int id, unsigned int timeslice_len) { obj->id = id; obj->is_run = TASK_STOP; obj->task_hdl = task_hdl; obj->timer = timeslice_len; obj->timeslice_len = timeslice_len; }
在这个函数中将任务结构体参数初始化,id类似于任务名称用于区分任务,is_run是一个标志位用于判断该任务在该次中断是否需要执行,task_hd1表示函数指针也就是我们的任务函数,timer表示每多少次中断触发一次计数,timeslice_len 表示没多少次中断触发一次计数初始值,在timeslice_tick中当timer的值减到0任务将触发并会重新复位timer的值为 timeslice_len ;
timeslice_task_add
添加任务到双向链表中
void timeslice_task_add(TimesilceTaskObj* obj) { list_insert_before(×lice_task_list, &obj->timeslice_task_list); } void list_insert_before(ListObj* list, ListObj* node) { list->prev->next = node; node->prev = list->prev; list->prev = node; node->next = list; }
该链表(timeslice_task_list)在timeslice_tick中会轮询进行遍历
timeslice_tak_del
删除正在运行的任务链表
void timeslice_task_del(TimesilceTaskObj* obj) { if (timeslice_task_isexist(obj)) list_remove(&obj->timeslice_task_list); else return; }
在该函数中会通过timeslice_task_isexist函数去判断链表中是否存在该任务的id,如果存在将返回退出允许,这里涉及到一个Linux中的函数list_entry->container_of,该函数是通过结构体的某个变量获取整个结构体的指针位置,有兴趣可以去学习一下该项目代码的实现;
timeslice_get_task_num
获取当前任务数量,也就是链表的长度
unsigned int timeslice_get_task_num() { return list_len(×lice_task_list); } unsigned int list_len(const ListObj* list) { unsigned int len = 0; const ListObj* p = list; while (p->next != list) { p = p->next; len++; } return len; }
结尾
整体的代码不算复杂但是是值得学习的一个项目,我是凉开水白菜祝各位程序员们节日快乐~ 咱们下文见~