Mbed OS Reference
Loading...
Searching...
No Matches
us_ticker_api.h
1
2/** \addtogroup hal */
3/** @{*/
4/* mbed Microcontroller Library
5 * Copyright (c) 2006-2015 ARM Limited
6 * SPDX-License-Identifier: Apache-2.0
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20#ifndef MBED_US_TICKER_API_H
21#define MBED_US_TICKER_API_H
22
23#include <stdint.h>
24#include "hal/ticker_api.h"
25
26#ifdef __cplusplus
27extern "C" {
28#endif
29
30/**
31 * \defgroup hal_us_ticker Microsecond Ticker
32 * Low level interface to the microsecond ticker of a target
33 *
34 * # Defined behavior
35 * * Has a reported frequency between 250KHz and 8MHz for counters which are less than 32 bits wide - Verified by test us_ticker_info_test
36 * * Has a reported frequency up to 100MHz for counters which are 32 bits wide - Verified by test us_ticker_info_test
37 * * Has a counter that is at least 16 bits wide - Verified by test us_ticker_info_test
38 * * All behavior defined by the @ref hal_ticker_shared "ticker specification"
39 *
40 * # Undefined behavior
41 * * See the @ref hal_ticker_shared "ticker specification"
42 *
43 * @see hal_us_ticker_tests
44 *
45 * # Compile-time optimization macros
46 *
47 * To permit compile-time optimization, particularly of wait_us, the following macros should
48 * be defined by a target's device.h:
49 *
50 * US_TICKER_PERIOD_NUM, US_TICKER_PERIOD_DEN: These denote the ratio (numerator, denominator)
51 * of the ticker period to a microsecond. For example, an 8MHz ticker would have NUM = 1, DEN = 8;
52 * a 1MHz ticker would have NUM = 1, DEN = 1; a 250kHz ticker would have NUM = 4, DEN = 1.
53 * Both numerator and denominator must be 16 bits or less, but need not be fully simplified.
54 *
55 * US_TICKER_MASK: The value mask for the ticker - eg 0x07FFFFFF for a 27-bit ticker.
56 *
57 * If any are defined, all 3 must be defined, and the macros are checked for consistency with
58 * us_ticker_get_info by test us_ticker_info_test.
59
60 * @{
61 */
62
63/**
64 * \defgroup hal_us_ticker_tests Microsecond Ticker tests
65 * Tests to validate the proper implementation of the microsecond ticker
66 *
67 * To run the microsecond ticker hal tests use the command:
68 *
69 * mbed test -t <toolchain> -m <target> -n tests-mbed_hal-common_ticker*,tests-mbed_hal-us_ticker*
70 *
71 * @see hal_ticker_tests
72 *
73 */
74
75/**
76 * \defgroup hal_ticker_shared Ticker Hal
77 * Low level interface to the ticker of a target
78 *
79 * # Defined behavior
80 * * The function ticker_init is safe to call repeatedly - Verified by test ticker_init_test
81 * * The function ticker_init allows the ticker to keep counting and disables the ticker interrupt - Verified by test ticker_init_test
82 * * Ticker frequency is non-zero and counter is at least 8 bits - Verified by ticker_info_test
83 * * The ticker rolls over at (1 << bits) and continues counting starting from 0 - Verified by ticker_overflow_test
84 * * The ticker counts at the specified frequency +- 10% - Verified by ticker_frequency_test
85 * * The ticker increments by 1 each tick - Verified by ticker_increment_test
86 * * The ticker interrupt fires only when the ticker times increments to or past the value set by ticker_set_interrupt.
87 * Verified by ticker_interrupt_test and ticker_past_test
88 * * It is safe to call ticker_set_interrupt repeatedly before the handler is called - Verified by ticker_repeat_reschedule_test
89 * * The function ticker_fire_interrupt causes ticker_irq_handler to be called immediately from interrupt context -
90 * Verified by ticker_fire_now_test
91 * * The ticker operations ticker_read, ticker_clear_interrupt, ticker_set_interrupt and ticker_fire_interrupt
92 * take less than 20us to complete - Verified by ticker_speed_test
93 * * The ticker operations ticker_init and ticker_read are atomic.
94 *
95 * # Undefined behavior
96 * * Calling any function other than ticker_init before the initialization of the ticker
97 * * Whether ticker_irq_handler is called a second time if the time wraps and matches the value set by ticker_set_interrupt again
98 * * Calling ticker_set_interrupt with a value that has more than the supported number of bits
99 * * Calling any function other than us_ticker_init after calling us_ticker_free
100 *
101 * # Potential bugs
102 * * Drift due to reschedule - Verified by ticker_repeat_reschedule_test
103 * * Incorrect overflow handling of timers - Verified by ticker_overflow_test
104 * * Interrupting at a time of 0 - Verified by ticker_overflow_test
105 * * Interrupt triggered more than once - Verified by ticker_interrupt_test
106 *
107 * @ingroup hal_us_ticker
108 * @ingroup hal_lp_ticker
109 */
110
111/**
112 * \defgroup hal_ticker_tests Ticker Tests
113 * Tests to validate the proper implementation of a ticker
114 *
115 * To run the ticker hal tests use the command:
116 *
117 * mbed test -t <toolchain> -m <target> -n tests-mbed_hal-common_ticker*
118 *
119 * @ingroup hal_us_ticker
120 * @ingroup hal_lp_ticker
121 */
122
123
124typedef void (*ticker_irq_handler_type)(const ticker_data_t *const);
125
126/** Set ticker IRQ handler
127 *
128 * @param ticker_irq_handler IRQ handler to be connected
129 *
130 * @return previous ticker IRQ handler
131 *
132 * @note by default IRQ handler is set to ::ticker_irq_handler
133 * @note this function is primarily for testing purposes and it's not required part of HAL implementation
134 *
135 */
136ticker_irq_handler_type set_us_ticker_irq_handler(ticker_irq_handler_type ticker_irq_handler);
137
138/** Get ticker's data
139 *
140 * @return The microsecond ticker data
141 */
143
144
145/** The wrapper for ticker_irq_handler, to pass us ticker's data
146 *
147 */
149
150/* HAL us ticker */
151
152/** Initialize the ticker
153 *
154 * Initialize or re-initialize the ticker. This resets all the
155 * clocking and prescaler registers, along with disabling
156 * the compare interrupt.
157 *
158 * Implementations must tolerate this function being called multiple times, and subsequent calls
159 * after the first one shall clear any configured interrupt.
160 *
161 * @note Initialization properties tested by ticker_init_test
162 *
163 * Pseudo Code:
164 * @code
165 * void us_ticker_init()
166 * {
167 * // Enable clock gate so processor can read TIMER registers
168 * POWER_CTRL |= POWER_CTRL_TIMER_Msk;
169 *
170 * // Disable the timer and ensure it is powered down
171 * TIMER_CTRL &= ~(TIMER_CTRL_ENABLE_Msk | TIMER_CTRL_COMPARE_ENABLE_Msk);
172 *
173 * // Configure divisors
174 * uint32_t prescale = SystemCoreClock / 1000000;
175 * TIMER_PRESCALE = prescale - 1;
176 * TIMER_CTRL |= TIMER_CTRL_ENABLE_Msk;
177 *
178 * // Install the interrupt handler
179 * NVIC_SetVector(TIMER_IRQn, (uint32_t)us_ticker_irq_handler);
180 * NVIC_EnableIRQ(TIMER_IRQn);
181 * }
182 * @endcode
183 */
184void us_ticker_init(void);
185
186/** Deinitialize the us ticker
187 *
188 * Powerdown the us ticker in preparation for sleep, powerdown, or reset.
189 *
190 * After this function is called, no other ticker functions should be called
191 * except us_ticker_init(), calling any function other than init is undefined.
192 *
193 * @note This function stops the ticker from counting.
194 *
195 * Pseudo Code:
196 * @code
197 * uint32_t us_ticker_free()
198 * {
199 * // Disable timer
200 * TIMER_CTRL &= ~TIMER_CTRL_ENABLE_Msk;
201 *
202 * // Disable the compare interrupt
203 * TIMER_CTRL &= ~TIMER_CTRL_COMPARE_ENABLE_Msk;
204 *
205 * // Disable timer interrupt
206 * NVIC_DisableIRQ(TIMER_IRQn);
207 *
208 * // Disable clock gate so processor cannot read TIMER registers
209 * POWER_CTRL &= ~POWER_CTRL_TIMER_Msk;
210 * }
211 * @endcode
212 *
213 */
214void us_ticker_free(void);
215
216/** Read the current counter
217 *
218 * Read the current counter value without performing frequency conversions.
219 * If no rollover has occurred, the seconds passed since us_ticker_init()
220 * was called can be found by dividing the ticks returned by this function
221 * by the frequency returned by ::us_ticker_get_info.
222 *
223 * @return The current timer's counter value in ticks
224 *
225 * Pseudo Code:
226 * @code
227 * uint32_t us_ticker_read()
228 * {
229 * return TIMER_COUNT;
230 * }
231 * @endcode
232 */
233// note: parenthesis around the function name make sure this isn't replaced by the us_ticker_read() macro version.
234uint32_t (us_ticker_read)(void);
235
236/** Set interrupt for specified timestamp
237 *
238 * @param timestamp The time in ticks to be set
239 *
240 * @note no special handling needs to be done for times in the past
241 * as the common timer code will detect this and call
242 * us_ticker_fire_interrupt() if this is the case
243 *
244 * @note calling this function with timestamp of more than the supported
245 * number of bits returned by ::us_ticker_get_info results in undefined
246 * behavior.
247 *
248 * Pseudo Code:
249 * @code
250 * void us_ticker_set_interrupt(timestamp_t timestamp)
251 * {
252 * TIMER_COMPARE = timestamp;
253 * TIMER_CTRL |= TIMER_CTRL_COMPARE_ENABLE_Msk;
254 * }
255 * @endcode
256 */
258
259/** Disable us ticker interrupt
260 *
261 * Pseudo Code:
262 * @code
263 * void us_ticker_disable_interrupt(void)
264 * {
265 * // Disable the compare interrupt
266 * TIMER_CTRL &= ~TIMER_CTRL_COMPARE_ENABLE_Msk;
267 * }
268 * @endcode
269 */
271
272/** Clear us ticker interrupt
273 *
274 * Pseudo Code:
275 * @code
276 * void us_ticker_clear_interrupt(void)
277 * {
278 * // Write to the ICR (interrupt clear register) of the TIMER
279 * TIMER_ICR = TIMER_ICR_COMPARE_Msk;
280 * }
281 * @endcode
282 */
284
285/** Set pending interrupt that should be fired right away.
286 *
287 * The ticker should be initialized prior calling this function.
288 *
289 * Note: This function might be called from the ticker ISR, in which case it should
290 * schedule the ticker ISR to be executed again as soon as it returns.
291 *
292 * Pseudo Code:
293 * @code
294 * void us_ticker_fire_interrupt(void)
295 * {
296 * NVIC_SetPendingIRQ(TIMER_IRQn);
297 * }
298 * @endcode
299 */
301
302/** Get frequency and counter bits of this ticker.
303 *
304 * Pseudo Code:
305 * @code
306 * const ticker_info_t* us_ticker_get_info()
307 * {
308 * static const ticker_info_t info = {
309 * 1000000, // 1 MHz
310 * 32 // 32 bit counter
311 * };
312 * return &info;
313 * }
314 * @endcode
315 */
317
318/**@}*/
319
320#ifdef __cplusplus
321}
322#endif
323
324#endif
325
326/** @}*/
void ticker_irq_handler(const ticker_data_t *const ticker)
IRQ handler that goes through the events to trigger overdue events.
void us_ticker_clear_interrupt(void)
Clear us ticker interrupt.
uint32_t() us_ticker_read(void)
Read the current counter.
void us_ticker_irq_handler(void)
The wrapper for ticker_irq_handler, to pass us ticker's data.
void us_ticker_fire_interrupt(void)
Set pending interrupt that should be fired right away.
void us_ticker_disable_interrupt(void)
Disable us ticker interrupt.
void us_ticker_free(void)
Deinitialize the us ticker.
void us_ticker_init(void)
Initialize the ticker.
const ticker_info_t * us_ticker_get_info(void)
Get frequency and counter bits of this ticker.
ticker_irq_handler_type set_us_ticker_irq_handler(ticker_irq_handler_type ticker_irq_handler)
Set ticker IRQ handler.
void us_ticker_set_interrupt(timestamp_t timestamp)
Set interrupt for specified timestamp.
const ticker_data_t * get_us_ticker_data(void)
Get ticker's data.
uint32_t timestamp_t
Legacy format representing a timestamp in us.
Definition: ticker_api.h:33
Ticker's data structure.
Definition: ticker_api.h:144
Information about the ticker implementation.
Definition: ticker_api.h:53