ref_clock_impl_rmt_pcnt.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 "hal/pcnt_ll.h"
  41. #include "esp_rom_gpio.h"
  42. #include "esp_rom_sys.h"
  43. #define REF_CLOCK_RMT_CHANNEL 0 // RMT channel 0
  44. #define REF_CLOCK_PCNT_UNIT 0 // PCNT unit 0
  45. #define REF_CLOCK_PCNT_CHANNEL 0// PCNT channel 0
  46. #define REF_CLOCK_GPIO 21 // GPIO used to combine RMT out signal with PCNT input signal
  47. #define REF_CLOCK_PRESCALER_MS 30 // PCNT high threshold interrupt fired every 30ms
  48. static void IRAM_ATTR pcnt_isr(void *arg);
  49. static intr_handle_t s_intr_handle;
  50. static portMUX_TYPE s_lock = portMUX_INITIALIZER_UNLOCKED;
  51. static volatile uint32_t s_milliseconds;
  52. static rmt_hal_context_t s_rmt_hal;
  53. static pcnt_hal_context_t s_pcnt_hal;
  54. void ref_clock_init(void)
  55. {
  56. assert(s_intr_handle == NULL && "ref clock already initialized");
  57. // Route RMT output to GPIO matrix
  58. esp_rom_gpio_connect_out_signal(REF_CLOCK_GPIO, RMT_SIG_OUT0_IDX, false, false);
  59. // Initialize RMT
  60. periph_module_enable(PERIPH_RMT_MODULE);
  61. rmt_hal_init(&s_rmt_hal);
  62. rmt_item32_t data = {
  63. .duration0 = 1,
  64. .level0 = 1,
  65. .duration1 = 0,
  66. .level1 = 0
  67. };
  68. rmt_ll_enable_drive_clock(s_rmt_hal.regs, true);
  69. #if SOC_RMT_SUPPORT_XTAL
  70. rmt_ll_set_group_clock_src(s_rmt_hal.regs, REF_CLOCK_RMT_CHANNEL, RMT_BASECLK_XTAL, 39, 0, 0); // XTAL(40MHz), rmt_sclk => 1MHz (40/(1+39))
  71. #elif SOC_RMT_SUPPORT_REF_TICK
  72. rmt_ll_set_group_clock_src(s_rmt_hal.regs, REF_CLOCK_RMT_CHANNEL, RMT_BASECLK_REF, 0, 0, 0); // select REF_TICK (1MHz)
  73. #endif
  74. rmt_hal_tx_set_channel_clock(&s_rmt_hal, REF_CLOCK_RMT_CHANNEL, 1000000, 1000000); // counter clock: 1MHz
  75. rmt_ll_tx_enable_idle(s_rmt_hal.regs, REF_CLOCK_RMT_CHANNEL, true); // enable idle output
  76. rmt_ll_tx_set_idle_level(s_rmt_hal.regs, REF_CLOCK_RMT_CHANNEL, 1); // idle level: 1
  77. rmt_ll_tx_enable_carrier_modulation(s_rmt_hal.regs, REF_CLOCK_RMT_CHANNEL, true);
  78. #if !CONFIG_IDF_TARGET_ESP32
  79. rmt_ll_tx_set_carrier_always_on(s_rmt_hal.regs, REF_CLOCK_RMT_CHANNEL, true);
  80. #endif
  81. rmt_hal_set_carrier_clock(&s_rmt_hal, REF_CLOCK_RMT_CHANNEL, 1000000, 500000, 0.5); // set carrier to 500KHz
  82. rmt_ll_tx_set_carrier_level(s_rmt_hal.regs, REF_CLOCK_RMT_CHANNEL, 1);
  83. rmt_ll_enable_mem_access(s_rmt_hal.regs, true);
  84. rmt_ll_tx_reset_pointer(s_rmt_hal.regs, REF_CLOCK_RMT_CHANNEL);
  85. rmt_ll_tx_set_mem_blocks(s_rmt_hal.regs, REF_CLOCK_RMT_CHANNEL, 1);
  86. rmt_ll_write_memory(s_rmt_hal.mem, REF_CLOCK_RMT_CHANNEL, &data, 1, 0);
  87. rmt_ll_tx_enable_loop(s_rmt_hal.regs, REF_CLOCK_RMT_CHANNEL, false);
  88. rmt_ll_tx_start(s_rmt_hal.regs, REF_CLOCK_RMT_CHANNEL);
  89. // Route signal to PCNT
  90. esp_rom_gpio_connect_in_signal(REF_CLOCK_GPIO, PCNT_SIG_CH0_IN0_IDX, false);
  91. if (REF_CLOCK_GPIO != 20) {
  92. PIN_INPUT_ENABLE(GPIO_PIN_MUX_REG[REF_CLOCK_GPIO]);
  93. } else {
  94. PIN_INPUT_ENABLE(PERIPHS_IO_MUX_GPIO20_U);
  95. }
  96. // Initialize PCNT
  97. periph_module_enable(PERIPH_PCNT_MODULE);
  98. pcnt_hal_init(&s_pcnt_hal, REF_CLOCK_PCNT_UNIT);
  99. pcnt_ll_set_edge_action(s_pcnt_hal.dev, REF_CLOCK_PCNT_UNIT, REF_CLOCK_PCNT_CHANNEL,
  100. PCNT_CHANNEL_EDGE_ACTION_INCREASE, PCNT_CHANNEL_EDGE_ACTION_INCREASE);
  101. pcnt_ll_set_level_action(s_pcnt_hal.dev, REF_CLOCK_PCNT_UNIT, REF_CLOCK_PCNT_CHANNEL,
  102. PCNT_CHANNEL_LEVEL_ACTION_KEEP, PCNT_CHANNEL_LEVEL_ACTION_KEEP);
  103. pcnt_ll_disable_all_events(s_pcnt_hal.dev, REF_CLOCK_PCNT_UNIT);
  104. pcnt_ll_set_high_limit_value(s_pcnt_hal.dev, REF_CLOCK_PCNT_UNIT, REF_CLOCK_PRESCALER_MS * 1000);
  105. pcnt_ll_enable_high_limit_event(s_pcnt_hal.dev, REF_CLOCK_PCNT_UNIT, true);
  106. // Enable PCNT and wait for it to start counting
  107. pcnt_ll_start_count(s_pcnt_hal.dev, REF_CLOCK_PCNT_UNIT);
  108. pcnt_ll_clear_count(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_enable_intr(s_pcnt_hal.dev, 1 << REF_CLOCK_PCNT_UNIT, true);
  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_enable_intr(s_pcnt_hal.dev, 1 << REF_CLOCK_PCNT_UNIT, false);
  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_stop_count(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. int microseconds = 0;
  141. microseconds = pcnt_ll_get_count(s_pcnt_hal.dev, REF_CLOCK_PCNT_UNIT);
  142. uint32_t milliseconds = s_milliseconds;
  143. uint32_t intr_status = pcnt_ll_get_intr_status(s_pcnt_hal.dev);
  144. if (intr_status & BIT(REF_CLOCK_PCNT_UNIT)) {
  145. // refresh counter value, in case the overflow has happened after reading cnt_val
  146. microseconds = pcnt_ll_get_count(s_pcnt_hal.dev, REF_CLOCK_PCNT_UNIT);
  147. milliseconds += REF_CLOCK_PRESCALER_MS;
  148. }
  149. portEXIT_CRITICAL(&s_lock);
  150. return 1000 * (uint64_t)milliseconds + (uint64_t)microseconds;
  151. }