The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Dependents:   hello SerialTestv11 SerialTestv12 Sierpinski ... more

mbed 2

This is the mbed 2 library. If you'd like to learn about Mbed OS please see the mbed-os docs.

Committer:
Anna Bridge
Date:
Fri Jun 22 15:38:59 2018 +0100
Revision:
169:a7c7b631e539
Parent:
160:5571c4ff569f
Child:
170:e95d10626187
mbed library. Release version 162

Who changed what in which revision?

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