am_hal_stimer.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. //*****************************************************************************
  2. //
  3. // am_hal_stimer.h
  4. //! @file
  5. //!
  6. //! @brief Functions for accessing and configuring the STIMER.
  7. //!
  8. //! @addtogroup stimer2 Counter/Timer (STIMER)
  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_STIMER_H
  48. #define AM_HAL_STIMER_H
  49. //
  50. // Compute address of a given COMPARE or CAPTURE register.
  51. // Note - For Apollo2, the parameter n should be 0 (as only 1 stimer module
  52. // exists) and the parameter r should be 0-7 (compare) or 0-3 (capture).
  53. //
  54. #define AM_REG_STIMER_COMPARE(n, r) (AM_REG_CTIMERn(n) + \
  55. AM_REG_CTIMER_SCMPR0_O + (r * 4))
  56. #define AM_REG_STIMER_CAPTURE(n, r) (AM_REG_CTIMERn(n) + \
  57. AM_REG_CTIMER_SCAPT0_O + (r * 4))
  58. //*****************************************************************************
  59. //
  60. //! @name Interrupt Status Bits
  61. //! @brief Interrupt Status Bits for enable/disble use
  62. //!
  63. //! These macros may be used to set and clear interrupt bits
  64. //! @{
  65. //
  66. //*****************************************************************************
  67. #define AM_HAL_STIMER_INT_COMPAREA AM_REG_CTIMER_STMINTSTAT_COMPAREA_M
  68. #define AM_HAL_STIMER_INT_COMPAREB AM_REG_CTIMER_STMINTSTAT_COMPAREB_M
  69. #define AM_HAL_STIMER_INT_COMPAREC AM_REG_CTIMER_STMINTSTAT_COMPAREC_M
  70. #define AM_HAL_STIMER_INT_COMPARED AM_REG_CTIMER_STMINTSTAT_COMPARED_M
  71. #define AM_HAL_STIMER_INT_COMPAREE AM_REG_CTIMER_STMINTSTAT_COMPAREE_M
  72. #define AM_HAL_STIMER_INT_COMPAREF AM_REG_CTIMER_STMINTSTAT_COMPAREF_M
  73. #define AM_HAL_STIMER_INT_COMPAREG AM_REG_CTIMER_STMINTSTAT_COMPAREG_M
  74. #define AM_HAL_STIMER_INT_COMPAREH AM_REG_CTIMER_STMINTSTAT_COMPAREH_M
  75. #define AM_HAL_STIMER_INT_OVERFLOW AM_REG_CTIMER_STMINTSTAT_OVERFLOW_M
  76. #define AM_HAL_STIMER_INT_CAPTUREA AM_REG_CTIMER_STMINTSTAT_CAPTUREA_M
  77. #define AM_HAL_STIMER_INT_CAPTUREB AM_REG_CTIMER_STMINTSTAT_CAPTUREB_M
  78. #define AM_HAL_STIMER_INT_CAPTUREC AM_REG_CTIMER_STMINTSTAT_CAPTUREC_M
  79. #define AM_HAL_STIMER_INT_CAPTURED AM_REG_CTIMER_STMINTSTAT_CAPTURED_M
  80. //! @}
  81. //*****************************************************************************
  82. //
  83. //! @name STimer Configuration Bits
  84. //! @brief Interrupt Status Bits for enable/disble use
  85. //!
  86. //! These macros may be used to set and clear interrupt bits
  87. //! @{
  88. //
  89. //*****************************************************************************
  90. #define AM_HAL_STIMER_CFG_THAW \
  91. AM_REG_CTIMER_STCFG_FREEZE_THAW
  92. #define AM_HAL_STIMER_CFG_FREEZE \
  93. AM_REG_CTIMER_STCFG_FREEZE_FREEZE
  94. #define AM_HAL_STIMER_CFG_RUN \
  95. AM_REG_CTIMER_STCFG_CLEAR_RUN
  96. #define AM_HAL_STIMER_CFG_CLEAR \
  97. AM_REG_CTIMER_STCFG_CLEAR_CLEAR
  98. #define AM_HAL_STIMER_CFG_COMPARE_A_ENABLE \
  99. AM_REG_CTIMER_STCFG_COMPARE_A_EN_ENABLE
  100. #define AM_HAL_STIMER_CFG_COMPARE_B_ENABLE \
  101. AM_REG_CTIMER_STCFG_COMPARE_B_EN_ENABLE
  102. #define AM_HAL_STIMER_CFG_COMPARE_C_ENABLE \
  103. AM_REG_CTIMER_STCFG_COMPARE_C_EN_ENABLE
  104. #define AM_HAL_STIMER_CFG_COMPARE_D_ENABLE \
  105. AM_REG_CTIMER_STCFG_COMPARE_D_EN_ENABLE
  106. #define AM_HAL_STIMER_CFG_COMPARE_E_ENABLE \
  107. AM_REG_CTIMER_STCFG_COMPARE_E_EN_ENABLE
  108. #define AM_HAL_STIMER_CFG_COMPARE_F_ENABLE \
  109. AM_REG_CTIMER_STCFG_COMPARE_F_EN_ENABLE
  110. #define AM_HAL_STIMER_CFG_COMPARE_G_ENABLE \
  111. AM_REG_CTIMER_STCFG_COMPARE_G_EN_ENABLE
  112. #define AM_HAL_STIMER_CFG_COMPARE_H_ENABLE \
  113. AM_REG_CTIMER_STCFG_COMPARE_H_EN_ENABLE
  114. //! @}
  115. //*****************************************************************************
  116. //
  117. //! @name Clock Configuration options
  118. //! @brief STimer Configuration register options.
  119. //!
  120. //! These options are to be used with the am_hal_stimer_config() function.
  121. //! @{
  122. //
  123. //*****************************************************************************
  124. #define AM_HAL_STIMER_NO_CLK \
  125. AM_REG_CTIMER_STCFG_CLKSEL(AM_REG_CTIMER_STCFG_CLKSEL_NOCLK)
  126. #define AM_HAL_STIMER_HFRC_3MHZ \
  127. AM_REG_CTIMER_STCFG_CLKSEL(AM_REG_CTIMER_STCFG_CLKSEL_HFRC_DIV16)
  128. #define AM_HAL_STIMER_HFRC_187_5KHZ \
  129. AM_REG_CTIMER_STCFG_CLKSEL(AM_REG_CTIMER_STCFG_CLKSEL_HFRC_DIV256)
  130. #define AM_HAL_STIMER_XTAL_32KHZ \
  131. AM_REG_CTIMER_STCFG_CLKSEL(AM_REG_CTIMER_STCFG_CLKSEL_XTAL_DIV1)
  132. #define AM_HAL_STIMER_XTAL_16KHZ \
  133. AM_REG_CTIMER_STCFG_CLKSEL(AM_REG_CTIMER_STCFG_CLKSEL_XTAL_DIV2)
  134. #define AM_HAL_STIMER_XTAL_1KHZ \
  135. AM_REG_CTIMER_STCFG_CLKSEL(AM_REG_CTIMER_STCFG_CLKSEL_XTAL_DIV32)
  136. #define AM_HAL_STIMER_LFRC_1KHZ \
  137. AM_REG_CTIMER_STCFG_CLKSEL(AM_REG_CTIMER_STCFG_CLKSEL_LFRC_DIV1)
  138. #define AM_HAL_STIMER_HFRC_CTIMER0A \
  139. AM_REG_CTIMER_STCFG_CLKSEL(AM_REG_CTIMER_STCFG_CLKSEL_CTIMER0A)
  140. #define AM_HAL_STIMER_HFRC_CTIMER0B \
  141. AM_REG_CTIMER_STCFG_CLKSEL(AM_REG_CTIMER_STCFG_CLKSEL_CTIMER0B)
  142. //! @}
  143. //*****************************************************************************
  144. //
  145. //! @name Capture Control Register options.
  146. //! @brief Configuration options for capture control register.
  147. //!
  148. //! These options are to be used with the am_hal_stimer_capture_control_set
  149. //! function.
  150. //! @{
  151. //
  152. //*****************************************************************************
  153. #define AM_HAL_STIMER_CAPTURE_A_ENABLE \
  154. AM_REG_CTIMER_CAPTURE_CONTROL_CAPTURE_A_ENABLE
  155. #define AM_HAL_STIMER_CAPTURE_B_ENABLE \
  156. AM_REG_CTIMER_CAPTURE_CONTROL_CAPTURE_B_ENABLE
  157. #define AM_HAL_STIMER_CAPTURE_C_ENABLE \
  158. AM_REG_CTIMER_CAPTURE_CONTROL_CAPTURE_C_ENABLE
  159. #define AM_HAL_STIMER_CAPTURE_D_ENABLE \
  160. AM_REG_CTIMER_CAPTURE_CONTROL_CAPTURE_D_ENABLE
  161. //! @}
  162. //*****************************************************************************
  163. //
  164. //
  165. //
  166. //*****************************************************************************
  167. //*****************************************************************************
  168. //
  169. // Stimer configuration structure
  170. //
  171. //*****************************************************************************
  172. typedef struct
  173. {
  174. //
  175. //! Configuration options for the STIMER
  176. //
  177. uint32_t ui32STimerConfig;
  178. }
  179. am_hal_stimer_config_t;
  180. #ifdef __cplusplus
  181. extern "C"
  182. {
  183. #endif
  184. //*****************************************************************************
  185. //
  186. // External function definitions
  187. //
  188. //*****************************************************************************
  189. extern uint32_t am_hal_stimer_config(uint32_t ui32STimerConfig);
  190. extern uint32_t am_hal_stimer_counter_get(void);
  191. extern void am_hal_stimer_counter_clear(void);
  192. extern void am_hal_stimer_compare_delta_set(uint32_t ui32CmprInstance,
  193. uint32_t ui32Delta);
  194. extern uint32_t am_hal_stimer_compare_get(uint32_t ui32CmprInstance);
  195. extern void am_hal_stimer_capture_start(uint32_t ui32CaptureNum,
  196. uint32_t ui32GPIONumber,
  197. bool bPolarity);
  198. extern void am_hal_stimer_capture_stop(uint32_t ui32CaptureNum);
  199. extern uint32_t am_hal_stimer_capture_get(uint32_t ui32CaptureNum);
  200. extern void am_hal_stimer_int_enable(uint32_t ui32Interrupt);
  201. extern uint32_t am_hal_stimer_int_enable_get(void);
  202. extern void am_hal_stimer_int_disable(uint32_t ui32Interrupt);
  203. extern void am_hal_stimer_int_set(uint32_t ui32Interrupt);
  204. extern void am_hal_stimer_int_clear(uint32_t ui32Interrupt);
  205. extern uint32_t am_hal_stimer_int_status_get(bool bEnabledOnly);
  206. #ifdef __cplusplus
  207. }
  208. #endif
  209. #endif // AM_HAL_STIMER_H
  210. //*****************************************************************************
  211. //
  212. // End Doxygen group.
  213. //! @}
  214. //
  215. //*****************************************************************************