1
0

epit.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 epit.c
  32. * @brief EPIT driver source file.
  33. *
  34. * @ingroup diag_timer
  35. */
  36. #include "epit.h"
  37. #include "imx_timer.h"
  38. #include "interrupt.h"
  39. #include "ccm_pll.h"
  40. #include "registers/regsepit.h"
  41. ////////////////////////////////////////////////////////////////////////////////
  42. // Code
  43. ////////////////////////////////////////////////////////////////////////////////
  44. void epit_reload_counter(uint32_t instance, uint32_t load_val)
  45. {
  46. // set the load register especially if RLD=reload_mode=SET_AND_FORGET=1
  47. HW_EPIT_LR_WR(instance, load_val);
  48. }
  49. uint32_t epit_get_counter_value(uint32_t instance)
  50. {
  51. return HW_EPIT_CNR_RD(instance);
  52. }
  53. void epit_set_compare_event(uint32_t instance, uint32_t compare_val)
  54. {
  55. HW_EPIT_CMPR_WR(instance, compare_val);
  56. }
  57. uint32_t epit_get_compare_event(uint32_t instance)
  58. {
  59. uint32_t status_register;
  60. // get the status
  61. status_register = HW_EPIT_SR_RD(instance);
  62. // clear it if found set
  63. if (status_register & BM_EPIT_SR_OCIF)
  64. {
  65. HW_EPIT_SR_SET(instance, BM_EPIT_SR_OCIF);
  66. }
  67. // return the read value before the bit was cleared
  68. return status_register & BM_EPIT_SR_OCIF;
  69. }
  70. void epit_counter_disable(uint32_t instance)
  71. {
  72. /* temporary workaround for the discovered issue when disabling the
  73. * counter during end of count/reload/set compare flag ??.
  74. * Set to the max value so that it ensures that the counter couldn't
  75. * reach 0 when it is disabled.
  76. */
  77. HW_EPIT_LR_WR(instance, 0xFFFFFFFF);
  78. // disable the counter
  79. HW_EPIT_CR_CLR(instance, BM_EPIT_CR_EN);
  80. // ensure to leave the counter in a proper state
  81. // by disabling the output compare interrupt
  82. HW_EPIT_CR_CLR(instance, BM_EPIT_CR_OCIEN);
  83. // and clearing possible remaining compare event
  84. HW_EPIT_SR_SET(instance, BM_EPIT_SR_OCIF);
  85. }
  86. void epit_counter_enable(uint32_t instance, uint32_t load_val, uint32_t irq_mode)
  87. {
  88. // set the load register especially if RLD=reload_mode=SET_AND_FORGET=1
  89. // and if the value is different from 0 which is the lowest counter value
  90. if (load_val != 0)
  91. {
  92. HW_EPIT_LR_WR(instance, load_val);
  93. }
  94. // ensure to start the counter in a proper state
  95. // by clearing possible remaining compare event
  96. HW_EPIT_SR_SET(instance, BM_EPIT_SR_OCIF);
  97. // set the mode when the output compare event occur: IRQ or polling
  98. if (irq_mode == IRQ_MODE)
  99. {
  100. HW_EPIT_CR_SET(instance, BM_EPIT_CR_OCIEN);
  101. }
  102. else
  103. {
  104. // polling
  105. HW_EPIT_CR_CLR(instance, BM_EPIT_CR_OCIEN);
  106. }
  107. // finally, enable the counter
  108. HW_EPIT_CR_SET(instance, BM_EPIT_CR_EN);
  109. }
  110. void epit_setup_interrupt(uint32_t instance, void (*irq_subroutine)(void), bool enableIt)
  111. {
  112. uint32_t irq_id = EPIT_IRQS(instance);
  113. if (enableIt)
  114. {
  115. // register the IRQ sub-routine
  116. register_interrupt_routine(irq_id, irq_subroutine);
  117. // enable the IRQ
  118. enable_interrupt(irq_id, CPU_0, 0);
  119. }
  120. else
  121. {
  122. // disable the IRQ
  123. disable_interrupt(irq_id, CPU_0);
  124. }
  125. }
  126. void epit_init(uint32_t instance, uint32_t clock_src, uint32_t prescaler,
  127. uint32_t reload_mode, uint32_t load_val, uint32_t low_power_mode)
  128. {
  129. uint32_t control_reg_tmp = 0;
  130. uint32_t base = REGS_EPIT_BASE(instance);
  131. // enable the source clocks to the EPIT port
  132. clock_gating_config(base, CLOCK_ON);
  133. // start with a known state by disabling and reseting the module
  134. HW_EPIT_CR_WR(instance, BM_EPIT_CR_SWR);
  135. // wait for the reset to complete
  136. while ((HW_EPIT_CR(instance).B.SWR) != 0) ;
  137. // set the reference source clock for the counter
  138. control_reg_tmp |= BF_EPIT_CR_CLKSRC(clock_src);
  139. // set the counter clock prescaler value - 0 to 4095
  140. control_reg_tmp |= BF_EPIT_CR_PRESCALAR(prescaler-1);
  141. // set the reload mode
  142. if (reload_mode == SET_AND_FORGET)
  143. {
  144. control_reg_tmp |= BM_EPIT_CR_RLD;
  145. }
  146. // set behavior for low power mode
  147. if (low_power_mode & WAIT_MODE_EN)
  148. {
  149. control_reg_tmp |= BM_EPIT_CR_WAITEN;
  150. }
  151. if (low_power_mode & STOP_MODE_EN)
  152. {
  153. control_reg_tmp |= BM_EPIT_CR_STOPEN;
  154. }
  155. // make the counter start from a known value when enabled, this is loaded from
  156. // EPITLR register if RLD=reload_mode=1 or 0xFFFFFFFF if RLD=reload_mode=0
  157. control_reg_tmp |= BM_EPIT_CR_IOVW | BM_EPIT_CR_ENMOD;
  158. // finally write the control register
  159. HW_EPIT_CR_WR(instance, control_reg_tmp);
  160. // initialize the load register especially if RLD=reload_mode=SET_AND_FORGET=1
  161. // and if the value is different from 0 which is the lowest counter value
  162. if (load_val != 0)
  163. {
  164. HW_EPIT_LR_WR(instance, load_val);
  165. }
  166. }
  167. ////////////////////////////////////////////////////////////////////////////////
  168. // EOF
  169. ////////////////////////////////////////////////////////////////////////////////