ref_clock.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. #include "esp_rom_gpio.h"
  43. #include "esp_rom_sys.h"
  44. #include "sdkconfig.h"
  45. /* Select which RMT and PCNT channels, and GPIO to use */
  46. #define REF_CLOCK_RMT_CHANNEL SOC_RMT_CHANNELS_NUM - 1
  47. #define REF_CLOCK_PCNT_UNIT 0
  48. #define REF_CLOCK_GPIO 21
  49. #define REF_CLOCK_PRESCALER_MS 30
  50. static void IRAM_ATTR pcnt_isr(void* arg);
  51. static intr_handle_t s_intr_handle;
  52. static portMUX_TYPE s_lock = portMUX_INITIALIZER_UNLOCKED;
  53. static volatile uint32_t s_milliseconds;
  54. static int get_pcnt_sig(void)
  55. {
  56. #if CONFIG_IDF_TARGET_ESP32
  57. return (REF_CLOCK_PCNT_UNIT < 5) ?
  58. PCNT_SIG_CH0_IN0_IDX + 4 * REF_CLOCK_PCNT_UNIT :
  59. PCNT_SIG_CH0_IN5_IDX + 4 * (REF_CLOCK_PCNT_UNIT - 5);
  60. #elif CONFIG_IDF_TARGET_ESP32S2
  61. return PCNT_SIG_CH0_IN0_IDX + 4 * REF_CLOCK_PCNT_UNIT;
  62. #endif
  63. }
  64. static rmt_hal_context_t s_rmt;
  65. static pcnt_hal_context_t s_pcnt;
  66. void ref_clock_init()
  67. {
  68. assert(s_intr_handle == NULL && "already initialized");
  69. // Route RMT output to GPIO matrix
  70. esp_rom_gpio_connect_out_signal(REF_CLOCK_GPIO, RMT_SIG_OUT0_IDX + REF_CLOCK_RMT_CHANNEL, false, false);
  71. // Initialize RMT
  72. periph_module_enable(PERIPH_RMT_MODULE);
  73. rmt_hal_init(&s_rmt);
  74. rmt_ll_enable_mem_access(s_rmt.regs, true);
  75. rmt_item32_t data = {
  76. .duration0 = 1,
  77. .level0 = 1,
  78. .duration1 = 0,
  79. .level1 = 0
  80. };
  81. rmt_hal_transmit(&s_rmt, REF_CLOCK_RMT_CHANNEL, &data, 1, 0);
  82. rmt_ll_start_tx(s_rmt.regs, REF_CLOCK_RMT_CHANNEL);
  83. rmt_ll_set_mem_owner(s_rmt.regs, REF_CLOCK_RMT_CHANNEL, 0);
  84. rmt_ll_reset_tx_pointer(s_rmt.regs, REF_CLOCK_RMT_CHANNEL);
  85. rmt_ll_enable_carrier(s_rmt.regs, REF_CLOCK_RMT_CHANNEL, false);
  86. rmt_ll_set_counter_clock_div(s_rmt.regs, REF_CLOCK_RMT_CHANNEL, 1);
  87. rmt_ll_set_mem_blocks(s_rmt.regs, REF_CLOCK_RMT_CHANNEL, 1);
  88. rmt_ll_set_counter_clock_src(s_rmt.regs, REF_CLOCK_RMT_CHANNEL, 0);
  89. rmt_ll_enable_tx_loop(s_rmt.regs, REF_CLOCK_RMT_CHANNEL, true);
  90. rmt_ll_start_tx(s_rmt.regs, REF_CLOCK_RMT_CHANNEL);
  91. // Route signal to PCNT
  92. int pcnt_sig_idx = get_pcnt_sig();
  93. esp_rom_gpio_connect_in_signal(REF_CLOCK_GPIO, pcnt_sig_idx, false);
  94. if (REF_CLOCK_GPIO != 20) {
  95. PIN_INPUT_ENABLE(GPIO_PIN_MUX_REG[REF_CLOCK_GPIO]);
  96. } else {
  97. PIN_INPUT_ENABLE(PERIPHS_IO_MUX_GPIO20_U);
  98. }
  99. // Initialize PCNT
  100. periph_module_enable(PERIPH_PCNT_MODULE);
  101. pcnt_hal_init(&s_pcnt, REF_CLOCK_PCNT_UNIT);
  102. pcnt_ll_set_mode(s_pcnt.dev, REF_CLOCK_PCNT_UNIT, PCNT_CHANNEL_0,
  103. PCNT_COUNT_INC, PCNT_COUNT_INC,
  104. PCNT_MODE_KEEP, PCNT_MODE_KEEP);
  105. pcnt_ll_event_disable(s_pcnt.dev, REF_CLOCK_PCNT_UNIT, PCNT_EVT_L_LIM);
  106. pcnt_ll_event_enable(s_pcnt.dev, REF_CLOCK_PCNT_UNIT, PCNT_EVT_H_LIM);
  107. pcnt_ll_event_disable(s_pcnt.dev, REF_CLOCK_PCNT_UNIT, PCNT_EVT_ZERO);
  108. pcnt_ll_event_disable(s_pcnt.dev, REF_CLOCK_PCNT_UNIT, PCNT_EVT_THRES_0);
  109. pcnt_ll_event_disable(s_pcnt.dev, REF_CLOCK_PCNT_UNIT, PCNT_EVT_THRES_1);
  110. pcnt_ll_set_event_value(s_pcnt.dev, REF_CLOCK_PCNT_UNIT, PCNT_EVT_H_LIM, REF_CLOCK_PRESCALER_MS * 1000);
  111. // Enable PCNT and wait for it to start counting
  112. pcnt_ll_counter_resume(s_pcnt.dev, REF_CLOCK_PCNT_UNIT);
  113. pcnt_ll_counter_clear(s_pcnt.dev, REF_CLOCK_PCNT_UNIT);
  114. esp_rom_delay_us(10000);
  115. // Enable interrupt
  116. s_milliseconds = 0;
  117. ESP_ERROR_CHECK(esp_intr_alloc(ETS_PCNT_INTR_SOURCE, ESP_INTR_FLAG_IRAM, pcnt_isr, NULL, &s_intr_handle));
  118. pcnt_ll_clear_intr_status(s_pcnt.dev, BIT(REF_CLOCK_PCNT_UNIT));
  119. pcnt_ll_intr_enable(s_pcnt.dev, REF_CLOCK_PCNT_UNIT);
  120. }
  121. static void IRAM_ATTR pcnt_isr(void* arg)
  122. {
  123. portENTER_CRITICAL_ISR(&s_lock);
  124. pcnt_ll_clear_intr_status(s_pcnt.dev, BIT(REF_CLOCK_PCNT_UNIT));
  125. s_milliseconds += REF_CLOCK_PRESCALER_MS;
  126. portEXIT_CRITICAL_ISR(&s_lock);
  127. }
  128. void ref_clock_deinit()
  129. {
  130. assert(s_intr_handle && "deinit called without init");
  131. // Disable interrupt
  132. pcnt_ll_intr_disable(s_pcnt.dev, REF_CLOCK_PCNT_UNIT);
  133. esp_intr_free(s_intr_handle);
  134. s_intr_handle = NULL;
  135. // Disable RMT
  136. rmt_ll_stop_tx(s_rmt.regs, REF_CLOCK_RMT_CHANNEL);
  137. periph_module_disable(PERIPH_RMT_MODULE);
  138. // Disable PCNT
  139. pcnt_ll_counter_pause(s_pcnt.dev, REF_CLOCK_PCNT_UNIT);
  140. periph_module_disable(PERIPH_PCNT_MODULE);
  141. }
  142. uint64_t ref_clock_get()
  143. {
  144. portENTER_CRITICAL(&s_lock);
  145. int16_t microseconds = 0;
  146. pcnt_ll_get_counter_value(s_pcnt.dev, REF_CLOCK_PCNT_UNIT, &microseconds);
  147. uint32_t milliseconds = s_milliseconds;
  148. uint32_t intr_status = 0;
  149. pcnt_ll_get_intr_status(s_pcnt.dev, &intr_status);
  150. if (intr_status & BIT(REF_CLOCK_PCNT_UNIT)) {
  151. // refresh counter value, in case the overflow has happened after reading cnt_val
  152. pcnt_ll_get_counter_value(s_pcnt.dev, REF_CLOCK_PCNT_UNIT, &microseconds);
  153. milliseconds += REF_CLOCK_PRESCALER_MS;
  154. }
  155. portEXIT_CRITICAL(&s_lock);
  156. return 1000 * (uint64_t) milliseconds + (uint64_t) microseconds;
  157. }