systimer_hal.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 "hal/systimer_hal.h"
  16. #include "hal/systimer_ll.h"
  17. #include "hal/systimer_types.h"
  18. #include "soc/soc_caps.h"
  19. #include "hal/clk_gate_ll.h"
  20. #define SYSTIMER_TICKS_PER_US (16) // Systimer clock source is fixed to 16MHz
  21. uint64_t systimer_hal_get_counter_value(systimer_counter_id_t counter_id)
  22. {
  23. uint32_t lo, lo_start, hi;
  24. /* Set the "update" bit and wait for acknowledgment */
  25. systimer_ll_counter_snapshot(counter_id);
  26. while (!systimer_ll_is_counter_value_valid(counter_id));
  27. /* Read LO, HI, then LO again, check that LO returns the same value.
  28. * This accounts for the case when an interrupt may happen between reading
  29. * HI and LO values, and this function may get called from the ISR.
  30. * In this case, the repeated read will return consistent values.
  31. */
  32. lo_start = systimer_ll_get_counter_value_low(counter_id);
  33. do {
  34. lo = lo_start;
  35. hi = systimer_ll_get_counter_value_high(counter_id);
  36. lo_start = systimer_ll_get_counter_value_low(counter_id);
  37. } while (lo_start != lo);
  38. systimer_counter_value_t result = {
  39. .lo = lo,
  40. .hi = hi
  41. };
  42. return result.val;
  43. }
  44. uint64_t systimer_hal_get_time(systimer_counter_id_t counter_id)
  45. {
  46. return systimer_hal_get_counter_value(counter_id) / SYSTIMER_TICKS_PER_US;
  47. }
  48. void systimer_hal_set_alarm_target(systimer_alarm_id_t alarm_id, uint64_t target)
  49. {
  50. systimer_counter_value_t alarm = { .val = target * SYSTIMER_TICKS_PER_US};
  51. systimer_ll_disable_alarm(alarm_id);
  52. systimer_ll_set_alarm_target(alarm_id, alarm.val);
  53. systimer_ll_apply_alarm_value(alarm_id);
  54. systimer_ll_enable_alarm(alarm_id);
  55. }
  56. void systimer_hal_set_alarm_period(systimer_alarm_id_t alarm_id, uint32_t period)
  57. {
  58. systimer_ll_disable_alarm(alarm_id);
  59. systimer_ll_set_alarm_period(alarm_id, period * SYSTIMER_TICKS_PER_US);
  60. systimer_ll_apply_alarm_value(alarm_id);
  61. systimer_ll_enable_alarm(alarm_id);
  62. }
  63. uint64_t systimer_hal_get_alarm_value(systimer_alarm_id_t alarm_id)
  64. {
  65. return systimer_ll_get_alarm_target(alarm_id);
  66. }
  67. void systimer_hal_enable_alarm_int(systimer_alarm_id_t alarm_id)
  68. {
  69. systimer_ll_enable_alarm_int(alarm_id);
  70. }
  71. void systimer_hal_on_apb_freq_update(uint32_t apb_ticks_per_us)
  72. {
  73. /* Nothing to do here, SYSTIMER clock is independent of APB clock */
  74. (void)apb_ticks_per_us;
  75. }
  76. void systimer_hal_counter_value_advance(systimer_counter_id_t counter_id, int64_t time_us)
  77. {
  78. systimer_counter_value_t new_count = { .val = systimer_hal_get_counter_value(counter_id) + time_us * SYSTIMER_TICKS_PER_US };
  79. systimer_ll_set_counter_value(counter_id, new_count.val);
  80. systimer_ll_apply_counter_value(counter_id);
  81. }
  82. void systimer_hal_enable_counter(systimer_counter_id_t counter_id)
  83. {
  84. systimer_ll_enable_counter(counter_id);
  85. }
  86. void systimer_hal_init(void)
  87. {
  88. periph_ll_enable_clk_clear_rst(PERIPH_SYSTIMER_MODULE);
  89. systimer_ll_enable_clock();
  90. }
  91. void systimer_hal_select_alarm_mode(systimer_alarm_id_t alarm_id, systimer_alarm_mode_t mode)
  92. {
  93. switch (mode) {
  94. case SYSTIMER_ALARM_MODE_ONESHOT:
  95. systimer_ll_enable_alarm_oneshot(alarm_id);
  96. break;
  97. case SYSTIMER_ALARM_MODE_PERIOD:
  98. systimer_ll_enable_alarm_period(alarm_id);
  99. break;
  100. default:
  101. break;
  102. }
  103. }
  104. void systimer_hal_connect_alarm_counter(systimer_alarm_id_t alarm_id, systimer_counter_id_t counter_id)
  105. {
  106. systimer_ll_connect_alarm_counter(alarm_id, counter_id);
  107. }
  108. void systimer_hal_counter_can_stall_by_cpu(uint32_t counter_id, uint32_t cpu_id, bool can)
  109. {
  110. systimer_ll_counter_can_stall_by_cpu(counter_id, cpu_id, can);
  111. }