gpt.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * Copyright (c) 2011-2012, Freescale Semiconductor, Inc.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * o Redistributions of source code must retain the above copyright notice, this list
  9. * of conditions and the following disclaimer.
  10. *
  11. * o Redistributions in binary form must reproduce the above copyright notice, this
  12. * list of conditions and the following disclaimer in the documentation and/or
  13. * other materials provided with the distribution.
  14. *
  15. * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
  16. * contributors may be used to endorse or promote products derived from this
  17. * software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  23. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  26. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. //! @addtogroup diag_gpt
  31. //! @{
  32. /*!
  33. * @file gpt.h
  34. * @brief GPT driver public interface.
  35. */
  36. #ifndef __GPT_H__
  37. #define __GPT_H__
  38. #include "imx_timer.h"
  39. #include "sdk.h"
  40. ////////////////////////////////////////////////////////////////////////////////
  41. // Definitions
  42. ////////////////////////////////////////////////////////////////////////////////
  43. //! @brief Possible events for the GPT.
  44. //!
  45. //! These constants are intended to be combined together to form a bitmask. Several
  46. //! of the GPT driver APIs use such a bitmask. For instance, gpt_counter_enable()
  47. //! accepts a bitmask that selects the events for which interrupts should be enabled.
  48. //!
  49. //! Note that the values of these enums happen to be the bitmasks for the respective
  50. //! fields in the HW_GPT_SR and HW_GPT_IR registers, so a mask constructed from them
  51. //! can be used directly with register values.
  52. enum _gpt_events
  53. {
  54. kGPTNoEvent = 0, //!< No events enabled.
  55. kGPTRollover = 1 << 5, //!< Rollover event.
  56. kGPTInputCapture1 = 1 << 3, //!< Input capture 1 event.
  57. kGPTInputCapture2 = 1 << 4, //!< Input capture 2 event.
  58. kGPTOutputCompare1 = 1 << 0, //!< Output compare 1 event.
  59. kGPTOutputCompare2 = 1 << 1, //!< Output compare 2 event.
  60. kGPTOutputCompare3 = 1 << 2, //!< Output compare 3 event.
  61. //! Combined mask of all GPT events.
  62. kGPTAllEvents = kGPTRollover | kGPTInputCapture1 | kGPTInputCapture2
  63. | kGPTOutputCompare1 | kGPTOutputCompare2 | kGPTOutputCompare3
  64. };
  65. //! @brief GPT counter modes.
  66. enum _gpt_counter_mode
  67. {
  68. RESTART_MODE = 0,
  69. FREE_RUN_MODE = 1
  70. };
  71. //! @brief Supported input capture modes.
  72. enum _gpt_capture_modes
  73. {
  74. INPUT_CAP_DISABLE = 0, //!< input capture event disabled
  75. INPUT_CAP_RISING_EDGE = 1, //!< input capture event on a rising edge
  76. INPUT_CAP_FALLING_EDGE = 2, //!< input capture event on a falling edge
  77. INPUT_CAP_BOTH_EDGE = 3 //!< input capture event on a both edge
  78. };
  79. //! @brief Supported output modes.
  80. enum _gpt_compare_modes
  81. {
  82. OUTPUT_CMP_DISABLE = 0, //!< output disconnected from pad
  83. OUTPUT_CMP_TOGGLE = 1, //!< output toggle mode
  84. OUTPUT_CMP_CLEAR = 2, //!< output set low mode
  85. OUTPUT_CMP_SET = 3, //!< output set high mode
  86. OUTPUT_CMP_LOWPULSE = 4 //!< output set high mode
  87. };
  88. ////////////////////////////////////////////////////////////////////////////////
  89. // API
  90. ////////////////////////////////////////////////////////////////////////////////
  91. #if defined(__cplusplus)
  92. extern "C" {
  93. #endif
  94. /*!
  95. * @brief Initialize the GPT timer.
  96. *
  97. * @param clock_src Source clock of the counter: CLKSRC_OFF, CLKSRC_IPG_CLK,
  98. * CLKSRC_PER_CLK, CLKSRC_CKIL, CLKSRC_CLKIN.
  99. * @param prescaler Prescaler of the source clock from 1 to 4096.
  100. * @param counter_mode Counter mode: FREE_RUN_MODE or RESTART_MODE.
  101. * @param low_power_mode Low power during which the timer is enabled:
  102. * WAIT_MODE_EN and/or STOP_MODE_EN.
  103. */
  104. void gpt_init(uint32_t clock_src, uint32_t prescaler, uint32_t counter_mode, uint32_t low_power_mode);
  105. /*!
  106. * @brief Setup GPT interrupt.
  107. *
  108. * It enables or disables the related HW module interrupt, and attached the
  109. * related sub-routine into the vector table.
  110. *
  111. * @param irq_subroutine the GPT interrupt interrupt routine.
  112. * @param enableIt Pass true to enable the interrupt.
  113. */
  114. void gpt_setup_interrupt(void (*irq_subroutine)(void), bool enableIt);
  115. /*!
  116. * @brief Enable the GPT module.
  117. *
  118. * Used typically when the gpt_init is done, and other interrupt related settings are ready.
  119. *
  120. * If a value of #kGPTNoEvent is passed for @a irq_mode, then no interrupts will be enabled.
  121. * This effectively puts the timer into polling mode, where you must call gpt_get_x_event()
  122. * to check for an event having occurred.
  123. *
  124. * @param irq_mode Mask of events to enable interrupts for, such as #kGPTRollover or
  125. * #kGPTOutputCompare1. See the #_gpt_events enum for the complete list. Pass
  126. * #kGPTNoEvent to prevent any interrupts from being enabled, which effectively puts
  127. * the timer into polling mode.
  128. */
  129. void gpt_counter_enable(uint32_t irq_mode);
  130. /*!
  131. * @brief Disable the counter.
  132. *
  133. * It saves power when not used.
  134. *
  135. */
  136. void gpt_counter_disable(void);
  137. /*!
  138. * @brief Get rollover event flag and clear it if set.
  139. *
  140. * This function can typically be used for polling method, but
  141. * is also used to clear the status compare flag in IRQ mode.
  142. *
  143. * @return Either 0 of kGPTRollover.
  144. */
  145. uint32_t gpt_get_rollover_event(void);
  146. /*!
  147. * @brief Get a captured value when an event occured, and clear the flag if set.
  148. *
  149. * Use this function to check for an input capture event having occurred, either in
  150. * the event ISR or to check manually in polling mode. Pass the input channel to check
  151. * in the @a flag parameter. If that channel fired, its captured timer value will be
  152. * read and placed in @a capture_val (if not NULL). The event that fired will be cleared
  153. * and its event mask returned as the return value from the function. If no event
  154. * occurred, the function returns 0.
  155. *
  156. * @param flag Which channel to check, either #kGPTInputCapture1 or #kGPTInputCapture2.
  157. * Only one channel may be specified.
  158. * @param capture_val The capture register value is returned through this parameter if
  159. * the specified event occurred. May be NULL if not required.
  160. * @return Mask of input specified capture event that occurred, or 0 if no event occurred.
  161. */
  162. uint32_t gpt_get_capture_event(uint8_t flag, uint32_t * capture_val);
  163. /*!
  164. * @brief Set the input capture mode.
  165. *
  166. * @param cap_input The input capture channel to configure, either #kGPTInputCapture1
  167. * or #kGPTInputCapture2.
  168. * @param cap_input_mode Capture input mode: #INPUT_CAP_DISABLE, #INPUT_CAP_BOTH_EDGE,
  169. * #INPUT_CAP_FALLING_EDGE, #INPUT_CAP_RISING_EDGE.
  170. */
  171. void gpt_set_capture_event(uint8_t cap_input, uint8_t cap_input_mode);
  172. /*!
  173. * @brief Get a compare event flag and clear it if set.
  174. *
  175. * This function can typically be used for polling method, but
  176. * is also used to clear the status compare flag in IRQ mode.
  177. *
  178. * @param flag Checked compare event flag such GPTSR_OF1, GPTSR_OF2, GPTSR_OF3.
  179. * @return The value of the compare event flag.
  180. */
  181. uint32_t gpt_get_compare_event(uint8_t flag);
  182. /*!
  183. * @brief Set a compare event by programming the compare register and
  184. * compare output mode.
  185. *
  186. * @param cmp_output The channel to configure. Must be one of #kGPTOutputCompare1,
  187. * #kGPTOutputCompare2, or #kGPTOutputCompare3.
  188. * @param cmp_output_mode Compare output mode: #OUTPUT_CMP_DISABLE, #OUTPUT_CMP_TOGGLE,
  189. * #OUTPUT_CMP_CLEAR, #OUTPUT_CMP_SET, #OUTPUT_CMP_LOWPULSE.
  190. * @param cmp_value Compare value for the compare register.
  191. */
  192. void gpt_set_compare_event(uint8_t cmp_output, uint8_t cmp_output_mode, uint32_t cmp_value);
  193. #if defined(__cplusplus)
  194. }
  195. #endif
  196. //! @}
  197. #endif //__GPT_H__
  198. ////////////////////////////////////////////////////////////////////////////////
  199. // EOF
  200. ////////////////////////////////////////////////////////////////////////////////