当前位置:网站首页>[actual combat] STM32 FreeRTOS porting series Tutorial 4: FreeRTOS software timer

[actual combat] STM32 FreeRTOS porting series Tutorial 4: FreeRTOS software timer

2022-06-21 09:13:00 Huaqing vision it open laboratory

Write it at the front :

This article is 《STM32MP157 Developing a tutorial FreeRTOS Operating system 》 One in a series , The development platform used by the author is Huaqing vision FS-MP1A Development board (STM32MP157 Development board ).stm32mp157 yes ARM Dual core ,2 individual A7 nucleus ,1 individual M4 nucleus ,A7 You can run on the core Linux operating system ,M4 You can run on the core FreeRTOS、RT-Thread And other real-time operating systems ,STM32MP157 Development board, so you can learn Embedded linux, You can also learn. stm32 Single chip microcomputer .

in the light of FS-MP1A Development board , except FreeRTOS Outside the operating system , There are also many other series of tutorials , Include Cortex-A7 Development of article 、Cortex-M4 Development of article 、 Expansion board driver transplantation 、Linux Application development 、Linux System transplantation 、Linux Driving development 、 Hardware design 、 Artificial intelligence machine vision 、Qt Application programming 、Qt Comprehensive project practice, etc . Welcome to your attention , more stm32mp157 Develop tutorials and videos , Technical exchange can be added Q Group 459754978, Thank you for attention .

FS-MP1A Development board Details :https://item.taobao.com/item.htm?id=622457259672

2.FreeRTOS Software timer

2.1 Introduction to software timer

about MCU for , Its own timer belongs to hardware timer , stay FreeRTOS In the system , In addition, the software timer function is provided . The precision of software timer is not as good as that of hardware timer , But it's enough for less demanding periodic processing tasks .

The software timer can be set for a period of time , When the set time arrives, the callback function is executed , among , The interval between two executions of the callback function is the timer's timing period , That is, the callback function is executed every time the timing period arrives . Because the callback function is executed in the timer service function , Therefore, you must not call the callback function that will block the task API function .

The software timer is divided into two types: cycle timer and single timer . After the cycle timer is started, it will automatically restart after the callback function is executed , So the callback function can be executed periodically , For a single timer , The callback function is executed only once , After execution , The timer will stop , Of course, we can call the function to manually restart again .

2.2 Software timer function

2.2.1 Reset timer

When the timer is running normally , Sometimes we may need to reset the timer , When resetting the software timer , The system will recalculate the time point when the timing period arrives , But this time point is calculated relative to the reset time , It's not the time when the software timer is started for the first time .

In the new FreeRTOS in , There are two API Function to reset the software timer , Apply to tasks and interrupts respectively .

Task level reset software timer function xTimerReset(), This function is a macro , The function prototype is as follows

BaseType_t xTimerReset( TimerHandle_t xTimer,

TickType_t xTicksToWait)

Parameters :

xTimer: Handle of the software timer to be reset .

xTickToWait: Set blocking time .

Return value :

pdPASS: Software timer reset succeeded , That is, the command is successfully sent .

pdFAIL: Software timer reset failed , That is, command sending failed .

Task level reset software timer function xTimerReset(), This function is a macro , The function prototype is as follows

BaseType_t xTimerResetFromISR( TimerHandle_t xTimer,

BaseType_t * pxHigherPriorityTaskWoken );

Parameters :

xTimer: Handle of the software timer to be reset .

pxHigherPriorityTaskWoken: Remember whether to switch tasks after exiting this function , The user does not need to set .

Return value :

pdPASS: Software timer reset succeeded , That is, the command is successfully sent .

pdFAIL: Software timer reset failed , That is, command sending failed .

2.2.2 Create software timer

There are two functions for creating software timers , They are as follows :

xTimerCreate(), This function is used to create a software timer , The required memory is allocated through dynamic memory management , After creation, the software timer is in sleep state , The function prototype is as follows :

TimerHandle_t xTimerCreate( const char * const pcTimerName,

TickType_t xTimerPeriodInTicks,

UBaseType_t uxAutoReload,

void * pvTimerID,

TimerCallbackFunction_t pxCallbackFunction )

Parameters :

pcTimerName: Software timer name , It's a string , For debugging use .

xTimerPeriodInTicks: Timer cycle , The unit is the number of clock beats .

uxAutoReload: Set timer mode , Single timer or periodic timer .

pvTimerID: Timer ID Number .

pxCallbackFunction: Timer callback function .

Return value :

NULL: Software timer creation failed .

Other values : Create a successful software timer handle .

xTimerCreateStatic(), This function is also used to create software timers , The required memory is allocated by the user , After creation, the software timer is in sleep state , The function prototype is as follows :

TimerHandle_t xTimerCreateStatic(const char * const pcTimerName,

TickType_t xTimerPeriodInTicks,

UBaseType_t uxAutoReload,

void * pvTimerID,

TimerCallbackFunction_t pxCallbackFunction,

StaticTimer_t * pxTimerBuffer )

