tmr.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /**
  2. * @file tmr.h
  3. * @brief Timer (TMR) function prototypes and data types.
  4. */
  5. /* ****************************************************************************
  6. * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a
  9. * copy of this software and associated documentation files (the "Software"),
  10. * to deal in the Software without restriction, including without limitation
  11. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  12. * and/or sell copies of the Software, and to permit persons to whom the
  13. * Software is furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included
  16. * in all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  19. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  21. * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
  22. * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  23. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  24. * OTHER DEALINGS IN THE SOFTWARE.
  25. *
  26. * Except as contained in this notice, the name of Maxim Integrated
  27. * Products, Inc. shall not be used except as stated in the Maxim Integrated
  28. * Products, Inc. Branding Policy.
  29. *
  30. * The mere transfer of this software does not imply any licenses
  31. * of trade secrets, proprietary technology, copyrights, patents,
  32. * trademarks, maskwork rights, or any other form of intellectual
  33. * property whatsoever. Maxim Integrated Products, Inc. retains all
  34. * ownership rights.
  35. *
  36. * $Date: 2019-09-11 14:32:22 -0500 (Wed, 11 Sep 2019) $
  37. * $Revision: 46047 $
  38. *
  39. *************************************************************************** */
  40. /* Define to prevent redundant inclusion */
  41. #ifndef _TMR_H_
  42. #define _TMR_H_
  43. /* **** Includes **** */
  44. #include "mxc_config.h"
  45. #include "tmr_regs.h"
  46. #include "mxc_sys.h"
  47. #ifdef __cplusplus
  48. extern "C" {
  49. #endif
  50. /**
  51. * @defgroup tmr Timer (TMR)
  52. * @ingroup periphlibs
  53. * @{
  54. */
  55. /**
  56. * @brief Timer prescaler values
  57. */
  58. typedef enum {
  59. TMR_PRES_1 = MXC_S_TMR_CN_PRES_DIV1, /// Divide input clock by 1
  60. TMR_PRES_2 = MXC_S_TMR_CN_PRES_DIV2, /// Divide input clock by 2
  61. TMR_PRES_4 = MXC_S_TMR_CN_PRES_DIV4, /// Divide input clock by 4
  62. TMR_PRES_8 = MXC_S_TMR_CN_PRES_DIV8, /// Divide input clock by 8
  63. TMR_PRES_16 = MXC_S_TMR_CN_PRES_DIV16, /// Divide input clock by 16
  64. TMR_PRES_32 = MXC_S_TMR_CN_PRES_DIV32, /// Divide input clock by 32
  65. TMR_PRES_64 = MXC_S_TMR_CN_PRES_DIV64, /// Divide input clock by 64
  66. TMR_PRES_128 = MXC_S_TMR_CN_PRES_DIV128, /// Divide input clock by 128
  67. TMR_PRES_256 = MXC_F_TMR_CN_PRES3 | MXC_S_TMR_CN_PRES_DIV1, /// Divide input clock by 256
  68. TMR_PRES_512 = MXC_F_TMR_CN_PRES3 | MXC_S_TMR_CN_PRES_DIV2, /// Divide input clock by 512
  69. TMR_PRES_1024 = MXC_F_TMR_CN_PRES3 | MXC_S_TMR_CN_PRES_DIV4, /// Divide input clock by 1024
  70. TMR_PRES_2048 = MXC_F_TMR_CN_PRES3 | MXC_S_TMR_CN_PRES_DIV8, /// Divide input clock by 2048
  71. TMR_PRES_4096 = MXC_F_TMR_CN_PRES3 | MXC_S_TMR_CN_PRES_DIV16 /// Divide input clock by 4096
  72. } tmr_pres_t;
  73. /**
  74. * @brief Timer modes
  75. */
  76. typedef enum {
  77. TMR_MODE_ONESHOT = MXC_V_TMR_CN_TMODE_ONESHOT, /// Timer Mode ONESHOT
  78. TMR_MODE_CONTINUOUS = MXC_V_TMR_CN_TMODE_CONTINUOUS, /// Timer Mode CONTINUOUS
  79. TMR_MODE_COUNTER = MXC_V_TMR_CN_TMODE_COUNTER, /// Timer Mode COUNTER
  80. TMR_MODE_PWM = MXC_V_TMR_CN_TMODE_PWM, /// Timer Mode PWM
  81. TMR_MODE_CAPTURE = MXC_V_TMR_CN_TMODE_CAPTURE, /// Timer Mode CAPTURE
  82. TMR_MODE_COMPARE = MXC_V_TMR_CN_TMODE_COMPARE, /// Timer Mode COMPARE
  83. TMR_MODE_GATED = MXC_V_TMR_CN_TMODE_GATED, /// Timer Mode GATED
  84. TMR_MODE_CAPTURE_COMPARE = MXC_V_TMR_CN_TMODE_CAPTURECOMPARE /// Timer Mode CAPTURECOMPARE
  85. } tmr_mode_t;
  86. /**
  87. * @brief Timer units of time enumeration
  88. */
  89. typedef enum {
  90. TMR_UNIT_NANOSEC = 0, /**< Nanosecond Unit Indicator. */
  91. TMR_UNIT_MICROSEC, /**< Microsecond Unit Indicator. */
  92. TMR_UNIT_MILLISEC, /**< Millisecond Unit Indicator. */
  93. TMR_UNIT_SEC, /**< Second Unit Indicator. */
  94. } tmr_unit_t;
  95. /**
  96. * @brief Timer Configuration
  97. */
  98. typedef struct {
  99. tmr_mode_t mode; /// Desired timer mode
  100. uint32_t cmp_cnt; /// Compare register value in timer ticks
  101. unsigned pol; /// Polarity (0 or 1)
  102. } tmr_cfg_t;
  103. /**
  104. * @brief Timer PWM Configuration
  105. */
  106. typedef struct {
  107. unsigned pol; /// PWM polarity (0 or 1)
  108. uint32_t per_cnt; /// PWM period in timer ticks
  109. uint32_t duty_cnt; /// PWM duty in timer ticks
  110. } tmr_pwm_cfg_t;
  111. /* **** Definitions **** */
  112. /* **** Function Prototypes **** */
  113. /**
  114. * @brief Initialize timer module clock.
  115. * @param tmr Pointer to timer module to initialize.
  116. * @param pres Prescaler value.
  117. * @param sys_cfg System configuration object
  118. * @return #E_NO_ERROR if successful, error code otherwise.
  119. */
  120. int TMR_Init(mxc_tmr_regs_t *tmr, tmr_pres_t pres, const sys_cfg_tmr_t* sys_cfg);
  121. /**
  122. * @brief Shutdown timer module clock.
  123. * @param tmr Pointer to timer module to initialize.
  124. * @return #E_NO_ERROR if successful, error code otherwise.
  125. */
  126. int TMR_Shutdown(mxc_tmr_regs_t *tmr);
  127. /**
  128. * @brief Enable the timer.
  129. * @param tmr Pointer to timer module to initialize.
  130. */
  131. void TMR_Enable(mxc_tmr_regs_t* tmr);
  132. /**
  133. * @brief Disable the timer.
  134. * @param tmr Pointer to timer module to initialize.
  135. */
  136. void TMR_Disable(mxc_tmr_regs_t* tmr);
  137. /**
  138. * @brief Configure the timer.
  139. * @param tmr Pointer to timer module to initialize.
  140. * @param cfg Pointer to timer configuration struct.
  141. * @return #E_NO_ERROR if successful.
  142. */
  143. int TMR_Config(mxc_tmr_regs_t *tmr, const tmr_cfg_t *cfg);
  144. /**
  145. * @brief Configure the timer for PWM operation.
  146. * @param tmr Pointer to timer module to initialize.
  147. * @param cfg Pointer to timer PWM configuration struct.
  148. * @note Can cause a glitch if the Timer is currently running.
  149. * @return #E_BAD_PARAM if duty_cnt > per_cnt.
  150. */
  151. int TMR_PWMConfig(mxc_tmr_regs_t *tmr, const tmr_pwm_cfg_t *cfg);
  152. /**
  153. * @brief Set the timer duty cycle.
  154. * @param tmr Pointer to timer module to initialize
  155. * @param duty New duty cycle count
  156. * @note Will block until safe to change the duty count.
  157. * @return #E_BAD_PARAM if duty_cnt > per_cnt.
  158. */
  159. int TMR_PWMSetDuty(mxc_tmr_regs_t *tmr, uint32_t duty);
  160. /**
  161. * @brief Set the timer period.
  162. * @param tmr Pointer to timer module to initialize.
  163. * @param per New period count.
  164. * @note Will block until safe to change the period count.
  165. * @return #E_BAD_PARAM if duty_cnt > per_cnt.
  166. */
  167. int TMR_PWMSetPeriod(mxc_tmr_regs_t* tmr, uint32_t per);
  168. /**
  169. * @brief Get the timer compare count.
  170. * @param tmr Pointer to timer module to initialize.
  171. * @return Returns the current compare count.
  172. */
  173. uint32_t TMR_GetCompare(mxc_tmr_regs_t* tmr);
  174. /**
  175. * @brief Get the timer capture count.
  176. * @param tmr Pointer to timer module to initialize.
  177. * @return Returns the most recent capture count.
  178. */
  179. uint32_t TMR_GetCapture(mxc_tmr_regs_t* tmr);
  180. /**
  181. * @brief Get the timer count.
  182. * @param tmr Pointer to timer module to initialize.
  183. * @return Returns the current count.
  184. */
  185. uint32_t TMR_GetCount(mxc_tmr_regs_t* tmr);
  186. /**
  187. * @brief Clear the timer interrupt.
  188. * @param tmr Pointer to timer module to initialize.
  189. */
  190. void TMR_IntClear(mxc_tmr_regs_t* tmr);
  191. /**
  192. * @brief Get the timer interrupt status.
  193. * @param tmr Pointer to timer module to initialize.
  194. * @return Returns the interrupt status. 1 if interrupt has occurred.
  195. */
  196. uint32_t TMR_IntStatus(mxc_tmr_regs_t* tmr);
  197. /**
  198. * @brief Set the timer compare count.
  199. * @param tmr Pointer to timer module to initialize.
  200. * @param cmp_cnt New compare count.
  201. * @note This function does not protect against output glitches in PWM mode.
  202. * Use TMR_PWMSetPeriod when in PWM mode.
  203. */
  204. void TMR_SetCompare(mxc_tmr_regs_t *tmr, uint32_t cmp_cnt);
  205. /**
  206. * @brief Set the timer count.
  207. * @param tmr Pointer to timer module to initialize.
  208. * @param cnt New count.
  209. */
  210. void TMR_SetCount(mxc_tmr_regs_t *tmr, uint32_t cnt);
  211. /**
  212. * @brief Convert real time to timer ticks.
  213. * @param tmr Pointer to timer module to initialize.
  214. * @param time Number of units of time.
  215. * @param units Which units of time you want to convert.
  216. * @param ticks Pointer to store the number of ticks calculated.
  217. * @return #E_NO_ERROR if successful, error code otherwise.
  218. */
  219. int TMR_GetTicks(mxc_tmr_regs_t *tmr, uint32_t time, tmr_unit_t units, uint32_t *ticks);
  220. /**
  221. * @brief Convert timer ticks to real time.
  222. * @param tmr Pointer to timer module to initialize.
  223. * @param ticks Number of ticks.
  224. * @param time Pointer to store number of units of time.
  225. * @param units Pointer to store the units that time represents.
  226. * @return #E_NO_ERROR if successful, error code otherwise.
  227. */
  228. int TMR_GetTime(mxc_tmr_regs_t *tmr, uint32_t ticks, uint32_t *time, tmr_unit_t *units);
  229. /**@} end of group tmr */
  230. #ifdef __cplusplus
  231. }
  232. #endif
  233. #endif /* _TMR_H_ */