当前位置:网站首页>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
边栏推荐
- [leetcode daily question] install fence
- Summary of interfaces for JDBC and servlet to write CRUD
- php类与对象
- Mysql连接查询详解
- Leetcode学习计划之动态规划入门day3(198,213,740)
- C语言超全学习路线(收藏让你少走弯路)
- Application of skiplist in leveldb
- el-tree实现只显示某一级复选框且单选
- How did the computer reinstall the system? The display has no signal
- Reptile exercises (1)
猜你喜欢

Lotus DB design and Implementation - 1 Basic Concepts

Machine learning - logistic regression

On the day of entry, I cried (mushroom street was laid off and fought for seven months to win the offer)

G007-HWY-CC-ESTOR-03 华为 Dorado V6 存储仿真器搭建

Explanation 2 of redis database (redis high availability, persistence and performance management)

Detailed explanation of MySQL connection query

2022年中国数字科技专题分析

UML learning_ Day2

Byte interview programming question: the minimum number of K
![Detailed explanation of C language knowledge points -- first understanding of C language [1] - vs2022 debugging skills and code practice [1]](/img/07/c534238c2b5405bbe4655e51cfee51.png)
Detailed explanation of C language knowledge points -- first understanding of C language [1] - vs2022 debugging skills and code practice [1]
随机推荐
Summary of interfaces for JDBC and servlet to write CRUD
Have you learned the basic operation of circular queue?
X509 certificate cer format to PEM format
推荐搜索 常用评价指标
GFS distributed file system (Theory)
Llvm - generate for loop
YML references other variables
redis-shake 使用中遇到的错误整理
激活函数的优缺点和选择
重定向和请求转发详解
电脑怎么重装系统后显示器没有信号了
如果conda找不到想要安装的库怎么办PackagesNotFoundError: The following packages are not available from current
Mysql database explanation (8)
Openfaas practice 4: template operation
Explanation of redis database (IV) master-slave replication, sentinel and cluster
Sword finger offer (1) -- for Huawei
My raspberry PI zero 2W toss notes to record some problems and solutions
After time judgment of date
Use of common pod controller of kubernetes
How to use OCR in 5 minutes