[rtthread learning notes series] Part 1: timer

Time:2021-10-19

1、 Timer concept
1.1 clock beat

Clock beat is the smallest time unit of the system, macro RT_ TICK_ PER_ When the second is 100, the time of a clock beat is 10ms, and the clock beat is also the smallest unit of thread time slice. In STM32, the clock beat is realized through systick interrupt. In each systick interrupt, the global variable RT_ Tick plus 1.

void SysTick_Handler(void)
{

/Entry interrupt/
rt_interrupt_enter();
……
rt_tick_increase();
/Exit interrupt/
rt_interrupt_leave();

}

Gets the current clock beat RT_ Tick can be through RT_ tick_ Get function.
1.2 rtthread timer
1.2.1 rtthread timer concept

The timing time of rtthread timer is based on the time of one beat of the clock. The timers created and activated will be sorted by the timeout time and linked in RT_ timer_ On the list, execute RT every time the hardware interrupts_ tick_ Increase call RT_ timer_ Check to determine whether a timeout event is generated.
1.2.2 rtthread timer mode

HARD_ Timer: the timer timeout function is performed in the context of hardware systick interrupt. It should be noted that the execution time should be as short as possible. It does not execute memory application and release operations, and cannot execute functions that will hang.
SOFT_ Timer: through RT_ USING_ TIMER_ Soft determines whether to enable this function. The system will create a timer thread during initialization. The context of the timer timeout function in this mode is thread.

2、 Rtthread timer API

//Create a timer
/*
Name: timer name
Timeout: timeout function
Parameter: parameter of timeout function
Time: timeout, unit: clock beat
Flag: Flag
*/
rt_timer_t rt_timer_create(const char* name,

                        void (*timeout)(void* parameter),
                        void* parameter,
                        rt_tick_t time,
                        rt_uint8_t flag);

//Delete clock
/*
Timer: timer handle
*/
rt_err_t rt_timer_delete(rt_timer_t timer);

//Initialization timer
/*
Timer: timer handle
Name: timer name
Timeout: timeout function
Parameter: parameter of supermarket function
Time: timeout, unit: clock beat
Flag: Flag
*/
void rt_timer_init(rt_timer_t timer,

                const char* name,
                void (*timeout)(void* parameter),
                void* parameter,
                rt_tick_t time, rt_uint8_t flag);
     

Disengagement timer
/*
Timer: timer handle
*/
rt_err_t rt_timer_detach(rt_timer_t timer);

//Start timer
/*
Timer: timer handle
*/
rt_err_t rt_timer_start(rt_timer_t timer);

//Stop timer
/*
Timer: timer handle
*/
rt_err_t rt_timer_stop(rt_timer_t timer);

//Control timer
/*
Timer: timer handle
CMD: control command
Arg: control command parameters
*/
rt_err_t rt_timer_control(rt_timer_t timer, rt_uint8_t cmd, void* arg);

Timer control CMD:
define RT_ TIMER_ CTRL_ SET_ Time 0x0 / set timer timeout/
define RT_ TIMER_ CTRL_ GET_ Time 0x1 / get timer timeout/
define RT_ TIMER_ CTRL_ SET_ Oneshot 0x2 / set timer to single timer/
define RT_ TIMER_ CTRL_ SET_ Periodic 0x3 / set timer to periodic timer/

3、 Example

This example creates a periodic timer Timer1, a one-time timer timer2, and rtthread. The default timer working mode is hard_ Timer mode, execute timeout1 and timeout2 after the timeout is met. The periodic timer stops the timer after performing 10 timing tasks.

/*

Copyright (c) 2006-2018, RT-Thread Development Team
*
SPDX-License-Identifier: Apache-2.0
*
Change Logs:
Date Author Notes
2018-08-24 yangjie the first version
*/

/*

Program list: timer routine
*
This routine will create two dynamic timers, one is single timing and the other is periodic timing

And stop the cycle timer after running for a period of time
*/
include <rtthread.h>

/Timer control block/
static rt_timer_t timer1;
static rt_timer_t timer2;
static int cnt = 0;

/Timer 1 timeout function/
static void timeout1(void *parameter)
{

rt_kprintf(“periodic timer is timeout %d\n”, cnt);

/Run the 10th time and stop the cycle timer/
if (cnt++ >= 9)
{

rt_timer_stop(timer1);
rt_kprintf("periodic timer was stopped! \n");

}

}

/Timer 2 timeout function/
static void timeout2(void *parameter)
{

rt_kprintf(“one shot timer is timeout\n”);

}

int timer_sample(void)
{

/Create timer 1 cycle timer/
timer1 = rt_timer_create(“timer1”, timeout1,

                     RT_NULL, 10,
                     RT_TIMER_FLAG_PERIODIC);

/Start timer 1/
if (timer1 != RT_NULL)

rt_timer_start(timer1);

/Create timer 2 single timer/
timer2 = rt_timer_create(“timer2”, timeout2,

                     RT_NULL,  30,
                     RT_TIMER_FLAG_ONE_SHOT);

/Start timer 2/
if (timer2 != RT_NULL)

rt_timer_start(timer2);

return 0;

}

/Export to MSH command list/
MSH_CMD_EXPORT(timer_sample, timer sample);

Original text:https://club.rt-thread.org/as…

Recommended Today

SQL exercise 20 – Modeling & Reporting

This blog is used to review and sort out the common topic modeling architecture, analysis oriented architecture and integration topic reports in data warehouse. I have uploaded these reports to GitHub. If you are interested, you can have a lookAddress:https://github.com/nino-laiqiu/TiTanI recorded a relatively complete development process in my hexo blog deployed on GitHub. You can […]