lp_ticker_api.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /** \addtogroup hal */
  2. /** @{*/
  3. /* mbed Microcontroller Library
  4. * Copyright (c) 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_LPTICKER_API_H
  19. #define MBED_LPTICKER_API_H
  20. #include "device.h"
  21. #if DEVICE_LPTICKER
  22. #include "hal/ticker_api.h"
  23. #ifdef __cplusplus
  24. extern "C" {
  25. #endif
  26. /**
  27. * \defgroup hal_lp_ticker Low Power Ticker
  28. * Low level interface to the low power ticker of a target
  29. *
  30. * # Defined behavior
  31. * * Has a reported frequency between 4KHz and 64KHz - verified by ::lp_ticker_info_test
  32. * * Has a counter that is at least 12 bits wide - verified by ::lp_ticker_info_test
  33. * * Continues operating in deep sleep mode - verified by ::lp_ticker_deepsleep_test
  34. * * All behavior defined by the @ref hal_ticker_shared "ticker specification"
  35. *
  36. * # Undefined behavior
  37. * * See the @ref hal_ticker_shared "ticker specification"
  38. * * Calling any function other than lp_ticker_init after calling lp_ticker_free
  39. *
  40. * # Potential bugs
  41. * * Glitches due to ripple counter - Verified by ::lp_ticker_glitch_test
  42. *
  43. * @see hal_lp_ticker_tests
  44. *
  45. * @{
  46. */
  47. /**
  48. * \defgroup hal_lp_ticker_tests Low Power Ticker tests
  49. * Tests to validate the proper implementation of the low power ticker
  50. *
  51. * To run the low power ticker hal tests use the command:
  52. *
  53. * mbed test -t <toolchain> -m <target> -n tests-mbed_hal-common_ticker*,tests-mbed_hal-lp_ticker*
  54. *
  55. */
  56. typedef void (*ticker_irq_handler_type)(const ticker_data_t *const);
  57. /** Set low power ticker IRQ handler
  58. *
  59. * @param ticker_irq_handler IRQ handler to be connected
  60. *
  61. * @return previous ticker IRQ handler
  62. *
  63. * @note by default IRQ handler is set to ::ticker_irq_handler
  64. * @note this function is primarily for testing purposes and it's not required part of HAL implementation
  65. *
  66. */
  67. ticker_irq_handler_type set_lp_ticker_irq_handler(ticker_irq_handler_type ticker_irq_handler);
  68. /** Get low power ticker's data
  69. *
  70. * @return The low power ticker data
  71. */
  72. const ticker_data_t *get_lp_ticker_data(void);
  73. /** The wrapper for ticker_irq_handler, to pass lp ticker's data
  74. *
  75. */
  76. void lp_ticker_irq_handler(void);
  77. /* HAL lp ticker */
  78. /** Initialize the low power ticker
  79. *
  80. * Initialize or re-initialize the ticker. This resets all the
  81. * clocking and prescaler registers, along with disabling
  82. * the compare interrupt.
  83. *
  84. * Pseudo Code:
  85. * @code
  86. * void lp_ticker_init()
  87. * {
  88. * // Enable clock gate so processor can read LPTMR registers
  89. * POWER_CTRL |= POWER_CTRL_LPTMR_Msk;
  90. *
  91. * // Disable the timer and ensure it is powered down
  92. * LPTMR_CTRL &= ~(LPTMR_CTRL_ENABLE_Msk | LPTMR_CTRL_COMPARE_ENABLE_Msk);
  93. *
  94. * // Configure divisors - no division necessary
  95. * LPTMR_PRESCALE = 0;
  96. * LPTMR_CTRL |= LPTMR_CTRL_ENABLE_Msk;
  97. *
  98. * // Install the interrupt handler
  99. * NVIC_SetVector(LPTMR_IRQn, (uint32_t)lp_ticker_irq_handler);
  100. * NVIC_EnableIRQ(LPTMR_IRQn);
  101. * }
  102. * @endcode
  103. */
  104. void lp_ticker_init(void);
  105. /** Deinitialize the lower power ticker
  106. *
  107. * Powerdown the lp ticker in preparation for sleep, powerdown, or reset.
  108. *
  109. * After calling this function no other ticker functions should be called except
  110. * lp_ticker_init(). Calling any function other than init after freeing is
  111. * undefined.
  112. *
  113. * @note This function stops the ticker from counting.
  114. */
  115. void lp_ticker_free(void);
  116. /** Read the current tick
  117. *
  118. * If no rollover has occurred, the seconds passed since lp_ticker_init()
  119. * was called can be found by dividing the ticks returned by this function
  120. * by the frequency returned by ::lp_ticker_get_info.
  121. *
  122. * @return The current timer's counter value in ticks
  123. *
  124. * Pseudo Code:
  125. * @code
  126. * uint32_t lp_ticker_read()
  127. * {
  128. * uint16_t count;
  129. * uint16_t last_count;
  130. *
  131. * // Loop until the same tick is read twice since this
  132. * // is ripple counter on a different clock domain.
  133. * count = LPTMR_COUNT;
  134. * do {
  135. * last_count = count;
  136. * count = LPTMR_COUNT;
  137. * } while (last_count != count);
  138. *
  139. * return count;
  140. * }
  141. * @endcode
  142. */
  143. uint32_t lp_ticker_read(void);
  144. /** Set interrupt for specified timestamp
  145. *
  146. * @param timestamp The time in ticks to be set
  147. *
  148. * @note no special handling needs to be done for times in the past
  149. * as the common timer code will detect this and call
  150. * lp_ticker_fire_interrupt() if this is the case
  151. *
  152. * @note calling this function with timestamp of more than the supported
  153. * number of bits returned by ::lp_ticker_get_info results in undefined
  154. * behavior.
  155. *
  156. * Pseudo Code:
  157. * @code
  158. * void lp_ticker_set_interrupt(timestamp_t timestamp)
  159. * {
  160. * LPTMR_COMPARE = timestamp;
  161. * LPTMR_CTRL |= LPTMR_CTRL_COMPARE_ENABLE_Msk;
  162. * }
  163. * @endcode
  164. */
  165. void lp_ticker_set_interrupt(timestamp_t timestamp);
  166. /** Disable low power ticker interrupt
  167. *
  168. * Pseudo Code:
  169. * @code
  170. * void lp_ticker_disable_interrupt(void)
  171. * {
  172. * // Disable the compare interrupt
  173. * LPTMR_CTRL &= ~LPTMR_CTRL_COMPARE_ENABLE_Msk;
  174. * }
  175. * @endcode
  176. */
  177. void lp_ticker_disable_interrupt(void);
  178. /** Clear the low power ticker interrupt
  179. *
  180. * Pseudo Code:
  181. * @code
  182. * void lp_ticker_clear_interrupt(void)
  183. * {
  184. * // Write to the ICR (interrupt clear register) of the LPTMR
  185. * LPTMR_ICR = LPTMR_ICR_COMPARE_Msk;
  186. * }
  187. * @endcode
  188. */
  189. void lp_ticker_clear_interrupt(void);
  190. /** Set pending interrupt that should be fired right away.
  191. *
  192. * Pseudo Code:
  193. * @code
  194. * void lp_ticker_fire_interrupt(void)
  195. * {
  196. * NVIC_SetPendingIRQ(LPTMR_IRQn);
  197. * }
  198. * @endcode
  199. */
  200. void lp_ticker_fire_interrupt(void);
  201. /** Get frequency and counter bits of this ticker.
  202. *
  203. * Pseudo Code:
  204. * @code
  205. * const ticker_info_t* lp_ticker_get_info()
  206. * {
  207. * static const ticker_info_t info = {
  208. * 32768, // 32KHz
  209. * 16 // 16 bit counter
  210. * };
  211. * return &info;
  212. * }
  213. * @endcode
  214. */
  215. const ticker_info_t *lp_ticker_get_info(void);
  216. /**@}*/
  217. #ifdef __cplusplus
  218. }
  219. #endif
  220. #endif
  221. #endif
  222. /** @}*/