当前位置:网站首页>[FreeRTOS] 11 software timer
[FreeRTOS] 11 software timer
2022-07-24 07:21:00 【xiaobaibai_ two thousand and twenty-one】
The timer is MCU Common peripherals , We are bound to learn its hardware timer when learning all kinds of MCU . however ,MCU The self-contained hardware timer resources are limited , And it is generally used in places with strong real-time requirements .
stay freeRTOS The function of software timer is provided in , To achieve more timer resources . In this section, we will learn about software timers .
1)freeRTOS Software timer introduction
freeRTOS Software timer for , It can be set as a single execution 、 Periodically ;
When the fixed time expires , Will execute the callback function ( Interrupt service program equivalent to hardware timer );
Timer and callback function , You cannot call functions that block tasks , such as vTaskDelay()、vTaskDelayUntil() And waiting for semaphores 、 Functions like message queue , Otherwise, the timer will be abnormal .
Using software timer , Need to be in FreeRTOSConfig.h In file , take configUSE_TIMERS A macro is defined as 1.
freeRTOS Software timer for , It is realized through a task used inside the system , Namely prvTimerTask function , The mission is timers.c The document defines , It is generated by the system itself , Users can leave it alone .
Besides , stay FreeRTOSConfig.h In file , There are also some macro definitions that need attention , See the picture below :

configTIMER_TASK_PRIORITY It defines the priority of timer tasks , Values can be 0~(configMAX_PRIORITIES-1), This value defines the timer task prvTimerTask The priority of the ;
configTIMER_QUEUE_LENGTH Defines the length of the timer queue , That is, it can define several software timers ;
configTIMER_TASK_STACK_DEPTH Defines the stack size of timer tasks , What is defined in the figure is 256 word , This stack is also the stack that the timer callback function will use , So we can't define it too small .
2) Common timer operation functions
freeRTOS The timer of is controlled by TimerHandle_t The type definition .
Create timer :
TimerHandle_t xTimerCreate(
const char * const pcTimerName, /* Timer name , character string */
const TickType_t xTimerPeriod, /* Timer cycle , The unit is the system clock beat */
const UBaseType_t uxAutoReload, /* Single execution or periodic execution ,pdTRUE cycle ,pdFALSE A single */
void * const pvTimerID, /* Timer ID, This parameter is mainly used when multiple timers share a callback function , Get... In the callback function ID To identify which timer called it */
TimerCallbackFunction_t pxCallbackFunction /* Timer callback function */ );
This function actually creates timers dynamically , You can also use xTimerCreateStatic To statically create .
Another thing to be aware of , Timer callback function , It can only be defined in the following form :(*TimerCallbackFunction_t)( TimerHandle_t xTimer ); That is, there is only one parameter passed in , Is the handle of the timer ;
Start timer :
BaseType_t xTimerStart( TimerHandle_t xTimer, /* Timer handle */
TickType_t xBlockTime ); /* The maximum waiting time before successfully starting the timer is set , The unit is the system clock beat */
This function can only be called in the task , If you want to start the timer in the interrupt , Need to use xTimerStartFromISR().
Stop timer :
BaseType_t xTimerStop( TimerHandle_t xTimer, /* Timer handle */
TickType_t xTicksToWait );/* The maximum waiting time before successfully stopping the timer , The unit is the system clock beat */
This function can only be called in the task , If you want to stop the timer in the interrupt , Need to use xTimerStopFromISR().
Get the of the timer ID value :
void *pvTimerGetTimerID( TimerHandle_t xTimer ); /* The input parameter is timer handle , return ID */
3) Programming test
adopt cubemx After generating the project file , stay keil in , Three tasks are generated :

Notice the task priority here ,defaultTask The priority of the osPriorityNormal Pass by HAL Transformation of library function , Corresponding freeRTOS Priority in 3
Let's first define the handles of three timers :

Before starting the task , Create three timers , And activate them :
Timer 01 and 02 Are set to work periodically , Timer 01 The period is 1s, Timer 02 The period is 2s; Timer 03 Just run 1 Time ; Here we are to test the timer ID Acquisition function , Let them share a callback function TimerCallback

The timer returns to the function TimerCallback The implementation is as follows , Get the of the timer ID, And print it out :

stay defaultTask in , Time delay 10s Then stop three timers :

compile , Download to the development board to run , The output of this program is as follows :

You can see , Timer 01 Every time 1s To run a , Timer 02 Every time 2s To run a , Timer 03 Only in the first 3s It's running 1 Time . The test results are consistent with the timer settings .
The timer has been running 9s, This is because defaultTask The priority of is 3, and FreeRTOSConfig.h Priority of timer task in configTIMER_TASK_PRIORITY Define a 2, Than defaultTask Low priority , So in the first 10s when , First execute the timer stop , The timer callback function will no longer be called .
If you put configTIMER_TASK_PRIORITY The macro definition is changed to 4, Than defaultTask High priority , So the first 10s The timer task will be executed first ,defaultTask The operation of stopping the timer in will be executed after ; The output will be more than 10s The callback function prints twice , The test results are as follows :

You can see that a total of 10s Information about .
Okay , That's all for this section .
If you find it useful, you can pay attention to the author's wechat “ Xiaobai learns Electronics ”, You can find the code and data download address in the official account :

边栏推荐
猜你喜欢

【LeetCode】11. 盛最多水的容器 - Go 语言题解

Win10 sound icon has no sound

From the perspective of CIA, common network attacks (blasting, PE, traffic attacks)

My creation anniversary

Mongodb application scenario and model selection (massive data storage model selection)

JMeter notes 2 | JMeter principle and test plan elements

Chapter007 FPGA learning IIC bus EEPROM reading

我的创作纪念日

InjectFix原理学习(实现修复加法的热更)

项目上线就炸,这谁受得了
随机推荐
MongoDB应用场景及选型(海量数据存储选型)
记账APP:小哈记账1——欢迎页的制作
Flow control statement of avascript
Blockbuster live broadcast | orb-slam3 series code explanation map points (topic 2)
第二部分—C语言提高篇_3. 指针强化
C language from entry to soil function
MySQL语句
Mqtt learning
[leetcode] 444. Sequence reconstruction
电子商务时代,企业社交电商转型要做什么?
Raspberry pie change source
定制 or 通用,中国 SaaS 未来发展趋势是什么?
树莓派换源
MITRE ATT&CK超详细学习笔记-02(大量案例)
B. Also Try Minecraft
【C语言入门】ZZULIOJ 1011-1015
Opencascade notes: GP package
Unity中使用深度和法线纹理
OpenCascade笔记:gp包
CSDN, it's time to say goodbye!