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.9 of the AmbiqSuite Development Package.
  45. //
  46. //*****************************************************************************
  47. #ifndef AM_HAL_STIMER_H
  48. #define AM_HAL_STIMER_H
  49. #ifdef __cplusplus
  50. extern "C"
  51. {
  52. #endif
  53. //
  54. // Compute address of a given COMPARE or CAPTURE register.
  55. // Note - For Apollo2, the parameter n should be 0 (as only 1 stimer module
  56. // exists) and the parameter r should be 0-7 (compare) or 0-3 (capture).
  57. //
  58. #define AM_REG_STIMER_COMPARE(n, r) (AM_REG_CTIMERn(n) + \
  59. AM_REG_CTIMER_SCMPR0_O + (r * 4))
  60. #define AM_REG_STIMER_CAPTURE(n, r) (AM_REG_CTIMERn(n) + \
  61. AM_REG_CTIMER_SCAPT0_O + (r * 4))
  62. //*****************************************************************************
  63. //
  64. //! @name Interrupt Status Bits
  65. //! @brief Interrupt Status Bits for enable/disble use
  66. //!
  67. //! These macros may be used to set and clear interrupt bits
  68. //! @{
  69. //
  70. //*****************************************************************************
  71. #define AM_HAL_STIMER_INT_COMPAREA AM_REG_CTIMER_STMINTSTAT_COMPAREA_M
  72. #define AM_HAL_STIMER_INT_COMPAREB AM_REG_CTIMER_STMINTSTAT_COMPAREB_M
  73. #define AM_HAL_STIMER_INT_COMPAREC AM_REG_CTIMER_STMINTSTAT_COMPAREC_M
  74. #define AM_HAL_STIMER_INT_COMPARED AM_REG_CTIMER_STMINTSTAT_COMPARED_M
  75. #define AM_HAL_STIMER_INT_COMPAREE AM_REG_CTIMER_STMINTSTAT_COMPAREE_M
  76. #define AM_HAL_STIMER_INT_COMPAREF AM_REG_CTIMER_STMINTSTAT_COMPAREF_M
  77. #define AM_HAL_STIMER_INT_COMPAREG AM_REG_CTIMER_STMINTSTAT_COMPAREG_M
  78. #define AM_HAL_STIMER_INT_COMPAREH AM_REG_CTIMER_STMINTSTAT_COMPAREH_M
  79. #define AM_HAL_STIMER_INT_OVERFLOW AM_REG_CTIMER_STMINTSTAT_OVERFLOW_M
  80. #define AM_HAL_STIMER_INT_CAPTUREA AM_REG_CTIMER_STMINTSTAT_CAPTUREA_M
  81. #define AM_HAL_STIMER_INT_CAPTUREB AM_REG_CTIMER_STMINTSTAT_CAPTUREB_M
  82. #define AM_HAL_STIMER_INT_CAPTUREC AM_REG_CTIMER_STMINTSTAT_CAPTUREC_M
  83. #define AM_HAL_STIMER_INT_CAPTURED AM_REG_CTIMER_STMINTSTAT_CAPTURED_M
  84. //! @}
  85. //*****************************************************************************
  86. //
  87. //! @name STimer Configuration Bits
  88. //! @brief Interrupt Status Bits for enable/disble use
  89. //!
  90. //! These macros may be used to set and clear interrupt bits
  91. //! @{
  92. //
  93. //*****************************************************************************
  94. #define AM_HAL_STIMER_CFG_THAW \
  95. AM_REG_CTIMER_STCFG_FREEZE_THAW
  96. #define AM_HAL_STIMER_CFG_FREEZE \
  97. AM_REG_CTIMER_STCFG_FREEZE_FREEZE
  98. #define AM_HAL_STIMER_CFG_RUN \
  99. AM_REG_CTIMER_STCFG_CLEAR_RUN
  100. #define AM_HAL_STIMER_CFG_CLEAR \
  101. AM_REG_CTIMER_STCFG_CLEAR_CLEAR
  102. #define AM_HAL_STIMER_CFG_COMPARE_A_ENABLE \
  103. AM_REG_CTIMER_STCFG_COMPARE_A_EN_ENABLE
  104. #define AM_HAL_STIMER_CFG_COMPARE_B_ENABLE \
  105. AM_REG_CTIMER_STCFG_COMPARE_B_EN_ENABLE
  106. #define AM_HAL_STIMER_CFG_COMPARE_C_ENABLE \
  107. AM_REG_CTIMER_STCFG_COMPARE_C_EN_ENABLE
  108. #define AM_HAL_STIMER_CFG_COMPARE_D_ENABLE \
  109. AM_REG_CTIMER_STCFG_COMPARE_D_EN_ENABLE
  110. #define AM_HAL_STIMER_CFG_COMPARE_E_ENABLE \
  111. AM_REG_CTIMER_STCFG_COMPARE_E_EN_ENABLE
  112. #define AM_HAL_STIMER_CFG_COMPARE_F_ENABLE \
  113. AM_REG_CTIMER_STCFG_COMPARE_F_EN_ENABLE
  114. #define AM_HAL_STIMER_CFG_COMPARE_G_ENABLE \
  115. AM_REG_CTIMER_STCFG_COMPARE_G_EN_ENABLE
  116. #define AM_HAL_STIMER_CFG_COMPARE_H_ENABLE \
  117. AM_REG_CTIMER_STCFG_COMPARE_H_EN_ENABLE
  118. //! @}
  119. //*****************************************************************************
  120. //
  121. //! @name Clock Configuration options
  122. //! @brief STimer Configuration register options.
  123. //!
  124. //! These options are to be used with the am_hal_stimer_config() function.
  125. //! @{
  126. //
  127. //*****************************************************************************
  128. #define AM_HAL_STIMER_NO_CLK \
  129. AM_REG_CTIMER_STCFG_CLKSEL(AM_REG_CTIMER_STCFG_CLKSEL_NOCLK)
  130. #define AM_HAL_STIMER_HFRC_3MHZ \
  131. AM_REG_CTIMER_STCFG_CLKSEL(AM_REG_CTIMER_STCFG_CLKSEL_HFRC_DIV16)
  132. #define AM_HAL_STIMER_HFRC_187_5KHZ \
  133. AM_REG_CTIMER_STCFG_CLKSEL(AM_REG_CTIMER_STCFG_CLKSEL_HFRC_DIV256)
  134. #define AM_HAL_STIMER_XTAL_32KHZ \
  135. AM_REG_CTIMER_STCFG_CLKSEL(AM_REG_CTIMER_STCFG_CLKSEL_XTAL_DIV1)
  136. #define AM_HAL_STIMER_XTAL_16KHZ \
  137. AM_REG_CTIMER_STCFG_CLKSEL(AM_REG_CTIMER_STCFG_CLKSEL_XTAL_DIV2)
  138. #define AM_HAL_STIMER_XTAL_1KHZ \
  139. AM_REG_CTIMER_STCFG_CLKSEL(AM_REG_CTIMER_STCFG_CLKSEL_XTAL_DIV32)
  140. #define AM_HAL_STIMER_LFRC_1KHZ \
  141. AM_REG_CTIMER_STCFG_CLKSEL(AM_REG_CTIMER_STCFG_CLKSEL_LFRC_DIV1)
  142. #define AM_HAL_STIMER_HFRC_CTIMER0A \
  143. AM_REG_CTIMER_STCFG_CLKSEL(AM_REG_CTIMER_STCFG_CLKSEL_CTIMER0A)
  144. #define AM_HAL_STIMER_HFRC_CTIMER0B \
  145. AM_REG_CTIMER_STCFG_CLKSEL(AM_REG_CTIMER_STCFG_CLKSEL_CTIMER0B)
  146. //! @}
  147. //*****************************************************************************
  148. //
  149. //! @name Capture Control Register options.
  150. //! @brief Configuration options for capture control register.
  151. //!
  152. //! These options are to be used with the am_hal_stimer_capture_control_set
  153. //! function.
  154. //! @{
  155. //
  156. //*****************************************************************************
  157. #define AM_HAL_STIMER_CAPTURE_A_ENABLE \
  158. AM_REG_CTIMER_CAPTURE_CONTROL_CAPTURE_A_ENABLE
  159. #define AM_HAL_STIMER_CAPTURE_B_ENABLE \
  160. AM_REG_CTIMER_CAPTURE_CONTROL_CAPTURE_B_ENABLE
  161. #define AM_HAL_STIMER_CAPTURE_C_ENABLE \
  162. AM_REG_CTIMER_CAPTURE_CONTROL_CAPTURE_C_ENABLE
  163. #define AM_HAL_STIMER_CAPTURE_D_ENABLE \
  164. AM_REG_CTIMER_CAPTURE_CONTROL_CAPTURE_D_ENABLE
  165. //! @}
  166. //*****************************************************************************
  167. //
  168. //
  169. //
  170. //*****************************************************************************
  171. //*****************************************************************************
  172. //
  173. // Stimer configuration structure
  174. //
  175. //*****************************************************************************
  176. typedef struct
  177. {
  178. //
  179. //! Configuration options for the STIMER
  180. //
  181. uint32_t ui32STimerConfig;
  182. }
  183. am_hal_stimer_config_t;
  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. //*****************************************************************************