ref_clock_impl_rmt_pcnt.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * SPDX-FileCopyrightText: 2017-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * Some unit test cases need to have access to reliable timestamps even when CPU and APB clock frequencies change over time.
  8. * 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).
  9. *
  10. * +---------------------+ 500KHz Square Wave +--------------------------+
  11. * | RMT (channel 0, TX) +----------------------------------->+ PCNT (unit 0, channel 0) |
  12. * +---------------------+ +--------------------------+
  13. *
  14. * 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.
  15. * Configure RMT channel to generate a 500KHz square wave (using carrier feature) to one GPIO.
  16. * PCNT takes the input signal from the GPIO and counts the edges (which occur at 1MHz frequency).
  17. * PCNT counter is only 16 bit wide, an interrupt is configured to trigger when the counter reaches 30000,
  18. * incrementing a 32-bit millisecond counter maintained by software.
  19. */
  20. #include "sdkconfig.h"
  21. #include "test_utils.h"
  22. #include "freertos/FreeRTOS.h"
  23. #include "esp_intr_alloc.h"
  24. #include "esp_private/periph_ctrl.h"
  25. #include "soc/gpio_sig_map.h"
  26. #include "soc/gpio_periph.h"
  27. #include "soc/soc_caps.h"
  28. #include "hal/rmt_types.h"
  29. #include "hal/rmt_hal.h"
  30. #include "hal/rmt_ll.h"
  31. #include "hal/pcnt_hal.h"
  32. #include "hal/pcnt_ll.h"
  33. #include "esp_rom_gpio.h"
  34. #include "esp_rom_sys.h"
  35. #define REF_CLOCK_RMT_CHANNEL 0 // RMT channel 0
  36. #define REF_CLOCK_PCNT_UNIT 0 // PCNT unit 0
  37. #define REF_CLOCK_PCNT_CHANNEL 0// PCNT channel 0
  38. #define REF_CLOCK_GPIO 21 // GPIO used to combine RMT out signal with PCNT input signal
  39. #define REF_CLOCK_PRESCALER_MS 30 // PCNT high threshold interrupt fired every 30ms
  40. static void IRAM_ATTR pcnt_isr(void *arg);
  41. static intr_handle_t s_intr_handle;
  42. static portMUX_TYPE s_lock = portMUX_INITIALIZER_UNLOCKED;
  43. static volatile uint32_t s_milliseconds;
  44. static rmt_hal_context_t s_rmt_hal;
  45. static pcnt_hal_context_t s_pcnt_hal;
  46. void ref_clock_init(void)
  47. {
  48. assert(s_intr_handle == NULL && "ref clock already initialized");
  49. // Route RMT output to GPIO matrix
  50. esp_rom_gpio_connect_out_signal(REF_CLOCK_GPIO, RMT_SIG_OUT0_IDX, false, false);
  51. // Initialize RMT
  52. periph_module_enable(PERIPH_RMT_MODULE);
  53. rmt_hal_init(&s_rmt_hal);
  54. rmt_item32_t data = {
  55. .duration0 = 1,
  56. .level0 = 1,
  57. .duration1 = 0,
  58. .level1 = 0
  59. };
  60. rmt_ll_enable_drive_clock(s_rmt_hal.regs, true);
  61. #if SOC_RMT_SUPPORT_XTAL
  62. 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))
  63. #elif SOC_RMT_SUPPORT_REF_TICK
  64. rmt_ll_set_group_clock_src(s_rmt_hal.regs, REF_CLOCK_RMT_CHANNEL, RMT_BASECLK_REF, 0, 0, 0); // select REF_TICK (1MHz)
  65. #endif
  66. rmt_hal_tx_set_channel_clock(&s_rmt_hal, REF_CLOCK_RMT_CHANNEL, 1000000, 1000000); // counter clock: 1MHz
  67. rmt_ll_tx_enable_idle(s_rmt_hal.regs, REF_CLOCK_RMT_CHANNEL, true); // enable idle output
  68. rmt_ll_tx_set_idle_level(s_rmt_hal.regs, REF_CLOCK_RMT_CHANNEL, 1); // idle level: 1
  69. rmt_ll_tx_enable_carrier_modulation(s_rmt_hal.regs, REF_CLOCK_RMT_CHANNEL, true);
  70. #if !CONFIG_IDF_TARGET_ESP32
  71. rmt_ll_tx_set_carrier_always_on(s_rmt_hal.regs, REF_CLOCK_RMT_CHANNEL, true);
  72. #endif
  73. rmt_hal_set_carrier_clock(&s_rmt_hal, REF_CLOCK_RMT_CHANNEL, 1000000, 500000, 0.5); // set carrier to 500KHz
  74. rmt_ll_tx_set_carrier_level(s_rmt_hal.regs, REF_CLOCK_RMT_CHANNEL, 1);
  75. rmt_ll_enable_mem_access(s_rmt_hal.regs, true);
  76. rmt_ll_tx_reset_pointer(s_rmt_hal.regs, REF_CLOCK_RMT_CHANNEL);
  77. rmt_ll_tx_set_mem_blocks(s_rmt_hal.regs, REF_CLOCK_RMT_CHANNEL, 1);
  78. rmt_ll_write_memory(s_rmt_hal.mem, REF_CLOCK_RMT_CHANNEL, &data, 1, 0);
  79. rmt_ll_tx_enable_loop(s_rmt_hal.regs, REF_CLOCK_RMT_CHANNEL, false);
  80. rmt_ll_tx_start(s_rmt_hal.regs, REF_CLOCK_RMT_CHANNEL);
  81. // Route signal to PCNT
  82. esp_rom_gpio_connect_in_signal(REF_CLOCK_GPIO, PCNT_SIG_CH0_IN0_IDX, false);
  83. if (REF_CLOCK_GPIO != 20) {
  84. PIN_INPUT_ENABLE(GPIO_PIN_MUX_REG[REF_CLOCK_GPIO]);
  85. } else {
  86. PIN_INPUT_ENABLE(PERIPHS_IO_MUX_GPIO20_U);
  87. }
  88. // Initialize PCNT
  89. periph_module_enable(PERIPH_PCNT_MODULE);
  90. pcnt_hal_init(&s_pcnt_hal, REF_CLOCK_PCNT_UNIT);
  91. pcnt_ll_set_edge_action(s_pcnt_hal.dev, REF_CLOCK_PCNT_UNIT, REF_CLOCK_PCNT_CHANNEL,
  92. PCNT_CHANNEL_EDGE_ACTION_INCREASE, PCNT_CHANNEL_EDGE_ACTION_INCREASE);
  93. pcnt_ll_set_level_action(s_pcnt_hal.dev, REF_CLOCK_PCNT_UNIT, REF_CLOCK_PCNT_CHANNEL,
  94. PCNT_CHANNEL_LEVEL_ACTION_KEEP, PCNT_CHANNEL_LEVEL_ACTION_KEEP);
  95. pcnt_ll_disable_all_events(s_pcnt_hal.dev, REF_CLOCK_PCNT_UNIT);
  96. pcnt_ll_set_high_limit_value(s_pcnt_hal.dev, REF_CLOCK_PCNT_UNIT, REF_CLOCK_PRESCALER_MS * 1000);
  97. pcnt_ll_enable_high_limit_event(s_pcnt_hal.dev, REF_CLOCK_PCNT_UNIT, true);
  98. // Enable PCNT and wait for it to start counting
  99. pcnt_ll_start_count(s_pcnt_hal.dev, REF_CLOCK_PCNT_UNIT);
  100. pcnt_ll_clear_count(s_pcnt_hal.dev, REF_CLOCK_PCNT_UNIT);
  101. esp_rom_delay_us(10000);
  102. // Enable interrupt
  103. s_milliseconds = 0;
  104. ESP_ERROR_CHECK(esp_intr_alloc(ETS_PCNT_INTR_SOURCE, ESP_INTR_FLAG_IRAM, pcnt_isr, NULL, &s_intr_handle));
  105. pcnt_ll_clear_intr_status(s_pcnt_hal.dev, BIT(REF_CLOCK_PCNT_UNIT));
  106. pcnt_ll_enable_intr(s_pcnt_hal.dev, 1 << REF_CLOCK_PCNT_UNIT, true);
  107. }
  108. static void IRAM_ATTR pcnt_isr(void *arg)
  109. {
  110. portENTER_CRITICAL_ISR(&s_lock);
  111. pcnt_ll_clear_intr_status(s_pcnt_hal.dev, BIT(REF_CLOCK_PCNT_UNIT));
  112. s_milliseconds += REF_CLOCK_PRESCALER_MS;
  113. portEXIT_CRITICAL_ISR(&s_lock);
  114. }
  115. void ref_clock_deinit()
  116. {
  117. assert(s_intr_handle && "ref clock deinit called without init");
  118. // Disable interrupt
  119. pcnt_ll_enable_intr(s_pcnt_hal.dev, 1 << REF_CLOCK_PCNT_UNIT, false);
  120. esp_intr_free(s_intr_handle);
  121. s_intr_handle = NULL;
  122. // Disable RMT
  123. rmt_ll_tx_enable_carrier_modulation(s_rmt_hal.regs, REF_CLOCK_RMT_CHANNEL, false);
  124. periph_module_disable(PERIPH_RMT_MODULE);
  125. // Disable PCNT
  126. pcnt_ll_stop_count(s_pcnt_hal.dev, REF_CLOCK_PCNT_UNIT);
  127. periph_module_disable(PERIPH_PCNT_MODULE);
  128. }
  129. uint64_t ref_clock_get()
  130. {
  131. portENTER_CRITICAL(&s_lock);
  132. int microseconds = 0;
  133. microseconds = pcnt_ll_get_count(s_pcnt_hal.dev, REF_CLOCK_PCNT_UNIT);
  134. uint32_t milliseconds = s_milliseconds;
  135. uint32_t intr_status = pcnt_ll_get_intr_status(s_pcnt_hal.dev);
  136. if (intr_status & BIT(REF_CLOCK_PCNT_UNIT)) {
  137. // refresh counter value, in case the overflow has happened after reading cnt_val
  138. microseconds = pcnt_ll_get_count(s_pcnt_hal.dev, REF_CLOCK_PCNT_UNIT);
  139. milliseconds += REF_CLOCK_PRESCALER_MS;
  140. }
  141. portEXIT_CRITICAL(&s_lock);
  142. return 1000 * (uint64_t)milliseconds + (uint64_t)microseconds;
  143. }