1 // Copyright 2015-2019 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 #pragma once
16 
17 #ifdef __cplusplus
18 extern "C" {
19 #endif
20 
21 #include <stdbool.h>
22 #include <stdint.h>
23 #include "esp_err.h"
24 #include "soc/soc_caps.h"
25 #include "driver/gpio.h"
26 #include "freertos/FreeRTOS.h"
27 #include "freertos/ringbuf.h"
28 #include "soc/rmt_struct.h"
29 #include "hal/rmt_types.h"
30 
31 #define RMT_CHANNEL_FLAGS_AWARE_DFS (1 << 0) /*!< Channel can work during APB clock scaling */
32 
33 /** @cond */
34 #define RMT_CHANNEL_FLAGS_ALWAYS_ON RMT_CHANNEL_FLAGS_AWARE_DFS  /*!< Deprecated name, defined here for compatibility */
35 /** @endcond */
36 
37 /**
38  * @brief Define memory space of each RMT channel (in words = 4 bytes)
39  *
40  */
41 #define RMT_MEM_ITEM_NUM SOC_RMT_CHANNEL_MEM_WORDS
42 
43 /**
44 * @brief Data struct of RMT TX configure parameters
45 */
46 typedef struct {
47     uint32_t carrier_freq_hz;          /*!< RMT carrier frequency */
48     rmt_carrier_level_t carrier_level; /*!< Level of the RMT output, when the carrier is applied */
49     rmt_idle_level_t idle_level;       /*!< RMT idle level */
50     uint8_t carrier_duty_percent;      /*!< RMT carrier duty (%) */
51 #if SOC_RMT_SUPPORT_TX_LOOP_COUNT
52     uint32_t loop_count;               /*!< Maximum loop count */
53 #endif
54     bool carrier_en;                   /*!< RMT carrier enable */
55     bool loop_en;                      /*!< Enable sending RMT items in a loop */
56     bool idle_output_en;               /*!< RMT idle level output enable */
57 } rmt_tx_config_t;
58 
59 /**
60 * @brief Data struct of RMT RX configure parameters
61 */
62 typedef struct {
63     uint16_t idle_threshold;     /*!< RMT RX idle threshold */
64     uint8_t filter_ticks_thresh; /*!< RMT filter tick number */
65     bool filter_en;              /*!< RMT receiver filter enable */
66 #if SOC_RMT_SUPPORT_RX_DEMODULATION
67     bool rm_carrier;                   /*!< RMT receiver remove carrier enable */
68     uint32_t carrier_freq_hz;          /*!< RMT carrier frequency */
69     uint8_t carrier_duty_percent;      /*!< RMT carrier duty (%) */
70     rmt_carrier_level_t carrier_level; /*!< The level to remove the carrier */
71 #endif
72 } rmt_rx_config_t;
73 
74 /**
75 * @brief Data struct of RMT configure parameters
76 */
77 typedef struct {
78     rmt_mode_t rmt_mode;   /*!< RMT mode: transmitter or receiver */
79     rmt_channel_t channel; /*!< RMT channel */
80     gpio_num_t gpio_num;   /*!< RMT GPIO number */
81     uint8_t clk_div;       /*!< RMT channel counter divider */
82     uint8_t mem_block_num; /*!< RMT memory block number */
83     uint32_t flags;        /*!< RMT channel extra configurations, OR'd with RMT_CHANNEL_FLAGS_[*] */
84     union {
85         rmt_tx_config_t tx_config; /*!< RMT TX parameter */
86         rmt_rx_config_t rx_config; /*!< RMT RX parameter */
87     };
88 } rmt_config_t;
89 
90 /**
91  * @brief Default configuration for Tx channel
92  *
93  */
94 #define RMT_DEFAULT_CONFIG_TX(gpio, channel_id)      \
95     {                                                \
96         .rmt_mode = RMT_MODE_TX,                     \
97         .channel = channel_id,                       \
98         .gpio_num = gpio,                            \
99         .clk_div = 80,                               \
100         .mem_block_num = 1,                          \
101         .flags = 0,                                  \
102         .tx_config = {                               \
103             .carrier_freq_hz = 38000,                \
104             .carrier_level = RMT_CARRIER_LEVEL_HIGH, \
105             .idle_level = RMT_IDLE_LEVEL_LOW,        \
106             .carrier_duty_percent = 33,              \
107             .carrier_en = false,                     \
108             .loop_en = false,                        \
109             .idle_output_en = true,                  \
110         }                                            \
111     }
112 
113 /**
114  * @brief Default configuration for RX channel
115  *
116  */
117 #define RMT_DEFAULT_CONFIG_RX(gpio, channel_id) \
118     {                                           \
119         .rmt_mode = RMT_MODE_RX,                \
120         .channel = channel_id,                  \
121         .gpio_num = gpio,                       \
122         .clk_div = 80,                          \
123         .mem_block_num = 1,                     \
124         .flags = 0,                             \
125         .rx_config = {                          \
126             .idle_threshold = 12000,            \
127             .filter_ticks_thresh = 100,         \
128             .filter_en = true,                  \
129         }                                       \
130     }
131 
132 /**
133 * @brief RMT interrupt handle
134 *
135 */
136 typedef intr_handle_t rmt_isr_handle_t;
137 
138 /**
139 * @brief Type of RMT Tx End callback function
140 *
141 */
142 typedef void (*rmt_tx_end_fn_t)(rmt_channel_t channel, void *arg);
143 
144 /**
145 * @brief Structure encapsulating a RMT TX end callback
146 */
147 typedef struct {
148     rmt_tx_end_fn_t function; /*!< Function which is called on RMT TX end */
149     void *arg;                /*!< Optional argument passed to function */
150 } rmt_tx_end_callback_t;
151 
152 /**
153 * @brief User callback function to convert uint8_t type data to rmt format(rmt_item32_t).
154 *
155 *        This function may be called from an ISR, so, the code should be short and efficient.
156 *
157 * @param  src Pointer to the buffer storing the raw data that needs to be converted to rmt format.
158 * @param[out] dest Pointer to the buffer storing the rmt format data.
159 * @param  src_size The raw data size.
160 * @param  wanted_num The number of rmt format data that wanted to get.
161 * @param[out] translated_size The size of the raw data that has been converted to rmt format,
162 *             it should return 0 if no data is converted in user callback.
163 * @param[out] item_num The number of the rmt format data that actually converted to,
164 *             it can be less than wanted_num if there is not enough raw data, but cannot exceed wanted_num.
165 *             it should return 0 if no data was converted.
166 *
167 * @note
168 *       In fact, item_num should be a multiple of translated_size, e.g. :
169 *       When we convert each byte of uint8_t type data to rmt format data,
170 *       the relation between item_num and translated_size should be `item_num = translated_size*8`.
171 */
172 typedef void (*sample_to_rmt_t)(const void *src, rmt_item32_t *dest, size_t src_size, size_t wanted_num, size_t *translated_size, size_t *item_num);
173 
174 /**
175 * @brief Set RMT clock divider, channel clock is divided from source clock.
176 *
177 * @param channel RMT channel
178 * @param div_cnt RMT counter clock divider
179 *
180 * @return
181 *     - ESP_ERR_INVALID_ARG Parameter error
182 *     - ESP_OK Success
183 */
184 esp_err_t rmt_set_clk_div(rmt_channel_t channel, uint8_t div_cnt);
185 
186 /**
187 * @brief Get RMT clock divider, channel clock is divided from source clock.
188 *
189 * @param channel RMT channel
190 * @param div_cnt pointer to accept RMT counter divider
191 *
192 * @return
193 *     - ESP_ERR_INVALID_ARG Parameter error
194 *     - ESP_OK Success
195 */
196 esp_err_t rmt_get_clk_div(rmt_channel_t channel, uint8_t *div_cnt);
197 
198 /**
199 * @brief Set RMT RX idle threshold value
200 *
201 *        In receive mode, when no edge is detected on the input signal
202 *        for longer than idle_thres channel clock cycles,
203 *        the receive process is finished.
204 *
205 * @param channel RMT channel
206 * @param thresh RMT RX idle threshold
207 *
208 * @return
209 *     - ESP_ERR_INVALID_ARG Parameter error
210 *     - ESP_OK Success
211 */
212 esp_err_t rmt_set_rx_idle_thresh(rmt_channel_t channel, uint16_t thresh);
213 
214 /**
215 * @brief Get RMT idle threshold value.
216 *
217 *        In receive mode, when no edge is detected on the input signal
218 *        for longer than idle_thres channel clock cycles,
219 *        the receive process is finished.
220 *
221 * @param channel RMT channel
222 * @param thresh pointer to accept RMT RX idle threshold value
223 *
224 * @return
225 *     - ESP_ERR_INVALID_ARG Parameter error
226 *     - ESP_OK Success
227 */
228 esp_err_t rmt_get_rx_idle_thresh(rmt_channel_t channel, uint16_t *thresh);
229 
230 /**
231 * @brief Set RMT memory block number for RMT channel
232 *
233 *        This function is used to configure the amount of memory blocks allocated to channel n
234 *        The 8 channels share a 512x32-bit RAM block which can be read and written
235 *        by the processor cores over the APB bus, as well as read by the transmitters
236 *        and written by the receivers.
237 *
238 *        The RAM address range for channel n is start_addr_CHn to end_addr_CHn, which are defined by:
239 *        Memory block start address is RMT_CHANNEL_MEM(n) (in soc/rmt_reg.h),
240 *        that is, start_addr_chn = RMT base address + 0x800 + 64 ∗ 4 ∗ n, and
241 *        end_addr_chn = RMT base address + 0x800 +  64 ∗ 4 ∗ n + 64 ∗ 4 ∗ RMT_MEM_SIZE_CHn mod 512 ∗ 4
242 *
243 *        @note
244 *        If memory block number of one channel is set to a value greater than 1, this channel will occupy the memory
245 *        block of the next channel.
246 *        Channel 0 can use at most 8 blocks of memory, accordingly channel 7 can only use one memory block.
247 *
248 * @param channel RMT channel
249 * @param rmt_mem_num RMT RX memory block number, one block has 64 * 32 bits.
250 *
251 * @return
252 *     - ESP_ERR_INVALID_ARG Parameter error
253 *     - ESP_OK Success
254 */
255 esp_err_t rmt_set_mem_block_num(rmt_channel_t channel, uint8_t rmt_mem_num);
256 
257 /**
258 * @brief Get RMT memory block number
259 *
260 * @param channel RMT channel
261 * @param rmt_mem_num Pointer to accept RMT RX memory block number
262 *
263 * @return
264 *     - ESP_ERR_INVALID_ARG Parameter error
265 *     - ESP_OK Success
266 */
267 esp_err_t rmt_get_mem_block_num(rmt_channel_t channel, uint8_t *rmt_mem_num);
268 
269 /**
270 * @brief Configure RMT carrier for TX signal.
271 *
272 *        Set different values for carrier_high and carrier_low to set different frequency of carrier.
273 *        The unit of carrier_high/low is the source clock tick, not the divided channel counter clock.
274 *
275 * @param channel RMT channel
276 * @param carrier_en Whether to enable output carrier.
277 * @param high_level High level duration of carrier
278 * @param low_level Low level duration of carrier.
279 * @param carrier_level Configure the way carrier wave is modulated for channel.
280 *     - 1'b1:transmit on low output level
281 *     - 1'b0:transmit on high output level
282 *
283 * @return
284 *     - ESP_ERR_INVALID_ARG Parameter error
285 *     - ESP_OK Success
286 */
287 esp_err_t rmt_set_tx_carrier(rmt_channel_t channel, bool carrier_en, uint16_t high_level, uint16_t low_level, rmt_carrier_level_t carrier_level);
288 
289 /**
290 * @brief Set RMT memory in low power mode.
291 *
292 *        Reduce power consumed by memory. 1:memory is in low power state.
293 *
294 * @param channel RMT channel
295 * @param pd_en RMT memory low power enable.
296 *
297 * @return
298 *     - ESP_ERR_INVALID_ARG Parameter error
299 *     - ESP_OK Success
300 */
301 esp_err_t rmt_set_mem_pd(rmt_channel_t channel, bool pd_en);
302 
303 /**
304 * @brief Get RMT memory low power mode.
305 *
306 * @param channel RMT channel
307 * @param pd_en Pointer to accept RMT memory low power mode.
308 *
309 * @return
310 *     - ESP_ERR_INVALID_ARG Parameter error
311 *     - ESP_OK Success
312 */
313 esp_err_t rmt_get_mem_pd(rmt_channel_t channel, bool *pd_en);
314 
315 /**
316 * @brief Set RMT start sending data from memory.
317 *
318 * @param channel RMT channel
319 * @param tx_idx_rst Set true to reset memory index for TX.
320 *                   Otherwise, transmitter will continue sending from the last index in memory.
321 *
322 * @return
323 *     - ESP_ERR_INVALID_ARG Parameter error
324 *     - ESP_OK Success
325 */
326 esp_err_t rmt_tx_start(rmt_channel_t channel, bool tx_idx_rst);
327 
328 /**
329 * @brief Set RMT stop sending.
330 *
331 * @param channel RMT channel
332 *
333 * @return
334 *     - ESP_ERR_INVALID_ARG Parameter error
335 *     - ESP_OK Success
336 */
337 esp_err_t rmt_tx_stop(rmt_channel_t channel);
338 
339 /**
340 * @brief Set RMT start receiving data.
341 *
342 * @param channel RMT channel
343 * @param rx_idx_rst Set true to reset memory index for receiver.
344 *                   Otherwise, receiver will continue receiving data to the last index in memory.
345 *
346 * @return
347 *     - ESP_ERR_INVALID_ARG Parameter error
348 *     - ESP_OK Success
349 */
350 esp_err_t rmt_rx_start(rmt_channel_t channel, bool rx_idx_rst);
351 
352 /**
353 * @brief Set RMT stop receiving data.
354 *
355 * @param channel RMT channel
356 *
357 * @return
358 *     - ESP_ERR_INVALID_ARG Parameter error
359 *     - ESP_OK Success
360 */
361 esp_err_t rmt_rx_stop(rmt_channel_t channel);
362 
363 /**
364 * @brief Reset RMT TX memory
365 *
366 * @param channel RMT channel
367 *
368 * @return
369 *     - ESP_ERR_INVALID_ARG Parameter error
370 *     - ESP_OK Success
371 */
372 esp_err_t rmt_tx_memory_reset(rmt_channel_t channel);
373 
374 /**
375 * @brief Reset RMT RX memory
376 *
377 * @param channel RMT channel
378 *
379 * @return
380 *     - ESP_ERR_INVALID_ARG Parameter error
381 *     - ESP_OK Success
382 */
383 esp_err_t rmt_rx_memory_reset(rmt_channel_t channel);
384 
385 /**
386 * @brief Set RMT memory owner.
387 *
388 * @param channel RMT channel
389 * @param owner To set when the transmitter or receiver can process the memory of channel.
390 *
391 * @return
392 *     - ESP_ERR_INVALID_ARG Parameter error
393 *     - ESP_OK Success
394 */
395 esp_err_t rmt_set_memory_owner(rmt_channel_t channel, rmt_mem_owner_t owner);
396 
397 /**
398 * @brief Get RMT memory owner.
399 *
400 * @param channel RMT channel
401 * @param owner Pointer to get memory owner.
402 *
403 * @return
404 *     - ESP_ERR_INVALID_ARG Parameter error
405 *     - ESP_OK Success
406 */
407 esp_err_t rmt_get_memory_owner(rmt_channel_t channel, rmt_mem_owner_t *owner);
408 
409 /**
410 * @brief Set RMT tx loop mode.
411 *
412 * @param channel RMT channel
413 * @param loop_en Enable RMT transmitter loop sending mode.
414 *                If set true, transmitter will continue sending from the first data
415 *                to the last data in channel over and over again in a loop.
416 *
417 * @return
418 *     - ESP_ERR_INVALID_ARG Parameter error
419 *     - ESP_OK Success
420 */
421 esp_err_t rmt_set_tx_loop_mode(rmt_channel_t channel, bool loop_en);
422 
423 /**
424 * @brief Get RMT tx loop mode.
425 *
426 * @param channel RMT channel
427 * @param loop_en Pointer to accept RMT transmitter loop sending mode.
428 *
429 * @return
430 *     - ESP_ERR_INVALID_ARG Parameter error
431 *     - ESP_OK Success
432 */
433 esp_err_t rmt_get_tx_loop_mode(rmt_channel_t channel, bool *loop_en);
434 
435 /**
436 * @brief Set RMT RX filter.
437 *
438 *        In receive mode, channel will ignore input pulse when the pulse width is smaller than threshold.
439 *        Counted in source clock, not divided counter clock.
440 *
441 * @param channel RMT channel
442 * @param rx_filter_en To enable RMT receiver filter.
443 * @param thresh Threshold of pulse width for receiver.
444 *
445 * @return
446 *     - ESP_ERR_INVALID_ARG Parameter error
447 *     - ESP_OK Success
448 */
449 esp_err_t rmt_set_rx_filter(rmt_channel_t channel, bool rx_filter_en, uint8_t thresh);
450 
451 /**
452 * @brief Set RMT source clock
453 *
454 *        RMT module has two clock sources:
455 *        1. APB clock which is 80Mhz
456 *        2. REF tick clock, which would be 1Mhz (not supported in this version).
457 *
458 * @param channel RMT channel
459 * @param base_clk To choose source clock for RMT module.
460 *
461 * @return
462 *     - ESP_ERR_INVALID_ARG Parameter error
463 *     - ESP_OK Success
464 */
465 esp_err_t rmt_set_source_clk(rmt_channel_t channel, rmt_source_clk_t base_clk);
466 
467 /**
468 * @brief Get RMT source clock
469 *
470 *        RMT module has two clock sources:
471 *        1. APB clock which is 80Mhz
472 *        2. REF tick clock, which would be 1Mhz (not supported in this version).
473 *
474 * @param channel RMT channel
475 * @param src_clk Pointer to accept source clock for RMT module.
476 *
477 * @return
478 *     - ESP_ERR_INVALID_ARG Parameter error
479 *     - ESP_OK Success
480 */
481 esp_err_t rmt_get_source_clk(rmt_channel_t channel, rmt_source_clk_t *src_clk);
482 
483 /**
484 * @brief Set RMT idle output level for transmitter
485 *
486 * @param channel RMT channel
487 * @param idle_out_en To enable idle level output.
488 * @param level To set the output signal's level for channel in idle state.
489 *
490 * @return
491 *     - ESP_ERR_INVALID_ARG Parameter error
492 *     - ESP_OK Success
493 */
494 esp_err_t rmt_set_idle_level(rmt_channel_t channel, bool idle_out_en, rmt_idle_level_t level);
495 
496 /**
497 * @brief Get RMT idle output level for transmitter
498 *
499 * @param channel RMT channel
500 * @param idle_out_en Pointer to accept value of enable idle.
501 * @param level Pointer to accept value of output signal's level in idle state for specified channel.
502 *
503 * @return
504 *     - ESP_ERR_INVALID_ARG Parameter error
505 *     - ESP_OK Success
506 */
507 esp_err_t rmt_get_idle_level(rmt_channel_t channel, bool *idle_out_en, rmt_idle_level_t *level);
508 
509 /**
510 * @brief Get RMT status
511 *
512 * @param channel RMT channel
513 * @param status Pointer to accept channel status.
514 *        Please refer to RMT_CHnSTATUS_REG(n=0~7) in `rmt_reg.h` for more details of each field.
515 *
516 * @return
517 *     - ESP_ERR_INVALID_ARG Parameter error
518 *     - ESP_OK Success
519 */
520 esp_err_t rmt_get_status(rmt_channel_t channel, uint32_t *status);
521 
522 /**
523 * @brief Set RMT RX interrupt enable
524 *
525 * @param channel RMT channel
526 * @param en enable or disable RX interrupt.
527 *
528 * @return
529 *     - ESP_ERR_INVALID_ARG Parameter error
530 *     - ESP_OK Success
531 */
532 esp_err_t rmt_set_rx_intr_en(rmt_channel_t channel, bool en);
533 
534 /**
535 * @brief Set RMT RX error interrupt enable
536 *
537 * @param channel RMT channel
538 * @param en enable or disable RX err interrupt.
539 *
540 * @return
541 *     - ESP_ERR_INVALID_ARG Parameter error
542 *     - ESP_OK Success
543 */
544 esp_err_t rmt_set_err_intr_en(rmt_channel_t channel, bool en);
545 
546 /**
547 * @brief Set RMT TX interrupt enable
548 *
549 * @param channel RMT channel
550 * @param en enable or disable TX interrupt.
551 *
552 * @return
553 *     - ESP_ERR_INVALID_ARG Parameter error
554 *     - ESP_OK Success
555 */
556 esp_err_t rmt_set_tx_intr_en(rmt_channel_t channel, bool en);
557 
558 /**
559 * @brief Set RMT TX threshold event interrupt enable
560 *
561 * An interrupt will be triggered when the number of transmitted items reaches the threshold value
562 *
563 * @param channel RMT channel
564 * @param en enable or disable TX event interrupt.
565 * @param evt_thresh RMT event interrupt threshold value
566 *
567 * @return
568 *     - ESP_ERR_INVALID_ARG Parameter error
569 *     - ESP_OK Success
570 */
571 esp_err_t rmt_set_tx_thr_intr_en(rmt_channel_t channel, bool en, uint16_t evt_thresh);
572 
573 /**
574 * @brief Set RMT pin
575 *
576 * @param channel RMT channel
577 * @param mode TX or RX mode for RMT
578 * @param gpio_num GPIO number to transmit or receive the signal.
579 *
580 * @return
581 *     - ESP_ERR_INVALID_ARG Parameter error
582 *     - ESP_OK Success
583 */
584 esp_err_t rmt_set_pin(rmt_channel_t channel, rmt_mode_t mode, gpio_num_t gpio_num);
585 
586 /**
587 * @brief Configure RMT parameters
588 *
589 * @param rmt_param RMT parameter struct
590 *
591 * @return
592 *     - ESP_ERR_INVALID_ARG Parameter error
593 *     - ESP_OK Success
594 */
595 esp_err_t rmt_config(const rmt_config_t *rmt_param);
596 
597 /**
598 * @brief Register RMT interrupt handler, the handler is an ISR.
599 *
600 *        The handler will be attached to the same CPU core that this function is running on.
601 *
602 * @note  If you already called rmt_driver_install to use system RMT driver,
603 *        please do not register ISR handler again.
604 *
605 * @param fn Interrupt handler function.
606 * @param arg Parameter for the handler function
607 * @param intr_alloc_flags Flags used to allocate the interrupt. One or multiple (ORred)
608 *        ESP_INTR_FLAG_* values. See esp_intr_alloc.h for more info.
609 * @param handle If non-zero, a handle to later clean up the ISR gets stored here.
610 *
611 * @return
612 *     - ESP_OK Success
613 *     - ESP_ERR_INVALID_ARG Function pointer error.
614 *     - ESP_FAIL System driver installed, can not register ISR handler for RMT
615 */
616 esp_err_t rmt_isr_register(void (*fn)(void *), void *arg, int intr_alloc_flags, rmt_isr_handle_t *handle);
617 
618 /**
619 * @brief   Deregister previously registered RMT interrupt handler
620 *
621 * @param handle Handle obtained from rmt_isr_register
622 *
623 * @return
624 *     - ESP_OK Success
625 *     - ESP_ERR_INVALID_ARG Handle invalid
626 */
627 esp_err_t rmt_isr_deregister(rmt_isr_handle_t handle);
628 
629 /**
630 * @brief Fill memory data of channel with given RMT items.
631 *
632 * @param channel RMT channel
633 * @param item Pointer of items.
634 * @param item_num RMT sending items number.
635 * @param mem_offset Index offset of memory.
636 *
637 * @return
638 *     - ESP_ERR_INVALID_ARG Parameter error
639 *     - ESP_OK Success
640 */
641 esp_err_t rmt_fill_tx_items(rmt_channel_t channel, const rmt_item32_t *item, uint16_t item_num, uint16_t mem_offset);
642 
643 /**
644 * @brief Initialize RMT driver
645 *
646 * @param channel RMT channel
647 * @param rx_buf_size Size of RMT RX ringbuffer. Can be 0 if the RX ringbuffer is not used.
648 * @param intr_alloc_flags Flags for the RMT driver interrupt handler. Pass 0 for default flags. See esp_intr_alloc.h for details.
649 *        If ESP_INTR_FLAG_IRAM is used, please do not use the memory allocated from psram when calling rmt_write_items.
650 *
651 * @return
652 *     - ESP_ERR_INVALID_STATE Driver is already installed, call rmt_driver_uninstall first.
653 *     - ESP_ERR_NO_MEM Memory allocation failure
654 *     - ESP_ERR_INVALID_ARG Parameter error
655 *     - ESP_OK Success
656 */
657 esp_err_t rmt_driver_install(rmt_channel_t channel, size_t rx_buf_size, int intr_alloc_flags);
658 
659 /**
660 * @brief Uninstall RMT driver.
661 *
662 * @param channel RMT channel
663 *
664 * @return
665 *     - ESP_ERR_INVALID_ARG Parameter error
666 *     - ESP_OK Success
667 */
668 esp_err_t rmt_driver_uninstall(rmt_channel_t channel);
669 
670 /**
671 * @brief Get the current status of eight channels.
672 *
673 * @note Do not call this function if it is possible that `rmt_driver_uninstall` will be called at the same time.
674 *
675 * @param[out] channel_status store the current status of each channel
676 *
677 * @return
678 *     - ESP_ERR_INVALID_ARG Parameter is NULL
679 *     - ESP_OK Success
680 */
681 esp_err_t rmt_get_channel_status(rmt_channel_status_result_t *channel_status);
682 
683 /**
684 * @brief Get speed of channel's internal counter clock.
685 *
686 * @param channel RMT channel
687 * @param[out] clock_hz counter clock speed, in hz
688 *
689 * @return
690 *     - ESP_ERR_INVALID_ARG Parameter is NULL
691 *     - ESP_OK Success
692 */
693 esp_err_t rmt_get_counter_clock(rmt_channel_t channel, uint32_t *clock_hz);
694 
695 /**
696 * @brief RMT send waveform from rmt_item array.
697 *
698 *        This API allows user to send waveform with any length.
699 *
700 * @param channel RMT channel
701 * @param rmt_item head point of RMT items array.
702 *        If ESP_INTR_FLAG_IRAM is used, please do not use the memory allocated from psram when calling rmt_write_items.
703 * @param item_num RMT data item number.
704 * @param wait_tx_done
705 *        - If set 1, it will block the task and wait for sending done.
706 *        - If set 0, it will not wait and return immediately.
707 *
708 *         @note
709 *         This function will not copy data, instead, it will point to the original items,
710 *         and send the waveform items.
711 *         If wait_tx_done is set to true, this function will block and will not return until
712 *         all items have been sent out.
713 *         If wait_tx_done is set to false, this function will return immediately, and the driver
714 *         interrupt will continue sending the items. We must make sure the item data will not be
715 *         damaged when the driver is still sending items in driver interrupt.
716 *
717 * @return
718 *     - ESP_ERR_INVALID_ARG Parameter error
719 *     - ESP_OK Success
720 */
721 esp_err_t rmt_write_items(rmt_channel_t channel, const rmt_item32_t *rmt_item, int item_num, bool wait_tx_done);
722 
723 /**
724 * @brief Wait RMT TX finished.
725 *
726 * @param channel RMT channel
727 * @param wait_time Maximum time in ticks to wait for transmission to be complete.  If set 0, return immediately with ESP_ERR_TIMEOUT if TX is busy (polling).
728 *
729 * @return
730 *     - ESP_OK RMT Tx done successfully
731 *     - ESP_ERR_TIMEOUT Exceeded the 'wait_time' given
732 *     - ESP_ERR_INVALID_ARG Parameter error
733 *     - ESP_FAIL Driver not installed
734 */
735 esp_err_t rmt_wait_tx_done(rmt_channel_t channel, TickType_t wait_time);
736 
737 /**
738 * @brief Get ringbuffer from RMT.
739 *
740 *        Users can get the RMT RX ringbuffer handle, and process the RX data.
741 *
742 * @param channel RMT channel
743 * @param buf_handle Pointer to buffer handle to accept RX ringbuffer handle.
744 *
745 * @return
746 *     - ESP_ERR_INVALID_ARG Parameter error
747 *     - ESP_OK Success
748 */
749 esp_err_t rmt_get_ringbuf_handle(rmt_channel_t channel, RingbufHandle_t *buf_handle);
750 
751 /**
752 * @brief Init rmt translator and register user callback.
753 *        The callback will convert the raw data that needs to be sent to rmt format.
754 *        If a channel is initialized more than once, tha user callback will be replaced by the later.
755 *
756 * @param channel RMT channel .
757 * @param fn Point to the data conversion function.
758 *
759 * @return
760 *     - ESP_FAIL Init fail.
761 *     - ESP_OK Init success.
762 */
763 esp_err_t rmt_translator_init(rmt_channel_t channel, sample_to_rmt_t fn);
764 
765 /**
766 * @brief Set user context for the translator of specific channel
767 *
768 * @param channel RMT channel number
769 * @param context User context
770 *
771 * @return
772 *     - ESP_FAIL Set context fail
773 *     - ESP_OK Set context success
774 */
775 esp_err_t rmt_translator_set_context(rmt_channel_t channel, void *context);
776 
777 /**
778 * @brief Get the user context set by 'rmt_translator_set_context'
779 *
780 * @note This API must be invoked in the RMT translator callback function,
781 *       and the first argument must be the actual parameter 'item_num' you got in that callback function.
782 *
783 * @param item_num Address of the memory which contains the number of translated items (It's from driver's internal memroy)
784 * @param context Returned User context
785 *
786 * @return
787 *     - ESP_FAIL Get context fail
788 *     - ESP_OK Get context success
789 */
790 esp_err_t rmt_translator_get_context(const size_t *item_num, void **context);
791 
792 /**
793 * @brief Translate uint8_t type of data into rmt format and send it out.
794 *        Requires rmt_translator_init to init the translator first.
795 *
796 * @param channel RMT channel .
797 * @param src Pointer to the raw data.
798 * @param src_size The size of the raw data.
799 * @param wait_tx_done Set true to wait all data send done.
800 *
801 * @return
802 *     - ESP_FAIL Send fail
803 *     - ESP_OK Send success
804 */
805 esp_err_t rmt_write_sample(rmt_channel_t channel, const uint8_t *src, size_t src_size, bool wait_tx_done);
806 
807 /**
808 * @brief Registers a callback that will be called when transmission ends.
809 *
810 *        Called by rmt_driver_isr_default in interrupt context.
811 *
812 * @note Requires rmt_driver_install to install the default ISR handler.
813 *
814 * @param function Function to be called from the default interrupt handler or NULL.
815 * @param arg Argument which will be provided to the callback when it is called.
816 *
817 * @return the previous callback settings (members will be set to NULL if there was none)
818 */
819 rmt_tx_end_callback_t rmt_register_tx_end_callback(rmt_tx_end_fn_t function, void *arg);
820 
821 #if SOC_RMT_SUPPORT_RX_PINGPONG
822 /**
823 * @brief Set RMT RX threshold event interrupt enable
824 *
825 * An interrupt will be triggered when the number of received items reaches the threshold value
826 *
827 * @param channel RMT channel
828 * @param en enable or disable RX event interrupt.
829 * @param evt_thresh RMT event interrupt threshold value
830 *
831 * @return
832 *     - ESP_ERR_INVALID_ARG Parameter error
833 *     - ESP_OK Success
834 */
835 esp_err_t rmt_set_rx_thr_intr_en(rmt_channel_t channel, bool en, uint16_t evt_thresh);
836 #endif
837 
838 #if SOC_RMT_SUPPORT_TX_GROUP
839 /**
840 * @brief Add channel into a group (channels in the same group will transmit simultaneously)
841 *
842 * @param channel RMT channel
843 *
844 * @return
845 *     - ESP_ERR_INVALID_ARG Parameter error
846 *     - ESP_OK Success
847 */
848 esp_err_t rmt_add_channel_to_group(rmt_channel_t channel);
849 
850 /**
851 * @brief Remove channel out of a group
852 *
853 * @param channel RMT channel
854 *
855 * @return
856 *     - ESP_ERR_INVALID_ARG Parameter error
857 *     - ESP_OK Success
858 */
859 esp_err_t rmt_remove_channel_from_group(rmt_channel_t channel);
860 #endif
861 
862 #if SOC_RMT_SUPPORT_TX_LOOP_COUNT
863 /**
864  * @brief Set loop count for RMT TX channel
865  *
866  * @param channel RMT channel
867  * @param count loop count
868  * @return
869  *      - ESP_ERR_INVALID_ARG Parameter error
870  *      - ESP_OK Success
871  */
872 esp_err_t rmt_set_tx_loop_count(rmt_channel_t channel, uint32_t count);
873 #endif
874 
875 /**
876 * @brief Reset RMT TX/RX memory index.
877 *
878 * @param channel RMT channel
879 *
880 * @return
881 *     - ESP_ERR_INVALID_ARG Parameter error
882 *     - ESP_OK Success
883 */
884 esp_err_t rmt_memory_rw_rst(rmt_channel_t channel)
885 __attribute__((deprecated("use rmt_tx_memory_reset or rmt_rx_memory_reset instead")));
886 
887 /**
888 * @brief Set mask value to RMT interrupt enable register.
889 *
890 * @param mask Bit mask to set to the register
891 *
892 */
893 void rmt_set_intr_enable_mask(uint32_t mask)
894 __attribute__((deprecated("interrupt should be handled by driver")));
895 
896 /**
897 * @brief Clear mask value to RMT interrupt enable register.
898 *
899 * @param mask Bit mask to clear the register
900 *
901 */
902 void rmt_clr_intr_enable_mask(uint32_t mask)
903 __attribute__((deprecated("interrupt should be handled by driver")));
904 
905 #ifdef __cplusplus
906 }
907 #endif
908