rmt_private.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. #include <stdatomic.h>
  8. #include "sdkconfig.h"
  9. #include "freertos/FreeRTOS.h"
  10. #include "freertos/task.h"
  11. #include "freertos/queue.h"
  12. #include "freertos/idf_additions.h"
  13. #include "esp_err.h"
  14. #include "soc/soc_caps.h"
  15. #include "soc/gdma_channel.h"
  16. #include "hal/rmt_types.h"
  17. #include "hal/rmt_hal.h"
  18. #include "hal/dma_types.h"
  19. #include "hal/cache_ll.h"
  20. #include "esp_intr_alloc.h"
  21. #include "esp_heap_caps.h"
  22. #include "esp_pm.h"
  23. #include "esp_attr.h"
  24. #include "esp_private/gdma.h"
  25. #include "driver/rmt_common.h"
  26. #ifdef __cplusplus
  27. extern "C" {
  28. #endif
  29. #if CONFIG_RMT_ISR_IRAM_SAFE || CONFIG_RMT_RECV_FUNC_IN_IRAM
  30. #define RMT_MEM_ALLOC_CAPS (MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT)
  31. #else
  32. #define RMT_MEM_ALLOC_CAPS MALLOC_CAP_DEFAULT
  33. #endif
  34. // RMT driver object is per-channel, the interrupt source is shared between channels
  35. #if CONFIG_RMT_ISR_IRAM_SAFE
  36. #define RMT_INTR_ALLOC_FLAG (ESP_INTR_FLAG_SHARED | ESP_INTR_FLAG_IRAM)
  37. #else
  38. #define RMT_INTR_ALLOC_FLAG (ESP_INTR_FLAG_SHARED)
  39. #endif
  40. // Hopefully the channel offset won't change in other targets
  41. #define RMT_TX_CHANNEL_OFFSET_IN_GROUP 0
  42. #define RMT_RX_CHANNEL_OFFSET_IN_GROUP (SOC_RMT_CHANNELS_PER_GROUP - SOC_RMT_TX_CANDIDATES_PER_GROUP)
  43. #define RMT_ALLOW_INTR_PRIORITY_MASK ESP_INTR_FLAG_LOWMED
  44. // DMA buffer size must align to `rmt_symbol_word_t`
  45. #define RMT_DMA_DESC_BUF_MAX_SIZE (DMA_DESCRIPTOR_BUFFER_MAX_SIZE & ~(sizeof(rmt_symbol_word_t) - 1))
  46. #define RMT_DMA_NODES_PING_PONG 2 // two nodes ping-pong
  47. #define RMT_PM_LOCK_NAME_LEN_MAX 16
  48. #define RMT_GROUP_INTR_PRIORITY_UNINITIALIZED (-1)
  49. // RMT is a slow peripheral, it only supports AHB-GDMA
  50. #define RMT_DMA_DESC_ALIGN 4
  51. typedef dma_descriptor_align4_t rmt_dma_descriptor_t;
  52. #ifdef CACHE_LL_L2MEM_NON_CACHE_ADDR
  53. #define RMT_GET_NON_CACHE_ADDR(addr) ((addr) ? CACHE_LL_L2MEM_NON_CACHE_ADDR(addr) : 0)
  54. #else
  55. #define RMT_GET_NON_CACHE_ADDR(addr) (addr)
  56. #endif
  57. typedef struct {
  58. struct {
  59. rmt_symbol_word_t symbols[SOC_RMT_MEM_WORDS_PER_CHANNEL];
  60. } channels[SOC_RMT_CHANNELS_PER_GROUP];
  61. } rmt_block_mem_t;
  62. // RMTMEM address is declared in <target>.peripherals.ld
  63. extern rmt_block_mem_t RMTMEM;
  64. typedef enum {
  65. RMT_CHANNEL_DIRECTION_TX,
  66. RMT_CHANNEL_DIRECTION_RX,
  67. } rmt_channel_direction_t;
  68. typedef enum {
  69. RMT_FSM_INIT_WAIT,
  70. RMT_FSM_INIT,
  71. RMT_FSM_ENABLE_WAIT,
  72. RMT_FSM_ENABLE,
  73. RMT_FSM_RUN_WAIT,
  74. RMT_FSM_RUN,
  75. } rmt_fsm_t;
  76. enum {
  77. RMT_TX_QUEUE_READY,
  78. RMT_TX_QUEUE_PROGRESS,
  79. RMT_TX_QUEUE_COMPLETE,
  80. RMT_TX_QUEUE_MAX,
  81. };
  82. typedef struct rmt_group_t rmt_group_t;
  83. typedef struct rmt_channel_t rmt_channel_t;
  84. typedef struct rmt_tx_channel_t rmt_tx_channel_t;
  85. typedef struct rmt_rx_channel_t rmt_rx_channel_t;
  86. typedef struct rmt_sync_manager_t rmt_sync_manager_t;
  87. struct rmt_group_t {
  88. int group_id; // group ID, index from 0
  89. portMUX_TYPE spinlock; // to protect per-group register level concurrent access
  90. rmt_hal_context_t hal; // hal layer for each group
  91. rmt_clock_source_t clk_src; // record the group clock source, group clock is shared by all channels
  92. uint32_t resolution_hz; // resolution of group clock
  93. uint32_t occupy_mask; // a set bit in the mask indicates the channel is not available
  94. rmt_tx_channel_t *tx_channels[SOC_RMT_TX_CANDIDATES_PER_GROUP]; // array of RMT TX channels
  95. rmt_rx_channel_t *rx_channels[SOC_RMT_RX_CANDIDATES_PER_GROUP]; // array of RMT RX channels
  96. rmt_sync_manager_t *sync_manager; // sync manager, this can be extended into an array if there're more sync controllers in one RMT group
  97. int intr_priority; // RMT interrupt priority
  98. };
  99. struct rmt_channel_t {
  100. int channel_id; // channel ID, index from 0
  101. int gpio_num; // GPIO number used by RMT RX channel
  102. uint32_t channel_mask; // mask of the memory blocks that occupied by the channel
  103. size_t mem_block_num; // number of occupied RMT memory blocks
  104. rmt_group_t *group; // which group the channel belongs to
  105. portMUX_TYPE spinlock; // prevent channel resource accessing by user and interrupt concurrently
  106. uint32_t resolution_hz; // channel clock resolution
  107. intr_handle_t intr; // allocated interrupt handle for each channel
  108. _Atomic rmt_fsm_t fsm; // channel life cycle specific FSM
  109. rmt_channel_direction_t direction; // channel direction
  110. rmt_symbol_word_t *hw_mem_base; // base address of RMT channel hardware memory
  111. rmt_symbol_word_t *dma_mem_base; // base address of RMT channel DMA buffer
  112. gdma_channel_handle_t dma_chan; // DMA channel
  113. esp_pm_lock_handle_t pm_lock; // power management lock
  114. #if CONFIG_PM_ENABLE
  115. char pm_lock_name[RMT_PM_LOCK_NAME_LEN_MAX]; // pm lock name
  116. #endif
  117. // RMT channel common interface
  118. // The following IO functions will have per-implementation for TX and RX channel
  119. esp_err_t (*del)(rmt_channel_t *channel);
  120. esp_err_t (*set_carrier_action)(rmt_channel_t *channel, const rmt_carrier_config_t *config);
  121. esp_err_t (*enable)(rmt_channel_t *channel);
  122. esp_err_t (*disable)(rmt_channel_t *channel);
  123. };
  124. typedef struct {
  125. rmt_encoder_handle_t encoder; // encode user payload into RMT symbols
  126. const void *payload; // encoder payload
  127. size_t payload_bytes; // payload size
  128. int loop_count; // transaction can be continued in a loop for specific times
  129. int remain_loop_count; // user required loop count may exceed hardware limitation, the driver will transfer them in batches
  130. size_t transmitted_symbol_num; // track the number of transmitted symbols
  131. struct {
  132. uint32_t eot_level : 1; // Set the output level for the "End Of Transmission"
  133. uint32_t encoding_done: 1; // Indicate whether the encoding has finished (not the encoding of transmission)
  134. } flags;
  135. } rmt_tx_trans_desc_t;
  136. struct rmt_tx_channel_t {
  137. rmt_channel_t base; // channel base class
  138. size_t mem_off; // runtime argument, indicating the next writing position in the RMT hardware memory
  139. size_t mem_end; // runtime argument, indicating the end of current writing region
  140. size_t ping_pong_symbols; // ping-pong size (half of the RMT channel memory)
  141. size_t queue_size; // size of transaction queue
  142. size_t num_trans_inflight; // indicates the number of transactions that are undergoing but not recycled to ready_queue
  143. QueueHandle_t trans_queues[RMT_TX_QUEUE_MAX]; // transaction queues
  144. rmt_tx_trans_desc_t *cur_trans; // points to current transaction
  145. void *user_data; // user context
  146. rmt_tx_done_callback_t on_trans_done; // callback, invoked on trans done
  147. rmt_dma_descriptor_t *dma_nodes; // DMA descriptor nodes
  148. rmt_dma_descriptor_t *dma_nodes_nc; // DMA descriptor nodes accessed in non-cached way
  149. rmt_tx_trans_desc_t trans_desc_pool[]; // transfer descriptor pool
  150. };
  151. typedef struct {
  152. void *buffer; // buffer for saving the received symbols
  153. size_t buffer_size; // size of the buffer, in bytes
  154. size_t received_symbol_num; // track the number of received symbols
  155. size_t copy_dest_off; // tracking offset in the copy destination
  156. } rmt_rx_trans_desc_t;
  157. struct rmt_rx_channel_t {
  158. rmt_channel_t base; // channel base class
  159. size_t mem_off; // starting offset to fetch the symbols in RMT-MEM
  160. size_t ping_pong_symbols; // ping-pong size (half of the RMT channel memory)
  161. rmt_rx_done_callback_t on_recv_done; // callback, invoked on receive done
  162. void *user_data; // user context
  163. rmt_rx_trans_desc_t trans_desc; // transaction description
  164. size_t num_dma_nodes; // number of DMA nodes, determined by how big the memory block that user configures
  165. rmt_dma_descriptor_t *dma_nodes; // DMA link nodes
  166. rmt_dma_descriptor_t *dma_nodes_nc; // DMA descriptor nodes accessed in non-cached way
  167. };
  168. /**
  169. * @brief Acquire RMT group handle
  170. *
  171. * @param group_id Group ID
  172. * @return RMT group handle
  173. */
  174. rmt_group_t *rmt_acquire_group_handle(int group_id);
  175. /**
  176. * @brief Release RMT group handle
  177. *
  178. * @param group RMT group handle, returned from `rmt_acquire_group_handle`
  179. */
  180. void rmt_release_group_handle(rmt_group_t *group);
  181. /**
  182. * @brief Set clock source for RMT peripheral
  183. *
  184. * @param chan RMT channel handle
  185. * @param clk_src Clock source
  186. * @return
  187. * - ESP_OK: Set clock source successfully
  188. * - ESP_ERR_NOT_SUPPORTED: Set clock source failed because the clk_src is not supported
  189. * - ESP_ERR_INVALID_STATE: Set clock source failed because the clk_src is different from other RMT channel
  190. * - ESP_FAIL: Set clock source failed because of other error
  191. */
  192. esp_err_t rmt_select_periph_clock(rmt_channel_handle_t chan, rmt_clock_source_t clk_src);
  193. /**
  194. * @brief Set interrupt priority to RMT group
  195. * @param group RMT group to set interrupt priority to
  196. * @param intr_priority User-specified interrupt priority (in num, not bitmask)
  197. * @return If the priority conflicts
  198. * - true: Interrupt priority conflict with previous specified
  199. * - false: Interrupt priority set successfully
  200. */
  201. bool rmt_set_intr_priority_to_group(rmt_group_t *group, int intr_priority);
  202. /**
  203. * @brief Get isr_flags to be passed to `esp_intr_alloc_intrstatus()` according to `intr_priority` set in RMT group
  204. * @param group RMT group
  205. * @return isr_flags
  206. */
  207. int rmt_get_isr_flags(rmt_group_t *group);
  208. #ifdef __cplusplus
  209. }
  210. #endif