am_hal_ctimer.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. //*****************************************************************************
  2. //
  3. // am_hal_ctimer.h
  4. //! @file
  5. //!
  6. //! @brief Functions for accessing and configuring the CTIMER.
  7. //!
  8. //! @addtogroup ctimer2 Counter/Timer (CTIMER)
  9. //! @ingroup apollo2hal
  10. //! @{
  11. //
  12. //*****************************************************************************
  13. //*****************************************************************************
  14. //
  15. // Copyright (c) 2017, Ambiq Micro
  16. // All rights reserved.
  17. //
  18. // Redistribution and use in source and binary forms, with or without
  19. // modification, are permitted provided that the following conditions are met:
  20. //
  21. // 1. Redistributions of source code must retain the above copyright notice,
  22. // this list of conditions and the following disclaimer.
  23. //
  24. // 2. Redistributions in binary form must reproduce the above copyright
  25. // notice, this list of conditions and the following disclaimer in the
  26. // documentation and/or other materials provided with the distribution.
  27. //
  28. // 3. Neither the name of the copyright holder nor the names of its
  29. // contributors may be used to endorse or promote products derived from this
  30. // software without specific prior written permission.
  31. //
  32. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  33. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  34. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  35. // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  36. // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  37. // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  38. // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  39. // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  40. // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41. // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  42. // POSSIBILITY OF SUCH DAMAGE.
  43. //
  44. // This is part of revision 1.2.11 of the AmbiqSuite Development Package.
  45. //
  46. //*****************************************************************************
  47. #ifndef AM_HAL_CTIMER_H
  48. #define AM_HAL_CTIMER_H
  49. //*****************************************************************************
  50. //
  51. //! Number of timers
  52. //
  53. //*****************************************************************************
  54. #define AM_HAL_CTIMER_TIMERS_NUM 4
  55. //*****************************************************************************
  56. //
  57. //! Timer offset value
  58. //
  59. //*****************************************************************************
  60. #define AM_HAL_CTIMER_TIMER_OFFSET (AM_REG_CTIMER_TMR1_O - AM_REG_CTIMER_TMR0_O)
  61. //*****************************************************************************
  62. //
  63. //! @name Interrupt Status Bits
  64. //! @brief Interrupt Status Bits for enable/disble use
  65. //!
  66. //! These macros may be used to set and clear interrupt bits
  67. //! @{
  68. //
  69. //*****************************************************************************
  70. #define AM_HAL_CTIMER_INT_TIMERA0C0 AM_REG_CTIMER_INTEN_CTMRA0C0INT_M
  71. #define AM_HAL_CTIMER_INT_TIMERA0C1 AM_REG_CTIMER_INTEN_CTMRA0C1INT_M
  72. #define AM_HAL_CTIMER_INT_TIMERA1C0 AM_REG_CTIMER_INTEN_CTMRA1C0INT_M
  73. #define AM_HAL_CTIMER_INT_TIMERA1C1 AM_REG_CTIMER_INTEN_CTMRA1C1INT_M
  74. #define AM_HAL_CTIMER_INT_TIMERA2C0 AM_REG_CTIMER_INTEN_CTMRA2C0INT_M
  75. #define AM_HAL_CTIMER_INT_TIMERA2C1 AM_REG_CTIMER_INTEN_CTMRA2C1INT_M
  76. #define AM_HAL_CTIMER_INT_TIMERA3C0 AM_REG_CTIMER_INTEN_CTMRA3C0INT_M
  77. #define AM_HAL_CTIMER_INT_TIMERA3C1 AM_REG_CTIMER_INTEN_CTMRA3C1INT_M
  78. #define AM_HAL_CTIMER_INT_TIMERB0C0 AM_REG_CTIMER_INTEN_CTMRB0C0INT_M
  79. #define AM_HAL_CTIMER_INT_TIMERB0C1 AM_REG_CTIMER_INTEN_CTMRB0C1INT_M
  80. #define AM_HAL_CTIMER_INT_TIMERB1C0 AM_REG_CTIMER_INTEN_CTMRB1C0INT_M
  81. #define AM_HAL_CTIMER_INT_TIMERB1C1 AM_REG_CTIMER_INTEN_CTMRB1C1INT_M
  82. #define AM_HAL_CTIMER_INT_TIMERB2C0 AM_REG_CTIMER_INTEN_CTMRB2C0INT_M
  83. #define AM_HAL_CTIMER_INT_TIMERB2C1 AM_REG_CTIMER_INTEN_CTMRB2C1INT_M
  84. #define AM_HAL_CTIMER_INT_TIMERB3C0 AM_REG_CTIMER_INTEN_CTMRB3C0INT_M
  85. #define AM_HAL_CTIMER_INT_TIMERB3C1 AM_REG_CTIMER_INTEN_CTMRB3C1INT_M
  86. //
  87. // Deprecated, use the newer macros above.
  88. //
  89. #define AM_HAL_CTIMER_INT_TIMERA0 AM_HAL_CTIMER_INT_TIMERA0C0
  90. #define AM_HAL_CTIMER_INT_TIMERB0 AM_HAL_CTIMER_INT_TIMERB0C0
  91. #define AM_HAL_CTIMER_INT_TIMERA1 AM_HAL_CTIMER_INT_TIMERA1C0
  92. #define AM_HAL_CTIMER_INT_TIMERB1 AM_HAL_CTIMER_INT_TIMERB1C0
  93. #define AM_HAL_CTIMER_INT_TIMERA2 AM_HAL_CTIMER_INT_TIMERA2C0
  94. #define AM_HAL_CTIMER_INT_TIMERB2 AM_HAL_CTIMER_INT_TIMERB2C0
  95. #define AM_HAL_CTIMER_INT_TIMERA3 AM_HAL_CTIMER_INT_TIMERA3C0
  96. #define AM_HAL_CTIMER_INT_TIMERB3 AM_HAL_CTIMER_INT_TIMERB3C0
  97. //! @}
  98. //*****************************************************************************
  99. //
  100. //! @name Configuration options
  101. //! @brief Configuration options for \e am_hal_ctimer_config_t
  102. //!
  103. //! These options are to be used with the \e am_hal_ctimer_config_t structure
  104. //! used by \e am_hal_ctimer_config
  105. //! @{
  106. //
  107. //*****************************************************************************
  108. #define AM_HAL_CTIMER_CLK_PIN AM_REG_CTIMER_CTRL0_TMRA0CLK(0x0)
  109. #define AM_HAL_CTIMER_HFRC_12MHZ AM_REG_CTIMER_CTRL0_TMRA0CLK(0x1)
  110. #define AM_HAL_CTIMER_HFRC_3MHZ AM_REG_CTIMER_CTRL0_TMRA0CLK(0x2)
  111. #define AM_HAL_CTIMER_HFRC_187_5KHZ AM_REG_CTIMER_CTRL0_TMRA0CLK(0x3)
  112. #define AM_HAL_CTIMER_HFRC_47KHZ AM_REG_CTIMER_CTRL0_TMRA0CLK(0x4)
  113. #define AM_HAL_CTIMER_HFRC_12KHZ AM_REG_CTIMER_CTRL0_TMRA0CLK(0x5)
  114. #define AM_HAL_CTIMER_XT_32_768KHZ AM_REG_CTIMER_CTRL0_TMRA0CLK(0x6)
  115. #define AM_HAL_CTIMER_XT_16_384KHZ AM_REG_CTIMER_CTRL0_TMRA0CLK(0x7)
  116. #define AM_HAL_CTIMER_XT_2_048KHZ AM_REG_CTIMER_CTRL0_TMRA0CLK(0x8)
  117. #define AM_HAL_CTIMER_XT_256HZ AM_REG_CTIMER_CTRL0_TMRA0CLK(0x9)
  118. #define AM_HAL_CTIMER_LFRC_512HZ AM_REG_CTIMER_CTRL0_TMRA0CLK(0xA)
  119. #define AM_HAL_CTIMER_LFRC_32HZ AM_REG_CTIMER_CTRL0_TMRA0CLK(0xB)
  120. #define AM_HAL_CTIMER_LFRC_1HZ AM_REG_CTIMER_CTRL0_TMRA0CLK(0xC)
  121. #define AM_HAL_CTIMER_LFRC_1_16HZ AM_REG_CTIMER_CTRL0_TMRA0CLK(0xD)
  122. #define AM_HAL_CTIMER_RTC_100HZ AM_REG_CTIMER_CTRL0_TMRA0CLK(0xE)
  123. #define AM_HAL_CTIMER_HCLK AM_REG_CTIMER_CTRL0_TMRA0CLK(0xF)
  124. #define AM_HAL_CTIMER_BUCK AM_REG_CTIMER_CTRL0_TMRA0CLK(0x10)
  125. //! @}
  126. //*****************************************************************************
  127. //
  128. // Timer function macros.
  129. //
  130. //*****************************************************************************
  131. #define AM_HAL_CTIMER_FN_ONCE AM_REG_CTIMER_CTRL0_TMRA0FN(0)
  132. #define AM_HAL_CTIMER_FN_REPEAT AM_REG_CTIMER_CTRL0_TMRA0FN(1)
  133. #define AM_HAL_CTIMER_FN_PWM_ONCE AM_REG_CTIMER_CTRL0_TMRA0FN(2)
  134. #define AM_HAL_CTIMER_FN_PWM_REPEAT AM_REG_CTIMER_CTRL0_TMRA0FN(3)
  135. #define AM_HAL_CTIMER_FN_CONTINUOUS AM_REG_CTIMER_CTRL0_TMRA0FN(4)
  136. //*****************************************************************************
  137. //
  138. // Half-timer options.
  139. //
  140. //*****************************************************************************
  141. #define AM_HAL_CTIMER_INT_ENABLE AM_REG_CTIMER_CTRL0_TMRA0IE0_M
  142. #define AM_HAL_CTIMER_PIN_ENABLE AM_REG_CTIMER_CTRL0_TMRA0PE_M
  143. #define AM_HAL_CTIMER_PIN_INVERT AM_REG_CTIMER_CTRL0_TMRA0POL_M
  144. #define AM_HAL_CTIMER_CLEAR AM_REG_CTIMER_CTRL0_TMRA0CLR_M
  145. //*****************************************************************************
  146. //
  147. // Additional timer options.
  148. //
  149. //*****************************************************************************
  150. #define AM_HAL_CTIMER_LINK AM_REG_CTIMER_CTRL0_CTLINK0_M
  151. #define AM_HAL_CTIMER_ADC_TRIG AM_REG_CTIMER_CTRL3_ADCEN_M
  152. //*****************************************************************************
  153. //
  154. // Timer selection macros.
  155. //
  156. //*****************************************************************************
  157. #define AM_HAL_CTIMER_TIMERA 0x0000FFFF
  158. #define AM_HAL_CTIMER_TIMERB 0xFFFF0000
  159. #define AM_HAL_CTIMER_BOTH 0xFFFFFFFF
  160. //! @}
  161. //*****************************************************************************
  162. //
  163. // Timer configuration structure
  164. //
  165. //*****************************************************************************
  166. typedef struct
  167. {
  168. //
  169. //! Set to 1 to operate this timer as a 32-bit timer instead of two 16-bit
  170. //! timers.
  171. //
  172. uint32_t ui32Link;
  173. //
  174. //! Configuration options for TIMERA
  175. //
  176. uint32_t ui32TimerAConfig;
  177. //
  178. //! Configuration options for TIMERB
  179. //
  180. uint32_t ui32TimerBConfig;
  181. }
  182. am_hal_ctimer_config_t;
  183. //*****************************************************************************
  184. //
  185. // Function pointer type for CTimer interrupt handlers.
  186. //
  187. //*****************************************************************************
  188. typedef void (*am_hal_ctimer_handler_t)(void);
  189. #ifdef __cplusplus
  190. extern "C"
  191. {
  192. #endif
  193. //*****************************************************************************
  194. //
  195. // External function definitions
  196. //
  197. //*****************************************************************************
  198. extern void am_hal_ctimer_config(uint32_t ui32TimerNumber,
  199. am_hal_ctimer_config_t *psConfig);
  200. extern void am_hal_ctimer_config_single(uint32_t ui32TimerNumber,
  201. uint32_t ui32TimerSegment,
  202. uint32_t ui32ConfigVal);
  203. extern void am_hal_ctimer_start(uint32_t ui32TimerNumber,
  204. uint32_t ui32TimerSegment);
  205. extern void am_hal_ctimer_stop(uint32_t ui32TimerNumber,
  206. uint32_t ui32TimerSegment);
  207. extern void am_hal_ctimer_clear(uint32_t ui32TimerNumber,
  208. uint32_t ui32TimerSegment);
  209. extern uint32_t am_hal_ctimer_read(uint32_t ui32TimerNumber,
  210. uint32_t ui32TimerSegment);
  211. extern void am_hal_ctimer_pin_enable(uint32_t ui32TimerNumber,
  212. uint32_t ui32TimerSegment);
  213. extern void am_hal_ctimer_pin_disable(uint32_t ui32TimerNumber,
  214. uint32_t ui32TimerSegment);
  215. extern void am_hal_ctimer_pin_invert(uint32_t ui32TimerNumber,
  216. uint32_t ui32TimerSegment,
  217. bool bInvertOutput);
  218. extern void am_hal_ctimer_compare_set(uint32_t ui32TimerNumber,
  219. uint32_t ui32TimerSegment,
  220. uint32_t ui32CompareReg,
  221. uint32_t ui32Value);
  222. extern void am_hal_ctimer_period_set(uint32_t ui32TimerNumber,
  223. uint32_t ui32TimerSegment,
  224. uint32_t ui32Period,
  225. uint32_t ui32OnTime);
  226. extern void am_hal_ctimer_adc_trigger_enable(void);
  227. extern void am_hal_ctimer_adc_trigger_disable(void);
  228. extern void am_hal_ctimer_int_enable(uint32_t ui32Interrupt);
  229. extern uint32_t am_hal_ctimer_int_enable_get(void);
  230. extern void am_hal_ctimer_int_disable(uint32_t ui32Interrupt);
  231. extern void am_hal_ctimer_int_set(uint32_t ui32Interrupt);
  232. extern void am_hal_ctimer_int_clear(uint32_t ui32Interrupt);
  233. extern uint32_t am_hal_ctimer_int_status_get(bool bEnabledOnly);
  234. extern void am_hal_ctimer_int_register(uint32_t ui32Interrupt,
  235. am_hal_ctimer_handler_t pfnHandler);
  236. extern void am_hal_ctimer_int_service(uint32_t ui32Status);
  237. #ifdef __cplusplus
  238. }
  239. #endif
  240. #endif // AM_HAL_CTIMER_H
  241. //*****************************************************************************
  242. //
  243. // End Doxygen group.
  244. //! @}
  245. //
  246. //*****************************************************************************