ref_clock.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // Copyright 2017 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. /* Unit tests need to have access to reliable timestamps even if CPU and APB
  15. * clock frequencies change over time. This reference clock is built upon two
  16. * peripherals: one RMT channel and one PCNT channel, plus one GPIO to connect
  17. * these peripherals.
  18. *
  19. * RMT channel is configured to use REF_TICK as clock source, which is a 1 MHz
  20. * clock derived from APB_CLK using a set of dividers. The divider is changed
  21. * automatically by hardware depending on the current clock source of APB_CLK.
  22. * For example, if APB_CLK is derived from PLL, one divider is used, and when
  23. * APB_CLK is derived from XTAL, another divider is used. RMT channel clocked
  24. * by REF_TICK is configured to generate a continuous 0.5 MHz signal, which is
  25. * connected to a GPIO. PCNT takes the input signal from this GPIO and counts
  26. * the edges (which occur at 1MHz frequency). PCNT counter is only 16 bit wide,
  27. * so an interrupt is configured to trigger when the counter reaches 30000,
  28. * incrementing a 32-bit millisecond counter maintained by software.
  29. * Together these two counters may be used at any time to obtain the timestamp.
  30. */
  31. #include "test_utils.h"
  32. #include "soc/soc.h"
  33. #include "hal/rmt_hal.h"
  34. #include "hal/rmt_ll.h"
  35. #include "soc/pcnt_caps.h"
  36. #include "hal/pcnt_hal.h"
  37. #include "soc/gpio_periph.h"
  38. #include "soc/dport_reg.h"
  39. #include "esp_intr_alloc.h"
  40. #include "freertos/FreeRTOS.h"
  41. #include "driver/periph_ctrl.h"
  42. #if CONFIG_IDF_TARGET_ESP32
  43. #include "esp32/rom/gpio.h"
  44. #elif CONFIG_IDF_TARGET_ESP32S2
  45. #include "esp32s2/rom/gpio.h"
  46. #endif
  47. #include "sdkconfig.h"
  48. /* Select which RMT and PCNT channels, and GPIO to use */
  49. #define REF_CLOCK_RMT_CHANNEL SOC_RMT_CHANNELS_NUM - 1
  50. #define REF_CLOCK_PCNT_UNIT 0
  51. #define REF_CLOCK_GPIO 21
  52. #define REF_CLOCK_PRESCALER_MS 30
  53. static void IRAM_ATTR pcnt_isr(void* arg);
  54. static intr_handle_t s_intr_handle;
  55. static portMUX_TYPE s_lock = portMUX_INITIALIZER_UNLOCKED;
  56. static volatile uint32_t s_milliseconds;
  57. static int get_pcnt_sig(void)
  58. {
  59. #if CONFIG_IDF_TARGET_ESP32
  60. return (REF_CLOCK_PCNT_UNIT < 5) ?
  61. PCNT_SIG_CH0_IN0_IDX + 4 * REF_CLOCK_PCNT_UNIT :
  62. PCNT_SIG_CH0_IN5_IDX + 4 * (REF_CLOCK_PCNT_UNIT - 5);
  63. #elif CONFIG_IDF_TARGET_ESP32S2
  64. return PCNT_SIG_CH0_IN0_IDX + 4 * REF_CLOCK_PCNT_UNIT;
  65. #endif
  66. }
  67. static rmt_hal_context_t s_rmt;
  68. static pcnt_hal_context_t s_pcnt;
  69. void ref_clock_init()
  70. {
  71. assert(s_intr_handle == NULL && "already initialized");
  72. // Route RMT output to GPIO matrix
  73. gpio_matrix_out(REF_CLOCK_GPIO, RMT_SIG_OUT0_IDX + REF_CLOCK_RMT_CHANNEL, false, false);
  74. // Initialize RMT
  75. periph_module_enable(PERIPH_RMT_MODULE);
  76. rmt_hal_init(&s_rmt);
  77. rmt_ll_enable_mem_access(s_rmt.regs, true);
  78. rmt_item32_t data = {
  79. .duration0 = 1,
  80. .level0 = 1,
  81. .duration1 = 0,
  82. .level1 = 0
  83. };
  84. rmt_hal_transmit(&s_rmt, REF_CLOCK_RMT_CHANNEL, &data, 1, 0);
  85. rmt_ll_start_tx(s_rmt.regs, REF_CLOCK_RMT_CHANNEL);
  86. rmt_ll_set_mem_owner(s_rmt.regs, REF_CLOCK_RMT_CHANNEL, 0);
  87. rmt_ll_reset_tx_pointer(s_rmt.regs, REF_CLOCK_RMT_CHANNEL);
  88. rmt_ll_enable_carrier(s_rmt.regs, REF_CLOCK_RMT_CHANNEL, false);
  89. rmt_ll_set_counter_clock_div(s_rmt.regs, REF_CLOCK_RMT_CHANNEL, 1);
  90. rmt_ll_set_mem_blocks(s_rmt.regs, REF_CLOCK_RMT_CHANNEL, 1);
  91. rmt_ll_set_counter_clock_src(s_rmt.regs, REF_CLOCK_RMT_CHANNEL, 0);
  92. rmt_ll_enable_tx_loop(s_rmt.regs, REF_CLOCK_RMT_CHANNEL, true);
  93. rmt_ll_start_tx(s_rmt.regs, REF_CLOCK_RMT_CHANNEL);
  94. // Route signal to PCNT
  95. int pcnt_sig_idx = get_pcnt_sig();
  96. gpio_matrix_in(REF_CLOCK_GPIO, pcnt_sig_idx, false);
  97. if (REF_CLOCK_GPIO != 20) {
  98. PIN_INPUT_ENABLE(GPIO_PIN_MUX_REG[REF_CLOCK_GPIO]);
  99. } else {
  100. PIN_INPUT_ENABLE(PERIPHS_IO_MUX_GPIO20_U);
  101. }
  102. // Initialize PCNT
  103. periph_module_enable(PERIPH_PCNT_MODULE);
  104. pcnt_hal_init(&s_pcnt, REF_CLOCK_PCNT_UNIT);
  105. pcnt_ll_set_mode(s_pcnt.dev, REF_CLOCK_PCNT_UNIT, PCNT_CHANNEL_0,
  106. PCNT_COUNT_INC, PCNT_COUNT_INC,
  107. PCNT_MODE_KEEP, PCNT_MODE_KEEP);
  108. pcnt_ll_event_disable(s_pcnt.dev, REF_CLOCK_PCNT_UNIT, PCNT_EVT_L_LIM);
  109. pcnt_ll_event_enable(s_pcnt.dev, REF_CLOCK_PCNT_UNIT, PCNT_EVT_H_LIM);
  110. pcnt_ll_event_disable(s_pcnt.dev, REF_CLOCK_PCNT_UNIT, PCNT_EVT_ZERO);
  111. pcnt_ll_event_disable(s_pcnt.dev, REF_CLOCK_PCNT_UNIT, PCNT_EVT_THRES_0);
  112. pcnt_ll_event_disable(s_pcnt.dev, REF_CLOCK_PCNT_UNIT, PCNT_EVT_THRES_1);
  113. pcnt_ll_set_event_value(s_pcnt.dev, REF_CLOCK_PCNT_UNIT, PCNT_EVT_H_LIM, REF_CLOCK_PRESCALER_MS * 1000);
  114. // Enable PCNT and wait for it to start counting
  115. pcnt_ll_counter_resume(s_pcnt.dev, REF_CLOCK_PCNT_UNIT);
  116. pcnt_ll_counter_clear(s_pcnt.dev, REF_CLOCK_PCNT_UNIT);
  117. ets_delay_us(10000);
  118. // Enable interrupt
  119. s_milliseconds = 0;
  120. ESP_ERROR_CHECK(esp_intr_alloc(ETS_PCNT_INTR_SOURCE, ESP_INTR_FLAG_IRAM, pcnt_isr, NULL, &s_intr_handle));
  121. pcnt_ll_clear_intr_status(s_pcnt.dev, BIT(REF_CLOCK_PCNT_UNIT));
  122. pcnt_ll_intr_enable(s_pcnt.dev, REF_CLOCK_PCNT_UNIT);
  123. }
  124. static void IRAM_ATTR pcnt_isr(void* arg)
  125. {
  126. portENTER_CRITICAL_ISR(&s_lock);
  127. pcnt_ll_clear_intr_status(s_pcnt.dev, BIT(REF_CLOCK_PCNT_UNIT));
  128. s_milliseconds += REF_CLOCK_PRESCALER_MS;
  129. portEXIT_CRITICAL_ISR(&s_lock);
  130. }
  131. void ref_clock_deinit()
  132. {
  133. assert(s_intr_handle && "deinit called without init");
  134. // Disable interrupt
  135. pcnt_ll_intr_disable(s_pcnt.dev, REF_CLOCK_PCNT_UNIT);
  136. esp_intr_free(s_intr_handle);
  137. s_intr_handle = NULL;
  138. // Disable RMT
  139. rmt_ll_stop_tx(s_rmt.regs, REF_CLOCK_RMT_CHANNEL);
  140. periph_module_disable(PERIPH_RMT_MODULE);
  141. // Disable PCNT
  142. pcnt_ll_counter_pause(s_pcnt.dev, REF_CLOCK_PCNT_UNIT);
  143. periph_module_disable(PERIPH_PCNT_MODULE);
  144. }
  145. uint64_t ref_clock_get()
  146. {
  147. portENTER_CRITICAL(&s_lock);
  148. int16_t microseconds = 0;
  149. pcnt_ll_get_counter_value(s_pcnt.dev, REF_CLOCK_PCNT_UNIT, &microseconds);
  150. uint32_t milliseconds = s_milliseconds;
  151. uint32_t intr_status = 0;
  152. pcnt_ll_get_intr_status(s_pcnt.dev, &intr_status);
  153. if (intr_status & BIT(REF_CLOCK_PCNT_UNIT)) {
  154. // refresh counter value, in case the overflow has happened after reading cnt_val
  155. pcnt_ll_get_counter_value(s_pcnt.dev, REF_CLOCK_PCNT_UNIT, &microseconds);
  156. milliseconds += REF_CLOCK_PRESCALER_MS;
  157. }
  158. portEXIT_CRITICAL(&s_lock);
  159. return 1000 * (uint64_t) milliseconds + (uint64_t) microseconds;
  160. }