systimer_hal.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // Copyright 2020 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 <assert.h>
  16. #include "soc/soc_caps.h"
  17. #include "hal/systimer_hal.h"
  18. #include "hal/systimer_ll.h"
  19. #include "hal/systimer_types.h"
  20. #include "soc/rtc.h"
  21. #define SYSTIMER_TICKS_PER_US (80) // Number of timer ticks per microsecond
  22. uint64_t systimer_hal_get_counter_value(systimer_counter_id_t counter_id)
  23. {
  24. uint32_t lo, lo_start, hi;
  25. /* Set the "update" bit and wait for acknowledgment */
  26. systimer_ll_counter_snapshot();
  27. while (!systimer_ll_is_counter_value_valid());
  28. /* Read LO, HI, then LO again, check that LO returns the same value.
  29. * This accounts for the case when an interrupt may happen between reading
  30. * HI and LO values, and this function may get called from the ISR.
  31. * In this case, the repeated read will return consistent values.
  32. */
  33. lo_start = systimer_ll_get_counter_value_low();
  34. do {
  35. lo = lo_start;
  36. hi = systimer_ll_get_counter_value_high();
  37. lo_start = systimer_ll_get_counter_value_low();
  38. } while (lo_start != lo);
  39. systimer_counter_value_t result = {
  40. .lo = lo,
  41. .hi = hi
  42. };
  43. return result.val;
  44. }
  45. uint64_t systimer_hal_get_time(systimer_counter_id_t counter_id)
  46. {
  47. return systimer_hal_get_counter_value(counter_id) / SYSTIMER_TICKS_PER_US;
  48. }
  49. void systimer_hal_set_alarm_target(systimer_alarm_id_t alarm_id, uint64_t timestamp)
  50. {
  51. int64_t offset = SYSTIMER_TICKS_PER_US * 2;
  52. uint64_t now_time = systimer_hal_get_counter_value(SYSTIMER_COUNTER_0);
  53. systimer_counter_value_t alarm = { .val = MAX(timestamp * SYSTIMER_TICKS_PER_US, now_time + offset) };
  54. do {
  55. systimer_ll_disable_alarm(alarm_id);
  56. systimer_ll_set_alarm_value(alarm_id, alarm.val);
  57. systimer_ll_enable_alarm(alarm_id);
  58. now_time = systimer_hal_get_counter_value(SYSTIMER_COUNTER_0);
  59. int64_t delta = (int64_t)alarm.val - (int64_t)now_time;
  60. if (delta <= 0 && !systimer_ll_is_alarm_int_fired(alarm_id)) {
  61. // new alarm is less than the counter and the interrupt flag is not set
  62. offset += -1 * delta + SYSTIMER_TICKS_PER_US * 2;
  63. alarm.val = now_time + offset;
  64. } else {
  65. // finish if either (alarm > counter) or the interrupt flag is already set.
  66. break;
  67. }
  68. } while (1);
  69. }
  70. uint64_t systimer_hal_get_alarm_value(systimer_alarm_id_t alarm_id)
  71. {
  72. return systimer_ll_get_alarm_value(alarm_id);
  73. }
  74. void systimer_hal_enable_alarm_int(systimer_alarm_id_t alarm_id)
  75. {
  76. systimer_ll_enable_alarm_int(alarm_id);
  77. }
  78. void systimer_hal_on_apb_freq_update(uint32_t apb_ticks_per_us)
  79. {
  80. /* If this function was called when switching APB clock to PLL, don't need
  81. * do anything: the SYSTIMER_TIMER_PLL_STEP is already correct.
  82. * If this was called when switching APB clock to XTAL, need to adjust
  83. * XTAL_STEP value accordingly.
  84. */
  85. if (apb_ticks_per_us != SYSTIMER_TICKS_PER_US) {
  86. assert((SYSTIMER_TICKS_PER_US % apb_ticks_per_us) == 0 && "TICK_PER_US should be divisible by APB frequency (in MHz)");
  87. systimer_ll_set_step_for_xtal(SYSTIMER_TICKS_PER_US / apb_ticks_per_us);
  88. }
  89. }
  90. void systimer_hal_counter_value_advance(systimer_counter_id_t counter_id, int64_t time_us)
  91. {
  92. systimer_counter_value_t new_count = { .val = systimer_hal_get_counter_value(counter_id) + time_us * SYSTIMER_TICKS_PER_US };
  93. systimer_ll_load_counter_value(new_count.val);
  94. systimer_ll_apply_counter_value();
  95. }
  96. void systimer_hal_enable_counter(systimer_counter_id_t counter_id)
  97. {
  98. (void)counter_id;
  99. }
  100. void systimer_hal_init(void)
  101. {
  102. assert(rtc_clk_xtal_freq_get() == 40 && "update the step for xtal to support other XTAL:APB frequency ratios");
  103. systimer_ll_enable_clock();
  104. /* Configure the counter:
  105. * - increment by 1 when running from PLL (80 ticks per microsecond),
  106. * - increment by 2 when running from XTAL (40 ticks per microsecond).
  107. * Note that if the APB frequency is derived from XTAL with divider != 1,
  108. * XTAL_STEP needs to be adjusted accordingly. For example, if
  109. * the APB frequency is XTAL/4 = 10 MHz, then XTAL_STEP should be set to 8.
  110. * This is handled in systimer_hal_on_apb_freq_update function.
  111. */
  112. systimer_ll_set_step_for_pll(1);
  113. systimer_ll_set_step_for_xtal(2);
  114. }
  115. void systimer_hal_select_alarm_mode(systimer_alarm_id_t alarm_id, systimer_alarm_mode_t mode)
  116. {
  117. switch (mode) {
  118. case SYSTIMER_ALARM_MODE_ONESHOT:
  119. systimer_ll_enable_alarm_oneshot(alarm_id);
  120. break;
  121. case SYSTIMER_ALARM_MODE_PERIOD:
  122. systimer_ll_enable_alarm_period(alarm_id);
  123. break;
  124. default:
  125. break;
  126. }
  127. }
  128. void systimer_hal_connect_alarm_counter(systimer_alarm_id_t alarm_id, systimer_counter_id_t counter_id)
  129. {
  130. // esp32s2 only has one counter, so there's no need to connect alarm unit to counter
  131. (void)alarm_id;
  132. (void)counter_id;
  133. }