gpt.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. /*!
  31. * @file gpt.c
  32. * @brief GPT driver source file.
  33. *
  34. * @ingroup diag_timer
  35. */
  36. #include "sdk.h"
  37. #include "gpt.h"
  38. #include "imx_timer.h"
  39. #include "registers/regsgpt.h"
  40. #include "interrupt.h"
  41. #include "ccm_pll.h"
  42. ////////////////////////////////////////////////////////////////////////////////
  43. // Code
  44. ////////////////////////////////////////////////////////////////////////////////
  45. static inline void gpt_clear_all_events(void)
  46. {
  47. HW_GPT_SR_WR(kGPTAllEvents);
  48. }
  49. uint32_t gpt_get_rollover_event(void)
  50. {
  51. // clear it if found set
  52. if (HW_GPT_SR.B.ROV)
  53. {
  54. HW_GPT_SR_WR(BM_GPT_SR_ROV);
  55. return kGPTRollover;
  56. }
  57. // return the read value before the bit was cleared
  58. return 0;
  59. }
  60. uint32_t gpt_get_capture_event(uint8_t flag, uint32_t * capture_val)
  61. {
  62. // get the capture status bit
  63. flag &= kGPTInputCapture1 | kGPTInputCapture2;
  64. uint32_t status_register = HW_GPT_SR_RD() & flag;
  65. // Read the captured timer value.
  66. if (capture_val)
  67. {
  68. if (status_register == kGPTInputCapture1)
  69. {
  70. *(uint32_t *) capture_val = HW_GPT_ICR1.B.CAPT;
  71. }
  72. else if (status_register == kGPTInputCapture2)
  73. {
  74. *(uint32_t *) capture_val = HW_GPT_ICR2.B.CAPT;
  75. }
  76. }
  77. // Clear the flag.
  78. HW_GPT_SR_WR(status_register);
  79. // return the read value before the bit was cleared
  80. return status_register;
  81. }
  82. void gpt_set_capture_event(uint8_t cap_input, uint8_t cap_input_mode)
  83. {
  84. // set the new input mode
  85. switch (cap_input)
  86. {
  87. case kGPTInputCapture1:
  88. HW_GPT_CR.B.IM1 = cap_input_mode;
  89. break;
  90. case kGPTInputCapture2:
  91. HW_GPT_CR.B.IM2 = cap_input_mode;
  92. break;
  93. }
  94. }
  95. uint32_t gpt_get_compare_event(uint8_t flag)
  96. {
  97. // get the active compare flags
  98. flag &= kGPTOutputCompare1 | kGPTOutputCompare2 | kGPTOutputCompare3;
  99. uint32_t status_register = HW_GPT_SR_RD() & flag;
  100. // clear flags which are active
  101. if (status_register)
  102. {
  103. HW_GPT_SR_WR(status_register);
  104. }
  105. // return the read value before the flags were cleared
  106. return status_register;
  107. }
  108. void gpt_set_compare_event(uint8_t cmp_output, uint8_t cmp_output_mode, uint32_t cmp_value)
  109. {
  110. // set the value to compare with
  111. switch (cmp_output)
  112. {
  113. case kGPTOutputCompare1:
  114. BW_GPT_CR_OM1(cmp_output_mode);
  115. HW_GPT_OCR1_WR(cmp_value);
  116. break;
  117. case kGPTOutputCompare2:
  118. BW_GPT_CR_OM2(cmp_output_mode);
  119. HW_GPT_OCR2_WR(cmp_value);
  120. break;
  121. case kGPTOutputCompare3:
  122. BW_GPT_CR_OM3(cmp_output_mode);
  123. HW_GPT_OCR3_WR(cmp_value);
  124. break;
  125. }
  126. }
  127. void gpt_counter_disable(void)
  128. {
  129. // disable the counter
  130. HW_GPT_CR.B.EN = 0;
  131. // ensure to leave the counter in a proper state by disabling the interrupt sources
  132. HW_GPT_IR_WR(0);
  133. // and by clearing possible remaining events
  134. gpt_clear_all_events();
  135. }
  136. void gpt_counter_enable(uint32_t irq_mode)
  137. {
  138. // ensure to start the counter in a proper state by clearing possible remaining events
  139. gpt_clear_all_events();
  140. // enable the interrupts or clear the register for polling
  141. HW_GPT_IR_WR(irq_mode & kGPTAllEvents);
  142. // finally, enable the counter
  143. HW_GPT_CR.B.EN = 1;
  144. }
  145. void gpt_setup_interrupt(void (*irq_subroutine)(void), bool enableIt)
  146. {
  147. uint32_t irq_id = IMX_INT_GPT;
  148. if (enableIt)
  149. {
  150. // register the IRQ sub-routine
  151. register_interrupt_routine(irq_id, irq_subroutine);
  152. // enable the IRQ
  153. enable_interrupt(irq_id, CPU_0, 0);
  154. }
  155. else
  156. {
  157. // disable the IRQ
  158. disable_interrupt(irq_id, CPU_0);
  159. }
  160. }
  161. void gpt_init(uint32_t clock_src, uint32_t prescaler, uint32_t counter_mode, uint32_t low_power_mode)
  162. {
  163. uint32_t control_reg_tmp = 0;
  164. uint32_t base = GPT_BASE_ADDR;
  165. // enable the source clocks to the GPT port
  166. clock_gating_config(base, CLOCK_ON);
  167. // start with a known state by disabling and reseting the module
  168. HW_GPT_CR_WR(BM_GPT_CR_SWR);
  169. // wait for the reset to complete
  170. while (HW_GPT_CR.B.SWR != 0) ;
  171. // set the reference source clock for the counter
  172. if (clock_src == CLKSRC_CKIL)
  173. {
  174. // CKIL source is 0x4 for GPT but 0x3 for EPIT
  175. clock_src++;
  176. }
  177. control_reg_tmp |= BF_GPT_CR_CLKSRC(clock_src);
  178. // the prescaler can be changed at any time, and
  179. // this affects the output clock immediately
  180. HW_GPT_PR_WR(BF_GPT_PR_PRESCALER(prescaler - 1));
  181. // set the counter mode
  182. control_reg_tmp |= BF_GPT_CR_FRR(counter_mode);
  183. // set behavior for low power mode
  184. if (low_power_mode & WAIT_MODE_EN)
  185. {
  186. control_reg_tmp |= BM_GPT_CR_WAITEN;
  187. }
  188. if (low_power_mode & STOP_MODE_EN)
  189. {
  190. control_reg_tmp |= BM_GPT_CR_STOPEN;
  191. }
  192. // specify from where the counter starts to count when enabled
  193. // this code makes it start from 0
  194. control_reg_tmp |= BM_GPT_CR_ENMOD;
  195. // finally write the control register
  196. HW_GPT_CR_WR(control_reg_tmp);
  197. }
  198. ////////////////////////////////////////////////////////////////////////////////
  199. // EOF
  200. ////////////////////////////////////////////////////////////////////////////////