Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers us_ticker_api.h Source File

us_ticker_api.h

00001 
00002 /** \addtogroup hal */
00003 /** @{*/
00004 /* mbed Microcontroller Library
00005  * Copyright (c) 2006-2015 ARM Limited
00006  * SPDX-License-Identifier: Apache-2.0
00007  *
00008  * Licensed under the Apache License, Version 2.0 (the "License");
00009  * you may not use this file except in compliance with the License.
00010  * You may obtain a copy of the License at
00011  *
00012  *     http://www.apache.org/licenses/LICENSE-2.0
00013  *
00014  * Unless required by applicable law or agreed to in writing, software
00015  * distributed under the License is distributed on an "AS IS" BASIS,
00016  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00017  * See the License for the specific language governing permissions and
00018  * limitations under the License.
00019  */
00020 #ifndef MBED_US_TICKER_API_H
00021 #define MBED_US_TICKER_API_H
00022 
00023 #include <stdint.h>
00024 #include "hal/ticker_api.h"
00025 
00026 #ifdef __cplusplus
00027 extern "C" {
00028 #endif
00029 
00030 /**
00031  * \defgroup hal_us_ticker Microsecond Ticker
00032  * Low level interface to the microsecond ticker of a target
00033  *
00034  * # Defined behavior
00035  * * Has a reported frequency between 250KHz and 8MHz for counters which are less than 32 bits wide - Verified by test ::us_ticker_info_test
00036  * * Has a reported frequency up to 100MHz for counters which are 32 bits wide - Verified by test ::us_ticker_info_test
00037  * * Has a counter that is at least 16 bits wide - Verified by test ::us_ticker_info_test
00038  * * All behavior defined by the @ref hal_ticker_shared "ticker specification"
00039  *
00040  * # Undefined behavior
00041  * * See the @ref hal_ticker_shared "ticker specification"
00042  *
00043  * @see hal_us_ticker_tests
00044  *
00045  * # Compile-time optimization macros
00046  *
00047  * To permit compile-time optimization, particularly of wait_us, the following macros should
00048  * be defined by a target's device.h:
00049  *
00050  * US_TICKER_PERIOD_NUM, US_TICKER_PERIOD_DEN: These denote the ratio (numerator, denominator)
00051  * of the ticker period to a microsecond. For example, an 8MHz ticker would have NUM = 1, DEN = 8;
00052  * a 1MHz ticker would have NUM = 1, DEN = 1; a 250kHz ticker would have NUM = 4, DEN = 1.
00053  * Both numerator and denominator must be 16 bits or less.
00054  *
00055  * US_TICKER_MASK: The value mask for the ticker - eg 0x07FFFFFF for a 27-bit ticker.
00056  *
00057  * If any are defined, all 3 must be defined, and the macros are checked for consistency with
00058  * us_ticker_get_info by test ::us_ticker_info_test.
00059 
00060  * @{
00061  */
00062 
00063 /**
00064  * \defgroup hal_us_ticker_tests Microsecond Ticker tests
00065  * Tests to validate the proper implementation of the microsecond ticker
00066  *
00067  * To run the microsecond ticker hal tests use the command:
00068  *
00069  *     mbed test -t <toolchain> -m <target> -n tests-mbed_hal-common_ticker*,tests-mbed_hal-us_ticker*
00070  *
00071  * @see hal_ticker_tests
00072  *
00073  */
00074 
00075 /**
00076  * \defgroup hal_ticker_shared Ticker Hal
00077  * Low level interface to the ticker of a target
00078  *
00079  * # Defined behavior
00080  * * The function ticker_init is safe to call repeatedly - Verified by test ::ticker_init_test
00081  * * The function ticker_init allows the ticker to keep counting and disables the ticker interrupt - Verified by test ::ticker_init_test
00082  * * Ticker frequency is non-zero and counter is at least 8 bits - Verified by ::ticker_info_test
00083  * * The ticker rolls over at (1 << bits) and continues counting starting from 0 - Verified by ::ticker_overflow_test
00084  * * The ticker counts at the specified frequency +- 10% - Verified by ::ticker_frequency_test
00085  * * The ticker increments by 1 each tick - Verified by ::ticker_increment_test
00086  * * The ticker interrupt fires only when the ticker times increments to or past the value set by ticker_set_interrupt.
00087  * Verified by ::ticker_interrupt_test and ::ticker_past_test
00088  * * It is safe to call ticker_set_interrupt repeatedly before the handler is called - Verified by ::ticker_repeat_reschedule_test
00089  * * The function ticker_fire_interrupt causes ticker_irq_handler to be called immediately from interrupt context -
00090  * Verified by ::ticker_fire_now_test
00091  * * The ticker operations ticker_read, ticker_clear_interrupt, ticker_set_interrupt and ticker_fire_interrupt
00092  * take less than 20us to complete - Verified by ::ticker_speed_test
00093  * * The ticker operations ticker_init and ticker_read are atomic.
00094  *
00095  * # Undefined behavior
00096  * * Calling any function other than ticker_init before the initialization of the ticker
00097  * * Whether ticker_irq_handler is called a second time if the time wraps and matches the value set by ticker_set_interrupt again
00098  * * Calling ticker_set_interrupt with a value that has more than the supported number of bits
00099  * * Calling any function other than us_ticker_init after calling us_ticker_free
00100  *
00101  * # Potential bugs
00102  * * Drift due to reschedule - Verified by ::ticker_repeat_reschedule_test
00103  * * Incorrect overflow handling of timers - Verified by ::ticker_overflow_test
00104  * * Interrupting at a time of 0 - Verified by ::ticker_overflow_test
00105  * * Interrupt triggered more than once - Verified by ::ticker_interrupt_test
00106  *
00107  * @ingroup hal_us_ticker
00108  * @ingroup hal_lp_ticker
00109  */
00110 
00111 /**
00112  * \defgroup hal_ticker_tests Ticker Tests
00113  * Tests to validate the proper implementation of a ticker
00114  *
00115  * To run the ticker hal tests use the command:
00116  *
00117  *     mbed test -t <toolchain> -m <target> -n tests-mbed_hal-common_ticker*
00118  *
00119  * @ingroup hal_us_ticker
00120  * @ingroup hal_lp_ticker
00121  */
00122 
00123 
00124 typedef void (*ticker_irq_handler_type)(const ticker_data_t *const);
00125 
00126 /** Set ticker IRQ handler
00127  *
00128  * @param ticker_irq_handler IRQ handler to be connected
00129  *
00130  * @return previous ticker IRQ handler
00131  *
00132  * @note by default IRQ handler is set to ::ticker_irq_handler
00133  * @note this function is primarily for testing purposes and it's not required part of HAL implementation
00134  *
00135  */
00136 ticker_irq_handler_type set_us_ticker_irq_handler(ticker_irq_handler_type ticker_irq_handler);
00137 
00138 /** Get ticker's data
00139  *
00140  * @return The microsecond ticker data
00141  */
00142 const ticker_data_t *get_us_ticker_data(void);
00143 
00144 
00145 /** The wrapper for ticker_irq_handler, to pass us ticker's data
00146  *
00147  */
00148 void us_ticker_irq_handler(void);
00149 
00150 /* HAL us ticker */
00151 
00152 /** Initialize the ticker
00153  *
00154  * Initialize or re-initialize the ticker. This resets all the
00155  * clocking and prescaler registers, along with disabling
00156  * the compare interrupt.
00157  *
00158  * @note Initialization properties tested by ::ticker_init_test
00159  *
00160  * Pseudo Code:
00161  * @code
00162  * void us_ticker_init()
00163  * {
00164  *     // Enable clock gate so processor can read TIMER registers
00165  *     POWER_CTRL |= POWER_CTRL_TIMER_Msk;
00166  *
00167  *     // Disable the timer and ensure it is powered down
00168  *     TIMER_CTRL &= ~(TIMER_CTRL_ENABLE_Msk | TIMER_CTRL_COMPARE_ENABLE_Msk);
00169  *
00170  *     // Configure divisors
00171  *     uint32_t prescale = SystemCoreClock / 1000000;
00172  *     TIMER_PRESCALE = prescale - 1;
00173  *     TIMER_CTRL |= TIMER_CTRL_ENABLE_Msk;
00174  *
00175  *     // Install the interrupt handler
00176  *     NVIC_SetVector(TIMER_IRQn, (uint32_t)us_ticker_irq_handler);
00177  *     NVIC_EnableIRQ(TIMER_IRQn);
00178  * }
00179  * @endcode
00180  */
00181 void us_ticker_init(void);
00182 
00183 /** Deinitialize the us ticker
00184  *
00185  * Powerdown the us ticker in preparation for sleep, powerdown, or reset.
00186  *
00187  * After this function is called, no other ticker functions should be called
00188  * except us_ticker_init(), calling any function other than init is undefined.
00189  *
00190  * @note This function stops the ticker from counting.
00191  *
00192  * Pseudo Code:
00193  * @code
00194  * uint32_t us_ticker_free()
00195  * {
00196  *     // Disable timer
00197  *     TIMER_CTRL &= ~TIMER_CTRL_ENABLE_Msk;
00198  *
00199  *     // Disable the compare interrupt
00200  *     TIMER_CTRL &= ~TIMER_CTRL_COMPARE_ENABLE_Msk;
00201  *
00202  *     // Disable timer interrupt
00203  *     NVIC_DisableIRQ(TIMER_IRQn);
00204  *
00205  *     // Disable clock gate so processor cannot read TIMER registers
00206  *     POWER_CTRL &= ~POWER_CTRL_TIMER_Msk;
00207  * }
00208  * @endcode
00209  *
00210  */
00211 void us_ticker_free(void);
00212 
00213 /** Read the current counter
00214  *
00215  * Read the current counter value without performing frequency conversions.
00216  * If no rollover has occurred, the seconds passed since us_ticker_init()
00217  * was called can be found by dividing the ticks returned by this function
00218  * by the frequency returned by ::us_ticker_get_info.
00219  *
00220  * @return The current timer's counter value in ticks
00221  *
00222  * Pseudo Code:
00223  * @code
00224  * uint32_t us_ticker_read()
00225  * {
00226  *     return TIMER_COUNT;
00227  * }
00228  * @endcode
00229  */
00230 uint32_t (us_ticker_read)(void);
00231 
00232 /** Set interrupt for specified timestamp
00233  *
00234  * @param timestamp The time in ticks to be set
00235  *
00236  * @note no special handling needs to be done for times in the past
00237  * as the common timer code will detect this and call
00238  * us_ticker_fire_interrupt() if this is the case
00239  *
00240  * @note calling this function with timestamp of more than the supported
00241  * number of bits returned by ::us_ticker_get_info results in undefined
00242  * behavior.
00243  *
00244  * Pseudo Code:
00245  * @code
00246  * void us_ticker_set_interrupt(timestamp_t timestamp)
00247  * {
00248  *     TIMER_COMPARE = timestamp;
00249  *     TIMER_CTRL |= TIMER_CTRL_COMPARE_ENABLE_Msk;
00250  * }
00251  * @endcode
00252  */
00253 void us_ticker_set_interrupt(timestamp_t timestamp);
00254 
00255 /** Disable us ticker interrupt
00256  *
00257  * Pseudo Code:
00258  * @code
00259  * void us_ticker_disable_interrupt(void)
00260  * {
00261  *     // Disable the compare interrupt
00262  *     TIMER_CTRL &= ~TIMER_CTRL_COMPARE_ENABLE_Msk;
00263  * }
00264  * @endcode
00265  */
00266 void us_ticker_disable_interrupt(void);
00267 
00268 /** Clear us ticker interrupt
00269  *
00270  * Pseudo Code:
00271  * @code
00272  * void us_ticker_clear_interrupt(void)
00273  * {
00274  *     // Write to the ICR (interrupt clear register) of the TIMER
00275  *     TIMER_ICR = TIMER_ICR_COMPARE_Msk;
00276  * }
00277  * @endcode
00278  */
00279 void us_ticker_clear_interrupt(void);
00280 
00281 /** Set pending interrupt that should be fired right away.
00282  *
00283  * The ticker should be initialized prior calling this function.
00284  *
00285  * Pseudo Code:
00286  * @code
00287  * void us_ticker_fire_interrupt(void)
00288  * {
00289  *     NVIC_SetPendingIRQ(TIMER_IRQn);
00290  * }
00291  * @endcode
00292  */
00293 void us_ticker_fire_interrupt(void);
00294 
00295 /** Get frequency and counter bits of this ticker.
00296  *
00297  * Pseudo Code:
00298  * @code
00299  * const ticker_info_t* us_ticker_get_info()
00300  * {
00301  *     static const ticker_info_t info = {
00302  *         1000000,    // 1 MHz
00303  *         32          // 32 bit counter
00304  *     };
00305  *     return &info;
00306  * }
00307  * @endcode
00308  */
00309 const ticker_info_t *us_ticker_get_info(void);
00310 
00311 /**@}*/
00312 
00313 #ifdef __cplusplus
00314 }
00315 #endif
00316 
00317 #endif
00318 
00319 /** @}*/