Parameters :

pcTimerName: Software timer name , It's a string , For debugging use .

xTimerPeriodInTicks: Timer cycle , The unit is the number of clock beats .

uxAutoReload: Set timer mode , Single timer or periodic timer .

pvTimerID: Timer ID Number .

pxCallbackFunction: Timer callback function .

pxTimerBuffer: Point to one StaticTimer_t Variable of type , Used to save timer structure .

Return value :

NULL: Software timer creation failed .

Other values : Create a successful software timer handle .

2.2.3 Turn on the software timer

There are also two functions to start the software timer , They are as follows :

Task level start timer function xTimerStart(), This function is a macro , If the software timer is not running, call the function xTimerStart() Will calculate the timer expiration time , If the software timer is running, call the function xTimerStart() The result and xTimerReset() equally , The function prototype is as follows :

BaseType_t xTimerStart( TimerHandle_t xTimer,

TickType_t xTicksToWait )

Parameters :

xTimer: The software timer handle to open .

xTickToWait: Set blocking time .

Return value :

pdPASS: The software timer is turned on successfully , That is, the command is successfully sent .

pdFAIL: Software timer failed to start , That is, command sending failed .

Interrupt level start timer function xTimerStartFromISR(), This function is also a macro , For the function xTimerStart() Broken version of , Can only be used in interrupt service functions , The function prototype is as follows :

BaseType_t xTimerStartFromISR( TimerHandle_t xTimer,

BaseType_t * pxHigherPriorityTaskWoken );

Parameters :

xTimer: The software timer handle to open .

pxHigherPriorityTaskWoken: Mark whether to switch tasks after exiting this function .

Return value :

pdPASS: The software timer is turned on successfully , That is, the command is successfully sent .

pdFAIL: Software timer failed to start , That is, command sending failed .

2.2.4 Stop the software timer

It is the same as turning on the software timer , There are also two functions to stop the software timer , They are as follows :

Task level stop timer function xTimerStop(), This function is a macro , The function prototype is as follows :

BaseType_t xTimerStop( TimerHandle_t xTimer,

TickType_t xTicksToWait )

Parameters :

xTimer: Software timer handle to stop .

xTickToWait: Set blocking time .

Return value :

pdPASS: Software timer stopped successfully , That is, the command is successfully sent .

pdFAIL: Software timer stop failed , That is, command sending failed .

Interrupt level stop timer function xTimerStopFromISR(), This function is also a macro , For the function xTimerStop() Broken version of , Can only be used in interrupt service functions , The function prototype is as follows :

BaseType_t xTimerStopFromISR( TimerHandle_t xTimer,

BaseType_t * pxHigherPriorityTaskWoken );

Parameters :

xTimer: Software timer handle to stop .

pxHigherPriorityTaskWoken: Mark whether to switch tasks after exiting this function .

Return value :

pdPASS: Software timer stopped successfully , That is, the command is successfully sent .

pdFAIL: Software timer stop failed , That is, command sending failed .

2.3 Operation experiment

2.3.1 Experimental design

This experiment creates two software timers , They are cycle timers CycleTimer_Handle And single timer SingleTimer_Handle, among ,CycleTimer_Handle The timer period of is 1000 Individual clock time ,SingleTimer_Handle The timer period of is 2000 Individual clock time .

May refer to 12.3.2 Chapter to import the existing project , Project storage path 【 Huaqing vision -FS-MP1A Development of information \02- Program source code \ARM Architecture and interface technology \FreeRTOS\4_MP1A-FreeRTOS-TIMER】

Set different commands through key interrupt , In the task, it controls the work of the cycle timer and the single timer according to the received instructions .

The tasks and their functions are as follows :

StartTask02(): control LED3 flashing , Indicates that the system is running .

StartDefaultTask(): Processing instructions , Control the timer to work according to the received instructions .

2.3.2 Experimental process and analysis

First , Configure... According to the previous chapters CubeMX, Configure... According to the previous section “FREERTOS”, Generate code when finished . stay StartDefaultTask() And StartTask02() Add the following code to .

STM32MP157 Resource expansion board driven migration 10:FreeRTOS Software timer

When pressed KEY1 Key time , The single timer works , When the timer time is up, it will call OneShotCallback() function , You will find LED1 The level of is reversed , Then stop running . When pressed KEY2 Key time , The cycle timer works , When the timer time is up, it will call AutoReloadCallback() function , Because the cycle timer doesn't stop , You will find LED2 every other 1 The second level is reversed . When pressed KEY3 Key time , Single timer and cycle timer off ,LED The level of the lamp is no longer changed .

Hardware platform : Huaqing vision FS-MP1A Development board (STM32MP157)

Download some development tutorials : Add QQ Group 459754978, There are in the group file .

Watch some video courses : Personal space of Huaqing vision R & D Center _ Bili, Bili _Bilibili

原网站

版权声明
本文为[Huaqing vision it open laboratory]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/172/202206210908576352.html