ref_clock_impl_rmt_pcnt.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // Copyright 2017-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. /**
  15. * Some unit test cases need to have access to reliable timestamps even when CPU and APB clock frequencies change over time.
  16. * This reference clock is built upon two peripherals: one RMT channel and one PCNT channel (hopefully we can have these two peripherals in all ESP chips).
  17. *
  18. * +---------------------+ 500KHz Square Wave +--------------------------+
  19. * | RMT (channel 0, TX) +----------------------------------->+ PCNT (unit 0, channel 0) |
  20. * +---------------------+ +--------------------------+
  21. *
  22. * RMT TX channel is configured to use a fixed clock (e.g. REF_TICK, XTAL) as clock source, so that our ref clock won't be affected during APB/CPU clock switch.
  23. * Configure RMT channel to generate a 500KHz square wave (using carrier feature) to one GPIO.
  24. * PCNT takes the input signal from the GPIO and counts the edges (which occur at 1MHz frequency).
  25. * PCNT counter is only 16 bit wide, an interrupt is configured to trigger when the counter reaches 30000,
  26. * incrementing a 32-bit millisecond counter maintained by software.
  27. */
  28. #include "sdkconfig.h"
  29. #include "test_utils.h"
  30. #include "freertos/FreeRTOS.h"
  31. #include "esp_intr_alloc.h"
  32. #include "driver/periph_ctrl.h"
  33. #include "soc/gpio_sig_map.h"
  34. #include "soc/gpio_periph.h"
  35. #include "soc/soc_caps.h"
  36. #include "hal/rmt_types.h"
  37. #include "hal/rmt_hal.h"
  38. #include "hal/rmt_ll.h"
  39. #include "hal/pcnt_hal.h"
  40. #include "esp_rom_gpio.h"
  41. #include "esp_rom_sys.h"
  42. #define REF_CLOCK_RMT_CHANNEL 0 // RMT channel 0
  43. #define REF_CLOCK_PCNT_UNIT 0 // PCNT unit 0 channel 0
  44. #define REF_CLOCK_GPIO 21 // GPIO used to combine RMT out signal with PCNT input signal
  45. #define REF_CLOCK_PRESCALER_MS 30 // PCNT high threshold interrupt fired every 30ms
  46. static void IRAM_ATTR pcnt_isr(void *arg);
  47. static intr_handle_t s_intr_handle;
  48. static portMUX_TYPE s_lock = portMUX_INITIALIZER_UNLOCKED;
  49. static volatile uint32_t s_milliseconds;
  50. static rmt_hal_context_t s_rmt_hal;
  51. static pcnt_hal_context_t s_pcnt_hal;
  52. void ref_clock_init(void)
  53. {
  54. assert(s_intr_handle == NULL && "ref clock already initialized");
  55. // Route RMT output to GPIO matrix
  56. esp_rom_gpio_connect_out_signal(REF_CLOCK_GPIO, RMT_SIG_OUT0_IDX, false, false);
  57. // Initialize RMT
  58. periph_module_enable(PERIPH_RMT_MODULE);
  59. rmt_hal_init(&s_rmt_hal);
  60. rmt_item32_t data = {
  61. .duration0 = 1,
  62. .level0 = 1,
  63. .duration1 = 0,
  64. .level1 = 0
  65. };
  66. rmt_ll_enable_drive_clock(s_rmt_hal.regs, true);
  67. #if SOC_RMT_SUPPORT_XTAL
  68. rmt_ll_set_counter_clock_src(s_rmt_hal.regs, REF_CLOCK_RMT_CHANNEL, RMT_BASECLK_XTAL, 39, 0, 0); // XTAL(40MHz), rmt_sclk => 1MHz (40/(1+39))
  69. #elif SOC_RMT_SUPPORT_REF_TICK
  70. rmt_ll_set_counter_clock_src(s_rmt_hal.regs, REF_CLOCK_RMT_CHANNEL, RMT_BASECLK_REF, 0, 0, 0); // select REF_TICK (1MHz)
  71. #endif
  72. rmt_hal_tx_set_counter_clock(&s_rmt_hal, REF_CLOCK_RMT_CHANNEL, 1000000, 1000000); // counter clock: 1MHz
  73. rmt_ll_tx_enable_idle(s_rmt_hal.regs, REF_CLOCK_RMT_CHANNEL, true); // enable idle output
  74. rmt_ll_tx_set_idle_level(s_rmt_hal.regs, REF_CLOCK_RMT_CHANNEL, 1); // idle level: 1
  75. rmt_ll_tx_enable_carrier_modulation(s_rmt_hal.regs, REF_CLOCK_RMT_CHANNEL, true);
  76. #if !CONFIG_IDF_TARGET_ESP32
  77. rmt_ll_tx_set_carrier_always_on(s_rmt_hal.regs, REF_CLOCK_RMT_CHANNEL, true);
  78. #endif
  79. rmt_hal_set_carrier_clock(&s_rmt_hal, REF_CLOCK_RMT_CHANNEL, 1000000, 500000, 0.5); // set carrier to 500KHz
  80. rmt_ll_tx_set_carrier_level(s_rmt_hal.regs, REF_CLOCK_RMT_CHANNEL, 1);
  81. rmt_ll_enable_mem_access(s_rmt_hal.regs, true);
  82. rmt_ll_tx_reset_pointer(s_rmt_hal.regs, REF_CLOCK_RMT_CHANNEL);
  83. rmt_ll_tx_set_mem_blocks(s_rmt_hal.regs, REF_CLOCK_RMT_CHANNEL, 1);
  84. rmt_ll_write_memory(s_rmt_hal.mem, REF_CLOCK_RMT_CHANNEL, &data, 1, 0);
  85. rmt_ll_tx_enable_loop(s_rmt_hal.regs, REF_CLOCK_RMT_CHANNEL, false);
  86. rmt_ll_tx_start(s_rmt_hal.regs, REF_CLOCK_RMT_CHANNEL);
  87. // Route signal to PCNT
  88. esp_rom_gpio_connect_in_signal(REF_CLOCK_GPIO, PCNT_SIG_CH0_IN0_IDX, false);
  89. if (REF_CLOCK_GPIO != 20) {
  90. PIN_INPUT_ENABLE(GPIO_PIN_MUX_REG[REF_CLOCK_GPIO]);
  91. } else {
  92. PIN_INPUT_ENABLE(PERIPHS_IO_MUX_GPIO20_U);
  93. }
  94. // Initialize PCNT
  95. periph_module_enable(PERIPH_PCNT_MODULE);
  96. pcnt_hal_init(&s_pcnt_hal, REF_CLOCK_PCNT_UNIT);
  97. pcnt_ll_set_mode(s_pcnt_hal.dev, REF_CLOCK_PCNT_UNIT, PCNT_CHANNEL_0,
  98. PCNT_COUNT_INC, PCNT_COUNT_INC,
  99. PCNT_MODE_KEEP, PCNT_MODE_KEEP);
  100. pcnt_ll_event_disable(s_pcnt_hal.dev, REF_CLOCK_PCNT_UNIT, PCNT_EVT_L_LIM);
  101. pcnt_ll_event_enable(s_pcnt_hal.dev, REF_CLOCK_PCNT_UNIT, PCNT_EVT_H_LIM);
  102. pcnt_ll_event_disable(s_pcnt_hal.dev, REF_CLOCK_PCNT_UNIT, PCNT_EVT_ZERO);
  103. pcnt_ll_event_disable(s_pcnt_hal.dev, REF_CLOCK_PCNT_UNIT, PCNT_EVT_THRES_0);
  104. pcnt_ll_event_disable(s_pcnt_hal.dev, REF_CLOCK_PCNT_UNIT, PCNT_EVT_THRES_1);
  105. pcnt_ll_set_event_value(s_pcnt_hal.dev, REF_CLOCK_PCNT_UNIT, PCNT_EVT_H_LIM, REF_CLOCK_PRESCALER_MS * 1000);
  106. // Enable PCNT and wait for it to start counting
  107. pcnt_ll_counter_resume(s_pcnt_hal.dev, REF_CLOCK_PCNT_UNIT);
  108. pcnt_ll_counter_clear(s_pcnt_hal.dev, REF_CLOCK_PCNT_UNIT);
  109. esp_rom_delay_us(10000);
  110. // Enable interrupt
  111. s_milliseconds = 0;
  112. ESP_ERROR_CHECK(esp_intr_alloc(ETS_PCNT_INTR_SOURCE, ESP_INTR_FLAG_IRAM, pcnt_isr, NULL, &s_intr_handle));
  113. pcnt_ll_clear_intr_status(s_pcnt_hal.dev, BIT(REF_CLOCK_PCNT_UNIT));
  114. pcnt_ll_intr_enable(s_pcnt_hal.dev, REF_CLOCK_PCNT_UNIT);
  115. }
  116. static void IRAM_ATTR pcnt_isr(void *arg)
  117. {
  118. portENTER_CRITICAL_ISR(&s_lock);
  119. pcnt_ll_clear_intr_status(s_pcnt_hal.dev, BIT(REF_CLOCK_PCNT_UNIT));
  120. s_milliseconds += REF_CLOCK_PRESCALER_MS;
  121. portEXIT_CRITICAL_ISR(&s_lock);
  122. }
  123. void ref_clock_deinit()
  124. {
  125. assert(s_intr_handle && "ref clock deinit called without init");
  126. // Disable interrupt
  127. pcnt_ll_intr_disable(s_pcnt_hal.dev, REF_CLOCK_PCNT_UNIT);
  128. esp_intr_free(s_intr_handle);
  129. s_intr_handle = NULL;
  130. // Disable RMT
  131. rmt_ll_tx_enable_carrier_modulation(s_rmt_hal.regs, REF_CLOCK_RMT_CHANNEL, false);
  132. periph_module_disable(PERIPH_RMT_MODULE);
  133. // Disable PCNT
  134. pcnt_ll_counter_pause(s_pcnt_hal.dev, REF_CLOCK_PCNT_UNIT);
  135. periph_module_disable(PERIPH_PCNT_MODULE);
  136. }
  137. uint64_t ref_clock_get()
  138. {
  139. portENTER_CRITICAL(&s_lock);
  140. int16_t microseconds = 0;
  141. pcnt_ll_get_counter_value(s_pcnt_hal.dev, REF_CLOCK_PCNT_UNIT, &microseconds);
  142. uint32_t milliseconds = s_milliseconds;
  143. uint32_t intr_status = 0;
  144. pcnt_ll_get_intr_status(s_pcnt_hal.dev, &intr_status);
  145. if (intr_status & BIT(REF_CLOCK_PCNT_UNIT)) {
  146. // refresh counter value, in case the overflow has happened after reading cnt_val
  147. pcnt_ll_get_counter_value(s_pcnt_hal.dev, REF_CLOCK_PCNT_UNIT, &microseconds);
  148. milliseconds += REF_CLOCK_PRESCALER_MS;
  149. }
  150. portEXIT_CRITICAL(&s_lock);
  151. return 1000 * (uint64_t)milliseconds + (uint64_t)microseconds;
  152. }