px-fwlib 0.10.0
Cross-platform embedded library and documentation for 8/16/32-bit microcontrollers generated with Doxygen 1.9.2
px_systmr.c
1/* =============================================================================
2 ____ ___ ____ ___ _ _ ___ __ __ ___ __ __ TM
3 | _ \ |_ _| / ___| / _ \ | \ | | / _ \ | \/ | |_ _| \ \/ /
4 | |_) | | | | | | | | | | \| | | | | | | |\/| | | | \ /
5 | __/ | | | |___ | |_| | | |\ | | |_| | | | | | | | / \
6 |_| |___| \____| \___/ |_| \_| \___/ |_| |_| |___| /_/\_\
7
8 Copyright (c) 2008 Pieter Conradie <https://piconomix.com>
9
10 License: MIT
11 https://github.com/piconomix/px-fwlib/blob/master/LICENSE.md
12
13 Title: px_systmr.h : Polled software timers
14 Author(s): Pieter Conradie
15 Creation Date: 2008-02-11
16
17============================================================================= */
18
19/* _____PROJECT INCLUDES_____________________________________________________ */
20#include "px_systmr.h"
21
22/* _____LOCAL DEFINITIONS____________________________________________________ */
23
24/* _____MACROS_______________________________________________________________ */
25
26/* _____GLOBAL VARIABLES_____________________________________________________ */
27
28/* _____LOCAL VARIABLES______________________________________________________ */
29
30/* _____LOCAL FUNCTION DECLARATIONS__________________________________________ */
31
32/* _____LOCAL FUNCTIONS______________________________________________________ */
33
34/* _____GLOBAL FUNCTIONS_____________________________________________________ */
35void px_systmr_start(px_systmr_t * systmr, const px_systmr_ticks_t delay_in_ticks)
36{
37 // Save delay in case timer is restarted
38 systmr->delay_in_ticks = delay_in_ticks;
39 // Store start tick
41 // Set state to indicate that timer has started
42 systmr->state = PX_SYSTMR_STARTED;
43}
44
46{
47 if (systmr->state == PX_SYSTMR_STARTED)
48 {
49 return true;
50 }
51 else
52 {
53 return false;
54 }
55}
56
58{
62
63 // See if timer has been stopped
64 if (systmr->state == PX_SYSTMR_STOPPED)
65 {
66 return false;
67 }
68 // See if timer has already expired
69 if (systmr->state == PX_SYSTMR_EXPIRED)
70 {
71 return true;
72 }
73 // Timer expire test
75 start = systmr->start_tick;
76 end = systmr->start_tick + systmr->delay_in_ticks;
77 if(start < end)
78 {
79 // |________xxxxxxxxxx__________|
80 // S>>>>>>>>E
81 if( (tick >= start) && (tick < end) )
82 {
83 // Timer has not expired yet
84 return false;
85 }
86 }
87 else
88 {
89 // |xxxxxxx__________________xxx|
90 // >>>>>>E S>>
91 if( (tick >= start) || (tick < end) )
92 {
93 // Timer has not expired yet
94 return false;
95 }
96 }
97 // Set state to indicate that timer has expired
98 systmr->state = PX_SYSTMR_EXPIRED;
99
100 return true;
101}
102
104{
105 // Stop timer
106 systmr->state = PX_SYSTMR_STOPPED;
107}
108
110{
111 // Store start tick
113 // Set state to indicate that timer has started
114 systmr->state = PX_SYSTMR_STARTED;
115}
116
118{
119 // Calculate and store new start tick
120 systmr->start_tick += systmr->delay_in_ticks;
121 // Set state to indicate that timer has started
122 systmr->state = PX_SYSTMR_STARTED;
123}
124
125void px_systmr_wait(const px_systmr_ticks_t delay_in_ticks)
126{
127 px_systmr_t systmr;
128
129 px_systmr_start(&systmr, delay_in_ticks);
130 while (!px_systmr_has_expired(&systmr))
131 {
132 ;
133 }
134}
136{
137 // Fetch current time
139 px_systmr_ticks_t ticks_elapsed = tick - systmr->start_tick;
140
141 return ticks_elapsed;
142}
px_systmr_state_t state
State of timer: STOPPED, STARTED or EXPIRED.
Definition: px_systmr.h:76
px_systmr_ticks_t delay_in_ticks
Timer delay, used for subsequent timer restarts/resets.
Definition: px_systmr.h:78
px_systmr_ticks_t start_tick
Tick when timer started.
Definition: px_systmr.h:77
px_systmr_ticks_t px_systmr_ticks_elapsed(px_systmr_t *systmr)
Return the number of ticks that have elapsed sinced the timer has been started.
Definition: px_systmr.c:135
bool px_systmr_has_started(const px_systmr_t *systmr)
See if a timer has been started.
Definition: px_systmr.c:45
void px_systmr_stop(px_systmr_t *systmr)
Stop a running timer.
Definition: px_systmr.c:103
void px_systmr_start(px_systmr_t *systmr, const px_systmr_ticks_t delay_in_ticks)
Start a timer.
Definition: px_systmr.c:35
px_sysclk_ticks_t px_systmr_ticks_t
Size definition of the tick counter.
Definition: px_systmr.h:63
void px_systmr_restart(px_systmr_t *systmr)
Restart a timer with the delay set with px_systmr_start().
Definition: px_systmr.c:109
void px_systmr_reset(px_systmr_t *systmr)
Reset a timer with the delay set with px_systmr_start().
Definition: px_systmr.c:117
void px_systmr_wait(const px_systmr_ticks_t delay_in_ticks)
Blocking wait for specified number of ticks.
Definition: px_systmr.c:125
bool px_systmr_has_expired(px_systmr_t *systmr)
See if a timer has expired.
Definition: px_systmr.c:57
Structure to track state of a timer.
Definition: px_systmr.h:75
px_sysclk_ticks_t px_sysclk_get_tick_count(void)
Return number of ticks since system clock started.
Definition: px_sysclk.c:97