mbed library sources. Supersedes mbed-src.

Fork of mbed-dev by mbed official

Committer:
Anna Bridge
Date:
Fri Jun 22 16:45:37 2018 +0100
Revision:
186:707f6e361f3e
Parent:
180:96ed750bd169
mbed-dev library. Release version 162

Who changed what in which revision?

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