Mbed OS Reference
Loading...
Searching...
No Matches
lp_ticker_api.h
1
2/** \addtogroup hal */
3/** @{*/
4/* mbed Microcontroller Library
5 * Copyright (c) 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_LPTICKER_API_H
21#define MBED_LPTICKER_API_H
22
23#include "device.h"
24
25#if DEVICE_LPTICKER
26
27#include "hal/ticker_api.h"
28
29#ifdef __cplusplus
30extern "C" {
31#endif
32
33/**
34 * \defgroup hal_lp_ticker Low Power Ticker
35 * Low level interface to the low power ticker of a target
36 *
37 * # Defined behavior
38 * * Has a reported frequency between 4KHz and 64KHz - verified by lp_ticker_info_test
39 * * Has a counter that is at least 12 bits wide - verified by lp_ticker_info_test
40 * * Continues operating in deep sleep mode - verified by lp_ticker_deepsleep_test
41 * * All behavior defined by the @ref hal_ticker_shared "ticker specification"
42 *
43 * # Undefined behavior
44 * * See the @ref hal_ticker_shared "ticker specification"
45 * * Calling any function other than lp_ticker_init after calling lp_ticker_free
46 *
47 * # Potential bugs
48 * * Glitches due to ripple counter - Verified by lp_ticker_glitch_test
49 *
50 * @see hal_lp_ticker_tests
51 *
52 * # Compile-time optimization macros
53 *
54 * To permit compile-time optimization, the following macros can be defined by a target's device.h:
55 *
56 * LP_TICKER_PERIOD_NUM, LP_TICKER_PERIOD_DEN: These denote the ratio (numerator, denominator)
57 * of the ticker period to a microsecond. For example, a 64kHz ticker would have NUM = 125, DEN = 8;
58 * a 4kHz ticker would have NUM = 250, DEN = 1; a 32.768kHz ticker would have NUM = 15625, DEN = 512.
59 * Both numerator and denominator must be 32 bits or less. They do not need to be fully simplified,
60 * so 32.768kHz could also be NUM = 1000000, DEN = 32768, but more simplification may be a minor
61 * speed optimisation, as can matching numerator or denominator with US_TICKER.
62 *
63 * LP_TICKER_MASK: The value mask for the ticker - eg 0x07FFFFFF for a 27-bit ticker.
64 *
65 * If any are defined, all 3 must be defined, and the macros are checked for consistency with
66 * lp_ticker_get_info by test lp_ticker_info_test.
67
68 * @{
69 */
70
71/**
72 * \defgroup hal_lp_ticker_tests Low Power Ticker tests
73 * Tests to validate the proper implementation of the low power ticker
74 *
75 * To run the low power ticker hal tests use the command:
76 *
77 * mbed test -t <toolchain> -m <target> -n tests-mbed_hal-common_ticker*,tests-mbed_hal-lp_ticker*
78 *
79 */
80
81typedef void (*ticker_irq_handler_type)(const ticker_data_t *const);
82
83/** Set low power ticker IRQ handler
84 *
85 * @param ticker_irq_handler IRQ handler to be connected
86 *
87 * @return previous ticker IRQ handler
88 *
89 * @note by default IRQ handler is set to ::ticker_irq_handler
90 * @note this function is primarily for testing purposes and it's not required part of HAL implementation
91 *
92 */
93ticker_irq_handler_type set_lp_ticker_irq_handler(ticker_irq_handler_type ticker_irq_handler);
94
95/** Get low power ticker's data
96 *
97 * @return The low power ticker data
98 */
100
101/** The wrapper for ticker_irq_handler, to pass lp ticker's data
102 *
103 */
105
106/* HAL lp ticker */
107
108/** Initialize the low power ticker
109 *
110 * Initialize or re-initialize the ticker. This resets all the
111 * clocking and prescaler registers, along with disabling
112 * the compare interrupt.
113 *
114 * Pseudo Code:
115 * @code
116 * void lp_ticker_init()
117 * {
118 * // Enable clock gate so processor can read LPTMR registers
119 * POWER_CTRL |= POWER_CTRL_LPTMR_Msk;
120 *
121 * // Disable the timer and ensure it is powered down
122 * LPTMR_CTRL &= ~(LPTMR_CTRL_ENABLE_Msk | LPTMR_CTRL_COMPARE_ENABLE_Msk);
123 *
124 * // Configure divisors - no division necessary
125 * LPTMR_PRESCALE = 0;
126 * LPTMR_CTRL |= LPTMR_CTRL_ENABLE_Msk;
127 *
128 * // Install the interrupt handler
129 * NVIC_SetVector(LPTMR_IRQn, (uint32_t)lp_ticker_irq_handler);
130 * NVIC_EnableIRQ(LPTMR_IRQn);
131 * }
132 * @endcode
133 */
134void lp_ticker_init(void);
135
136/** Deinitialize the lower power ticker
137 *
138 * Powerdown the lp ticker in preparation for sleep, powerdown, or reset.
139 *
140 * After calling this function no other ticker functions should be called except
141 * lp_ticker_init(). Calling any function other than init after freeing is
142 * undefined.
143 *
144 * @note This function stops the ticker from counting.
145 */
146void lp_ticker_free(void);
147
148/** Read the current tick
149 *
150 * If no rollover has occurred, the seconds passed since lp_ticker_init()
151 * was called can be found by dividing the ticks returned by this function
152 * by the frequency returned by ::lp_ticker_get_info.
153 *
154 * @return The current timer's counter value in ticks
155 *
156 * Pseudo Code:
157 * @code
158 * uint32_t lp_ticker_read()
159 * {
160 * uint16_t count;
161 * uint16_t last_count;
162 *
163 * // Loop until the same tick is read twice since this
164 * // is ripple counter on a different clock domain.
165 * count = LPTMR_COUNT;
166 * do {
167 * last_count = count;
168 * count = LPTMR_COUNT;
169 * } while (last_count != count);
170 *
171 * return count;
172 * }
173 * @endcode
174 */
175uint32_t lp_ticker_read(void);
176
177/**
178 * @brief Set interrupt for specified timestamp
179 *
180 * @param timestamp The time in ticks to be set. Guaranteed to be between 0 and 2^bits-1, where bits is
181 * the number of bits returned by ::lp_ticker_get_info
182 *
183 * @note If \c timestamp is less than the current time read by the ticker, then the intention is to set an
184 * interrupt for once the ticker rolls over and reaches this time.
185 *
186 * @note Upper level Mbed OS code is responsible for ensuring that if we try to set a wake-up for a time
187 * in the past, ::lp_ticker_fire_interrupt is called instead. It will also ensure that if
188 * we want to wake up far in the future, we will instead set a wakeup for about (rollover period/2) ticks
189 * in the future, then reschedule the timer for the correct time.
190 *
191 * Calling this function with timestamp of more than the supported
192 * number of bits returned by ::lp_ticker_get_info results in undefined
193 * behavior.
194 *
195 * If the timer interrupt is pending when this function is called (e.g. due to the ticker being set,
196 * but the interrupt being disabled before it could fire), this function shall clear the pending interrupt
197 * before reenabling and setting it to prevent a spurious execution of the interrupt.
198 *
199 * Pseudo Code:
200 * @code
201 * void lp_ticker_set_interrupt(timestamp_t timestamp)
202 * {
203 * lp_ticker_clear_interrupt();
204 * LPTMR_COMPARE = timestamp;
205 * LPTMR_CTRL |= LPTMR_CTRL_COMPARE_ENABLE_Msk;
206 * }
207 * @endcode
208 */
210
211/** Disable low power ticker interrupt
212 *
213 * Pseudo Code:
214 * @code
215 * void lp_ticker_disable_interrupt(void)
216 * {
217 * // Disable the compare interrupt
218 * LPTMR_CTRL &= ~LPTMR_CTRL_COMPARE_ENABLE_Msk;
219 * }
220 * @endcode
221 */
223
224/**
225 * @brief Clear the low power ticker interrupt.
226 *
227 * This is called from Mbed's interrupt handler and should clear the interrupt flag in the peripheral to stop
228 * the interrupt from being executed again after it returns. This does not do anything if called before the interrupt
229 * fires (e.g. it doesn't cancel the interrupt if it's set in the future).
230 *
231 * Pseudo Code:
232 * @code
233 * void lp_ticker_clear_interrupt(void)
234 * {
235 * // Write to the ICR (interrupt clear register) of the LPTMR
236 * LPTMR_ICR = LPTMR_ICR_COMPARE_Msk;
237 * }
238 * @endcode
239 */
241
242/** Set pending interrupt that should be fired right away.
243 *
244 * Pseudo Code:
245 * @code
246 * void lp_ticker_fire_interrupt(void)
247 * {
248 * NVIC_SetPendingIRQ(LPTMR_IRQn);
249 * }
250 * @endcode
251 */
253
254/** Get frequency and counter bits of this ticker.
255 *
256 * Pseudo Code:
257 * @code
258 * const ticker_info_t* lp_ticker_get_info()
259 * {
260 * static const ticker_info_t info = {
261 * 32768, // 32KHz
262 * 16 // 16 bit counter
263 * };
264 * return &info;
265 * }
266 * @endcode
267 */
269
270/**@}*/
271
272#ifdef __cplusplus
273}
274#endif
275
276#endif
277
278#endif
279
280/** @}*/
void lp_ticker_init(void)
Initialize the low power ticker.
void lp_ticker_fire_interrupt(void)
Set pending interrupt that should be fired right away.
ticker_irq_handler_type set_lp_ticker_irq_handler(ticker_irq_handler_type ticker_irq_handler)
Set low power ticker IRQ handler.
const ticker_info_t * lp_ticker_get_info(void)
Get frequency and counter bits of this ticker.
void lp_ticker_disable_interrupt(void)
Disable low power ticker interrupt.
const ticker_data_t * get_lp_ticker_data(void)
Get low power ticker's data.
void lp_ticker_free(void)
Deinitialize the lower power ticker.
void lp_ticker_set_interrupt(timestamp_t timestamp)
Set interrupt for specified timestamp.
void lp_ticker_irq_handler(void)
The wrapper for ticker_irq_handler, to pass lp ticker's data.
uint32_t lp_ticker_read(void)
Read the current tick.
void lp_ticker_clear_interrupt(void)
Clear the low power ticker interrupt.
void ticker_irq_handler(const ticker_data_t *const ticker)
IRQ handler that goes through the events to trigger overdue events.
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