AFBR-S50 API Reference Manual v1.5.6
AFBR-S50 Time-of-Flight Sensor SDK for Embedded Software
Loading...
Searching...
No Matches
argus_timer.h
Go to the documentation of this file.
1/*************************************************************************/
37#ifndef ARGUS_TIMER_H
38#define ARGUS_TIMER_H
39#ifdef __cplusplus
40extern "C" {
41#endif
42
43/*!***************************************************************************
44 * @defgroup argus_timer Timer: Hardware Timer Interface
45 * @ingroup argus_hal
46 *
47 * @brief Timer implementations for lifetime counting as well as periodic
48 * callback.
49 *
50 * @details The module provides an interface to the timing utilities that
51 * are required by the AFBR-S50 time-of-flight sensor API.
52 *
53 * Two essential features have to be provided by the user code:
54 * 1. Time Measurement Capability: In order to keep track of outgoing
55 * signals, the API needs to measure elapsed time. In order to
56 * provide optimum device performance, the granularity should be
57 * around 10 to 100 microseconds.
58 * 2. Periodic Callback: The API provides an automatic starting of
59 * measurement cycles at a fixed frame rate via a periodic
60 * interrupt timer. If this feature is not used, implementation
61 * of the periodic interrupts can be skipped. An weak default
62 * implementation is provide in the API.
63 * .
64 *
65 * The time measurement feature is simply implemented by the function
66 * #Timer_GetCounterValue. Whenever the function is called, the
67 * provided counter values must be written with the values obtained
68 * by the current time.
69 *
70 * The periodic interrupt timer is a simple callback interface.
71 * After installing the callback function pointer via #Timer_SetCallback,
72 * the timer can be started by setting interval via #Timer_SetInterval.
73 * From then, the callback is invoked periodically as the corresponding
74 * interval may specify. The timer is stopped by setting the interval
75 * to 0 using the #Timer_SetInterval function. The interval can be
76 * updated at any time by updating the interval via the #Timer_SetInterval
77 * function. To any of these functions, an abstract parameter pointer
78 * must be passed. This parameter is passed back to the callback any
79 * time it is invoked.
80 *
81 * In order to provide the usage of multiple devices, an mechanism is
82 * introduced to allow the installation of multiple callback interval
83 * at the same time. Therefore, the abstract parameter pointer is used
84 * to identify the corresponding callback interval. For example, there
85 * are two callbacks for two intervals, t1 and t2, required. The user
86 * can start two timers by calling the #Timer_SetInterval method twice,
87 * but with an individual parameter pointer, ptr1 and ptr2, each:
88 * \code
89 * Timer_SetInterval(100000, ptr1); // 10 ms callback w/ parameter ptr1
90 * Timer_SetInterval(200000, ptr2); // 20 ms callback w/ parameter ptr1
91 * \endcode
92 *
93 * Note that the implemented timer module must therefore support
94 * as many different intervals as instances of the AFBR-S50 device are
95 * used.
96 *
97 * @addtogroup argus_timer
98 * @{
99 *****************************************************************************/
100
101#include "utility/status.h"
102
103/*******************************************************************************
104 * Lifetime Counter Timer Interface
105 ******************************************************************************/
106
107/*!***************************************************************************
108 * @brief Obtains the lifetime counter value from the timers.
109 *
110 * @details The function is required to get the current time relative to any
111 * point in time, e.g. the startup time. The returned values \p hct and
112 * \p lct are given in seconds and microseconds respectively. The current
113 * elapsed time since the reference time is then calculated from:
114 *
115 * t_now [µsec] = hct * 1000000 µsec + lct * 1 µsec
116 *
117 * Note that the accuracy/granularity of the lifetime counter does
118 * not need to be 1 µsec. Usually, a granularity of approximately
119 * 100 µsec is sufficient. However, in case of very high frame rates
120 * (above 1000 frames per second), it is recommended to implement
121 * an even lower granularity (somewhere in the 10 µsec regime).
122 *
123 * It must be guaranteed, that each call of the #Timer_GetCounterValue
124 * function must provide a value that is greater or equal, but never lower,
125 * than the value returned from the previous call.
126 *
127 * A hardware based implementation of the lifetime counter functionality
128 * would be to chain two distinct timers such that counter 2 increases
129 * its value when counter 1 wraps to 0. The easiest way is to setup
130 * counter 1 to wrap exactly every second. Counter 1 would than count
131 * the sub-seconds (i.e. µsec) value (\p lct) and counter 2 the seconds
132 * (\p hct) value. A 16-bit counter is sufficient in case of counter 1
133 * while counter 2 must be a 32-bit version.
134 *
135 * In case of a lack of available hardware timers, a software solution
136 * can be used that requires only a 16-bit timer. In a simple scenario,
137 * the timer is configured to wrap around every second and increase
138 * a software counter value in its interrupt service routine (triggered
139 * with the wrap around event) every time the wrap around occurs.
140 *
141 *
142 * @note The implementation of this function is mandatory for the correct
143 * execution of the API.
144 *
145 * @param hct A pointer to the high counter value bits representing current
146 * time in seconds.
147 *
148 * @param lct A pointer to the low counter value bits representing current
149 * time in microseconds. Range: 0, .., 999999 µsec
150 *****************************************************************************/
151void Timer_GetCounterValue(uint32_t * hct, uint32_t * lct);
152
153/*******************************************************************************
154 * Periodic Interrupt Timer Interface
155 ******************************************************************************/
156
157/*!***************************************************************************
158 * @brief The callback function type for periodic interrupt timer.
159 *
160 * @details The function that is invoked every time a specified interval elapses.
161 * An abstract parameter is passed to the function whenever it is called.
162 *
163 * @param param An abstract parameter to be passed to the callback. This is
164 * also the identifier of the given interval.
165 *****************************************************************************/
166typedef void (*timer_cb_t)(void * param);
167
168/*!***************************************************************************
169 * @brief Installs an periodic timer callback function.
170 *
171 * @details Installs an periodic timer callback function that is invoked whenever
172 * an interval elapses. The callback is the same for any interval,
173 * however, the single intervals can be identified by the passed
174 * parameter.
175 * Passing a zero-pointer removes and disables the callback.
176 *
177 * @note The implementation of this function is optional for the correct
178 * execution of the API. If not implemented, a weak implementation
179 * within the API will be used that disable the periodic timer callback
180 * and thus the automatic starting of measurements from the background.
181 *
182 * @param f The timer callback function.
183 *
184 * @return Returns the \link #status_t status\endlink (#STATUS_OK on success).
185 *****************************************************************************/
187
188/*!***************************************************************************
189 * @brief Sets the timer interval for a specified callback parameter.
190 *
191 * @details Sets the callback interval for the specified parameter and starts
192 * the timer with a new interval. If there is already an interval with
193 * the given parameter, the timer is restarted with the given interval.
194 * If the same time interval as already set is passed, nothing happens.
195 * Passing a interval of 0 disables the timer.
196 *
197 * When enabling the timer (or resetting by applying another interval),
198 * the first timer interrupt must happen after the specified interval.
199 *
200 * Note that a microsecond granularity for the timer interrupt period is
201 * not required. Usually a milliseconds granularity is sufficient.
202 * The required granularity depends on the targeted frame rate, e.g. in
203 * case of more than 1 kHz measurement rate, a granularity of less than
204 * a millisecond is required to achieve the given frame rate.
205 *
206 * @note The implementation of this function is optional for the correct
207 * execution of the API. If not implemented, a weak implementation
208 * within the API will be used that disable the periodic timer callback
209 * and thus the automatic starting of measurements from the background.
210 *
211 * @param dt_microseconds The callback interval in microseconds.
212 *
213 * @param param An abstract parameter to be passed to the callback. This is
214 * also the identifier of the given interval.
215 *
216 * @return Returns the \link #status_t status\endlink (#STATUS_OK on success).
217 *****************************************************************************/
218status_t Timer_SetInterval(uint32_t dt_microseconds, void * param);
219
221#ifdef __cplusplus
222} // extern "C"
223#endif
224#endif /* ARGUS_TIMER_H */
int32_t status_t
Type used for all status and error return values.
Definition argus_status.h:70
void Timer_GetCounterValue(uint32_t *hct, uint32_t *lct)
Obtains the lifetime counter value from the timers.
status_t Timer_SetInterval(uint32_t dt_microseconds, void *param)
Sets the timer interval for a specified callback parameter.
status_t Timer_SetCallback(timer_cb_t f)
Installs an periodic timer callback function.
void(* timer_cb_t)(void *param)
The callback function type for periodic interrupt timer.
Definition argus_timer.h:166
This file is part of the AFBR-S50 API.