完全解耦的时间片轮询框架构
- 简介
- 项目代码
- timeslice.h
- timeslice.c
- list.h
- list.c
- 创建工程
- 移植代码
- 实验
- 函数说明
- timeslice_task_init
- timeslice_task_add
- timeslice_tak_del
- timeslice_get_task_num
- 结尾
简介
timeslice是一个时间片轮询框架,他是一个完全解耦的时间片轮询框架,他的使用非常方便,该项目一共有四个文件分别是tieslice的头文件和源文件以及list的头文件和源文件,tieslice是负责轮询任务,list是一个双向链表负责任务的管理,在Linux内核中使用非常广泛也很经典,该框架是参考rtt实时操作系统的侵入式链表实现的,本章文章是将该框架移植到stm32单片机上实验,使用也非常容易,单片机只需要启用一个定时器作为时钟即可;
本章使用环境:
stm32f407vet6
代码工程使用cubemx创建
项目代码
该项目的代码是我在微信公众号上看到的一个文章,代码并没有上传在github上,这里直接贴上源代码;
timeslice.h
#ifndef _TIMESLICE_H
#define _TIMESLICE_H
#include "list.h"
typedef enum {
TASK_STOP,
TASK_RUN
} IsTaskRun;
typedef struct timesilce
{
unsigned int id;
void (*task_hdl)(void);
IsTaskRun is_run;
unsigned int timer;
unsigned int timeslice_len;
ListObj timeslice_task_list;
} TimesilceTaskObj;
void timeslice_exec(void);
void timeslice_tick(void);
void timeslice_task_init(TimesilceTaskObj* obj, void (*task_hdl)(void), unsigned int id, unsigned int timeslice_len);
void timeslice_task_add(TimesilceTaskObj* obj);
void timeslice_task_del(TimesilceTaskObj* obj);
unsigned int timeslice_get_task_timeslice_len(TimesilceTaskObj* obj);
unsigned int timeslice_get_task_num(void);
unsigned char timeslice_task_isexist(TimesilceTaskObj* obj);
#endif
timeslice.c
#include "timeslice.h"
static LIST_HEAD(timeslice_task_list);
void timeslice_exec()
{
ListObj* node;
TimesilceTaskObj* task;
list_for_each(node, ×lice_task_list)
{
task = list_entry(node, TimesilceTaskObj, timeslice_task_list);
if (task->is_run == TASK_RUN)
{
task->task_hdl();
task->is_run = TASK_STOP;
}
}
}
void timeslice_tick()
{
ListObj* node;
TimesilceTaskObj* task;
list_for_each(node, ×lice_task_list)
{
task = list_entry(node, TimesilceTaskObj, timeslice_task_list);
if (task->timer != 0)
{
task->timer--;
if (task->timer == 0)
{
task->is_run = TASK_RUN;
task->timer = task->timeslice_len;
}
}
}
}
unsigned int timeslice_get_task_num()
{
return list_len(×lice_task_list);
}
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;
}
void timeslice_task_add(TimesilceTaskObj* obj)
{
list_insert_before(×lice_task_list, &obj->timeslice_task_list);
}
void timeslice_task_del(TimesilceTaskObj* obj)
{
if (timeslice_task_isexist(obj))
list_remove(&obj->timeslice_task_list);
else
return;
}
unsigned char timeslice_task_isexist(TimesilceTaskObj* obj)
{
unsigned char isexist = 0;
ListObj* node;
TimesilceTaskObj* task;
list_for_each(node, ×lice_task_list)
{
task = list_entry(node, TimesilceTaskObj, timeslice_task_list);
if (obj->id == task->id)
isexist = 1;
}
return isexist;
}
unsigned int timeslice_get_task_timeslice_len(TimesilceTaskObj* obj)
{
return obj->timeslice_len;
}
list.h
#ifndef _LIST_H
#define _LIST_H
#define offset_of(type, member) (unsigned long) &((type*)0)->member
#define container_of(ptr, type, member) ((type *)((char *)(ptr) - offset_of(type, member)))
typedef struct list_structure
{
struct list_structure* next;
struct list_structure* prev;
} ListObj;
#define LIST_HEAD_INIT(name) {&(name), &(name)}
#define LIST_HEAD(name) ListObj name = LIST_HEAD_INIT(name)
void list_init(ListObj* list);
void list_insert_after(ListObj* list, ListObj* node);
void list_insert_before(ListObj* list, ListObj* node);
void list_remove(ListObj* node);
int list_isempty(const ListObj* list);
unsigned int list_len(const ListObj* list);
#define list_entry(node, type, member) \
container_of(node, type, member)
#define list_for_each(pos, head) \
for (pos = (head)->next; pos != (head); pos = pos->next)
#define list_for_each_safe(pos, n, head) \
for (pos = (head)->next, n = pos->next; pos != (head); \
pos = n, n = pos->next)
#endif
list.c
#include "list.h"
void list_init(ListObj* list)
{
list->next = list->prev = list;
}
void list_insert_after(ListObj* list, ListObj* node)
{
list->next->prev = node;
node->next = list->next;
list->next = node;
node->prev = list;
}
void list_insert_before(ListObj* list, ListObj* node)
{
list->prev->next = node;
node->prev = list->prev;
list->prev = node;
node->next = list;
}
void list_remove(ListObj* node)
{
node->next->prev = node->prev;
node->prev->next = node->next;
node->next = node->prev = node;
}
int list_isempty(const ListObj* list)
{
return list->next == 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;
}
创建工程
配置高速时钟和低速时钟为外部晶振提供
配置调试模式为sw调试模式
配置时钟频率
配置led,这里我的板子是这三个io接入的是led
配置一个10ms的定时器(1000000hz / 1000ms == 1ms = 1khz 就得到10ms需要计数10000重载)记得打开中断
配置工程并生成,工程名设置,单独生成.c.h文件拷贝库文件
下面开始代码的移植工作;
移植代码
首先我们需要将该开源项目的代码添加到工程中
在定时器中断服务函数中添加timeslice轮询函数
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if(htim->Instance == TIM3)
{
timeslice_tick();
}
}
然后我们需要在main函数中启动定时器并在while1中调用exec函数调度时间片
HAL_TIM_Base_Start_IT(&htim3);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
timeslice_exec();
}
到这里我们的移植工作就做完了,该项目的解耦效果真的非常好,移植相当简单,然后我们创建几个任务实验一下效果;
实验
/* 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;
}
结尾
整体的代码不算复杂但是是值得学习的一个项目,我是凉开水白菜祝各位程序员们节日快乐~ 咱们下文见~