ref_clock.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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/rmt_periph.h"
  33. #include "soc/pcnt_periph.h"
  34. #include "soc/gpio_periph.h"
  35. #include "soc/dport_reg.h"
  36. #include "esp_intr_alloc.h"
  37. #include "freertos/FreeRTOS.h"
  38. #include "driver/periph_ctrl.h"
  39. #include "esp32/rom/gpio.h"
  40. #include "sdkconfig.h"
  41. /* Select which RMT and PCNT channels, and GPIO to use */
  42. #define REF_CLOCK_PCNT_UNIT 0
  43. #define REF_CLOCK_GPIO 21
  44. #define REF_CLOCK_PRESCALER_MS 30
  45. static void IRAM_ATTR pcnt_isr(void* arg);
  46. static intr_handle_t s_intr_handle;
  47. static portMUX_TYPE s_lock = portMUX_INITIALIZER_UNLOCKED;
  48. static volatile uint32_t s_milliseconds;
  49. #if CONFIG_IDF_TARGET_ESP32
  50. #define REF_CLOCK_RMT_CHANNEL 7
  51. static int get_pcnt_sig(void)
  52. {
  53. return (REF_CLOCK_PCNT_UNIT < 5) ?
  54. PCNT_SIG_CH0_IN0_IDX + 4 * REF_CLOCK_PCNT_UNIT :
  55. PCNT_SIG_CH0_IN5_IDX + 4 * (REF_CLOCK_PCNT_UNIT - 5);
  56. }
  57. #elif CONFIG_IDF_TARGET_ESP32S2BETA
  58. #define REF_CLOCK_RMT_CHANNEL 3
  59. static int get_pcnt_sig(void)
  60. {
  61. return PCNT_SIG_CH0_IN0_IDX + 4 * REF_CLOCK_PCNT_UNIT;
  62. }
  63. #endif
  64. void ref_clock_init()
  65. {
  66. assert(s_intr_handle == NULL && "already initialized");
  67. // Route RMT output to GPIO matrix
  68. gpio_matrix_out(REF_CLOCK_GPIO, RMT_SIG_OUT0_IDX + REF_CLOCK_RMT_CHANNEL, false, false);
  69. // Initialize RMT
  70. periph_module_enable(PERIPH_RMT_MODULE);
  71. RMT.apb_conf.fifo_mask = 1;
  72. rmt_item32_t data = {
  73. .duration0 = 1,
  74. .level0 = 1,
  75. .duration1 = 0,
  76. .level1 = 0
  77. };
  78. RMTMEM.chan[REF_CLOCK_RMT_CHANNEL].data32[0] = data;
  79. RMTMEM.chan[REF_CLOCK_RMT_CHANNEL].data32[1].val = 0;
  80. RMT.conf_ch[REF_CLOCK_RMT_CHANNEL].conf0.clk_en = 1;
  81. RMT.conf_ch[REF_CLOCK_RMT_CHANNEL].conf1.tx_start = 0;
  82. RMT.conf_ch[REF_CLOCK_RMT_CHANNEL].conf1.mem_owner = 0;
  83. RMT.conf_ch[REF_CLOCK_RMT_CHANNEL].conf1.mem_rd_rst = 1;
  84. RMT.conf_ch[REF_CLOCK_RMT_CHANNEL].conf1.apb_mem_rst = 1;
  85. RMT.conf_ch[REF_CLOCK_RMT_CHANNEL].conf0.carrier_en = 0;
  86. RMT.conf_ch[REF_CLOCK_RMT_CHANNEL].conf0.div_cnt = 1;
  87. RMT.conf_ch[REF_CLOCK_RMT_CHANNEL].conf0.mem_size = 1;
  88. RMT.conf_ch[REF_CLOCK_RMT_CHANNEL].conf1.ref_always_on = 0;
  89. RMT.conf_ch[REF_CLOCK_RMT_CHANNEL].conf1.tx_conti_mode = 1;
  90. RMT.conf_ch[REF_CLOCK_RMT_CHANNEL].conf1.tx_start = 1;
  91. // Route signal to PCNT
  92. int pcnt_sig_idx = get_pcnt_sig();
  93. gpio_matrix_in(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.conf_unit[REF_CLOCK_PCNT_UNIT].conf0.ch0_hctrl_mode = 0;
  102. PCNT.conf_unit[REF_CLOCK_PCNT_UNIT].conf0.ch0_lctrl_mode = 0;
  103. PCNT.conf_unit[REF_CLOCK_PCNT_UNIT].conf0.ch0_pos_mode = 1;
  104. PCNT.conf_unit[REF_CLOCK_PCNT_UNIT].conf0.ch0_neg_mode = 1;
  105. PCNT.conf_unit[REF_CLOCK_PCNT_UNIT].conf0.thr_l_lim_en = 0;
  106. PCNT.conf_unit[REF_CLOCK_PCNT_UNIT].conf0.thr_h_lim_en = 1;
  107. PCNT.conf_unit[REF_CLOCK_PCNT_UNIT].conf0.thr_zero_en = 0;
  108. PCNT.conf_unit[REF_CLOCK_PCNT_UNIT].conf0.thr_thres0_en = 0;
  109. PCNT.conf_unit[REF_CLOCK_PCNT_UNIT].conf0.thr_thres1_en = 0;
  110. PCNT.conf_unit[REF_CLOCK_PCNT_UNIT].conf2.cnt_h_lim = REF_CLOCK_PRESCALER_MS * 1000;
  111. // Enable PCNT and wait for it to start counting
  112. PCNT.ctrl.val &= ~(BIT(REF_CLOCK_PCNT_UNIT * 2 + 1));
  113. PCNT.ctrl.val |= BIT(REF_CLOCK_PCNT_UNIT * 2);
  114. PCNT.ctrl.val &= ~BIT(REF_CLOCK_PCNT_UNIT * 2);
  115. ets_delay_us(10000);
  116. // Enable interrupt
  117. s_milliseconds = 0;
  118. ESP_ERROR_CHECK(esp_intr_alloc(ETS_PCNT_INTR_SOURCE, ESP_INTR_FLAG_IRAM, pcnt_isr, NULL, &s_intr_handle));
  119. PCNT.int_clr.val = BIT(REF_CLOCK_PCNT_UNIT);
  120. PCNT.int_ena.val = BIT(REF_CLOCK_PCNT_UNIT);
  121. }
  122. static void IRAM_ATTR pcnt_isr(void* arg)
  123. {
  124. portENTER_CRITICAL_ISR(&s_lock);
  125. PCNT.int_clr.val = BIT(REF_CLOCK_PCNT_UNIT);
  126. s_milliseconds += REF_CLOCK_PRESCALER_MS;
  127. portEXIT_CRITICAL_ISR(&s_lock);
  128. }
  129. void ref_clock_deinit()
  130. {
  131. assert(s_intr_handle && "deinit called without init");
  132. // Disable interrupt
  133. PCNT.int_ena.val &= ~BIT(REF_CLOCK_PCNT_UNIT);
  134. esp_intr_free(s_intr_handle);
  135. s_intr_handle = NULL;
  136. // Disable RMT
  137. RMT.conf_ch[REF_CLOCK_RMT_CHANNEL].conf1.tx_start = 0;
  138. RMT.conf_ch[REF_CLOCK_RMT_CHANNEL].conf0.clk_en = 0;
  139. periph_module_disable(PERIPH_RMT_MODULE);
  140. // Disable PCNT
  141. PCNT.ctrl.val |= ~(BIT(REF_CLOCK_PCNT_UNIT * 2 + 1));
  142. periph_module_disable(PERIPH_PCNT_MODULE);
  143. }
  144. uint64_t ref_clock_get()
  145. {
  146. portENTER_CRITICAL(&s_lock);
  147. uint32_t microseconds = PCNT.cnt_unit[REF_CLOCK_PCNT_UNIT].cnt_val;
  148. uint32_t milliseconds = s_milliseconds;
  149. if (PCNT.int_st.val & BIT(REF_CLOCK_PCNT_UNIT)) {
  150. // refresh counter value, in case the overflow has happened after reading cnt_val
  151. microseconds = PCNT.cnt_unit[REF_CLOCK_PCNT_UNIT].cnt_val;
  152. milliseconds += REF_CLOCK_PRESCALER_MS;
  153. }
  154. portEXIT_CRITICAL(&s_lock);
  155. return 1000 * (uint64_t) milliseconds + (uint64_t) microseconds;
  156. }