-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsoftware_timer.h
More file actions
114 lines (104 loc) · 2.54 KB
/
software_timer.h
File metadata and controls
114 lines (104 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
* soft_timer/software_timer.h
*
* Created on: 31 sty 2021
* Author: slawek
*/
#ifndef SOFTWARE_TIMER_H_
#define SOFTWARE_TIMER_H_
#include "software_timer_port.h"
/*! \class Timer
* \brief Software timer
*
* Conveys foreground (ISR) time events to background loop
*
*/
typedef struct Timer {
_Bool active;
volatile Counter cnt;
} Timer;
/*! \def Ticks
* \brief Ticks calculator
*
* Calculates timer event periods into timer ticks
*
* @param period length of period in milliseconds
* @param timer_period length of hardware timer period in milliseconds
*
*/
#define TICKS(period,timer_period) ((period)/(timer_period)+(((period)%(timer_period))?1:0))
/*! \fn Constructor
* \brief Class initializer
*
* Initializes software timer instance. Not needed when instance is allocated
* in BSS section.
*
* @param me pointer to software timer instance
*/
static inline void Timer_ctor(Timer * const me)
{
me->active = 0;
CRITICAL_SECTION_BEGIN
me->cnt = 0;
CRITICAL_SECTION_END
}
/*! \fn Setter
* \brief Set period and start counting
*
* @param me pointer to software timer instance
* @param ticks number of ticks to go, usually computed by TICKS macro
*/
static inline void Timer_setPeriod(Timer * const me, Counter ticks)
{
CRITICAL_SECTION_BEGIN
me->cnt = ticks;
CRITICAL_SECTION_END
me->active = 1;
}
/*! \fn Activator
* \brief Activates/decactivates software timer
*
* Deactivation of timer which is set (!=0) doesn't stop counting ticks.
* Activation of timer which is not set (==0) fires next isTime function.
*
* @param me pointer to software timer instance
* @param activeness bool state of timer to be set
*/
static inline void Timer_activate(Timer * const me, _Bool activeness)
{
me->active = activeness;
}
/*! \fn Checker
* \brief checks if timer expired
*
* Function is to be called from background loop.
* First call after expiry deactivates software timer
*
* @param me pointer to software timer instance
* @return 1 if timer expired and active, 0 otherwise
*/
static inline _Bool Timer_isTime(Timer * const me)
{
Counter cnt;
_Bool result;
CRITICAL_SECTION_BEGIN
cnt = me->cnt;
CRITICAL_SECTION_END
result = !cnt && me->active && !(me->active=0);
return result;
}
/*! \fn Counter
* \brief Counts time
*
* Function should be called from within timer interrupt
*
* @param me pointer to software timer instance
*/
static inline void Timer_count(Timer * const me)
{
Counter tmp = me->cnt;
if(tmp) {
me->cnt=--tmp;
}
}
#endif /* SOFTWARE_TIMER_H_ */