us_ticker_api.h 8.4 KB

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