gtimer.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-03-30 huijie.feng first version
  9. */
  10. #include "cp15.h"
  11. #include <rtdef.h>
  12. /** Set CNTFRQ
  13. * This function assigns the given value to PL1 Physical Timer Counter Frequency Register (CNTFRQ).
  14. * @param value: CNTFRQ Register value to set
  15. */
  16. static inline void __set_cntfrq(rt_uint32_t value)
  17. {
  18. __set_cp(15, 0, value, 14, 0, 0);
  19. }
  20. /** Get CNTFRQ
  21. * This function returns the value of the PL1 Physical Timer Counter Frequency Register (CNTFRQ).
  22. * return CNTFRQ Register value
  23. */
  24. static inline rt_uint32_t __get_cntfrq(void)
  25. {
  26. rt_uint32_t result;
  27. __get_cp(15, 0, result, 14, 0 , 0);
  28. return result;
  29. }
  30. /** Set CNTP_TVAL
  31. * This function assigns the given value to PL1 Physical Timer Value Register (CNTP_TVAL).
  32. * param value: CNTP_TVAL Register value to set
  33. */
  34. static inline void __set_cntp_tval(rt_uint32_t value)
  35. {
  36. __set_cp(15, 0, value, 14, 2, 0);
  37. }
  38. /** Get CNTP_TVAL
  39. * This function returns the value of the PL1 Physical Timer Value Register (CNTP_TVAL).
  40. * return CNTP_TVAL Register value
  41. */
  42. static inline rt_uint32_t __get_cntp_tval(void)
  43. {
  44. rt_uint32_t result;
  45. __get_cp(15, 0, result, 14, 2, 0);
  46. return result;
  47. }
  48. /** Get CNTPCT
  49. * This function returns the value of the 64 bits PL1 Physical Count Register (CNTPCT).
  50. * return CNTPCT Register value
  51. */
  52. static inline rt_uint64_t __get_cntpct(void)
  53. {
  54. rt_uint64_t result;
  55. __get_cp64(15, 0, result, 14);
  56. return result;
  57. }
  58. /** Set CNTP_CVAL
  59. * This function assigns the given value to 64bits PL1 Physical Timer CompareValue Register (CNTP_CVAL).
  60. * param value: CNTP_CVAL Register value to set
  61. */
  62. static inline void __set_cntp_cval(rt_uint64_t value)
  63. {
  64. __set_cp64(15, 2, value, 14);
  65. }
  66. /** Get CNTP_CVAL
  67. * This function returns the value of the 64 bits PL1 Physical Timer CompareValue Register (CNTP_CVAL).
  68. * return CNTP_CVAL Register value
  69. */
  70. static inline rt_uint64_t __get_cntp_cval(void)
  71. {
  72. rt_uint64_t result;
  73. __get_cp64(15, 2, result, 14);
  74. return result;
  75. }
  76. /** Set CNTP_CTL
  77. * This function assigns the given value to PL1 Physical Timer Control Register (CNTP_CTL).
  78. * param value: CNTP_CTL Register value to set
  79. */
  80. static inline void __set_cntp_ctl(rt_uint32_t value)
  81. {
  82. __set_cp(15, 0, value, 14, 2, 1);
  83. }
  84. /** Get CNTP_CTL register
  85. * return CNTP_CTL Register value
  86. */
  87. static inline rt_uint32_t __get_cntp_ctl(void)
  88. {
  89. rt_uint32_t result;
  90. __get_cp(15, 0, result, 14, 2, 1);
  91. return result;
  92. }
  93. /** Configures the frequency the timer shall run at.
  94. * param value The timer frequency in Hz.
  95. */
  96. void gtimer_set_counter_frequency(rt_uint32_t value)
  97. {
  98. __set_cntfrq(value);
  99. __asm__ volatile ("isb 0xF":::"memory");
  100. }
  101. /** Get the frequency the timer shall run at.
  102. * return timer frequency in Hz.
  103. */
  104. rt_uint32_t gtimer_get_counter_frequency(void)
  105. {
  106. return(__get_cntfrq());
  107. }
  108. /** Sets the reset value of the timer.
  109. * param value: The value the timer is loaded with.
  110. */
  111. void gtimer_set_load_value(rt_uint32_t value)
  112. {
  113. __set_cntp_tval(value);
  114. __asm__ volatile ("isb 0xF":::"memory");
  115. }
  116. /** Get the current counter value.
  117. * return Current counter value.
  118. */
  119. rt_uint32_t gtimer_get_current_value(void)
  120. {
  121. return(__get_cntp_tval());
  122. }
  123. /** Get the current physical counter value.
  124. * return Current physical counter value.
  125. */
  126. rt_uint64_t gtimer_get_current_physical_value(void)
  127. {
  128. return(__get_cntpct());
  129. }
  130. /** Set the physical compare value.
  131. * param value: New physical timer compare value.
  132. */
  133. void gtimer_set_physical_compare_value(rt_uint64_t value)
  134. {
  135. __set_cntp_cval(value);
  136. __asm__ volatile ("isb 0xF":::"memory");
  137. }
  138. /** Get the physical compare value.
  139. * return Physical compare value.
  140. */
  141. rt_uint64_t gtimer_get_physical_compare_value(void)
  142. {
  143. return(__get_cntp_cval());
  144. }
  145. /** Configure the timer by setting the control value.
  146. * param value: New timer control value.
  147. */
  148. void gtimer_set_control(rt_uint32_t value)
  149. {
  150. __set_cntp_ctl(value);
  151. __asm__ volatile ("isb 0xF":::"memory");
  152. }
  153. /** Get the control value.
  154. * return Control value.
  155. */
  156. rt_uint32_t gtimer_get_control(void)
  157. {
  158. return(__get_cntp_ctl());
  159. }