1
0

imx_timer.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 timer.c
  32. * @brief Basic timer functions
  33. *
  34. * @ingroup diag_timer
  35. */
  36. #include <assert.h>
  37. #include "imx_timer.h"
  38. #include "sdk.h"
  39. #include "epit.h"
  40. #include "registers/regsarmglobaltimer.h"
  41. #include "ccm_pll.h"
  42. ////////////////////////////////////////////////////////////////////////////////
  43. // Prototypes
  44. ////////////////////////////////////////////////////////////////////////////////
  45. static void time_init_global_timer();
  46. ////////////////////////////////////////////////////////////////////////////////
  47. // Variables
  48. ////////////////////////////////////////////////////////////////////////////////
  49. uint32_t g_microsecondTimerMultiple=8;
  50. ////////////////////////////////////////////////////////////////////////////////
  51. // Code
  52. ////////////////////////////////////////////////////////////////////////////////
  53. void hal_delay_us(uint32_t usecs)
  54. {
  55. uint32_t instance = g_system_timer_port;
  56. if (usecs == 0) {
  57. return;
  58. }
  59. /* enable the counter first */
  60. epit_counter_enable(instance, usecs, POLLING_MODE);
  61. /* wait for the compare event */
  62. while (!epit_get_compare_event(instance)) ;
  63. /* disable the counter to save power */
  64. epit_counter_disable(instance);
  65. }
  66. void system_time_init(void)
  67. {
  68. uint32_t freq;
  69. // Init microsecond tick counter.
  70. time_init_global_timer();
  71. /* EPIT1 is used for the delay function */
  72. /* Initialize the EPIT timer used for system time functions */
  73. /* typical IPG_CLK is in MHz, so divide it to get a reference
  74. clock of 1MHz => 1us per count */
  75. freq = get_main_clock(IPG_CLK);
  76. epit_init(g_system_timer_port, CLKSRC_IPG_CLK, freq / 1000000,
  77. SET_AND_FORGET, 1000, WAIT_MODE_EN | STOP_MODE_EN);
  78. }
  79. #if defined(CHIP_MX6UL)
  80. //! Init the ARM global timer to a microsecond-frequency clock.
  81. void time_init_global_timer()
  82. {
  83. // Make sure the timer is off.
  84. HW_ARMGLOBALTIMER_CONTROL.B.TIMER_ENABLE = 0;
  85. HW_ARMGLOBALTIMER_CONTROL.B.FCR0 =1;
  86. HW_ARMGLOBALTIMER_CONTROL.B.FCR1 =0;
  87. HW_ARMGLOBALTIMER_CONTROL.B.DBG_ENABLE =0;
  88. // Clear counter.
  89. HW_ARMGLOBALTIMER_COUNTER_HI_WR(0);
  90. HW_ARMGLOBALTIMER_COUNTER_LO_WR(0);
  91. // Now turn on the timer.
  92. HW_ARMGLOBALTIMER_CONTROL.B.TIMER_ENABLE = 1;
  93. }
  94. uint64_t time_get_microseconds()
  95. {
  96. // First read upper.
  97. uint32_t upper = HW_ARMGLOBALTIMER_COUNTER_HI_RD();
  98. uint32_t lower = HW_ARMGLOBALTIMER_COUNTER_LO_RD();
  99. return (((uint64_t)upper << 32) | (uint64_t)lower)/8;
  100. }
  101. #else
  102. //! Init the ARM global timer to a microsecond-frequency clock.
  103. void time_init_global_timer()
  104. {
  105. // The ARM private peripheral clock is half the CPU clock.
  106. uint32_t periphClock = get_main_clock(CPU_CLK) / 2;
  107. uint32_t prescaler = (periphClock / 1000000) - 1;
  108. // Divide down the prescaler until it fits into 8 bits. We add up the number of ticks
  109. // it takes to equal a microsecond interval.
  110. g_microsecondTimerMultiple = 1;
  111. while (prescaler > 0xff)
  112. {
  113. prescaler /= 2;
  114. ++g_microsecondTimerMultiple;
  115. }
  116. // Make sure the timer is off.
  117. HW_ARMGLOBALTIMER_CONTROL.B.TIMER_ENABLE = 0;
  118. // Clear counter.
  119. HW_ARMGLOBALTIMER_COUNTERn_WR(0, 0);
  120. HW_ARMGLOBALTIMER_COUNTERn_WR(1, 0);
  121. // Set prescaler and clear other flags.
  122. HW_ARMGLOBALTIMER_CONTROL_WR(BF_ARMGLOBALTIMER_CONTROL_PRESCALER(prescaler));
  123. // Now turn on the timer.
  124. HW_ARMGLOBALTIMER_CONTROL.B.TIMER_ENABLE = 1;
  125. }
  126. uint64_t time_get_microseconds()
  127. {
  128. // First read upper.
  129. uint32_t upper = HW_ARMGLOBALTIMER_COUNTERn_RD(1);
  130. uint32_t lower;
  131. while (true)
  132. {
  133. // Read lower.
  134. lower = HW_ARMGLOBALTIMER_COUNTERn_RD(0);
  135. // Read upper again.
  136. uint32_t newUpper = HW_ARMGLOBALTIMER_COUNTERn_RD(1);
  137. // If the first and second read of the upper bits are the same, then return.
  138. if (newUpper == upper)
  139. {
  140. return (((uint64_t)upper << 32) | (uint64_t)lower) / g_microsecondTimerMultiple;
  141. }
  142. // Otherwise, start over again.
  143. upper = newUpper;
  144. }
  145. return 0;
  146. }
  147. #endif
  148. ////////////////////////////////////////////////////////////////////////////////
  149. // EOF
  150. ////////////////////////////////////////////////////////////////////////////////