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.h
1#ifndef __PX_SYSTMR_H__
2#define __PX_SYSTMR_H__
3/* =============================================================================
4 ____ ___ ____ ___ _ _ ___ __ __ ___ __ __ TM
5 | _ \ |_ _| / ___| / _ \ | \ | | / _ \ | \/ | |_ _| \ \/ /
6 | |_) | | | | | | | | | | \| | | | | | | |\/| | | | \ /
7 | __/ | | | |___ | |_| | | |\ | | |_| | | | | | | | / \
8 |_| |___| \____| \___/ |_| \_| \___/ |_| |_| |___| /_/\_\
9
10 Copyright (c) 2008 Pieter Conradie <https://piconomix.com>
11
12 License: MIT
13 https://github.com/piconomix/px-fwlib/blob/master/LICENSE.md
14
15 Title: px_systmr.h : Polled software timers
16 Author(s): Pieter Conradie
17 Creation Date: 2008-02-11
18
19=========================================================================== */
20
21/**
22 * @ingroup UTILS
23 * @defgroup PX_SYSTMR px_systmr.h : Polled software timers
24 *
25 * Non-blocking software timers that have to be polled to determine if they
26 * have expired.
27 *
28 * File(s):
29 * - utils/inc/px_systmr.h
30 * - utils/src/px_systmr.c
31 *
32 * These timers are suitable for applications that are not timing critical.
33 * A global counter is incremented with each clock tick and this value is
34 * polled to determine if a timer has expired. Provision is made for counter
35 * roll-over.
36 *
37 * This module depends on a system clock module, e.g. @ref AVR_SYSCLK to
38 * return a counter that is incremented with every system clock tick. The
39 * number of ticks per second (Hz) is defined with #PX_SYSTMR_TICKS_PER_SEC.
40 *
41 * Example:
42 *
43 * @include utils/test/px_systmr_test.c
44 *
45 * @{
46 */
47
48/* _____STANDARD INCLUDES____________________________________________________ */
49
50/* _____PROJECT INCLUDES_____________________________________________________ */
51#include "px_defs.h"
52#include "px_sysclk.h"
53
54#ifdef __cplusplus
55extern "C" {
56#endif
57/* _____DEFINITIONS__________________________________________________________ */
58/// The number of timer ticks per second
59#define PX_SYSTMR_TICKS_PER_SEC PX_SYSCLK_CFG_TICKS_PER_SEC
60
61/* _____TYPE DEFINITIONS_____________________________________________________ */
62/// Size definition of the tick counter
64
65/// Timer state
66typedef enum
67{
68 PX_SYSTMR_STOPPED = 0,
69 PX_SYSTMR_STARTED,
70 PX_SYSTMR_EXPIRED,
72
73/// Structure to track state of a timer
74typedef struct
75{
76 px_systmr_state_t state; ///< State of timer: STOPPED, STARTED or EXPIRED
77 px_systmr_ticks_t start_tick; ///< Tick when timer started
78 px_systmr_ticks_t delay_in_ticks; ///< Timer delay, used for subsequent timer restarts/resets
80
81/* _____GLOBAL FUNCTION DECLARATIONS_________________________________________ */
82/**
83 * Start a timer.
84 *
85 * @param[in,out] systmr Pointer to a timer object
86 * @param[in] delay_in_ticks Delay in timer ticks
87 */
88void px_systmr_start(px_systmr_t * systmr, const px_systmr_ticks_t delay_in_ticks);
89
90/**
91 * See if a timer has been started.
92 *
93 * @param[in] systmr Pointer to a timer object
94 *
95 * @retval true timer started
96 * @retval false timer stopped or expired
97 */
98bool px_systmr_has_started(const px_systmr_t * systmr);
99
100/**
101 * See if a timer has expired.
102 *
103 * @param[in,out] systmr Pointer to a timer object
104 *
105 * @retval true timer expired
106 * @retval false timer not expired or timer stopped
107 */
109
110/**
111 * Stop a running timer.
112 *
113 * @param[in,out] systmr Pointer to a timer object
114 */
115void px_systmr_stop(px_systmr_t * systmr);
116
117/**
118 * Restart a timer with the delay set with px_systmr_start().
119 *
120 * The timer will expire from the current timer tick + systmr->delay_in_ticks.
121 *
122 * @param[in,out] systmr Pointer to a timer object
123 */
124void px_systmr_restart(px_systmr_t * systmr);
125
126/**
127 * Reset a timer with the delay set with px_systmr_start().
128 *
129 * The timer will expire on systmr->start_tick + systmr->delay_in_ticks.
130 *
131 * Use this function instead of px_systmr_restart() for periodic timers,
132 * because the frequency will not drift over time. An error may accumulate if
133 * there is a delay between px_systmr_has_expired() and px_systmr_restart().
134 * Thus the prefered usage for a periodic timer is:
135 *
136 * @code{.c}
137 * while(true)
138 * {
139 * // Wait until timer has expired
140 * while(px_systmr_has_expired(&systmr) == false)
141 * {
142 * ;
143 * }
144 * // Reset periodic timer
145 * px_systmr_reset(&systmr);
146 * // Do something...
147 * }
148 * @endcode
149 *
150 * @param[in,out] systmr Pointer to a timer object
151 */
152void px_systmr_reset(px_systmr_t * systmr);
153
154/**
155 * Blocking wait for specified number of ticks.
156 *
157 * @param[in] delay_in_ticks Delay in timer ticks
158 */
159void px_systmr_wait(const px_systmr_ticks_t delay_in_ticks);
160
161/**
162 * Return the number of ticks that have elapsed sinced the timer has been started.
163 *
164 * @param[in,out] systmr Pointer to a timer object
165 *
166 * @return px_systmr_ticks_t Number of ticks elapsed
167 */
169
170/* _____MACROS_______________________________________________________________ */
171/**
172 * Macro used to convert a timeout in milliseconds to timer ticks.
173 *
174 * @param[in] delay_in_ms Delay in milliseconds
175 * @return Delay in timer ticks
176 */
177#define PX_SYSTMR_MS_TO_TICKS(delay_in_ms) \
178 PX_UDIV_ROUND((delay_in_ms) * PX_SYSTMR_TICKS_PER_SEC, 1000ul)
179
180#ifdef __cplusplus
181}
182#endif
183
184/// @}
185#endif
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
px_systmr_state_t
Timer state.
Definition: px_systmr.h:67
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
uint32_t px_sysclk_ticks_t
Size definition of the tick counter.
Definition: px_sysclk.h:74