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.9 of the AmbiqSuite Development Package.
  45. //
  46. //*****************************************************************************
  47. #ifndef AM_HAL_CTIMER_H
  48. #define AM_HAL_CTIMER_H
  49. #ifdef __cplusplus
  50. extern "C"
  51. {
  52. #endif
  53. //*****************************************************************************
  54. //
  55. //! Number of timers
  56. //
  57. //*****************************************************************************
  58. #define AM_HAL_CTIMER_TIMERS_NUM 4
  59. //*****************************************************************************
  60. //
  61. //! Timer offset value
  62. //
  63. //*****************************************************************************
  64. #define AM_HAL_CTIMER_TIMER_OFFSET (AM_REG_CTIMER_TMR1_O - AM_REG_CTIMER_TMR0_O)
  65. //*****************************************************************************
  66. //
  67. //! @name Interrupt Status Bits
  68. //! @brief Interrupt Status Bits for enable/disble use
  69. //!
  70. //! These macros may be used to set and clear interrupt bits
  71. //! @{
  72. //
  73. //*****************************************************************************
  74. #define AM_HAL_CTIMER_INT_TIMERA0C0 AM_REG_CTIMER_INTEN_CTMRA0C0INT_M
  75. #define AM_HAL_CTIMER_INT_TIMERA0C1 AM_REG_CTIMER_INTEN_CTMRA0C1INT_M
  76. #define AM_HAL_CTIMER_INT_TIMERA1C0 AM_REG_CTIMER_INTEN_CTMRA1C0INT_M
  77. #define AM_HAL_CTIMER_INT_TIMERA1C1 AM_REG_CTIMER_INTEN_CTMRA1C1INT_M
  78. #define AM_HAL_CTIMER_INT_TIMERA2C0 AM_REG_CTIMER_INTEN_CTMRA2C0INT_M
  79. #define AM_HAL_CTIMER_INT_TIMERA2C1 AM_REG_CTIMER_INTEN_CTMRA2C1INT_M
  80. #define AM_HAL_CTIMER_INT_TIMERA3C0 AM_REG_CTIMER_INTEN_CTMRA3C0INT_M
  81. #define AM_HAL_CTIMER_INT_TIMERA3C1 AM_REG_CTIMER_INTEN_CTMRA3C1INT_M
  82. #define AM_HAL_CTIMER_INT_TIMERB0C0 AM_REG_CTIMER_INTEN_CTMRB0C0INT_M
  83. #define AM_HAL_CTIMER_INT_TIMERB0C1 AM_REG_CTIMER_INTEN_CTMRB0C1INT_M
  84. #define AM_HAL_CTIMER_INT_TIMERB1C0 AM_REG_CTIMER_INTEN_CTMRB1C0INT_M
  85. #define AM_HAL_CTIMER_INT_TIMERB1C1 AM_REG_CTIMER_INTEN_CTMRB1C1INT_M
  86. #define AM_HAL_CTIMER_INT_TIMERB2C0 AM_REG_CTIMER_INTEN_CTMRB2C0INT_M
  87. #define AM_HAL_CTIMER_INT_TIMERB2C1 AM_REG_CTIMER_INTEN_CTMRB2C1INT_M
  88. #define AM_HAL_CTIMER_INT_TIMERB3C0 AM_REG_CTIMER_INTEN_CTMRB3C0INT_M
  89. #define AM_HAL_CTIMER_INT_TIMERB3C1 AM_REG_CTIMER_INTEN_CTMRB3C1INT_M
  90. //
  91. // Deprecated, use the newer macros above.
  92. //
  93. #define AM_HAL_CTIMER_INT_TIMERA0 AM_HAL_CTIMER_INT_TIMERA0C0
  94. #define AM_HAL_CTIMER_INT_TIMERB0 AM_HAL_CTIMER_INT_TIMERB0C0
  95. #define AM_HAL_CTIMER_INT_TIMERA1 AM_HAL_CTIMER_INT_TIMERA1C0
  96. #define AM_HAL_CTIMER_INT_TIMERB1 AM_HAL_CTIMER_INT_TIMERB1C0
  97. #define AM_HAL_CTIMER_INT_TIMERA2 AM_HAL_CTIMER_INT_TIMERA2C0
  98. #define AM_HAL_CTIMER_INT_TIMERB2 AM_HAL_CTIMER_INT_TIMERB2C0
  99. #define AM_HAL_CTIMER_INT_TIMERA3 AM_HAL_CTIMER_INT_TIMERA3C0
  100. #define AM_HAL_CTIMER_INT_TIMERB3 AM_HAL_CTIMER_INT_TIMERB3C0
  101. //! @}
  102. //*****************************************************************************
  103. //
  104. //! @name Configuration options
  105. //! @brief Configuration options for \e am_hal_ctimer_config_t
  106. //!
  107. //! These options are to be used with the \e am_hal_ctimer_config_t structure
  108. //! used by \e am_hal_ctimer_config
  109. //! @{
  110. //
  111. //*****************************************************************************
  112. #define AM_HAL_CTIMER_CLK_PIN AM_REG_CTIMER_CTRL0_TMRA0CLK(0x0)
  113. #define AM_HAL_CTIMER_HFRC_12MHZ AM_REG_CTIMER_CTRL0_TMRA0CLK(0x1)
  114. #define AM_HAL_CTIMER_HFRC_3MHZ AM_REG_CTIMER_CTRL0_TMRA0CLK(0x2)
  115. #define AM_HAL_CTIMER_HFRC_187_5KHZ AM_REG_CTIMER_CTRL0_TMRA0CLK(0x3)
  116. #define AM_HAL_CTIMER_HFRC_47KHZ AM_REG_CTIMER_CTRL0_TMRA0CLK(0x4)
  117. #define AM_HAL_CTIMER_HFRC_12KHZ AM_REG_CTIMER_CTRL0_TMRA0CLK(0x5)
  118. #define AM_HAL_CTIMER_XT_32_768KHZ AM_REG_CTIMER_CTRL0_TMRA0CLK(0x6)
  119. #define AM_HAL_CTIMER_XT_16_384KHZ AM_REG_CTIMER_CTRL0_TMRA0CLK(0x7)
  120. #define AM_HAL_CTIMER_XT_2_048KHZ AM_REG_CTIMER_CTRL0_TMRA0CLK(0x8)
  121. #define AM_HAL_CTIMER_XT_256HZ AM_REG_CTIMER_CTRL0_TMRA0CLK(0x9)
  122. #define AM_HAL_CTIMER_LFRC_512HZ AM_REG_CTIMER_CTRL0_TMRA0CLK(0xA)
  123. #define AM_HAL_CTIMER_LFRC_32HZ AM_REG_CTIMER_CTRL0_TMRA0CLK(0xB)
  124. #define AM_HAL_CTIMER_LFRC_1HZ AM_REG_CTIMER_CTRL0_TMRA0CLK(0xC)
  125. #define AM_HAL_CTIMER_LFRC_1_16HZ AM_REG_CTIMER_CTRL0_TMRA0CLK(0xD)
  126. #define AM_HAL_CTIMER_RTC_100HZ AM_REG_CTIMER_CTRL0_TMRA0CLK(0xE)
  127. #define AM_HAL_CTIMER_HCLK AM_REG_CTIMER_CTRL0_TMRA0CLK(0xF)
  128. #define AM_HAL_CTIMER_BUCK AM_REG_CTIMER_CTRL0_TMRA0CLK(0x10)
  129. //! @}
  130. //*****************************************************************************
  131. //
  132. // Timer function macros.
  133. //
  134. //*****************************************************************************
  135. #define AM_HAL_CTIMER_FN_ONCE AM_REG_CTIMER_CTRL0_TMRA0FN(0)
  136. #define AM_HAL_CTIMER_FN_REPEAT AM_REG_CTIMER_CTRL0_TMRA0FN(1)
  137. #define AM_HAL_CTIMER_FN_PWM_ONCE AM_REG_CTIMER_CTRL0_TMRA0FN(2)
  138. #define AM_HAL_CTIMER_FN_PWM_REPEAT AM_REG_CTIMER_CTRL0_TMRA0FN(3)
  139. #define AM_HAL_CTIMER_FN_CONTINUOUS AM_REG_CTIMER_CTRL0_TMRA0FN(4)
  140. //*****************************************************************************
  141. //
  142. // Half-timer options.
  143. //
  144. //*****************************************************************************
  145. #define AM_HAL_CTIMER_INT_ENABLE AM_REG_CTIMER_CTRL0_TMRA0IE0_M
  146. #define AM_HAL_CTIMER_PIN_ENABLE AM_REG_CTIMER_CTRL0_TMRA0PE_M
  147. #define AM_HAL_CTIMER_PIN_INVERT AM_REG_CTIMER_CTRL0_TMRA0POL_M
  148. #define AM_HAL_CTIMER_CLEAR AM_REG_CTIMER_CTRL0_TMRA0CLR_M
  149. //*****************************************************************************
  150. //
  151. // Additional timer options.
  152. //
  153. //*****************************************************************************
  154. #define AM_HAL_CTIMER_LINK AM_REG_CTIMER_CTRL0_CTLINK0_M
  155. #define AM_HAL_CTIMER_ADC_TRIG AM_REG_CTIMER_CTRL3_ADCEN_M
  156. //*****************************************************************************
  157. //
  158. // Timer selection macros.
  159. //
  160. //*****************************************************************************
  161. #define AM_HAL_CTIMER_TIMERA 0x0000FFFF
  162. #define AM_HAL_CTIMER_TIMERB 0xFFFF0000
  163. #define AM_HAL_CTIMER_BOTH 0xFFFFFFFF
  164. //! @}
  165. //*****************************************************************************
  166. //
  167. // Timer configuration structure
  168. //
  169. //*****************************************************************************
  170. typedef struct
  171. {
  172. //
  173. //! Set to 1 to operate this timer as a 32-bit timer instead of two 16-bit
  174. //! timers.
  175. //
  176. uint32_t ui32Link;
  177. //
  178. //! Configuration options for TIMERA
  179. //
  180. uint32_t ui32TimerAConfig;
  181. //
  182. //! Configuration options for TIMERB
  183. //
  184. uint32_t ui32TimerBConfig;
  185. }
  186. am_hal_ctimer_config_t;
  187. //*****************************************************************************
  188. //
  189. // Function pointer type for CTimer interrupt handlers.
  190. //
  191. //*****************************************************************************
  192. typedef void (*am_hal_ctimer_handler_t)(void);
  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. //*****************************************************************************