Initial commit

Dependencies:   FastPWM

Committer:
lypinator
Date:
Wed Sep 16 01:11:49 2020 +0000
Revision:
0:bb348c97df44
Added PWM

Who changed what in which revision?

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