当前位置:网站首页>Multitimer V2 reconstruction version | an infinitely scalable software timer
Multitimer V2 reconstruction version | an infinitely scalable software timer
2022-04-23 15:29:00 【Mculover666】
Preface
The selected column of embedded open source projects published an article on MultiTimer The article , MultiTimer | An infinitely scalable software timer , This week, some friends in the group reminded me MutilTimer It's not quite the same as the article , The first reaction is to reconstruct , After the technical level has been improved by one level, the big guys like reconstruction projects , Go to github See what happened .
master The branch is still the same as before v1 edition , It's the same as the article :
development The project was refactored on the branch , Released v2 edition :
Update the next tutorial synchronously .
One 、MultiTimer
The open source project brought to you in this issue is MultiTimer, An infinitely scalable software timer , author 0x1abin, The current harvest 399 individual star, follow MIT Open source license agreement .
MultiTimer Is a software timer extension module , Unlimited expansion of timer tasks you need , Instead of the traditional way of judging the sign bit , It is more elegant and convenient to manage the time trigger sequence of the program .
Project address :https://github.com/0x1abin/MultiTimer
Two 、 transplant MultiTimer
1. Transplantation ideas
Open source projects mainly refer to the of the project in the migration process readme file , It usually takes only two steps :
- ① Add the source code to the bare metal project ;
- ② Implement the required interfaces ;
In this article, I use little bear pie IoT Development Kit , The main control chip is STM32L431RCT6:
A bare metal project needs to be prepared before transplantation , I use STM32CubeMX Generate , The following configuration needs to be initialized :
- Configure a serial port for printing information
- printf Redirect
2.MDK transplant
① Copy MultiTimer Source code into the project :
② stay keil Add MultiTimer Source file :
③ take MultiTimer Add header file path to keil in :
3. gcc transplant
① Copy MultiTimer Source code into the project :
② stay Makefile Add MultiTimer Source file :
③ add to MultiTimer Header file path :
3、 ... and 、 Use MultiTimer
Include header files when using :
#include "multi_timer.h"
1. Provide Timer Time base signal
MultiTimer The time base signal in needs to be installed ,API as follows :
/** * @brief Platform ticks function. * * @param ticksFunc ticks function. * @return int 0 on success, -1 on error. */
int MultiTimerInstall(PlatformTicksFunction_t ticksFunc);
PlatformTicksFunction_t The function pointer is defined as follows :
typedef uint64_t (*PlatformTicksFunction_t)(void);
What is used in this article is STM32HAL library , So pass Systick To provide , No additional timer is required .
Write acquisition system tick Function of :
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
uint64_t PlatformTicksGetFunc(void)
{
return (uint64_t)HAL_GetTick();
}
/* USER CODE END 0 */
stay main Install the in the function tick function :
/* USER CODE BEGIN 2 */
printf("MultiTimer v2 Port on BearPi board by mculover666!\r\n");
MultiTimerInstall(PlatformTicksGetFunc);
/* USER CODE END 2 */
2. establish Timer object
Software timer Abstract MultiTimer Structure :
struct MultiTimerHandle {
MultiTimer* next;
uint64_t deadline;
MultiTimerCallback_t callback;
void* userData;
};
typedef struct MultiTimerHandle MultiTimer;
So use it directly MultiTimer Type create software timer :
/* USER CODE BEGIN PV */
MultiTimer timer1;
/* USER CODE END PV */
3. Timer Callback function
Callback function types are defined as follows :
typedef void (*MultiTimerCallback_t)(MultiTimer* timer, void* userData);
In callback function format , Create timeout callback function :
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
void timer1_callback(MultiTimer* timer, void* userData)
{
printf("timer1 timeout!\r\n");
}
/* USER CODE END 0 */
4. Initialize and start Timer
Start timer API as follows :
/** * @brief Start the timer work, add the handle into work list. * * @param timer target handle strcut. * @param timing Set the start time. * @param callback deadline callback. * @param userData user data. * @return int 0: success, -1: fail. */
int MultiTimerStart(MultiTimer* timer, uint64_t timing, MultiTimerCallback_t callback, void* userData);
Initialize timer object , Register timer callback handler , Set timeout (ms):
/* USER CODE BEGIN 2 */
printf("MultiTimer v2 Port on BearPi board by mculover666!\r\n");
MultiTimerStart(&timer1, 1000, timer1_callback, NULL);
/* USER CODE END 2 */
5. Timer Object processing
Timer Object handler API The definition is as follows :
/** * @brief Check the timer expried and call callback. * * @return int The next timer expires. */
int MultiTimerYield(void);
Call in main loop Timer Object handler , The processing function will judge whether each timer on the linked list times out , If exceeded , Pull up the registered callback function :
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
MultiTimerYield();
}
/* USER CODE END 3 */
Next, compile and download , See the printed log in the serial port assistant :
Four 、 How to cycle trigger
In the timer timeout function , Just restart the timer .
void timer1_callback(MultiTimer* timer, void* userData)
{
printf("timer1 timeout!\r\n");
// restart
MultiTimerStart(&timer1, 1000, timer1_callback, NULL);
}
5、 ... and 、 Interpretation of design ideas
be relative to v1 edition ,v2 The version obviously involves a lot of brevity ,c File implementation only 4 A function ,82 Line code .
v2 The registration mechanism used in the version is provided by the user tick, One advantage of this design is , More portability , System without intervention tick interrupt , Only MultiTimer When you get scheduled , It can be installed by us API Get system tick, Take this as the benchmark to judge whether the timer times out .
v2 The version also optimizes the linked list insertion mechanism , Before that, it was simple and straightforward to insert nodes into a single linked list , Now sort the inserts by timeout , More elegant :
In addition to the more elegant insertion , There are also two improvements to the performance of software timers , When scheduling :
- Timers with a near timeout are always given priority
- The previous timer has not timed out , Scheduling can be ended directly
The implementation idea of software timer can be referred to before v1 Version of the tutorial .
Receive more wonderful articles and resource push , Welcome to subscribe to my WeChat official account :『mculover666』.
版权声明
本文为[Mculover666]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231521555202.html
边栏推荐
- MySQL query library size
- Explanation 2 of redis database (redis high availability, persistence and performance management)
- 【backtrader源码解析18】yahoo.py 代码注释及解析(枯燥,对代码感兴趣,可以参考)
- Have you learned the basic operation of circular queue?
- 考试考试自用
- A series of problems about the best time to buy and sell stocks
- 自动化测试框架常见类型▏自动化测试就交给软件测评机构
- 如果conda找不到想要安装的库怎么办PackagesNotFoundError: The following packages are not available from current
- 字节面试 transformer相关问题 整理复盘
- 控制结构(二)
猜你喜欢
Detailed explanation of MySQL connection query
Sword finger offer (2) -- for Huawei
About UDP receiving ICMP port unreachable
Have you learned the basic operation of circular queue?
函数(第一部分)
Explanation 2 of redis database (redis high availability, persistence and performance management)
字节面试 transformer相关问题 整理复盘
推荐搜索 常用评价指标
Detailed explanation of C language knowledge points - data types and variables [2] - integer variables and constants [1]
T2 icloud calendar cannot be synchronized
随机推荐
自动化测试框架常见类型▏自动化测试就交给软件测评机构
PHP PDO ODBC loads files from one folder into the blob column of MySQL database and downloads the blob column to another folder
推荐搜索 常用评价指标
How to test mobile app?
Llvm - generate addition
el-tree实现只显示某一级复选框且单选
Wechat applet customer service access to send and receive messages
setcontext getcontext makecontext swapcontext
asp. Net method of sending mail using mailmessage
PSYNC synchronization of redis source code analysis
函数(第一部分)
软件性能测试报告起着什么作用?第三方测试报告如何收费?
Adobe Illustrator menu in Chinese and English
Explanation of redis database (IV) master-slave replication, sentinel and cluster
MySQL Basics
Squid agent
Mysql database explanation (10)
2022年中国数字科技专题分析
Sword finger offer (2) -- for Huawei
JSON date time date format