rmt.h 28 KB

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