rmt_common.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <sys/lock.h>
  7. #include "sdkconfig.h"
  8. #if CONFIG_RMT_ENABLE_DEBUG_LOG
  9. // The local log level must be defined before including esp_log.h
  10. // Set the maximum log level for this source file
  11. #define LOG_LOCAL_LEVEL ESP_LOG_DEBUG
  12. #endif
  13. #include "esp_log.h"
  14. #include "esp_check.h"
  15. #include "rmt_private.h"
  16. #include "clk_ctrl_os.h"
  17. #include "soc/rtc.h"
  18. #include "soc/rmt_periph.h"
  19. #include "hal/rmt_ll.h"
  20. #include "driver/gpio.h"
  21. #include "esp_private/esp_clk.h"
  22. #include "esp_private/periph_ctrl.h"
  23. static const char *TAG = "rmt";
  24. typedef struct rmt_platform_t {
  25. _lock_t mutex; // platform level mutex lock
  26. rmt_group_t *groups[SOC_RMT_GROUPS]; // array of RMT group instances
  27. int group_ref_counts[SOC_RMT_GROUPS]; // reference count used to protect group install/uninstall
  28. } rmt_platform_t;
  29. static rmt_platform_t s_platform; // singleton platform
  30. rmt_group_t *rmt_acquire_group_handle(int group_id)
  31. {
  32. bool new_group = false;
  33. rmt_group_t *group = NULL;
  34. // prevent install rmt group concurrently
  35. _lock_acquire(&s_platform.mutex);
  36. if (!s_platform.groups[group_id]) {
  37. group = heap_caps_calloc(1, sizeof(rmt_group_t), RMT_MEM_ALLOC_CAPS);
  38. if (group) {
  39. new_group = true;
  40. s_platform.groups[group_id] = group;
  41. group->group_id = group_id;
  42. group->spinlock = (portMUX_TYPE)portMUX_INITIALIZER_UNLOCKED;
  43. // initial occupy_mask: 1111...100...0
  44. group->occupy_mask = UINT32_MAX & ~((1 << SOC_RMT_CHANNELS_PER_GROUP) - 1);
  45. // group clock won't be configured at this stage, it will be set when allocate the first channel
  46. group->clk_src = 0;
  47. // enable APB access RMT registers
  48. periph_module_enable(rmt_periph_signals.groups[group_id].module);
  49. periph_module_reset(rmt_periph_signals.groups[group_id].module);
  50. // hal layer initialize
  51. rmt_hal_init(&group->hal);
  52. }
  53. } else { // group already install
  54. group = s_platform.groups[group_id];
  55. }
  56. if (group) {
  57. // someone acquired the group handle means we have a new object that refer to this group
  58. s_platform.group_ref_counts[group_id]++;
  59. }
  60. _lock_release(&s_platform.mutex);
  61. if (new_group) {
  62. ESP_LOGD(TAG, "new group(%d) at %p, occupy=%"PRIx32, group_id, group, group->occupy_mask);
  63. }
  64. return group;
  65. }
  66. void rmt_release_group_handle(rmt_group_t *group)
  67. {
  68. int group_id = group->group_id;
  69. bool do_deinitialize = false;
  70. _lock_acquire(&s_platform.mutex);
  71. s_platform.group_ref_counts[group_id]--;
  72. if (s_platform.group_ref_counts[group_id] == 0) {
  73. do_deinitialize = true;
  74. s_platform.groups[group_id] = NULL;
  75. // hal layer deinitialize
  76. rmt_hal_deinit(&group->hal);
  77. periph_module_disable(rmt_periph_signals.groups[group_id].module);
  78. free(group);
  79. }
  80. _lock_release(&s_platform.mutex);
  81. if (do_deinitialize) {
  82. ESP_LOGD(TAG, "del group(%d)", group_id);
  83. }
  84. }
  85. esp_err_t rmt_select_periph_clock(rmt_channel_handle_t chan, rmt_clock_source_t clk_src)
  86. {
  87. esp_err_t ret = ESP_OK;
  88. rmt_group_t *group = chan->group;
  89. int channel_id = chan->channel_id;
  90. uint32_t periph_src_clk_hz = 0;
  91. bool clock_selection_conflict = false;
  92. // check if we need to update the group clock source, group clock source is shared by all channels
  93. portENTER_CRITICAL(&group->spinlock);
  94. if (group->clk_src == 0) {
  95. group->clk_src = clk_src;
  96. } else {
  97. clock_selection_conflict = (group->clk_src != clk_src);
  98. }
  99. portEXIT_CRITICAL(&group->spinlock);
  100. ESP_RETURN_ON_FALSE(!clock_selection_conflict, ESP_ERR_INVALID_STATE, TAG,
  101. "group clock conflict, already is %d but attempt to %d", group->clk_src, clk_src);
  102. // [clk_tree] TODO: replace the following switch table by clk_tree API
  103. switch (clk_src) {
  104. #if SOC_RMT_SUPPORT_APB
  105. case RMT_CLK_SRC_APB:
  106. periph_src_clk_hz = esp_clk_apb_freq();
  107. #if CONFIG_PM_ENABLE
  108. sprintf(chan->pm_lock_name, "rmt_%d_%d", group->group_id, channel_id); // e.g. rmt_0_0
  109. ret = esp_pm_lock_create(ESP_PM_APB_FREQ_MAX, 0, chan->pm_lock_name, &chan->pm_lock);
  110. ESP_RETURN_ON_ERROR(ret, TAG, "create APB_FREQ_MAX lock failed");
  111. ESP_LOGD(TAG, "install APB_FREQ_MAX lock for RMT channel (%d,%d)", group->group_id, channel_id);
  112. #endif // CONFIG_PM_ENABLE
  113. #endif // SOC_RMT_SUPPORT_APB
  114. break;
  115. #if SOC_RMT_SUPPORT_AHB
  116. case RMT_CLK_SRC_AHB:
  117. // TODO: decide which kind of PM lock we should use for such clock
  118. periph_src_clk_hz = 48 * 1000 * 1000;
  119. break;
  120. #endif // SOC_RMT_SUPPORT_AHB
  121. #if SOC_RMT_SUPPORT_XTAL
  122. case RMT_CLK_SRC_XTAL:
  123. periph_src_clk_hz = esp_clk_xtal_freq();
  124. break;
  125. #endif // SOC_RMT_SUPPORT_XTAL
  126. #if SOC_RMT_SUPPORT_REF_TICK
  127. case RMT_CLK_SRC_REF_TICK:
  128. periph_src_clk_hz = REF_CLK_FREQ;
  129. break;
  130. #endif // SOC_RMT_SUPPORT_REF_TICK
  131. #if SOC_RMT_SUPPORT_RC_FAST
  132. case RMT_CLK_SRC_RC_FAST:
  133. periph_rtc_dig_clk8m_enable();
  134. periph_src_clk_hz = periph_rtc_dig_clk8m_get_freq();
  135. break;
  136. #endif // SOC_RMT_SUPPORT_RC_FAST
  137. default:
  138. ESP_RETURN_ON_FALSE(false, ESP_ERR_NOT_SUPPORTED, TAG, "clock source %d is not supported", clk_src);
  139. break;
  140. }
  141. // no division for group clock source, to achieve highest resolution
  142. rmt_ll_set_group_clock_src(group->hal.regs, channel_id, clk_src, 1, 1, 0);
  143. group->resolution_hz = periph_src_clk_hz;
  144. ESP_LOGD(TAG, "group clock resolution:%"PRIu32, group->resolution_hz);
  145. return ret;
  146. }
  147. esp_err_t rmt_apply_carrier(rmt_channel_handle_t channel, const rmt_carrier_config_t *config)
  148. {
  149. // specially, we allow config to be NULL, means to disable the carrier submodule
  150. ESP_RETURN_ON_FALSE(channel, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
  151. return channel->set_carrier_action(channel, config);
  152. }
  153. esp_err_t rmt_del_channel(rmt_channel_handle_t channel)
  154. {
  155. ESP_RETURN_ON_FALSE(channel, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
  156. ESP_RETURN_ON_FALSE(channel->fsm == RMT_FSM_INIT, ESP_ERR_INVALID_STATE, TAG, "channel not in init state");
  157. gpio_reset_pin(channel->gpio_num);
  158. return channel->del(channel);
  159. }
  160. esp_err_t rmt_enable(rmt_channel_handle_t channel)
  161. {
  162. ESP_RETURN_ON_FALSE(channel, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
  163. ESP_RETURN_ON_FALSE(channel->fsm == RMT_FSM_INIT, ESP_ERR_INVALID_STATE, TAG, "channel not in init state");
  164. return channel->enable(channel);
  165. }
  166. esp_err_t rmt_disable(rmt_channel_handle_t channel)
  167. {
  168. ESP_RETURN_ON_FALSE(channel, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
  169. ESP_RETURN_ON_FALSE(channel->fsm == RMT_FSM_ENABLE, ESP_ERR_INVALID_STATE, TAG, "channel not in enable state");
  170. return channel->disable(channel);
  171. }