systimer_hal.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // Copyright 2021 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <sys/param.h>
  15. #include "soc/soc_caps.h"
  16. #include "hal/systimer_hal.h"
  17. #include "hal/systimer_ll.h"
  18. #include "hal/systimer_types.h"
  19. #include "hal/clk_gate_ll.h"
  20. #include "hal/assert.h"
  21. void systimer_hal_init(systimer_hal_context_t *hal)
  22. {
  23. hal->dev = &SYSTIMER;
  24. periph_ll_enable_clk_clear_rst(PERIPH_SYSTIMER_MODULE);
  25. systimer_ll_enable_clock(hal->dev, true);
  26. }
  27. uint64_t systimer_hal_get_counter_value(systimer_hal_context_t *hal, uint32_t counter_id)
  28. {
  29. uint32_t lo, lo_start, hi;
  30. /* Set the "update" bit and wait for acknowledgment */
  31. systimer_ll_counter_snapshot(hal->dev, counter_id);
  32. while (!systimer_ll_is_counter_value_valid(hal->dev, counter_id));
  33. /* Read LO, HI, then LO again, check that LO returns the same value.
  34. * This accounts for the case when an interrupt may happen between reading
  35. * HI and LO values, and this function may get called from the ISR.
  36. * In this case, the repeated read will return consistent values.
  37. */
  38. lo_start = systimer_ll_get_counter_value_low(hal->dev, counter_id);
  39. do {
  40. lo = lo_start;
  41. hi = systimer_ll_get_counter_value_high(hal->dev, counter_id);
  42. lo_start = systimer_ll_get_counter_value_low(hal->dev, counter_id);
  43. } while (lo_start != lo);
  44. systimer_counter_value_t result = {
  45. .lo = lo,
  46. .hi = hi
  47. };
  48. return result.val;
  49. }
  50. uint64_t systimer_hal_get_time(systimer_hal_context_t *hal, uint32_t counter_id)
  51. {
  52. return systimer_hal_get_counter_value(hal, counter_id) / SYSTIMER_LL_TICKS_PER_US;
  53. }
  54. #if SOC_SYSTIMER_ALARM_MISS_COMPENSATE
  55. void systimer_hal_set_alarm_target(systimer_hal_context_t *hal, uint32_t alarm_id, uint64_t target)
  56. {
  57. systimer_counter_value_t alarm = { .val = target * SYSTIMER_LL_TICKS_PER_US};
  58. systimer_ll_enable_alarm(hal->dev, alarm_id, false);
  59. systimer_ll_set_alarm_target(hal->dev, alarm_id, alarm.val);
  60. systimer_ll_apply_alarm_value(hal->dev, alarm_id);
  61. systimer_ll_enable_alarm(hal->dev, alarm_id, true);
  62. }
  63. #else
  64. void systimer_hal_set_alarm_target(systimer_hal_context_t *hal, uint32_t alarm_id, uint64_t timestamp)
  65. {
  66. int64_t offset = SYSTIMER_LL_TICKS_PER_US * 2;
  67. uint64_t now_time = systimer_hal_get_counter_value(hal, 0);
  68. systimer_counter_value_t alarm = { .val = MAX(timestamp * SYSTIMER_LL_TICKS_PER_US, now_time + offset) };
  69. do {
  70. systimer_ll_enable_alarm(hal->dev, alarm_id, false);
  71. systimer_ll_set_alarm_target(hal->dev, alarm_id, alarm.val);
  72. systimer_ll_enable_alarm(hal->dev, alarm_id, true);
  73. now_time = systimer_hal_get_counter_value(hal, 0);
  74. int64_t delta = (int64_t)alarm.val - (int64_t)now_time;
  75. if (delta <= 0 && !systimer_ll_is_alarm_int_fired(hal->dev, alarm_id)) {
  76. // new alarm is less than the counter and the interrupt flag is not set
  77. offset += -1 * delta + SYSTIMER_LL_TICKS_PER_US * 2;
  78. alarm.val = now_time + offset;
  79. } else {
  80. // finish if either (alarm > counter) or the interrupt flag is already set.
  81. break;
  82. }
  83. } while (1);
  84. }
  85. #endif
  86. void systimer_hal_set_alarm_period(systimer_hal_context_t *hal, uint32_t alarm_id, uint32_t period)
  87. {
  88. systimer_ll_enable_alarm(hal->dev, alarm_id, false);
  89. systimer_ll_set_alarm_period(hal->dev, alarm_id, period * SYSTIMER_LL_TICKS_PER_US);
  90. systimer_ll_apply_alarm_value(hal->dev, alarm_id);
  91. systimer_ll_enable_alarm(hal->dev, alarm_id, true);
  92. }
  93. uint64_t systimer_hal_get_alarm_value(systimer_hal_context_t *hal, uint32_t alarm_id)
  94. {
  95. return systimer_ll_get_alarm_target(hal->dev, alarm_id);
  96. }
  97. void systimer_hal_enable_alarm_int(systimer_hal_context_t *hal, uint32_t alarm_id)
  98. {
  99. systimer_ll_enable_alarm_int(hal->dev, alarm_id, true);
  100. }
  101. void systimer_hal_counter_value_advance(systimer_hal_context_t *hal, uint32_t counter_id, int64_t time_us)
  102. {
  103. systimer_counter_value_t new_count = { .val = systimer_hal_get_counter_value(hal, counter_id) + time_us * SYSTIMER_LL_TICKS_PER_US };
  104. systimer_ll_set_counter_value(hal->dev, counter_id, new_count.val);
  105. systimer_ll_apply_counter_value(hal->dev, counter_id);
  106. }
  107. void systimer_hal_enable_counter(systimer_hal_context_t *hal, uint32_t counter_id)
  108. {
  109. systimer_ll_enable_counter(hal->dev, counter_id, true);
  110. }
  111. void systimer_hal_select_alarm_mode(systimer_hal_context_t *hal, uint32_t alarm_id, systimer_alarm_mode_t mode)
  112. {
  113. switch (mode) {
  114. case SYSTIMER_ALARM_MODE_ONESHOT:
  115. systimer_ll_enable_alarm_oneshot(hal->dev, alarm_id);
  116. break;
  117. case SYSTIMER_ALARM_MODE_PERIOD:
  118. systimer_ll_enable_alarm_period(hal->dev, alarm_id);
  119. break;
  120. default:
  121. break;
  122. }
  123. }
  124. void systimer_hal_connect_alarm_counter(systimer_hal_context_t *hal, uint32_t alarm_id, uint32_t counter_id)
  125. {
  126. systimer_ll_connect_alarm_counter(hal->dev, alarm_id, counter_id);
  127. }
  128. void systimer_hal_counter_can_stall_by_cpu(systimer_hal_context_t *hal, uint32_t counter_id, uint32_t cpu_id, bool can)
  129. {
  130. systimer_ll_counter_can_stall_by_cpu(hal->dev, counter_id, cpu_id, can);
  131. }
  132. #if !SOC_SYSTIMER_FIXED_TICKS_US
  133. void systimer_hal_set_steps_per_tick(systimer_hal_context_t *hal, int clock_source, uint32_t steps)
  134. {
  135. /* Configure the counter:
  136. * - increment by 1 when running from PLL (80 ticks per microsecond),
  137. * - increment by 2 when running from XTAL (40 ticks per microsecond).
  138. * Note that if the APB frequency is derived from XTAL with divider != 1,
  139. * XTAL_STEP needs to be adjusted accordingly. For example, if
  140. * the APB frequency is XTAL/4 = 10 MHz, then XTAL_STEP should be set to 8.
  141. * This is handled in systimer_hal_on_apb_freq_update function.
  142. */
  143. switch (clock_source) {
  144. case 0:
  145. systimer_ll_set_step_for_xtal(hal->dev, steps);
  146. break;
  147. case 1:
  148. systimer_ll_set_step_for_pll(hal->dev, steps);
  149. default:
  150. break;
  151. }
  152. }
  153. void systimer_hal_on_apb_freq_update(systimer_hal_context_t *hal, uint32_t apb_ticks_per_us)
  154. {
  155. /* If this function was called when switching APB clock to PLL, don't need
  156. * do anything: the SYSTIMER_TIMER_PLL_STEP is already correct.
  157. * If this was called when switching APB clock to XTAL, need to adjust
  158. * XTAL_STEP value accordingly.
  159. */
  160. if (apb_ticks_per_us != SYSTIMER_LL_TICKS_PER_US) {
  161. HAL_ASSERT((SYSTIMER_LL_TICKS_PER_US % apb_ticks_per_us) == 0 && "TICK_PER_US should be divisible by APB frequency (in MHz)");
  162. systimer_ll_set_step_for_xtal(hal->dev, SYSTIMER_LL_TICKS_PER_US / apb_ticks_per_us);
  163. }
  164. }
  165. #endif