uart.c 86 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <string.h>
  7. #include <sys/param.h>
  8. #include "esp_types.h"
  9. #include "esp_attr.h"
  10. #include "esp_intr_alloc.h"
  11. #include "esp_log.h"
  12. #include "esp_err.h"
  13. #include "esp_check.h"
  14. #include "malloc.h"
  15. #include "freertos/FreeRTOS.h"
  16. #include "freertos/queue.h"
  17. #include "freertos/semphr.h"
  18. #include "freertos/ringbuf.h"
  19. #include "freertos/idf_additions.h"
  20. #include "esp_private/critical_section.h"
  21. #include "hal/uart_hal.h"
  22. #include "hal/gpio_hal.h"
  23. #include "hal/clk_tree_ll.h"
  24. #include "soc/uart_periph.h"
  25. #include "driver/uart.h"
  26. #include "driver/gpio.h"
  27. #include "driver/rtc_io.h"
  28. #include "driver/uart_select.h"
  29. #include "driver/lp_io.h"
  30. #include "esp_private/uart_share_hw_ctrl.h"
  31. #include "esp_private/periph_ctrl.h"
  32. #include "esp_clk_tree.h"
  33. #include "sdkconfig.h"
  34. #include "esp_rom_gpio.h"
  35. #include "clk_ctrl_os.h"
  36. #ifdef CONFIG_UART_ISR_IN_IRAM
  37. #define UART_ISR_ATTR IRAM_ATTR
  38. #define UART_MALLOC_CAPS (MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT)
  39. #else
  40. #define UART_ISR_ATTR
  41. #define UART_MALLOC_CAPS MALLOC_CAP_DEFAULT
  42. #endif
  43. #define XOFF (0x13)
  44. #define XON (0x11)
  45. static const char *UART_TAG = "uart";
  46. #define UART_EMPTY_THRESH_DEFAULT (10)
  47. #define LP_UART_EMPTY_THRESH_DEFAULT (2)
  48. #define UART_FULL_THRESH_DEFAULT (120)
  49. #define LP_UART_FULL_THRESH_DEFAULT (10)
  50. #define UART_TOUT_THRESH_DEFAULT (10)
  51. #define UART_CLKDIV_FRAG_BIT_WIDTH (3)
  52. #define UART_TX_IDLE_NUM_DEFAULT (0)
  53. #define UART_PATTERN_DET_QLEN_DEFAULT (10)
  54. #define UART_MIN_WAKEUP_THRESH (UART_LL_MIN_WAKEUP_THRESH)
  55. #if (SOC_UART_LP_NUM >= 1)
  56. #define UART_THRESHOLD_NUM(uart_num, field_name) ((uart_num < SOC_UART_HP_NUM) ? field_name : LP_##field_name)
  57. #else
  58. #define UART_THRESHOLD_NUM(uart_num, field_name) (field_name)
  59. #endif
  60. #if SOC_UART_SUPPORT_WAKEUP_INT
  61. #define UART_INTR_CONFIG_FLAG ((UART_INTR_RXFIFO_FULL) \
  62. | (UART_INTR_RXFIFO_TOUT) \
  63. | (UART_INTR_RXFIFO_OVF) \
  64. | (UART_INTR_BRK_DET) \
  65. | (UART_INTR_PARITY_ERR)) \
  66. | (UART_INTR_WAKEUP)
  67. #else
  68. #define UART_INTR_CONFIG_FLAG ((UART_INTR_RXFIFO_FULL) \
  69. | (UART_INTR_RXFIFO_TOUT) \
  70. | (UART_INTR_RXFIFO_OVF) \
  71. | (UART_INTR_BRK_DET) \
  72. | (UART_INTR_PARITY_ERR))
  73. #endif
  74. #define UART_ENTER_CRITICAL_SAFE(spinlock) esp_os_enter_critical_safe(spinlock)
  75. #define UART_EXIT_CRITICAL_SAFE(spinlock) esp_os_exit_critical_safe(spinlock)
  76. #define UART_ENTER_CRITICAL_ISR(spinlock) esp_os_enter_critical_isr(spinlock)
  77. #define UART_EXIT_CRITICAL_ISR(spinlock) esp_os_exit_critical_isr(spinlock)
  78. #define UART_ENTER_CRITICAL(spinlock) esp_os_enter_critical(spinlock)
  79. #define UART_EXIT_CRITICAL(spinlock) esp_os_exit_critical(spinlock)
  80. // Check actual UART mode set
  81. #define UART_IS_MODE_SET(uart_number, mode) ((p_uart_obj[uart_number]->uart_mode == mode))
  82. #define UART_CONTEX_INIT_DEF(uart_num) {\
  83. .hal.dev = UART_LL_GET_HW(uart_num),\
  84. INIT_CRIT_SECTION_LOCK_IN_STRUCT(spinlock)\
  85. .hw_enabled = false,\
  86. }
  87. typedef struct {
  88. uart_event_type_t type; /*!< UART TX data type */
  89. struct {
  90. int brk_len;
  91. size_t size;
  92. uint8_t data[0];
  93. } tx_data;
  94. } uart_tx_data_t;
  95. typedef struct {
  96. int wr;
  97. int rd;
  98. int len;
  99. int *data;
  100. } uart_pat_rb_t;
  101. typedef struct {
  102. uart_port_t uart_num; /*!< UART port number*/
  103. int event_queue_size; /*!< UART event queue size*/
  104. intr_handle_t intr_handle; /*!< UART interrupt handle*/
  105. uart_mode_t uart_mode; /*!< UART controller actual mode set by uart_set_mode() */
  106. bool coll_det_flg; /*!< UART collision detection flag */
  107. bool rx_always_timeout_flg; /*!< UART always detect rx timeout flag */
  108. int rx_buffered_len; /*!< UART cached data length */
  109. int rx_buf_size; /*!< RX ring buffer size */
  110. bool rx_buffer_full_flg; /*!< RX ring buffer full flag. */
  111. uint32_t rx_cur_remain; /*!< Data number that waiting to be read out in ring buffer item*/
  112. uint8_t *rx_ptr; /*!< pointer to the current data in ring buffer*/
  113. uint8_t *rx_head_ptr; /*!< pointer to the head of RX item*/
  114. uint8_t *rx_data_buf; /*!< Data buffer to stash FIFO data*/
  115. uint8_t rx_stash_len; /*!< stashed data length.(When using flow control, after reading out FIFO data, if we fail to push to buffer, we can just stash them.) */
  116. uint32_t rx_int_usr_mask; /*!< RX interrupt status. Valid at any time, regardless of RX buffer status. */
  117. uart_pat_rb_t rx_pattern_pos;
  118. int tx_buf_size; /*!< TX ring buffer size */
  119. bool tx_waiting_fifo; /*!< this flag indicates that some task is waiting for FIFO empty interrupt, used to send all data without any data buffer*/
  120. uint8_t *tx_ptr; /*!< TX data pointer to push to FIFO in TX buffer mode*/
  121. uart_tx_data_t *tx_head; /*!< TX data pointer to head of the current buffer in TX ring buffer*/
  122. uint32_t tx_len_tot; /*!< Total length of current item in ring buffer*/
  123. uint32_t tx_len_cur;
  124. uint8_t tx_brk_flg; /*!< Flag to indicate to send a break signal in the end of the item sending procedure */
  125. uint8_t tx_brk_len; /*!< TX break signal cycle length/number */
  126. uint8_t tx_waiting_brk; /*!< Flag to indicate that TX FIFO is ready to send break signal after FIFO is empty, do not push data into TX FIFO right now.*/
  127. uart_select_notif_callback_t uart_select_notif_callback; /*!< Notification about select() events */
  128. QueueHandle_t event_queue; /*!< UART event queue handler*/
  129. RingbufHandle_t rx_ring_buf; /*!< RX ring buffer handler*/
  130. RingbufHandle_t tx_ring_buf; /*!< TX ring buffer handler*/
  131. SemaphoreHandle_t rx_mux; /*!< UART RX data mutex*/
  132. SemaphoreHandle_t tx_mux; /*!< UART TX mutex*/
  133. SemaphoreHandle_t tx_fifo_sem; /*!< UART TX FIFO semaphore*/
  134. SemaphoreHandle_t tx_done_sem; /*!< UART TX done semaphore*/
  135. SemaphoreHandle_t tx_brk_sem; /*!< UART TX send break done semaphore*/
  136. } uart_obj_t;
  137. typedef struct {
  138. uart_hal_context_t hal; /*!< UART hal context*/
  139. DECLARE_CRIT_SECTION_LOCK_IN_STRUCT(spinlock)
  140. bool hw_enabled;
  141. } uart_context_t;
  142. static uart_obj_t *p_uart_obj[UART_NUM_MAX] = {0};
  143. static uart_context_t uart_context[UART_NUM_MAX] = {
  144. UART_CONTEX_INIT_DEF(UART_NUM_0),
  145. UART_CONTEX_INIT_DEF(UART_NUM_1),
  146. #if SOC_UART_HP_NUM > 2
  147. UART_CONTEX_INIT_DEF(UART_NUM_2),
  148. #endif
  149. #if SOC_UART_HP_NUM > 3
  150. UART_CONTEX_INIT_DEF(UART_NUM_3),
  151. #endif
  152. #if SOC_UART_HP_NUM > 4
  153. UART_CONTEX_INIT_DEF(UART_NUM_4),
  154. #endif
  155. #if (SOC_UART_LP_NUM >= 1)
  156. UART_CONTEX_INIT_DEF(LP_UART_NUM_0),
  157. #endif
  158. };
  159. static portMUX_TYPE uart_selectlock = portMUX_INITIALIZER_UNLOCKED;
  160. static void uart_module_enable(uart_port_t uart_num)
  161. {
  162. UART_ENTER_CRITICAL(&(uart_context[uart_num].spinlock));
  163. if (uart_context[uart_num].hw_enabled != true) {
  164. if (uart_num < SOC_UART_HP_NUM) {
  165. HP_UART_BUS_CLK_ATOMIC() {
  166. uart_ll_enable_bus_clock(uart_num, true);
  167. }
  168. if (uart_num != CONFIG_ESP_CONSOLE_UART_NUM) {
  169. HP_UART_BUS_CLK_ATOMIC() {
  170. uart_ll_reset_register(uart_num);
  171. }
  172. }
  173. }
  174. #if (SOC_UART_LP_NUM >= 1)
  175. else {
  176. LP_UART_BUS_CLK_ATOMIC() {
  177. lp_uart_ll_enable_bus_clock(uart_num - SOC_UART_HP_NUM, true);
  178. lp_uart_ll_reset_register(uart_num - SOC_UART_HP_NUM);
  179. }
  180. }
  181. #endif
  182. uart_context[uart_num].hw_enabled = true;
  183. }
  184. UART_EXIT_CRITICAL(&(uart_context[uart_num].spinlock));
  185. }
  186. static void uart_module_disable(uart_port_t uart_num)
  187. {
  188. UART_ENTER_CRITICAL(&(uart_context[uart_num].spinlock));
  189. if (uart_context[uart_num].hw_enabled != false) {
  190. if (uart_num != CONFIG_ESP_CONSOLE_UART_NUM && uart_num < SOC_UART_HP_NUM) {
  191. HP_UART_BUS_CLK_ATOMIC() {
  192. uart_ll_enable_bus_clock(uart_num, false);
  193. }
  194. }
  195. #if (SOC_UART_LP_NUM >= 1)
  196. else if (uart_num >= SOC_UART_HP_NUM) {
  197. LP_UART_BUS_CLK_ATOMIC() {
  198. lp_uart_ll_enable_bus_clock(uart_num - SOC_UART_HP_NUM, false);
  199. }
  200. }
  201. #endif
  202. uart_context[uart_num].hw_enabled = false;
  203. }
  204. UART_EXIT_CRITICAL(&(uart_context[uart_num].spinlock));
  205. }
  206. esp_err_t uart_get_sclk_freq(uart_sclk_t sclk, uint32_t *out_freq_hz)
  207. {
  208. return esp_clk_tree_src_get_freq_hz((soc_module_clk_t)sclk, ESP_CLK_TREE_SRC_FREQ_PRECISION_CACHED, out_freq_hz);
  209. }
  210. esp_err_t uart_set_word_length(uart_port_t uart_num, uart_word_length_t data_bit)
  211. {
  212. ESP_RETURN_ON_FALSE((uart_num < UART_NUM_MAX), ESP_FAIL, UART_TAG, "uart_num error");
  213. ESP_RETURN_ON_FALSE((data_bit < UART_DATA_BITS_MAX), ESP_FAIL, UART_TAG, "data bit error");
  214. UART_ENTER_CRITICAL(&(uart_context[uart_num].spinlock));
  215. uart_hal_set_data_bit_num(&(uart_context[uart_num].hal), data_bit);
  216. UART_EXIT_CRITICAL(&(uart_context[uart_num].spinlock));
  217. return ESP_OK;
  218. }
  219. esp_err_t uart_get_word_length(uart_port_t uart_num, uart_word_length_t *data_bit)
  220. {
  221. ESP_RETURN_ON_FALSE((uart_num < UART_NUM_MAX), ESP_FAIL, UART_TAG, "uart_num error");
  222. uart_hal_get_data_bit_num(&(uart_context[uart_num].hal), data_bit);
  223. return ESP_OK;
  224. }
  225. esp_err_t uart_set_stop_bits(uart_port_t uart_num, uart_stop_bits_t stop_bit)
  226. {
  227. ESP_RETURN_ON_FALSE((uart_num < UART_NUM_MAX), ESP_FAIL, UART_TAG, "uart_num error");
  228. ESP_RETURN_ON_FALSE((stop_bit < UART_STOP_BITS_MAX), ESP_FAIL, UART_TAG, "stop bit error");
  229. UART_ENTER_CRITICAL(&(uart_context[uart_num].spinlock));
  230. uart_hal_set_stop_bits(&(uart_context[uart_num].hal), stop_bit);
  231. UART_EXIT_CRITICAL(&(uart_context[uart_num].spinlock));
  232. return ESP_OK;
  233. }
  234. esp_err_t uart_get_stop_bits(uart_port_t uart_num, uart_stop_bits_t *stop_bit)
  235. {
  236. ESP_RETURN_ON_FALSE((uart_num < UART_NUM_MAX), ESP_FAIL, UART_TAG, "uart_num error");
  237. UART_ENTER_CRITICAL(&(uart_context[uart_num].spinlock));
  238. uart_hal_get_stop_bits(&(uart_context[uart_num].hal), stop_bit);
  239. UART_EXIT_CRITICAL(&(uart_context[uart_num].spinlock));
  240. return ESP_OK;
  241. }
  242. esp_err_t uart_set_parity(uart_port_t uart_num, uart_parity_t parity_mode)
  243. {
  244. ESP_RETURN_ON_FALSE((uart_num < UART_NUM_MAX), ESP_FAIL, UART_TAG, "uart_num error");
  245. UART_ENTER_CRITICAL(&(uart_context[uart_num].spinlock));
  246. uart_hal_set_parity(&(uart_context[uart_num].hal), parity_mode);
  247. UART_EXIT_CRITICAL(&(uart_context[uart_num].spinlock));
  248. return ESP_OK;
  249. }
  250. esp_err_t uart_get_parity(uart_port_t uart_num, uart_parity_t *parity_mode)
  251. {
  252. ESP_RETURN_ON_FALSE((uart_num < UART_NUM_MAX), ESP_FAIL, UART_TAG, "uart_num error");
  253. UART_ENTER_CRITICAL(&(uart_context[uart_num].spinlock));
  254. uart_hal_get_parity(&(uart_context[uart_num].hal), parity_mode);
  255. UART_EXIT_CRITICAL(&(uart_context[uart_num].spinlock));
  256. return ESP_OK;
  257. }
  258. esp_err_t uart_set_baudrate(uart_port_t uart_num, uint32_t baud_rate)
  259. {
  260. ESP_RETURN_ON_FALSE((uart_num < UART_NUM_MAX), ESP_FAIL, UART_TAG, "uart_num error");
  261. soc_module_clk_t src_clk;
  262. uint32_t sclk_freq;
  263. uart_hal_get_sclk(&(uart_context[uart_num].hal), &src_clk);
  264. ESP_RETURN_ON_ERROR(esp_clk_tree_src_get_freq_hz(src_clk, ESP_CLK_TREE_SRC_FREQ_PRECISION_CACHED, &sclk_freq), UART_TAG, "Invalid src_clk");
  265. UART_ENTER_CRITICAL(&(uart_context[uart_num].spinlock));
  266. if (uart_num < SOC_UART_HP_NUM) {
  267. HP_UART_SRC_CLK_ATOMIC() {
  268. uart_hal_set_baudrate(&(uart_context[uart_num].hal), baud_rate, sclk_freq);
  269. }
  270. }
  271. #if (SOC_UART_LP_NUM >= 1)
  272. else {
  273. lp_uart_ll_set_baudrate(uart_context[uart_num].hal.dev, baud_rate, sclk_freq);
  274. }
  275. #endif
  276. UART_EXIT_CRITICAL(&(uart_context[uart_num].spinlock));
  277. return ESP_OK;
  278. }
  279. esp_err_t uart_get_baudrate(uart_port_t uart_num, uint32_t *baudrate)
  280. {
  281. ESP_RETURN_ON_FALSE((uart_num < UART_NUM_MAX), ESP_FAIL, UART_TAG, "uart_num error");
  282. soc_module_clk_t src_clk;
  283. uint32_t sclk_freq;
  284. uart_hal_get_sclk(&(uart_context[uart_num].hal), &src_clk);
  285. ESP_RETURN_ON_ERROR(esp_clk_tree_src_get_freq_hz(src_clk, ESP_CLK_TREE_SRC_FREQ_PRECISION_CACHED, &sclk_freq), UART_TAG, "Invalid src_clk");
  286. UART_ENTER_CRITICAL(&(uart_context[uart_num].spinlock));
  287. uart_hal_get_baudrate(&(uart_context[uart_num].hal), baudrate, sclk_freq);
  288. UART_EXIT_CRITICAL(&(uart_context[uart_num].spinlock));
  289. return ESP_OK;
  290. }
  291. esp_err_t uart_set_line_inverse(uart_port_t uart_num, uint32_t inverse_mask)
  292. {
  293. ESP_RETURN_ON_FALSE((uart_num < UART_NUM_MAX), ESP_FAIL, UART_TAG, "uart_num error");
  294. UART_ENTER_CRITICAL(&(uart_context[uart_num].spinlock));
  295. uart_hal_inverse_signal(&(uart_context[uart_num].hal), inverse_mask);
  296. UART_EXIT_CRITICAL(&(uart_context[uart_num].spinlock));
  297. return ESP_OK;
  298. }
  299. esp_err_t uart_set_sw_flow_ctrl(uart_port_t uart_num, bool enable, uint8_t rx_thresh_xon, uint8_t rx_thresh_xoff)
  300. {
  301. ESP_RETURN_ON_FALSE((uart_num < UART_NUM_MAX), ESP_FAIL, UART_TAG, "uart_num error");
  302. ESP_RETURN_ON_FALSE((rx_thresh_xon < UART_HW_FIFO_LEN(uart_num)), ESP_FAIL, UART_TAG, "rx flow xon thresh error");
  303. ESP_RETURN_ON_FALSE((rx_thresh_xoff < UART_HW_FIFO_LEN(uart_num)), ESP_FAIL, UART_TAG, "rx flow xoff thresh error");
  304. uart_sw_flowctrl_t sw_flow_ctl = {
  305. .xon_char = XON,
  306. .xoff_char = XOFF,
  307. .xon_thrd = rx_thresh_xon,
  308. .xoff_thrd = rx_thresh_xoff,
  309. };
  310. UART_ENTER_CRITICAL(&(uart_context[uart_num].spinlock));
  311. uart_hal_set_sw_flow_ctrl(&(uart_context[uart_num].hal), &sw_flow_ctl, enable);
  312. UART_EXIT_CRITICAL(&(uart_context[uart_num].spinlock));
  313. return ESP_OK;
  314. }
  315. esp_err_t uart_set_hw_flow_ctrl(uart_port_t uart_num, uart_hw_flowcontrol_t flow_ctrl, uint8_t rx_thresh)
  316. {
  317. ESP_RETURN_ON_FALSE((uart_num < UART_NUM_MAX), ESP_FAIL, UART_TAG, "uart_num error");
  318. ESP_RETURN_ON_FALSE((rx_thresh < UART_HW_FIFO_LEN(uart_num)), ESP_FAIL, UART_TAG, "rx flow thresh error");
  319. ESP_RETURN_ON_FALSE((flow_ctrl < UART_HW_FLOWCTRL_MAX), ESP_FAIL, UART_TAG, "hw_flowctrl mode error");
  320. UART_ENTER_CRITICAL(&(uart_context[uart_num].spinlock));
  321. uart_hal_set_hw_flow_ctrl(&(uart_context[uart_num].hal), flow_ctrl, rx_thresh);
  322. UART_EXIT_CRITICAL(&(uart_context[uart_num].spinlock));
  323. return ESP_OK;
  324. }
  325. esp_err_t uart_get_hw_flow_ctrl(uart_port_t uart_num, uart_hw_flowcontrol_t *flow_ctrl)
  326. {
  327. ESP_RETURN_ON_FALSE((uart_num < UART_NUM_MAX), ESP_FAIL, UART_TAG, "uart_num error");
  328. UART_ENTER_CRITICAL(&(uart_context[uart_num].spinlock));
  329. uart_hal_get_hw_flow_ctrl(&(uart_context[uart_num].hal), flow_ctrl);
  330. UART_EXIT_CRITICAL(&(uart_context[uart_num].spinlock));
  331. return ESP_OK;
  332. }
  333. esp_err_t UART_ISR_ATTR uart_clear_intr_status(uart_port_t uart_num, uint32_t clr_mask)
  334. {
  335. ESP_RETURN_ON_FALSE_ISR((uart_num < UART_NUM_MAX), ESP_FAIL, UART_TAG, "uart_num error");
  336. uart_hal_clr_intsts_mask(&(uart_context[uart_num].hal), clr_mask);
  337. return ESP_OK;
  338. }
  339. esp_err_t uart_enable_intr_mask(uart_port_t uart_num, uint32_t enable_mask)
  340. {
  341. ESP_RETURN_ON_FALSE((uart_num < UART_NUM_MAX), ESP_FAIL, UART_TAG, "uart_num error");
  342. UART_ENTER_CRITICAL(&(uart_context[uart_num].spinlock));
  343. /* Keep track of the interrupt toggling. In fact, without such variable,
  344. * once the RX buffer is full and the RX interrupts disabled, it is
  345. * impossible what was the previous state (enabled/disabled) of these
  346. * interrupt masks. Thus, this will be very particularly handy when
  347. * emptying a filled RX buffer. */
  348. p_uart_obj[uart_num]->rx_int_usr_mask |= enable_mask;
  349. uart_hal_clr_intsts_mask(&(uart_context[uart_num].hal), enable_mask);
  350. uart_hal_ena_intr_mask(&(uart_context[uart_num].hal), enable_mask);
  351. UART_EXIT_CRITICAL(&(uart_context[uart_num].spinlock));
  352. return ESP_OK;
  353. }
  354. /**
  355. * @brief Function re-enabling the given interrupts (mask) if and only if
  356. * they have not been disabled by the user.
  357. *
  358. * @param uart_num UART number to perform the operation on
  359. * @param enable_mask Interrupts (flags) to be re-enabled
  360. *
  361. * @return ESP_OK in success, ESP_FAIL if uart_num is incorrect
  362. */
  363. static esp_err_t uart_reenable_intr_mask(uart_port_t uart_num, uint32_t enable_mask)
  364. {
  365. ESP_RETURN_ON_FALSE((uart_num < UART_NUM_MAX), ESP_FAIL, UART_TAG, "uart_num error");
  366. UART_ENTER_CRITICAL(&(uart_context[uart_num].spinlock));
  367. /* Mask will only contain the interrupt flags that needs to be re-enabled
  368. * AND which have NOT been explicitly disabled by the user. */
  369. uint32_t mask = p_uart_obj[uart_num]->rx_int_usr_mask & enable_mask;
  370. uart_hal_clr_intsts_mask(&(uart_context[uart_num].hal), mask);
  371. uart_hal_ena_intr_mask(&(uart_context[uart_num].hal), mask);
  372. UART_EXIT_CRITICAL(&(uart_context[uart_num].spinlock));
  373. return ESP_OK;
  374. }
  375. esp_err_t uart_disable_intr_mask(uart_port_t uart_num, uint32_t disable_mask)
  376. {
  377. ESP_RETURN_ON_FALSE((uart_num < UART_NUM_MAX), ESP_FAIL, UART_TAG, "uart_num error");
  378. UART_ENTER_CRITICAL(&(uart_context[uart_num].spinlock));
  379. p_uart_obj[uart_num]->rx_int_usr_mask &= ~disable_mask;
  380. uart_hal_disable_intr_mask(&(uart_context[uart_num].hal), disable_mask);
  381. UART_EXIT_CRITICAL(&(uart_context[uart_num].spinlock));
  382. return ESP_OK;
  383. }
  384. static esp_err_t uart_pattern_link_free(uart_port_t uart_num)
  385. {
  386. int *pdata = NULL;
  387. UART_ENTER_CRITICAL(&(uart_context[uart_num].spinlock));
  388. if (p_uart_obj[uart_num]->rx_pattern_pos.data != NULL) {
  389. pdata = p_uart_obj[uart_num]->rx_pattern_pos.data;
  390. p_uart_obj[uart_num]->rx_pattern_pos.data = NULL;
  391. p_uart_obj[uart_num]->rx_pattern_pos.wr = 0;
  392. p_uart_obj[uart_num]->rx_pattern_pos.rd = 0;
  393. }
  394. UART_EXIT_CRITICAL(&(uart_context[uart_num].spinlock));
  395. free(pdata);
  396. return ESP_OK;
  397. }
  398. static esp_err_t UART_ISR_ATTR uart_pattern_enqueue(uart_port_t uart_num, int pos)
  399. {
  400. esp_err_t ret = ESP_OK;
  401. uart_pat_rb_t *p_pos = &p_uart_obj[uart_num]->rx_pattern_pos;
  402. int next = p_pos->wr + 1;
  403. if (next >= p_pos->len) {
  404. next = 0;
  405. }
  406. if (next == p_pos->rd) {
  407. #ifndef CONFIG_UART_ISR_IN_IRAM //Only log if ISR is not in IRAM
  408. ESP_EARLY_LOGW(UART_TAG, "Fail to enqueue pattern position, pattern queue is full.");
  409. #endif
  410. ret = ESP_FAIL;
  411. } else {
  412. p_pos->data[p_pos->wr] = pos;
  413. p_pos->wr = next;
  414. ret = ESP_OK;
  415. }
  416. return ret;
  417. }
  418. static esp_err_t uart_pattern_dequeue(uart_port_t uart_num)
  419. {
  420. if (p_uart_obj[uart_num]->rx_pattern_pos.data == NULL) {
  421. return ESP_ERR_INVALID_STATE;
  422. } else {
  423. esp_err_t ret = ESP_OK;
  424. uart_pat_rb_t *p_pos = &p_uart_obj[uart_num]->rx_pattern_pos;
  425. if (p_pos->rd == p_pos->wr) {
  426. ret = ESP_FAIL;
  427. } else {
  428. p_pos->rd++;
  429. }
  430. if (p_pos->rd >= p_pos->len) {
  431. p_pos->rd = 0;
  432. }
  433. return ret;
  434. }
  435. }
  436. static esp_err_t uart_pattern_queue_update(uart_port_t uart_num, int diff_len)
  437. {
  438. uart_pat_rb_t *p_pos = &p_uart_obj[uart_num]->rx_pattern_pos;
  439. int rd = p_pos->rd;
  440. while (rd != p_pos->wr) {
  441. p_pos->data[rd] -= diff_len;
  442. int rd_rec = rd;
  443. rd ++;
  444. if (rd >= p_pos->len) {
  445. rd = 0;
  446. }
  447. if (p_pos->data[rd_rec] < 0) {
  448. p_pos->rd = rd;
  449. }
  450. }
  451. return ESP_OK;
  452. }
  453. int uart_pattern_pop_pos(uart_port_t uart_num)
  454. {
  455. ESP_RETURN_ON_FALSE((p_uart_obj[uart_num]), (-1), UART_TAG, "uart driver error");
  456. UART_ENTER_CRITICAL(&(uart_context[uart_num].spinlock));
  457. uart_pat_rb_t *pat_pos = &p_uart_obj[uart_num]->rx_pattern_pos;
  458. int pos = -1;
  459. if (pat_pos != NULL && pat_pos->rd != pat_pos->wr) {
  460. pos = pat_pos->data[pat_pos->rd];
  461. uart_pattern_dequeue(uart_num);
  462. }
  463. UART_EXIT_CRITICAL(&(uart_context[uart_num].spinlock));
  464. return pos;
  465. }
  466. int uart_pattern_get_pos(uart_port_t uart_num)
  467. {
  468. ESP_RETURN_ON_FALSE((p_uart_obj[uart_num]), (-1), UART_TAG, "uart driver error");
  469. UART_ENTER_CRITICAL(&(uart_context[uart_num].spinlock));
  470. uart_pat_rb_t *pat_pos = &p_uart_obj[uart_num]->rx_pattern_pos;
  471. int pos = -1;
  472. if (pat_pos != NULL && pat_pos->rd != pat_pos->wr) {
  473. pos = pat_pos->data[pat_pos->rd];
  474. }
  475. UART_EXIT_CRITICAL(&(uart_context[uart_num].spinlock));
  476. return pos;
  477. }
  478. esp_err_t uart_pattern_queue_reset(uart_port_t uart_num, int queue_length)
  479. {
  480. ESP_RETURN_ON_FALSE((uart_num < UART_NUM_MAX), ESP_FAIL, UART_TAG, "uart_num error");
  481. ESP_RETURN_ON_FALSE((p_uart_obj[uart_num]), ESP_ERR_INVALID_STATE, UART_TAG, "uart driver error");
  482. int *pdata = (int *) malloc(queue_length * sizeof(int));
  483. if (pdata == NULL) {
  484. return ESP_ERR_NO_MEM;
  485. }
  486. UART_ENTER_CRITICAL(&(uart_context[uart_num].spinlock));
  487. int *ptmp = p_uart_obj[uart_num]->rx_pattern_pos.data;
  488. p_uart_obj[uart_num]->rx_pattern_pos.data = pdata;
  489. p_uart_obj[uart_num]->rx_pattern_pos.len = queue_length;
  490. p_uart_obj[uart_num]->rx_pattern_pos.rd = 0;
  491. p_uart_obj[uart_num]->rx_pattern_pos.wr = 0;
  492. UART_EXIT_CRITICAL(&(uart_context[uart_num].spinlock));
  493. free(ptmp);
  494. return ESP_OK;
  495. }
  496. esp_err_t uart_enable_pattern_det_baud_intr(uart_port_t uart_num, char pattern_chr, uint8_t chr_num, int chr_tout, int post_idle, int pre_idle)
  497. {
  498. ESP_RETURN_ON_FALSE(uart_num < UART_NUM_MAX, ESP_FAIL, UART_TAG, "uart_num error");
  499. ESP_RETURN_ON_FALSE(chr_tout >= 0 && chr_tout <= UART_THRESHOLD_NUM(uart_num, UART_RX_GAP_TOUT_V), ESP_FAIL, UART_TAG, "uart pattern set error\n");
  500. ESP_RETURN_ON_FALSE(post_idle >= 0 && post_idle <= UART_THRESHOLD_NUM(uart_num, UART_POST_IDLE_NUM_V), ESP_FAIL, UART_TAG, "uart pattern set error\n");
  501. ESP_RETURN_ON_FALSE(pre_idle >= 0 && pre_idle <= UART_THRESHOLD_NUM(uart_num, UART_PRE_IDLE_NUM_V), ESP_FAIL, UART_TAG, "uart pattern set error\n");
  502. uart_at_cmd_t at_cmd = {0};
  503. at_cmd.cmd_char = pattern_chr;
  504. at_cmd.char_num = chr_num;
  505. #if CONFIG_IDF_TARGET_ESP32
  506. uint32_t apb_clk_freq = 0;
  507. uint32_t uart_baud = 0;
  508. uint32_t uart_div = 0;
  509. uart_get_baudrate(uart_num, &uart_baud);
  510. esp_clk_tree_src_get_freq_hz((soc_module_clk_t)UART_SCLK_APB, ESP_CLK_TREE_SRC_FREQ_PRECISION_EXACT, &apb_clk_freq);
  511. uart_div = apb_clk_freq / uart_baud;
  512. at_cmd.gap_tout = chr_tout * uart_div;
  513. at_cmd.pre_idle = pre_idle * uart_div;
  514. at_cmd.post_idle = post_idle * uart_div;
  515. #else
  516. at_cmd.gap_tout = chr_tout;
  517. at_cmd.pre_idle = pre_idle;
  518. at_cmd.post_idle = post_idle;
  519. #endif
  520. uart_hal_clr_intsts_mask(&(uart_context[uart_num].hal), UART_INTR_CMD_CHAR_DET);
  521. UART_ENTER_CRITICAL(&(uart_context[uart_num].spinlock));
  522. uart_hal_set_at_cmd_char(&(uart_context[uart_num].hal), &at_cmd);
  523. uart_hal_ena_intr_mask(&(uart_context[uart_num].hal), UART_INTR_CMD_CHAR_DET);
  524. UART_EXIT_CRITICAL(&(uart_context[uart_num].spinlock));
  525. return ESP_OK;
  526. }
  527. esp_err_t uart_disable_pattern_det_intr(uart_port_t uart_num)
  528. {
  529. return uart_disable_intr_mask(uart_num, UART_INTR_CMD_CHAR_DET);
  530. }
  531. esp_err_t uart_enable_rx_intr(uart_port_t uart_num)
  532. {
  533. return uart_enable_intr_mask(uart_num, UART_INTR_RXFIFO_FULL | UART_INTR_RXFIFO_TOUT);
  534. }
  535. esp_err_t uart_disable_rx_intr(uart_port_t uart_num)
  536. {
  537. return uart_disable_intr_mask(uart_num, UART_INTR_RXFIFO_FULL | UART_INTR_RXFIFO_TOUT);
  538. }
  539. esp_err_t uart_disable_tx_intr(uart_port_t uart_num)
  540. {
  541. return uart_disable_intr_mask(uart_num, UART_INTR_TXFIFO_EMPTY);
  542. }
  543. esp_err_t uart_enable_tx_intr(uart_port_t uart_num, int enable, int thresh)
  544. {
  545. ESP_RETURN_ON_FALSE((uart_num < UART_NUM_MAX), ESP_FAIL, UART_TAG, "uart_num error");
  546. ESP_RETURN_ON_FALSE((thresh < UART_HW_FIFO_LEN(uart_num)), ESP_FAIL, UART_TAG, "empty intr threshold error");
  547. uart_hal_clr_intsts_mask(&(uart_context[uart_num].hal), UART_INTR_TXFIFO_EMPTY);
  548. UART_ENTER_CRITICAL(&(uart_context[uart_num].spinlock));
  549. uart_hal_set_txfifo_empty_thr(&(uart_context[uart_num].hal), thresh);
  550. uart_hal_ena_intr_mask(&(uart_context[uart_num].hal), UART_INTR_TXFIFO_EMPTY);
  551. UART_EXIT_CRITICAL(&(uart_context[uart_num].spinlock));
  552. return ESP_OK;
  553. }
  554. static bool uart_try_set_iomux_pin(uart_port_t uart_num, int io_num, uint32_t idx)
  555. {
  556. /* Store a pointer to the default pin, to optimize access to its fields. */
  557. const uart_periph_sig_t *upin = &uart_periph_signal[uart_num].pins[idx];
  558. /* In theory, if default_gpio is -1, iomux_func should also be -1, but
  559. * let's be safe and test both. */
  560. if (upin->iomux_func == -1 || upin->default_gpio == -1 || upin->default_gpio != io_num) {
  561. return false;
  562. }
  563. /* Assign the correct funct to the GPIO. */
  564. assert (upin->iomux_func != -1);
  565. if (uart_num < SOC_UART_HP_NUM) {
  566. gpio_iomux_out(io_num, upin->iomux_func, false);
  567. /* If the pin is input, we also have to redirect the signal,
  568. * in order to bypasse the GPIO matrix. */
  569. if (upin->input) {
  570. gpio_iomux_in(io_num, upin->signal);
  571. }
  572. }
  573. #if (SOC_UART_LP_NUM >= 1)
  574. else {
  575. if (upin->input) {
  576. rtc_gpio_set_direction(io_num, RTC_GPIO_MODE_INPUT_ONLY);
  577. } else {
  578. rtc_gpio_set_direction(io_num, RTC_GPIO_MODE_OUTPUT_ONLY);
  579. }
  580. rtc_gpio_init(io_num);
  581. rtc_gpio_iomux_func_sel(io_num, upin->iomux_func);
  582. }
  583. #endif
  584. return true;
  585. }
  586. //internal signal can be output to multiple GPIO pads
  587. //only one GPIO pad can connect with input signal
  588. esp_err_t uart_set_pin(uart_port_t uart_num, int tx_io_num, int rx_io_num, int rts_io_num, int cts_io_num)
  589. {
  590. ESP_RETURN_ON_FALSE((uart_num >= 0), ESP_FAIL, UART_TAG, "uart_num error");
  591. ESP_RETURN_ON_FALSE((uart_num < UART_NUM_MAX), ESP_FAIL, UART_TAG, "uart_num error");
  592. if (uart_num < SOC_UART_HP_NUM) {
  593. ESP_RETURN_ON_FALSE((tx_io_num < 0 || (GPIO_IS_VALID_OUTPUT_GPIO(tx_io_num))), ESP_FAIL, UART_TAG, "tx_io_num error");
  594. ESP_RETURN_ON_FALSE((rx_io_num < 0 || (GPIO_IS_VALID_GPIO(rx_io_num))), ESP_FAIL, UART_TAG, "rx_io_num error");
  595. ESP_RETURN_ON_FALSE((rts_io_num < 0 || (GPIO_IS_VALID_OUTPUT_GPIO(rts_io_num))), ESP_FAIL, UART_TAG, "rts_io_num error");
  596. ESP_RETURN_ON_FALSE((cts_io_num < 0 || (GPIO_IS_VALID_GPIO(cts_io_num))), ESP_FAIL, UART_TAG, "cts_io_num error");
  597. }
  598. #if (SOC_UART_LP_NUM >= 1)
  599. else { // LP_UART IO check
  600. #if !SOC_LP_GPIO_MATRIX_SUPPORTED
  601. const uart_periph_sig_t *pins = uart_periph_signal[uart_num].pins;
  602. // LP_UART has its fixed IOs
  603. ESP_RETURN_ON_FALSE((tx_io_num < 0 || (tx_io_num == pins[SOC_UART_TX_PIN_IDX].default_gpio)), ESP_FAIL, UART_TAG, "tx_io_num error");
  604. ESP_RETURN_ON_FALSE((rx_io_num < 0 || (rx_io_num == pins[SOC_UART_RX_PIN_IDX].default_gpio)), ESP_FAIL, UART_TAG, "rx_io_num error");
  605. ESP_RETURN_ON_FALSE((rts_io_num < 0 || (rts_io_num == pins[SOC_UART_RTS_PIN_IDX].default_gpio)), ESP_FAIL, UART_TAG, "rts_io_num error");
  606. ESP_RETURN_ON_FALSE((cts_io_num < 0 || (cts_io_num == pins[SOC_UART_CTS_PIN_IDX].default_gpio)), ESP_FAIL, UART_TAG, "cts_io_num error");
  607. #else
  608. // LP_UART signals can be routed to any LP_IOs
  609. ESP_RETURN_ON_FALSE((tx_io_num < 0 || rtc_gpio_is_valid_gpio(tx_io_num)), ESP_FAIL, UART_TAG, "tx_io_num error");
  610. ESP_RETURN_ON_FALSE((rx_io_num < 0 || rtc_gpio_is_valid_gpio(rx_io_num)), ESP_FAIL, UART_TAG, "rx_io_num error");
  611. ESP_RETURN_ON_FALSE((rts_io_num < 0 || rtc_gpio_is_valid_gpio(rts_io_num)), ESP_FAIL, UART_TAG, "rts_io_num error");
  612. ESP_RETURN_ON_FALSE((cts_io_num < 0 || rtc_gpio_is_valid_gpio(cts_io_num)), ESP_FAIL, UART_TAG, "cts_io_num error");
  613. #endif // SOC_LP_GPIO_MATRIX_SUPPORTED
  614. }
  615. #endif
  616. /* In the following statements, if the io_num is negative, no need to configure anything. */
  617. if (tx_io_num >= 0 && !uart_try_set_iomux_pin(uart_num, tx_io_num, SOC_UART_TX_PIN_IDX)) {
  618. if (uart_num < SOC_UART_HP_NUM) {
  619. gpio_hal_iomux_func_sel(GPIO_PIN_MUX_REG[tx_io_num], PIN_FUNC_GPIO);
  620. gpio_set_level(tx_io_num, 1);
  621. esp_rom_gpio_connect_out_signal(tx_io_num, UART_PERIPH_SIGNAL(uart_num, SOC_UART_TX_PIN_IDX), 0, 0);
  622. }
  623. #if SOC_LP_GPIO_MATRIX_SUPPORTED
  624. else {
  625. rtc_gpio_set_direction(tx_io_num, RTC_GPIO_MODE_OUTPUT_ONLY);
  626. rtc_gpio_init(tx_io_num);
  627. rtc_gpio_iomux_func_sel(tx_io_num, 1);
  628. lp_gpio_connect_out_signal(tx_io_num, UART_PERIPH_SIGNAL(uart_num, SOC_UART_TX_PIN_IDX), 0, 0);
  629. }
  630. #endif
  631. }
  632. if (rx_io_num >= 0 && !uart_try_set_iomux_pin(uart_num, rx_io_num, SOC_UART_RX_PIN_IDX)) {
  633. if (uart_num < SOC_UART_HP_NUM) {
  634. gpio_hal_iomux_func_sel(GPIO_PIN_MUX_REG[rx_io_num], PIN_FUNC_GPIO);
  635. gpio_set_pull_mode(rx_io_num, GPIO_PULLUP_ONLY);
  636. gpio_set_direction(rx_io_num, GPIO_MODE_INPUT);
  637. esp_rom_gpio_connect_in_signal(rx_io_num, UART_PERIPH_SIGNAL(uart_num, SOC_UART_RX_PIN_IDX), 0);
  638. }
  639. #if SOC_LP_GPIO_MATRIX_SUPPORTED
  640. else {
  641. rtc_gpio_set_direction(rx_io_num, RTC_GPIO_MODE_INPUT_ONLY);
  642. rtc_gpio_init(rx_io_num);
  643. rtc_gpio_iomux_func_sel(rx_io_num, 1);
  644. lp_gpio_connect_in_signal(rx_io_num, UART_PERIPH_SIGNAL(uart_num, SOC_UART_RX_PIN_IDX), 0);
  645. }
  646. #endif
  647. }
  648. if (rts_io_num >= 0 && !uart_try_set_iomux_pin(uart_num, rts_io_num, SOC_UART_RTS_PIN_IDX)) {
  649. if (uart_num < SOC_UART_HP_NUM) {
  650. gpio_hal_iomux_func_sel(GPIO_PIN_MUX_REG[rts_io_num], PIN_FUNC_GPIO);
  651. gpio_set_direction(rts_io_num, GPIO_MODE_OUTPUT);
  652. esp_rom_gpio_connect_out_signal(rts_io_num, UART_PERIPH_SIGNAL(uart_num, SOC_UART_RTS_PIN_IDX), 0, 0);
  653. }
  654. #if SOC_LP_GPIO_MATRIX_SUPPORTED
  655. else {
  656. rtc_gpio_set_direction(rts_io_num, RTC_GPIO_MODE_OUTPUT_ONLY);
  657. rtc_gpio_init(rts_io_num);
  658. rtc_gpio_iomux_func_sel(rts_io_num, 1);
  659. lp_gpio_connect_out_signal(rts_io_num, UART_PERIPH_SIGNAL(uart_num, SOC_UART_RTS_PIN_IDX), 0, 0);
  660. }
  661. #endif
  662. }
  663. if (cts_io_num >= 0 && !uart_try_set_iomux_pin(uart_num, cts_io_num, SOC_UART_CTS_PIN_IDX)) {
  664. if (uart_num < SOC_UART_HP_NUM) {
  665. gpio_hal_iomux_func_sel(GPIO_PIN_MUX_REG[cts_io_num], PIN_FUNC_GPIO);
  666. gpio_set_pull_mode(cts_io_num, GPIO_PULLUP_ONLY);
  667. gpio_set_direction(cts_io_num, GPIO_MODE_INPUT);
  668. esp_rom_gpio_connect_in_signal(cts_io_num, UART_PERIPH_SIGNAL(uart_num, SOC_UART_CTS_PIN_IDX), 0);
  669. }
  670. #if SOC_LP_GPIO_MATRIX_SUPPORTED
  671. else {
  672. rtc_gpio_set_direction(cts_io_num, RTC_GPIO_MODE_INPUT_ONLY);
  673. rtc_gpio_init(cts_io_num);
  674. rtc_gpio_iomux_func_sel(cts_io_num, 1);
  675. lp_gpio_connect_in_signal(cts_io_num, UART_PERIPH_SIGNAL(uart_num, SOC_UART_CTS_PIN_IDX), 0);
  676. }
  677. #endif
  678. }
  679. return ESP_OK;
  680. }
  681. esp_err_t uart_set_rts(uart_port_t uart_num, int level)
  682. {
  683. ESP_RETURN_ON_FALSE((uart_num < UART_NUM_MAX), ESP_FAIL, UART_TAG, "uart_num error");
  684. ESP_RETURN_ON_FALSE((!uart_hal_is_hw_rts_en(&(uart_context[uart_num].hal))), ESP_FAIL, UART_TAG, "disable hw flowctrl before using sw control");
  685. UART_ENTER_CRITICAL(&(uart_context[uart_num].spinlock));
  686. uart_hal_set_rts(&(uart_context[uart_num].hal), level);
  687. UART_EXIT_CRITICAL(&(uart_context[uart_num].spinlock));
  688. return ESP_OK;
  689. }
  690. esp_err_t uart_set_dtr(uart_port_t uart_num, int level)
  691. {
  692. ESP_RETURN_ON_FALSE((uart_num < UART_NUM_MAX), ESP_FAIL, UART_TAG, "uart_num error");
  693. UART_ENTER_CRITICAL(&(uart_context[uart_num].spinlock));
  694. uart_hal_set_dtr(&(uart_context[uart_num].hal), level);
  695. UART_EXIT_CRITICAL(&(uart_context[uart_num].spinlock));
  696. return ESP_OK;
  697. }
  698. esp_err_t uart_set_tx_idle_num(uart_port_t uart_num, uint16_t idle_num)
  699. {
  700. ESP_RETURN_ON_FALSE((uart_num < UART_NUM_MAX), ESP_FAIL, UART_TAG, "uart_num error");
  701. ESP_RETURN_ON_FALSE((idle_num <= UART_THRESHOLD_NUM(uart_num, UART_TX_IDLE_NUM_V)), ESP_FAIL, UART_TAG, "uart idle num error");
  702. UART_ENTER_CRITICAL(&(uart_context[uart_num].spinlock));
  703. uart_hal_set_tx_idle_num(&(uart_context[uart_num].hal), idle_num);
  704. UART_EXIT_CRITICAL(&(uart_context[uart_num].spinlock));
  705. return ESP_OK;
  706. }
  707. esp_err_t uart_param_config(uart_port_t uart_num, const uart_config_t *uart_config)
  708. {
  709. ESP_RETURN_ON_FALSE((uart_num < UART_NUM_MAX), ESP_FAIL, UART_TAG, "uart_num error");
  710. ESP_RETURN_ON_FALSE((uart_config), ESP_FAIL, UART_TAG, "param null");
  711. ESP_RETURN_ON_FALSE((uart_config->rx_flow_ctrl_thresh < UART_HW_FIFO_LEN(uart_num)), ESP_FAIL, UART_TAG, "rx flow thresh error");
  712. ESP_RETURN_ON_FALSE((uart_config->flow_ctrl < UART_HW_FLOWCTRL_MAX), ESP_FAIL, UART_TAG, "hw_flowctrl mode error");
  713. ESP_RETURN_ON_FALSE((uart_config->data_bits < UART_DATA_BITS_MAX), ESP_FAIL, UART_TAG, "data bit error");
  714. uart_module_enable(uart_num);
  715. soc_module_clk_t uart_sclk_sel = 0; // initialize to an invalid module clock ID
  716. if (uart_num < SOC_UART_HP_NUM) {
  717. uart_sclk_sel = (soc_module_clk_t)((uart_config->source_clk) ? uart_config->source_clk : UART_SCLK_DEFAULT); // if no specifying the clock source (soc_module_clk_t starts from 1), then just use the default clock
  718. }
  719. #if (SOC_UART_LP_NUM >= 1)
  720. else {
  721. uart_sclk_sel = (soc_module_clk_t)((uart_config->lp_source_clk) ? uart_config->lp_source_clk : LP_UART_SCLK_DEFAULT);
  722. }
  723. #endif
  724. #if SOC_UART_SUPPORT_RTC_CLK
  725. if (uart_sclk_sel == (soc_module_clk_t)UART_SCLK_RTC) {
  726. periph_rtc_dig_clk8m_enable();
  727. }
  728. #endif
  729. uint32_t sclk_freq;
  730. ESP_RETURN_ON_ERROR(esp_clk_tree_src_get_freq_hz(uart_sclk_sel, ESP_CLK_TREE_SRC_FREQ_PRECISION_CACHED, &sclk_freq), UART_TAG, "Invalid src_clk");
  731. UART_ENTER_CRITICAL(&(uart_context[uart_num].spinlock));
  732. uart_hal_init(&(uart_context[uart_num].hal), uart_num);
  733. if (uart_num < SOC_UART_HP_NUM) {
  734. HP_UART_SRC_CLK_ATOMIC() {
  735. uart_hal_set_sclk(&(uart_context[uart_num].hal), uart_sclk_sel);
  736. uart_hal_set_baudrate(&(uart_context[uart_num].hal), uart_config->baud_rate, sclk_freq);
  737. }
  738. }
  739. #if (SOC_UART_LP_NUM >= 1)
  740. else {
  741. LP_UART_SRC_CLK_ATOMIC() {
  742. lp_uart_ll_set_source_clk(uart_context[uart_num].hal.dev, (soc_periph_lp_uart_clk_src_t)uart_sclk_sel);
  743. }
  744. lp_uart_ll_set_baudrate(uart_context[uart_num].hal.dev, uart_config->baud_rate, sclk_freq);
  745. }
  746. #endif
  747. uart_hal_set_parity(&(uart_context[uart_num].hal), uart_config->parity);
  748. uart_hal_set_data_bit_num(&(uart_context[uart_num].hal), uart_config->data_bits);
  749. uart_hal_set_stop_bits(&(uart_context[uart_num].hal), uart_config->stop_bits);
  750. uart_hal_set_tx_idle_num(&(uart_context[uart_num].hal), UART_TX_IDLE_NUM_DEFAULT);
  751. uart_hal_set_hw_flow_ctrl(&(uart_context[uart_num].hal), uart_config->flow_ctrl, uart_config->rx_flow_ctrl_thresh);
  752. UART_EXIT_CRITICAL(&(uart_context[uart_num].spinlock));
  753. uart_hal_rxfifo_rst(&(uart_context[uart_num].hal));
  754. uart_hal_txfifo_rst(&(uart_context[uart_num].hal));
  755. return ESP_OK;
  756. }
  757. esp_err_t uart_intr_config(uart_port_t uart_num, const uart_intr_config_t *intr_conf)
  758. {
  759. ESP_RETURN_ON_FALSE((uart_num < UART_NUM_MAX), ESP_FAIL, UART_TAG, "uart_num error");
  760. ESP_RETURN_ON_FALSE((intr_conf), ESP_FAIL, UART_TAG, "param null");
  761. uart_hal_clr_intsts_mask(&(uart_context[uart_num].hal), UART_LL_INTR_MASK);
  762. UART_ENTER_CRITICAL(&(uart_context[uart_num].spinlock));
  763. if (intr_conf->intr_enable_mask & UART_INTR_RXFIFO_TOUT) {
  764. uart_hal_set_rx_timeout(&(uart_context[uart_num].hal), intr_conf->rx_timeout_thresh);
  765. } else {
  766. //Disable rx_tout intr
  767. uart_hal_set_rx_timeout(&(uart_context[uart_num].hal), 0);
  768. }
  769. if (intr_conf->intr_enable_mask & UART_INTR_RXFIFO_FULL) {
  770. uart_hal_set_rxfifo_full_thr(&(uart_context[uart_num].hal), intr_conf->rxfifo_full_thresh);
  771. }
  772. if (intr_conf->intr_enable_mask & UART_INTR_TXFIFO_EMPTY) {
  773. uart_hal_set_txfifo_empty_thr(&(uart_context[uart_num].hal), intr_conf->txfifo_empty_intr_thresh);
  774. }
  775. uart_hal_ena_intr_mask(&(uart_context[uart_num].hal), intr_conf->intr_enable_mask);
  776. UART_EXIT_CRITICAL(&(uart_context[uart_num].spinlock));
  777. return ESP_OK;
  778. }
  779. static int UART_ISR_ATTR uart_find_pattern_from_last(uint8_t *buf, int length, uint8_t pat_chr, uint8_t pat_num)
  780. {
  781. int cnt = 0;
  782. int len = length;
  783. while (len >= 0) {
  784. if (buf[len] == pat_chr) {
  785. cnt++;
  786. } else {
  787. cnt = 0;
  788. }
  789. if (cnt >= pat_num) {
  790. break;
  791. }
  792. len --;
  793. }
  794. return len;
  795. }
  796. static uint32_t UART_ISR_ATTR uart_enable_tx_write_fifo(uart_port_t uart_num, const uint8_t *pbuf, uint32_t len)
  797. {
  798. uint32_t sent_len = 0;
  799. UART_ENTER_CRITICAL_SAFE(&(uart_context[uart_num].spinlock));
  800. if (UART_IS_MODE_SET(uart_num, UART_MODE_RS485_HALF_DUPLEX)) {
  801. uart_hal_set_rts(&(uart_context[uart_num].hal), 0);
  802. // If any new things are written to fifo, then we can always clear the previous TX_DONE interrupt bit (if it was set)
  803. // Old TX_DONE bit might reset the RTS, leading new tx transmission failure for rs485 mode
  804. uart_hal_clr_intsts_mask(&(uart_context[uart_num].hal), UART_INTR_TX_DONE);
  805. uart_hal_ena_intr_mask(&(uart_context[uart_num].hal), UART_INTR_TX_DONE);
  806. }
  807. uart_hal_write_txfifo(&(uart_context[uart_num].hal), pbuf, len, &sent_len);
  808. UART_EXIT_CRITICAL_SAFE(&(uart_context[uart_num].spinlock));
  809. return sent_len;
  810. }
  811. //internal isr handler for default driver code.
  812. static void UART_ISR_ATTR uart_rx_intr_handler_default(void *param)
  813. {
  814. uart_obj_t *p_uart = (uart_obj_t *) param;
  815. uint8_t uart_num = p_uart->uart_num;
  816. int rx_fifo_len = 0;
  817. uint32_t uart_intr_status = 0;
  818. uart_event_t uart_event;
  819. BaseType_t HPTaskAwoken = 0;
  820. bool need_yield = false;
  821. static uint8_t pat_flg = 0;
  822. BaseType_t sent = pdFALSE;
  823. while (1) {
  824. // The `continue statement` may cause the interrupt to loop infinitely
  825. // we exit the interrupt here
  826. uart_intr_status = uart_hal_get_intsts_mask(&(uart_context[uart_num].hal));
  827. //Exit form while loop
  828. if (uart_intr_status == 0) {
  829. break;
  830. }
  831. uart_event.type = UART_EVENT_MAX;
  832. if (uart_intr_status & UART_INTR_TXFIFO_EMPTY) {
  833. UART_ENTER_CRITICAL_ISR(&(uart_context[uart_num].spinlock));
  834. uart_hal_disable_intr_mask(&(uart_context[uart_num].hal), UART_INTR_TXFIFO_EMPTY);
  835. UART_EXIT_CRITICAL_ISR(&(uart_context[uart_num].spinlock));
  836. uart_hal_clr_intsts_mask(&(uart_context[uart_num].hal), UART_INTR_TXFIFO_EMPTY);
  837. if (p_uart->tx_waiting_brk) {
  838. continue;
  839. }
  840. //TX semaphore will only be used when tx_buf_size is zero.
  841. if (p_uart->tx_waiting_fifo == true && p_uart->tx_buf_size == 0) {
  842. p_uart->tx_waiting_fifo = false;
  843. xSemaphoreGiveFromISR(p_uart->tx_fifo_sem, &HPTaskAwoken);
  844. need_yield |= (HPTaskAwoken == pdTRUE);
  845. } else {
  846. //We don't use TX ring buffer, because the size is zero.
  847. if (p_uart->tx_buf_size == 0) {
  848. continue;
  849. }
  850. bool en_tx_flg = false;
  851. uint32_t tx_fifo_rem = uart_hal_get_txfifo_len(&(uart_context[uart_num].hal));
  852. //We need to put a loop here, in case all the buffer items are very short.
  853. //That would cause a watch_dog reset because empty interrupt happens so often.
  854. //Although this is a loop in ISR, this loop will execute at most 128 turns.
  855. while (tx_fifo_rem) {
  856. if (p_uart->tx_len_tot == 0 || p_uart->tx_ptr == NULL || p_uart->tx_len_cur == 0) {
  857. size_t size;
  858. p_uart->tx_head = (uart_tx_data_t *) xRingbufferReceiveFromISR(p_uart->tx_ring_buf, &size);
  859. if (p_uart->tx_head) {
  860. //The first item is the data description
  861. //Get the first item to get the data information
  862. if (p_uart->tx_len_tot == 0) {
  863. p_uart->tx_ptr = NULL;
  864. p_uart->tx_len_tot = p_uart->tx_head->tx_data.size;
  865. if (p_uart->tx_head->type == UART_DATA_BREAK) {
  866. p_uart->tx_brk_flg = 1;
  867. p_uart->tx_brk_len = p_uart->tx_head->tx_data.brk_len;
  868. }
  869. //We have saved the data description from the 1st item, return buffer.
  870. vRingbufferReturnItemFromISR(p_uart->tx_ring_buf, p_uart->tx_head, &HPTaskAwoken);
  871. need_yield |= (HPTaskAwoken == pdTRUE);
  872. } else if (p_uart->tx_ptr == NULL) {
  873. //Update the TX item pointer, we will need this to return item to buffer.
  874. p_uart->tx_ptr = (uint8_t *)p_uart->tx_head;
  875. en_tx_flg = true;
  876. p_uart->tx_len_cur = size;
  877. }
  878. } else {
  879. //Can not get data from ring buffer, return;
  880. break;
  881. }
  882. }
  883. if (p_uart->tx_len_tot > 0 && p_uart->tx_ptr && p_uart->tx_len_cur > 0) {
  884. // To fill the TX FIFO.
  885. uint32_t send_len = uart_enable_tx_write_fifo(uart_num, (const uint8_t *) p_uart->tx_ptr,
  886. MIN(p_uart->tx_len_cur, tx_fifo_rem));
  887. p_uart->tx_ptr += send_len;
  888. p_uart->tx_len_tot -= send_len;
  889. p_uart->tx_len_cur -= send_len;
  890. tx_fifo_rem -= send_len;
  891. if (p_uart->tx_len_cur == 0) {
  892. //Return item to ring buffer.
  893. vRingbufferReturnItemFromISR(p_uart->tx_ring_buf, p_uart->tx_head, &HPTaskAwoken);
  894. need_yield |= (HPTaskAwoken == pdTRUE);
  895. p_uart->tx_head = NULL;
  896. p_uart->tx_ptr = NULL;
  897. //Sending item done, now we need to send break if there is a record.
  898. //Set TX break signal after FIFO is empty
  899. if (p_uart->tx_len_tot == 0 && p_uart->tx_brk_flg == 1) {
  900. uart_hal_clr_intsts_mask(&(uart_context[uart_num].hal), UART_INTR_TX_BRK_DONE);
  901. UART_ENTER_CRITICAL_ISR(&(uart_context[uart_num].spinlock));
  902. uart_hal_tx_break(&(uart_context[uart_num].hal), p_uart->tx_brk_len);
  903. uart_hal_ena_intr_mask(&(uart_context[uart_num].hal), UART_INTR_TX_BRK_DONE);
  904. UART_EXIT_CRITICAL_ISR(&(uart_context[uart_num].spinlock));
  905. p_uart->tx_waiting_brk = 1;
  906. //do not enable TX empty interrupt
  907. en_tx_flg = false;
  908. } else {
  909. //enable TX empty interrupt
  910. en_tx_flg = true;
  911. }
  912. UART_ENTER_CRITICAL_ISR(&uart_selectlock);
  913. if (p_uart->uart_select_notif_callback) {
  914. p_uart->uart_select_notif_callback(uart_num, UART_SELECT_WRITE_NOTIF, &HPTaskAwoken);
  915. need_yield |= (HPTaskAwoken == pdTRUE);
  916. }
  917. UART_EXIT_CRITICAL_ISR(&uart_selectlock);
  918. } else {
  919. //enable TX empty interrupt
  920. en_tx_flg = true;
  921. }
  922. }
  923. }
  924. if (en_tx_flg) {
  925. uart_hal_clr_intsts_mask(&(uart_context[uart_num].hal), UART_INTR_TXFIFO_EMPTY);
  926. UART_ENTER_CRITICAL_ISR(&(uart_context[uart_num].spinlock));
  927. uart_hal_ena_intr_mask(&(uart_context[uart_num].hal), UART_INTR_TXFIFO_EMPTY);
  928. UART_EXIT_CRITICAL_ISR(&(uart_context[uart_num].spinlock));
  929. }
  930. }
  931. } else if ((uart_intr_status & UART_INTR_RXFIFO_TOUT)
  932. || (uart_intr_status & UART_INTR_RXFIFO_FULL)
  933. || (uart_intr_status & UART_INTR_CMD_CHAR_DET)
  934. ) {
  935. if (pat_flg == 1) {
  936. uart_intr_status |= UART_INTR_CMD_CHAR_DET;
  937. pat_flg = 0;
  938. }
  939. if (p_uart->rx_buffer_full_flg == false) {
  940. rx_fifo_len = uart_hal_get_rxfifo_len(&(uart_context[uart_num].hal));
  941. if ((p_uart_obj[uart_num]->rx_always_timeout_flg) && !(uart_intr_status & UART_INTR_RXFIFO_TOUT)) {
  942. rx_fifo_len--; // leave one byte in the fifo in order to trigger uart_intr_rxfifo_tout
  943. }
  944. uart_hal_read_rxfifo(&(uart_context[uart_num].hal), p_uart->rx_data_buf, &rx_fifo_len);
  945. uint8_t pat_chr = 0;
  946. uint8_t pat_num = 0;
  947. int pat_idx = -1;
  948. uart_hal_get_at_cmd_char(&(uart_context[uart_num].hal), &pat_chr, &pat_num);
  949. //Get the buffer from the FIFO
  950. if (uart_intr_status & UART_INTR_CMD_CHAR_DET) {
  951. uart_hal_clr_intsts_mask(&(uart_context[uart_num].hal), UART_INTR_CMD_CHAR_DET);
  952. uart_event.type = UART_PATTERN_DET;
  953. uart_event.size = rx_fifo_len;
  954. pat_idx = uart_find_pattern_from_last(p_uart->rx_data_buf, rx_fifo_len - 1, pat_chr, pat_num);
  955. } else {
  956. //After Copying the Data From FIFO ,Clear intr_status
  957. uart_hal_clr_intsts_mask(&(uart_context[uart_num].hal), UART_INTR_RXFIFO_TOUT | UART_INTR_RXFIFO_FULL);
  958. uart_event.type = UART_DATA;
  959. uart_event.size = rx_fifo_len;
  960. uart_event.timeout_flag = (uart_intr_status & UART_INTR_RXFIFO_TOUT) ? true : false;
  961. UART_ENTER_CRITICAL_ISR(&uart_selectlock);
  962. if (p_uart->uart_select_notif_callback) {
  963. p_uart->uart_select_notif_callback(uart_num, UART_SELECT_READ_NOTIF, &HPTaskAwoken);
  964. need_yield |= (HPTaskAwoken == pdTRUE);
  965. }
  966. UART_EXIT_CRITICAL_ISR(&uart_selectlock);
  967. }
  968. p_uart->rx_stash_len = rx_fifo_len;
  969. //If we fail to push data to ring buffer, we will have to stash the data, and send next time.
  970. //Mainly for applications that uses flow control or small ring buffer.
  971. sent = xRingbufferSendFromISR(p_uart->rx_ring_buf, p_uart->rx_data_buf, p_uart->rx_stash_len, &HPTaskAwoken);
  972. need_yield |= (HPTaskAwoken == pdTRUE);
  973. if (sent == pdFALSE) {
  974. p_uart->rx_buffer_full_flg = true;
  975. UART_ENTER_CRITICAL_ISR(&(uart_context[uart_num].spinlock));
  976. uart_hal_disable_intr_mask(&(uart_context[uart_num].hal), UART_INTR_RXFIFO_TOUT | UART_INTR_RXFIFO_FULL);
  977. UART_EXIT_CRITICAL_ISR(&(uart_context[uart_num].spinlock));
  978. if (uart_event.type == UART_PATTERN_DET) {
  979. UART_ENTER_CRITICAL_ISR(&(uart_context[uart_num].spinlock));
  980. if (rx_fifo_len < pat_num) {
  981. //some of the characters are read out in last interrupt
  982. uart_pattern_enqueue(uart_num, p_uart->rx_buffered_len - (pat_num - rx_fifo_len));
  983. } else {
  984. uart_pattern_enqueue(uart_num,
  985. pat_idx <= -1 ?
  986. //can not find the pattern in buffer,
  987. p_uart->rx_buffered_len + p_uart->rx_stash_len :
  988. // find the pattern in buffer
  989. p_uart->rx_buffered_len + pat_idx);
  990. }
  991. UART_EXIT_CRITICAL_ISR(&(uart_context[uart_num].spinlock));
  992. sent = xQueueSendFromISR(p_uart->event_queue, (void * )&uart_event, &HPTaskAwoken);
  993. need_yield |= (HPTaskAwoken == pdTRUE);
  994. if ((p_uart->event_queue != NULL) && (sent == pdFALSE)) {
  995. #ifndef CONFIG_UART_ISR_IN_IRAM //Only log if ISR is not in IRAM
  996. ESP_EARLY_LOGV(UART_TAG, "UART event queue full");
  997. #endif
  998. }
  999. }
  1000. uart_event.type = UART_BUFFER_FULL;
  1001. } else {
  1002. UART_ENTER_CRITICAL_ISR(&(uart_context[uart_num].spinlock));
  1003. if (uart_intr_status & UART_INTR_CMD_CHAR_DET) {
  1004. if (rx_fifo_len < pat_num) {
  1005. //some of the characters are read out in last interrupt
  1006. uart_pattern_enqueue(uart_num, p_uart->rx_buffered_len - (pat_num - rx_fifo_len));
  1007. } else if (pat_idx >= 0) {
  1008. // find the pattern in stash buffer.
  1009. uart_pattern_enqueue(uart_num, p_uart->rx_buffered_len + pat_idx);
  1010. }
  1011. }
  1012. p_uart->rx_buffered_len += p_uart->rx_stash_len;
  1013. UART_EXIT_CRITICAL_ISR(&(uart_context[uart_num].spinlock));
  1014. }
  1015. } else {
  1016. UART_ENTER_CRITICAL_ISR(&(uart_context[uart_num].spinlock));
  1017. uart_hal_disable_intr_mask(&(uart_context[uart_num].hal), UART_INTR_RXFIFO_FULL | UART_INTR_RXFIFO_TOUT);
  1018. UART_EXIT_CRITICAL_ISR(&(uart_context[uart_num].spinlock));
  1019. uart_hal_clr_intsts_mask(&(uart_context[uart_num].hal), UART_INTR_RXFIFO_FULL | UART_INTR_RXFIFO_TOUT);
  1020. if (uart_intr_status & UART_INTR_CMD_CHAR_DET) {
  1021. uart_hal_clr_intsts_mask(&(uart_context[uart_num].hal), UART_INTR_CMD_CHAR_DET);
  1022. uart_event.type = UART_PATTERN_DET;
  1023. uart_event.size = rx_fifo_len;
  1024. pat_flg = 1;
  1025. }
  1026. }
  1027. } else if (uart_intr_status & UART_INTR_RXFIFO_OVF) {
  1028. // When fifo overflows, we reset the fifo.
  1029. UART_ENTER_CRITICAL_ISR(&(uart_context[uart_num].spinlock));
  1030. uart_hal_rxfifo_rst(&(uart_context[uart_num].hal));
  1031. UART_EXIT_CRITICAL_ISR(&(uart_context[uart_num].spinlock));
  1032. UART_ENTER_CRITICAL_ISR(&uart_selectlock);
  1033. if (p_uart->uart_select_notif_callback) {
  1034. p_uart->uart_select_notif_callback(uart_num, UART_SELECT_ERROR_NOTIF, &HPTaskAwoken);
  1035. need_yield |= (HPTaskAwoken == pdTRUE);
  1036. }
  1037. UART_EXIT_CRITICAL_ISR(&uart_selectlock);
  1038. uart_hal_clr_intsts_mask(&(uart_context[uart_num].hal), UART_INTR_RXFIFO_OVF);
  1039. uart_event.type = UART_FIFO_OVF;
  1040. } else if (uart_intr_status & UART_INTR_BRK_DET) {
  1041. uart_hal_clr_intsts_mask(&(uart_context[uart_num].hal), UART_INTR_BRK_DET);
  1042. uart_event.type = UART_BREAK;
  1043. } else if (uart_intr_status & UART_INTR_FRAM_ERR) {
  1044. UART_ENTER_CRITICAL_ISR(&uart_selectlock);
  1045. if (p_uart->uart_select_notif_callback) {
  1046. p_uart->uart_select_notif_callback(uart_num, UART_SELECT_ERROR_NOTIF, &HPTaskAwoken);
  1047. need_yield |= (HPTaskAwoken == pdTRUE);
  1048. }
  1049. UART_EXIT_CRITICAL_ISR(&uart_selectlock);
  1050. uart_hal_clr_intsts_mask(&(uart_context[uart_num].hal), UART_INTR_FRAM_ERR);
  1051. uart_event.type = UART_FRAME_ERR;
  1052. } else if (uart_intr_status & UART_INTR_PARITY_ERR) {
  1053. UART_ENTER_CRITICAL_ISR(&uart_selectlock);
  1054. if (p_uart->uart_select_notif_callback) {
  1055. p_uart->uart_select_notif_callback(uart_num, UART_SELECT_ERROR_NOTIF, &HPTaskAwoken);
  1056. need_yield |= (HPTaskAwoken == pdTRUE);
  1057. }
  1058. UART_EXIT_CRITICAL_ISR(&uart_selectlock);
  1059. uart_hal_clr_intsts_mask(&(uart_context[uart_num].hal), UART_INTR_PARITY_ERR);
  1060. uart_event.type = UART_PARITY_ERR;
  1061. } else if (uart_intr_status & UART_INTR_TX_BRK_DONE) {
  1062. UART_ENTER_CRITICAL_ISR(&(uart_context[uart_num].spinlock));
  1063. uart_hal_tx_break(&(uart_context[uart_num].hal), 0);
  1064. uart_hal_disable_intr_mask(&(uart_context[uart_num].hal), UART_INTR_TX_BRK_DONE);
  1065. if (p_uart->tx_brk_flg == 1) {
  1066. uart_hal_ena_intr_mask(&(uart_context[uart_num].hal), UART_INTR_TXFIFO_EMPTY);
  1067. }
  1068. UART_EXIT_CRITICAL_ISR(&(uart_context[uart_num].spinlock));
  1069. uart_hal_clr_intsts_mask(&(uart_context[uart_num].hal), UART_INTR_TX_BRK_DONE);
  1070. if (p_uart->tx_brk_flg == 1) {
  1071. p_uart->tx_brk_flg = 0;
  1072. p_uart->tx_waiting_brk = 0;
  1073. } else {
  1074. xSemaphoreGiveFromISR(p_uart->tx_brk_sem, &HPTaskAwoken);
  1075. need_yield |= (HPTaskAwoken == pdTRUE);
  1076. }
  1077. } else if (uart_intr_status & UART_INTR_TX_BRK_IDLE) {
  1078. UART_ENTER_CRITICAL_ISR(&(uart_context[uart_num].spinlock));
  1079. uart_hal_disable_intr_mask(&(uart_context[uart_num].hal), UART_INTR_TX_BRK_IDLE);
  1080. UART_EXIT_CRITICAL_ISR(&(uart_context[uart_num].spinlock));
  1081. uart_hal_clr_intsts_mask(&(uart_context[uart_num].hal), UART_INTR_TX_BRK_IDLE);
  1082. } else if (uart_intr_status & UART_INTR_CMD_CHAR_DET) {
  1083. uart_hal_clr_intsts_mask(&(uart_context[uart_num].hal), UART_INTR_CMD_CHAR_DET);
  1084. uart_event.type = UART_PATTERN_DET;
  1085. } else if ((uart_intr_status & UART_INTR_RS485_PARITY_ERR)
  1086. || (uart_intr_status & UART_INTR_RS485_FRM_ERR)
  1087. || (uart_intr_status & UART_INTR_RS485_CLASH)) {
  1088. // RS485 collision or frame error interrupt triggered
  1089. UART_ENTER_CRITICAL_ISR(&(uart_context[uart_num].spinlock));
  1090. uart_hal_rxfifo_rst(&(uart_context[uart_num].hal));
  1091. // Set collision detection flag
  1092. p_uart_obj[uart_num]->coll_det_flg = true;
  1093. UART_EXIT_CRITICAL_ISR(&(uart_context[uart_num].spinlock));
  1094. uart_hal_clr_intsts_mask(&(uart_context[uart_num].hal), UART_INTR_RS485_CLASH | UART_INTR_RS485_FRM_ERR | UART_INTR_RS485_PARITY_ERR);
  1095. uart_event.type = UART_EVENT_MAX;
  1096. } else if (uart_intr_status & UART_INTR_TX_DONE) {
  1097. if (UART_IS_MODE_SET(uart_num, UART_MODE_RS485_HALF_DUPLEX) && uart_hal_is_tx_idle(&(uart_context[uart_num].hal)) != true) {
  1098. // The TX_DONE interrupt is triggered but transmit is active
  1099. // then postpone interrupt processing for next interrupt
  1100. uart_event.type = UART_EVENT_MAX;
  1101. } else {
  1102. // Workaround for RS485: If the RS485 half duplex mode is active
  1103. // and transmitter is in idle state then reset received buffer and reset RTS pin
  1104. // skip this behavior for other UART modes
  1105. uart_hal_clr_intsts_mask(&(uart_context[uart_num].hal), UART_INTR_TX_DONE);
  1106. UART_ENTER_CRITICAL_ISR(&(uart_context[uart_num].spinlock));
  1107. uart_hal_disable_intr_mask(&(uart_context[uart_num].hal), UART_INTR_TX_DONE);
  1108. if (UART_IS_MODE_SET(uart_num, UART_MODE_RS485_HALF_DUPLEX)) {
  1109. uart_hal_rxfifo_rst(&(uart_context[uart_num].hal));
  1110. uart_hal_set_rts(&(uart_context[uart_num].hal), 1);
  1111. }
  1112. UART_EXIT_CRITICAL_ISR(&(uart_context[uart_num].spinlock));
  1113. xSemaphoreGiveFromISR(p_uart_obj[uart_num]->tx_done_sem, &HPTaskAwoken);
  1114. need_yield |= (HPTaskAwoken == pdTRUE);
  1115. }
  1116. }
  1117. #if SOC_UART_SUPPORT_WAKEUP_INT
  1118. else if (uart_intr_status & UART_INTR_WAKEUP) {
  1119. uart_hal_clr_intsts_mask(&(uart_context[uart_num].hal), UART_INTR_WAKEUP);
  1120. uart_event.type = UART_WAKEUP;
  1121. }
  1122. #endif
  1123. else {
  1124. uart_hal_clr_intsts_mask(&(uart_context[uart_num].hal), uart_intr_status); /*simply clear all other intr status*/
  1125. uart_event.type = UART_EVENT_MAX;
  1126. }
  1127. if (uart_event.type != UART_EVENT_MAX && p_uart->event_queue) {
  1128. sent = xQueueSendFromISR(p_uart->event_queue, (void * )&uart_event, &HPTaskAwoken);
  1129. need_yield |= (HPTaskAwoken == pdTRUE);
  1130. if (sent == pdFALSE) {
  1131. #ifndef CONFIG_UART_ISR_IN_IRAM //Only log if ISR is not in IRAM
  1132. ESP_EARLY_LOGV(UART_TAG, "UART event queue full");
  1133. #endif
  1134. }
  1135. }
  1136. }
  1137. if (need_yield) {
  1138. portYIELD_FROM_ISR();
  1139. }
  1140. }
  1141. /**************************************************************/
  1142. esp_err_t uart_wait_tx_done(uart_port_t uart_num, TickType_t ticks_to_wait)
  1143. {
  1144. ESP_RETURN_ON_FALSE((uart_num < UART_NUM_MAX), ESP_FAIL, UART_TAG, "uart_num error");
  1145. ESP_RETURN_ON_FALSE((p_uart_obj[uart_num]), ESP_FAIL, UART_TAG, "uart driver error");
  1146. BaseType_t res;
  1147. TickType_t ticks_start = xTaskGetTickCount();
  1148. //Take tx_mux
  1149. res = xSemaphoreTake(p_uart_obj[uart_num]->tx_mux, (TickType_t)ticks_to_wait);
  1150. if (res == pdFALSE) {
  1151. return ESP_ERR_TIMEOUT;
  1152. }
  1153. // Check the enable status of TX_DONE: If already enabled, then let the isr handle the status bit;
  1154. // If not enabled, then make sure to clear the status bit before enabling the TX_DONE interrupt bit
  1155. UART_ENTER_CRITICAL(&(uart_context[uart_num].spinlock));
  1156. bool is_rs485_mode = UART_IS_MODE_SET(uart_num, UART_MODE_RS485_HALF_DUPLEX);
  1157. bool disabled = !(uart_hal_get_intr_ena_status(&(uart_context[uart_num].hal)) & UART_INTR_TX_DONE);
  1158. // For RS485 mode, TX_DONE interrupt is enabled for every tx transmission, so there shouldn't be a case of
  1159. // interrupt not enabled but raw bit is set.
  1160. assert(!(is_rs485_mode &&
  1161. disabled &&
  1162. uart_hal_get_intraw_mask(&(uart_context[uart_num].hal)) & UART_INTR_TX_DONE));
  1163. // If decided to register for the TX_DONE event, then we should clear any possible old tx transmission status.
  1164. // The clear operation of RS485 mode should only be handled in isr or when writing to tx fifo.
  1165. if (disabled && !is_rs485_mode) {
  1166. uart_hal_clr_intsts_mask(&(uart_context[uart_num].hal), UART_INTR_TX_DONE);
  1167. }
  1168. UART_EXIT_CRITICAL(&(uart_context[uart_num].spinlock));
  1169. xSemaphoreTake(p_uart_obj[uart_num]->tx_done_sem, 0);
  1170. // FSM status register update comes later than TX_DONE interrupt raw bit raise
  1171. // The maximum time takes for FSM status register to update is (6 APB clock cycles + 3 UART core clock cycles)
  1172. // Therefore, to avoid the situation of TX_DONE bit being cleared but FSM didn't be recognized as IDLE (which
  1173. // would lead to timeout), a delay of 2us is added in between.
  1174. esp_rom_delay_us(2);
  1175. if (uart_hal_is_tx_idle(&(uart_context[uart_num].hal))) {
  1176. xSemaphoreGive(p_uart_obj[uart_num]->tx_mux);
  1177. return ESP_OK;
  1178. }
  1179. UART_ENTER_CRITICAL(&(uart_context[uart_num].spinlock));
  1180. uart_hal_ena_intr_mask(&(uart_context[uart_num].hal), UART_INTR_TX_DONE);
  1181. UART_EXIT_CRITICAL(&(uart_context[uart_num].spinlock));
  1182. TickType_t ticks_end = xTaskGetTickCount();
  1183. if (ticks_end - ticks_start > ticks_to_wait) {
  1184. ticks_to_wait = 0;
  1185. } else {
  1186. ticks_to_wait = ticks_to_wait - (ticks_end - ticks_start);
  1187. }
  1188. //take 2nd tx_done_sem, wait given from ISR
  1189. res = xSemaphoreTake(p_uart_obj[uart_num]->tx_done_sem, (TickType_t)ticks_to_wait);
  1190. if (res == pdFALSE) {
  1191. // The TX_DONE interrupt will be disabled in ISR
  1192. xSemaphoreGive(p_uart_obj[uart_num]->tx_mux);
  1193. return ESP_ERR_TIMEOUT;
  1194. }
  1195. xSemaphoreGive(p_uart_obj[uart_num]->tx_mux);
  1196. return ESP_OK;
  1197. }
  1198. int uart_tx_chars(uart_port_t uart_num, const char *buffer, uint32_t len)
  1199. {
  1200. ESP_RETURN_ON_FALSE((uart_num < UART_NUM_MAX), (-1), UART_TAG, "uart_num error");
  1201. ESP_RETURN_ON_FALSE((p_uart_obj[uart_num]), (-1), UART_TAG, "uart driver error");
  1202. ESP_RETURN_ON_FALSE(buffer, (-1), UART_TAG, "buffer null");
  1203. if (len == 0) {
  1204. return 0;
  1205. }
  1206. int tx_len = 0;
  1207. xSemaphoreTake(p_uart_obj[uart_num]->tx_mux, (TickType_t)portMAX_DELAY);
  1208. tx_len = (int)uart_enable_tx_write_fifo(uart_num, (const uint8_t *) buffer, len);
  1209. xSemaphoreGive(p_uart_obj[uart_num]->tx_mux);
  1210. return tx_len;
  1211. }
  1212. static int uart_tx_all(uart_port_t uart_num, const char *src, size_t size, bool brk_en, int brk_len)
  1213. {
  1214. if (size == 0) {
  1215. return 0;
  1216. }
  1217. size_t original_size = size;
  1218. //lock for uart_tx
  1219. xSemaphoreTake(p_uart_obj[uart_num]->tx_mux, (TickType_t)portMAX_DELAY);
  1220. p_uart_obj[uart_num]->coll_det_flg = false;
  1221. if (p_uart_obj[uart_num]->tx_buf_size > 0) {
  1222. size_t max_size = xRingbufferGetMaxItemSize(p_uart_obj[uart_num]->tx_ring_buf);
  1223. int offset = 0;
  1224. uart_tx_data_t evt;
  1225. evt.tx_data.size = size;
  1226. evt.tx_data.brk_len = brk_len;
  1227. if (brk_en) {
  1228. evt.type = UART_DATA_BREAK;
  1229. } else {
  1230. evt.type = UART_DATA;
  1231. }
  1232. xRingbufferSend(p_uart_obj[uart_num]->tx_ring_buf, (void *) &evt, sizeof(uart_tx_data_t), portMAX_DELAY);
  1233. while (size > 0) {
  1234. size_t send_size = size > max_size / 2 ? max_size / 2 : size;
  1235. xRingbufferSend(p_uart_obj[uart_num]->tx_ring_buf, (void *) (src + offset), send_size, portMAX_DELAY);
  1236. size -= send_size;
  1237. offset += send_size;
  1238. uart_enable_tx_intr(uart_num, 1, UART_THRESHOLD_NUM(uart_num, UART_EMPTY_THRESH_DEFAULT));
  1239. }
  1240. } else {
  1241. while (size) {
  1242. //semaphore for tx_fifo available
  1243. if (pdTRUE == xSemaphoreTake(p_uart_obj[uart_num]->tx_fifo_sem, (TickType_t)portMAX_DELAY)) {
  1244. uint32_t sent = uart_enable_tx_write_fifo(uart_num, (const uint8_t *) src, size);
  1245. if (sent < size) {
  1246. p_uart_obj[uart_num]->tx_waiting_fifo = true;
  1247. uart_enable_tx_intr(uart_num, 1, UART_THRESHOLD_NUM(uart_num, UART_EMPTY_THRESH_DEFAULT));
  1248. }
  1249. size -= sent;
  1250. src += sent;
  1251. }
  1252. }
  1253. if (brk_en) {
  1254. uart_hal_clr_intsts_mask(&(uart_context[uart_num].hal), UART_INTR_TX_BRK_DONE);
  1255. UART_ENTER_CRITICAL(&(uart_context[uart_num].spinlock));
  1256. uart_hal_tx_break(&(uart_context[uart_num].hal), brk_len);
  1257. uart_hal_ena_intr_mask(&(uart_context[uart_num].hal), UART_INTR_TX_BRK_DONE);
  1258. UART_EXIT_CRITICAL(&(uart_context[uart_num].spinlock));
  1259. xSemaphoreTake(p_uart_obj[uart_num]->tx_brk_sem, (TickType_t)portMAX_DELAY);
  1260. }
  1261. xSemaphoreGive(p_uart_obj[uart_num]->tx_fifo_sem);
  1262. }
  1263. xSemaphoreGive(p_uart_obj[uart_num]->tx_mux);
  1264. return original_size;
  1265. }
  1266. int uart_write_bytes(uart_port_t uart_num, const void *src, size_t size)
  1267. {
  1268. ESP_RETURN_ON_FALSE((uart_num < UART_NUM_MAX), (-1), UART_TAG, "uart_num error");
  1269. ESP_RETURN_ON_FALSE((p_uart_obj[uart_num] != NULL), (-1), UART_TAG, "uart driver error");
  1270. ESP_RETURN_ON_FALSE(src, (-1), UART_TAG, "buffer null");
  1271. return uart_tx_all(uart_num, src, size, 0, 0);
  1272. }
  1273. int uart_write_bytes_with_break(uart_port_t uart_num, const void *src, size_t size, int brk_len)
  1274. {
  1275. ESP_RETURN_ON_FALSE((uart_num < UART_NUM_MAX), (-1), UART_TAG, "uart_num error");
  1276. ESP_RETURN_ON_FALSE((p_uart_obj[uart_num]), (-1), UART_TAG, "uart driver error");
  1277. ESP_RETURN_ON_FALSE((size > 0), (-1), UART_TAG, "uart size error");
  1278. ESP_RETURN_ON_FALSE((src), (-1), UART_TAG, "uart data null");
  1279. ESP_RETURN_ON_FALSE((brk_len > 0 && brk_len < 256), (-1), UART_TAG, "break_num error");
  1280. return uart_tx_all(uart_num, src, size, 1, brk_len);
  1281. }
  1282. static bool uart_check_buf_full(uart_port_t uart_num)
  1283. {
  1284. if (p_uart_obj[uart_num]->rx_buffer_full_flg) {
  1285. BaseType_t res = xRingbufferSend(p_uart_obj[uart_num]->rx_ring_buf, p_uart_obj[uart_num]->rx_data_buf, p_uart_obj[uart_num]->rx_stash_len, 1);
  1286. if (res == pdTRUE) {
  1287. UART_ENTER_CRITICAL(&(uart_context[uart_num].spinlock));
  1288. p_uart_obj[uart_num]->rx_buffered_len += p_uart_obj[uart_num]->rx_stash_len;
  1289. p_uart_obj[uart_num]->rx_buffer_full_flg = false;
  1290. UART_EXIT_CRITICAL(&(uart_context[uart_num].spinlock));
  1291. /* Only re-activate UART_INTR_RXFIFO_TOUT or UART_INTR_RXFIFO_FULL
  1292. * interrupts if they were NOT explicitly disabled by the user. */
  1293. uart_reenable_intr_mask(p_uart_obj[uart_num]->uart_num, UART_INTR_RXFIFO_TOUT | UART_INTR_RXFIFO_FULL);
  1294. return true;
  1295. }
  1296. }
  1297. return false;
  1298. }
  1299. int uart_read_bytes(uart_port_t uart_num, void *buf, uint32_t length, TickType_t ticks_to_wait)
  1300. {
  1301. ESP_RETURN_ON_FALSE((uart_num < UART_NUM_MAX), (-1), UART_TAG, "uart_num error");
  1302. ESP_RETURN_ON_FALSE((buf), (-1), UART_TAG, "uart data null");
  1303. ESP_RETURN_ON_FALSE((p_uart_obj[uart_num]), (-1), UART_TAG, "uart driver error");
  1304. uint8_t *data = NULL;
  1305. size_t size;
  1306. size_t copy_len = 0;
  1307. int len_tmp;
  1308. if (xSemaphoreTake(p_uart_obj[uart_num]->rx_mux, (TickType_t)ticks_to_wait) != pdTRUE) {
  1309. return -1;
  1310. }
  1311. while (length) {
  1312. if (p_uart_obj[uart_num]->rx_cur_remain == 0) {
  1313. data = (uint8_t *) xRingbufferReceive(p_uart_obj[uart_num]->rx_ring_buf, &size, (TickType_t) ticks_to_wait);
  1314. if (data) {
  1315. p_uart_obj[uart_num]->rx_head_ptr = data;
  1316. p_uart_obj[uart_num]->rx_ptr = data;
  1317. p_uart_obj[uart_num]->rx_cur_remain = size;
  1318. } else {
  1319. //When using dual cores, `rx_buffer_full_flg` may read and write on different cores at same time,
  1320. //which may lose synchronization. So we also need to call `uart_check_buf_full` once when ringbuffer is empty
  1321. //to solve the possible asynchronous issues.
  1322. if (uart_check_buf_full(uart_num)) {
  1323. //This condition will never be true if `uart_read_bytes`
  1324. //and `uart_rx_intr_handler_default` are scheduled on the same core.
  1325. continue;
  1326. } else {
  1327. xSemaphoreGive(p_uart_obj[uart_num]->rx_mux);
  1328. return copy_len;
  1329. }
  1330. }
  1331. }
  1332. if (p_uart_obj[uart_num]->rx_cur_remain > length) {
  1333. len_tmp = length;
  1334. } else {
  1335. len_tmp = p_uart_obj[uart_num]->rx_cur_remain;
  1336. }
  1337. memcpy((uint8_t *)buf + copy_len, p_uart_obj[uart_num]->rx_ptr, len_tmp);
  1338. UART_ENTER_CRITICAL(&(uart_context[uart_num].spinlock));
  1339. p_uart_obj[uart_num]->rx_buffered_len -= len_tmp;
  1340. uart_pattern_queue_update(uart_num, len_tmp);
  1341. p_uart_obj[uart_num]->rx_ptr += len_tmp;
  1342. UART_EXIT_CRITICAL(&(uart_context[uart_num].spinlock));
  1343. p_uart_obj[uart_num]->rx_cur_remain -= len_tmp;
  1344. copy_len += len_tmp;
  1345. length -= len_tmp;
  1346. if (p_uart_obj[uart_num]->rx_cur_remain == 0) {
  1347. vRingbufferReturnItem(p_uart_obj[uart_num]->rx_ring_buf, p_uart_obj[uart_num]->rx_head_ptr);
  1348. p_uart_obj[uart_num]->rx_head_ptr = NULL;
  1349. p_uart_obj[uart_num]->rx_ptr = NULL;
  1350. uart_check_buf_full(uart_num);
  1351. }
  1352. }
  1353. xSemaphoreGive(p_uart_obj[uart_num]->rx_mux);
  1354. return copy_len;
  1355. }
  1356. esp_err_t uart_get_buffered_data_len(uart_port_t uart_num, size_t *size)
  1357. {
  1358. ESP_RETURN_ON_FALSE((uart_num < UART_NUM_MAX), ESP_FAIL, UART_TAG, "uart_num error");
  1359. ESP_RETURN_ON_FALSE((p_uart_obj[uart_num]), ESP_FAIL, UART_TAG, "uart driver error");
  1360. UART_ENTER_CRITICAL(&(uart_context[uart_num].spinlock));
  1361. *size = p_uart_obj[uart_num]->rx_buffered_len;
  1362. UART_EXIT_CRITICAL(&(uart_context[uart_num].spinlock));
  1363. return ESP_OK;
  1364. }
  1365. esp_err_t uart_get_tx_buffer_free_size(uart_port_t uart_num, size_t *size)
  1366. {
  1367. ESP_RETURN_ON_FALSE((uart_num < UART_NUM_MAX), ESP_ERR_INVALID_ARG, UART_TAG, "uart_num error");
  1368. ESP_RETURN_ON_FALSE((p_uart_obj[uart_num]), ESP_ERR_INVALID_ARG, UART_TAG, "uart driver error");
  1369. ESP_RETURN_ON_FALSE((size != NULL), ESP_ERR_INVALID_ARG, UART_TAG, "arg pointer is NULL");
  1370. *size = p_uart_obj[uart_num]->tx_buf_size - p_uart_obj[uart_num]->tx_len_tot;
  1371. return ESP_OK;
  1372. }
  1373. esp_err_t uart_flush(uart_port_t uart_num) __attribute__((alias("uart_flush_input")));
  1374. esp_err_t uart_flush_input(uart_port_t uart_num)
  1375. {
  1376. ESP_RETURN_ON_FALSE((uart_num < UART_NUM_MAX), ESP_FAIL, UART_TAG, "uart_num error");
  1377. ESP_RETURN_ON_FALSE((p_uart_obj[uart_num]), ESP_FAIL, UART_TAG, "uart driver error");
  1378. uart_obj_t *p_uart = p_uart_obj[uart_num];
  1379. uint8_t *data;
  1380. size_t size;
  1381. //rx sem protect the ring buffer read related functions
  1382. xSemaphoreTake(p_uart->rx_mux, (TickType_t)portMAX_DELAY);
  1383. UART_ENTER_CRITICAL(&(uart_context[uart_num].spinlock));
  1384. uart_hal_disable_intr_mask(&(uart_context[uart_num].hal), UART_INTR_RXFIFO_FULL | UART_INTR_RXFIFO_TOUT);
  1385. UART_EXIT_CRITICAL(&(uart_context[uart_num].spinlock));
  1386. while (true) {
  1387. if (p_uart->rx_head_ptr) {
  1388. vRingbufferReturnItem(p_uart->rx_ring_buf, p_uart->rx_head_ptr);
  1389. UART_ENTER_CRITICAL(&(uart_context[uart_num].spinlock));
  1390. p_uart_obj[uart_num]->rx_buffered_len -= p_uart->rx_cur_remain;
  1391. uart_pattern_queue_update(uart_num, p_uart->rx_cur_remain);
  1392. UART_EXIT_CRITICAL(&(uart_context[uart_num].spinlock));
  1393. p_uart->rx_ptr = NULL;
  1394. p_uart->rx_cur_remain = 0;
  1395. p_uart->rx_head_ptr = NULL;
  1396. }
  1397. data = (uint8_t*) xRingbufferReceive(p_uart->rx_ring_buf, &size, (TickType_t) 0);
  1398. if(data == NULL) {
  1399. bool error = false;
  1400. UART_ENTER_CRITICAL(&(uart_context[uart_num].spinlock));
  1401. if( p_uart_obj[uart_num]->rx_buffered_len != 0 ) {
  1402. p_uart_obj[uart_num]->rx_buffered_len = 0;
  1403. error = true;
  1404. }
  1405. //We also need to clear the `rx_buffer_full_flg` here.
  1406. p_uart_obj[uart_num]->rx_buffer_full_flg = false;
  1407. UART_EXIT_CRITICAL(&(uart_context[uart_num].spinlock));
  1408. if (error) {
  1409. // this must be called outside the critical section
  1410. ESP_LOGE(UART_TAG, "rx_buffered_len error");
  1411. }
  1412. break;
  1413. }
  1414. UART_ENTER_CRITICAL(&(uart_context[uart_num].spinlock));
  1415. p_uart_obj[uart_num]->rx_buffered_len -= size;
  1416. uart_pattern_queue_update(uart_num, size);
  1417. UART_EXIT_CRITICAL(&(uart_context[uart_num].spinlock));
  1418. vRingbufferReturnItem(p_uart->rx_ring_buf, data);
  1419. if (p_uart_obj[uart_num]->rx_buffer_full_flg) {
  1420. BaseType_t res = xRingbufferSend(p_uart_obj[uart_num]->rx_ring_buf, p_uart_obj[uart_num]->rx_data_buf, p_uart_obj[uart_num]->rx_stash_len, 1);
  1421. if (res == pdTRUE) {
  1422. UART_ENTER_CRITICAL(&(uart_context[uart_num].spinlock));
  1423. p_uart_obj[uart_num]->rx_buffered_len += p_uart_obj[uart_num]->rx_stash_len;
  1424. p_uart_obj[uart_num]->rx_buffer_full_flg = false;
  1425. UART_EXIT_CRITICAL(&(uart_context[uart_num].spinlock));
  1426. }
  1427. }
  1428. }
  1429. p_uart->rx_ptr = NULL;
  1430. p_uart->rx_cur_remain = 0;
  1431. p_uart->rx_head_ptr = NULL;
  1432. uart_hal_rxfifo_rst(&(uart_context[uart_num].hal));
  1433. /* Only re-enable UART_INTR_RXFIFO_TOUT or UART_INTR_RXFIFO_FULL if they
  1434. * were explicitly enabled by the user. */
  1435. uart_reenable_intr_mask(uart_num, UART_INTR_RXFIFO_TOUT | UART_INTR_RXFIFO_FULL);
  1436. xSemaphoreGive(p_uart->rx_mux);
  1437. return ESP_OK;
  1438. }
  1439. static void uart_free_driver_obj(uart_obj_t *uart_obj)
  1440. {
  1441. if (uart_obj->tx_fifo_sem) {
  1442. vSemaphoreDeleteWithCaps(uart_obj->tx_fifo_sem);
  1443. }
  1444. if (uart_obj->tx_done_sem) {
  1445. vSemaphoreDeleteWithCaps(uart_obj->tx_done_sem);
  1446. }
  1447. if (uart_obj->tx_brk_sem) {
  1448. vSemaphoreDeleteWithCaps(uart_obj->tx_brk_sem);
  1449. }
  1450. if (uart_obj->tx_mux) {
  1451. vSemaphoreDeleteWithCaps(uart_obj->tx_mux);
  1452. }
  1453. if (uart_obj->rx_mux) {
  1454. vSemaphoreDeleteWithCaps(uart_obj->rx_mux);
  1455. }
  1456. if (uart_obj->event_queue) {
  1457. vQueueDeleteWithCaps(uart_obj->event_queue);
  1458. }
  1459. if (uart_obj->rx_ring_buf) {
  1460. vRingbufferDeleteWithCaps(uart_obj->rx_ring_buf);
  1461. }
  1462. if (uart_obj->tx_ring_buf) {
  1463. vRingbufferDeleteWithCaps(uart_obj->tx_ring_buf);
  1464. }
  1465. heap_caps_free(uart_obj->rx_data_buf);
  1466. heap_caps_free(uart_obj);
  1467. }
  1468. static uart_obj_t *uart_alloc_driver_obj(uart_port_t uart_num, int event_queue_size, int tx_buffer_size, int rx_buffer_size)
  1469. {
  1470. uart_obj_t *uart_obj = heap_caps_calloc(1, sizeof(uart_obj_t), UART_MALLOC_CAPS);
  1471. if (!uart_obj) {
  1472. return NULL;
  1473. }
  1474. uart_obj->rx_data_buf = heap_caps_calloc(UART_HW_FIFO_LEN(uart_num), sizeof(uint32_t), UART_MALLOC_CAPS);
  1475. if (!uart_obj->rx_data_buf) {
  1476. goto err;
  1477. }
  1478. if (event_queue_size > 0) {
  1479. uart_obj->event_queue = xQueueCreateWithCaps(event_queue_size, sizeof(uart_event_t), UART_MALLOC_CAPS);
  1480. if (!uart_obj->event_queue) {
  1481. goto err;
  1482. }
  1483. }
  1484. if (tx_buffer_size > 0) {
  1485. uart_obj->tx_ring_buf = xRingbufferCreateWithCaps(tx_buffer_size, RINGBUF_TYPE_NOSPLIT, UART_MALLOC_CAPS);
  1486. if (!uart_obj->tx_ring_buf) {
  1487. goto err;
  1488. }
  1489. }
  1490. uart_obj->rx_ring_buf = xRingbufferCreateWithCaps(rx_buffer_size, RINGBUF_TYPE_BYTEBUF, UART_MALLOC_CAPS);
  1491. uart_obj->tx_mux = xSemaphoreCreateMutexWithCaps(UART_MALLOC_CAPS);
  1492. uart_obj->rx_mux = xSemaphoreCreateMutexWithCaps(UART_MALLOC_CAPS);
  1493. uart_obj->tx_brk_sem = xSemaphoreCreateBinaryWithCaps(UART_MALLOC_CAPS);
  1494. uart_obj->tx_done_sem = xSemaphoreCreateBinaryWithCaps(UART_MALLOC_CAPS);
  1495. uart_obj->tx_fifo_sem = xSemaphoreCreateBinaryWithCaps(UART_MALLOC_CAPS);
  1496. if (!uart_obj->rx_ring_buf || !uart_obj->rx_mux || !uart_obj->tx_mux || !uart_obj->tx_brk_sem ||
  1497. !uart_obj->tx_done_sem || !uart_obj->tx_fifo_sem) {
  1498. goto err;
  1499. }
  1500. return uart_obj;
  1501. err:
  1502. uart_free_driver_obj(uart_obj);
  1503. return NULL;
  1504. }
  1505. esp_err_t uart_driver_install(uart_port_t uart_num, int rx_buffer_size, int tx_buffer_size, int event_queue_size, QueueHandle_t *uart_queue, int intr_alloc_flags)
  1506. {
  1507. esp_err_t ret;
  1508. #ifdef CONFIG_ESP_SYSTEM_GDBSTUB_RUNTIME
  1509. ESP_RETURN_ON_FALSE((uart_num != CONFIG_ESP_CONSOLE_UART_NUM), ESP_FAIL, UART_TAG, "UART used by GDB-stubs! Please disable GDB in menuconfig.");
  1510. #endif // CONFIG_ESP_SYSTEM_GDBSTUB_RUNTIME
  1511. ESP_RETURN_ON_FALSE((uart_num < UART_NUM_MAX), ESP_FAIL, UART_TAG, "uart_num error");
  1512. ESP_RETURN_ON_FALSE((rx_buffer_size > UART_HW_FIFO_LEN(uart_num)), ESP_FAIL, UART_TAG, "uart rx buffer length error");
  1513. ESP_RETURN_ON_FALSE((tx_buffer_size > UART_HW_FIFO_LEN(uart_num)) || (tx_buffer_size == 0), ESP_FAIL, UART_TAG, "uart tx buffer length error");
  1514. #if CONFIG_UART_ISR_IN_IRAM
  1515. if ((intr_alloc_flags & ESP_INTR_FLAG_IRAM) == 0) {
  1516. ESP_LOGI(UART_TAG, "ESP_INTR_FLAG_IRAM flag not set while CONFIG_UART_ISR_IN_IRAM is enabled, flag updated");
  1517. intr_alloc_flags |= ESP_INTR_FLAG_IRAM;
  1518. }
  1519. #else
  1520. if ((intr_alloc_flags & ESP_INTR_FLAG_IRAM) != 0) {
  1521. ESP_LOGW(UART_TAG, "ESP_INTR_FLAG_IRAM flag is set while CONFIG_UART_ISR_IN_IRAM is not enabled, flag updated");
  1522. intr_alloc_flags &= ~ESP_INTR_FLAG_IRAM;
  1523. }
  1524. #endif
  1525. if (p_uart_obj[uart_num] == NULL) {
  1526. p_uart_obj[uart_num] = uart_alloc_driver_obj(uart_num, event_queue_size, tx_buffer_size, rx_buffer_size);
  1527. if (p_uart_obj[uart_num] == NULL) {
  1528. ESP_LOGE(UART_TAG, "UART driver malloc error");
  1529. return ESP_FAIL;
  1530. }
  1531. p_uart_obj[uart_num]->uart_num = uart_num;
  1532. p_uart_obj[uart_num]->uart_mode = UART_MODE_UART;
  1533. p_uart_obj[uart_num]->coll_det_flg = false;
  1534. p_uart_obj[uart_num]->rx_always_timeout_flg = false;
  1535. p_uart_obj[uart_num]->event_queue_size = event_queue_size;
  1536. p_uart_obj[uart_num]->tx_ptr = NULL;
  1537. p_uart_obj[uart_num]->tx_head = NULL;
  1538. p_uart_obj[uart_num]->tx_len_tot = 0;
  1539. p_uart_obj[uart_num]->tx_brk_flg = 0;
  1540. p_uart_obj[uart_num]->tx_brk_len = 0;
  1541. p_uart_obj[uart_num]->tx_waiting_brk = 0;
  1542. p_uart_obj[uart_num]->rx_buffered_len = 0;
  1543. p_uart_obj[uart_num]->rx_buffer_full_flg = false;
  1544. p_uart_obj[uart_num]->tx_waiting_fifo = false;
  1545. p_uart_obj[uart_num]->rx_ptr = NULL;
  1546. p_uart_obj[uart_num]->rx_cur_remain = 0;
  1547. p_uart_obj[uart_num]->rx_int_usr_mask = UART_INTR_RXFIFO_FULL | UART_INTR_RXFIFO_TOUT;
  1548. p_uart_obj[uart_num]->rx_head_ptr = NULL;
  1549. p_uart_obj[uart_num]->tx_buf_size = tx_buffer_size;
  1550. p_uart_obj[uart_num]->uart_select_notif_callback = NULL;
  1551. xSemaphoreGive(p_uart_obj[uart_num]->tx_fifo_sem);
  1552. uart_pattern_queue_reset(uart_num, UART_PATTERN_DET_QLEN_DEFAULT);
  1553. if (uart_queue) {
  1554. *uart_queue = p_uart_obj[uart_num]->event_queue;
  1555. ESP_LOGI(UART_TAG, "queue free spaces: %" PRIu32, (uint32_t)uxQueueSpacesAvailable(p_uart_obj[uart_num]->event_queue));
  1556. }
  1557. } else {
  1558. ESP_LOGE(UART_TAG, "UART driver already installed");
  1559. return ESP_FAIL;
  1560. }
  1561. uart_intr_config_t uart_intr = {
  1562. .intr_enable_mask = UART_INTR_CONFIG_FLAG,
  1563. .rxfifo_full_thresh = UART_THRESHOLD_NUM(uart_num, UART_FULL_THRESH_DEFAULT),
  1564. .rx_timeout_thresh = UART_TOUT_THRESH_DEFAULT,
  1565. .txfifo_empty_intr_thresh = UART_THRESHOLD_NUM(uart_num, UART_EMPTY_THRESH_DEFAULT),
  1566. };
  1567. uart_module_enable(uart_num);
  1568. uart_hal_disable_intr_mask(&(uart_context[uart_num].hal), UART_LL_INTR_MASK);
  1569. uart_hal_clr_intsts_mask(&(uart_context[uart_num].hal), UART_LL_INTR_MASK);
  1570. ret = esp_intr_alloc(uart_periph_signal[uart_num].irq, intr_alloc_flags,
  1571. uart_rx_intr_handler_default, p_uart_obj[uart_num],
  1572. &p_uart_obj[uart_num]->intr_handle);
  1573. ESP_GOTO_ON_ERROR(ret, err, UART_TAG, "Could not allocate an interrupt for UART");
  1574. ret = uart_intr_config(uart_num, &uart_intr);
  1575. ESP_GOTO_ON_ERROR(ret, err, UART_TAG, "Could not configure the interrupt for UART");
  1576. return ret;
  1577. err:
  1578. uart_driver_delete(uart_num);
  1579. return ret;
  1580. }
  1581. //Make sure no other tasks are still using UART before you call this function
  1582. esp_err_t uart_driver_delete(uart_port_t uart_num)
  1583. {
  1584. ESP_RETURN_ON_FALSE((uart_num < UART_NUM_MAX), ESP_FAIL, UART_TAG, "uart_num error");
  1585. if (p_uart_obj[uart_num] == NULL) {
  1586. ESP_LOGI(UART_TAG, "ALREADY NULL");
  1587. return ESP_OK;
  1588. }
  1589. esp_intr_free(p_uart_obj[uart_num]->intr_handle);
  1590. uart_disable_rx_intr(uart_num);
  1591. uart_disable_tx_intr(uart_num);
  1592. uart_pattern_link_free(uart_num);
  1593. uart_free_driver_obj(p_uart_obj[uart_num]);
  1594. p_uart_obj[uart_num] = NULL;
  1595. #if SOC_UART_SUPPORT_RTC_CLK
  1596. soc_module_clk_t sclk = 0;
  1597. uart_hal_get_sclk(&(uart_context[uart_num].hal), &sclk);
  1598. if (sclk == (soc_module_clk_t)UART_SCLK_RTC) {
  1599. periph_rtc_dig_clk8m_disable();
  1600. }
  1601. #endif
  1602. uart_module_disable(uart_num);
  1603. return ESP_OK;
  1604. }
  1605. bool uart_is_driver_installed(uart_port_t uart_num)
  1606. {
  1607. return uart_num < UART_NUM_MAX && (p_uart_obj[uart_num] != NULL);
  1608. }
  1609. void uart_set_select_notif_callback(uart_port_t uart_num, uart_select_notif_callback_t uart_select_notif_callback)
  1610. {
  1611. if (uart_num < UART_NUM_MAX && p_uart_obj[uart_num]) {
  1612. p_uart_obj[uart_num]->uart_select_notif_callback = (uart_select_notif_callback_t) uart_select_notif_callback;
  1613. }
  1614. }
  1615. portMUX_TYPE *uart_get_selectlock(void)
  1616. {
  1617. return &uart_selectlock;
  1618. }
  1619. // Set UART mode
  1620. esp_err_t uart_set_mode(uart_port_t uart_num, uart_mode_t mode)
  1621. {
  1622. ESP_RETURN_ON_FALSE((uart_num < UART_NUM_MAX), ESP_ERR_INVALID_ARG, UART_TAG, "uart_num error");
  1623. ESP_RETURN_ON_FALSE((p_uart_obj[uart_num]), ESP_ERR_INVALID_STATE, UART_TAG, "uart driver error");
  1624. if ((mode == UART_MODE_RS485_COLLISION_DETECT) || (mode == UART_MODE_RS485_APP_CTRL)
  1625. || (mode == UART_MODE_RS485_HALF_DUPLEX)) {
  1626. ESP_RETURN_ON_FALSE((!uart_hal_is_hw_rts_en(&(uart_context[uart_num].hal))), ESP_ERR_INVALID_ARG, UART_TAG,
  1627. "disable hw flowctrl before using RS485 mode");
  1628. }
  1629. if (uart_num >= SOC_UART_HP_NUM) {
  1630. ESP_RETURN_ON_FALSE((mode == UART_MODE_UART), ESP_ERR_INVALID_ARG, UART_TAG, "LP_UART can only be in normal UART mode");
  1631. }
  1632. UART_ENTER_CRITICAL(&(uart_context[uart_num].spinlock));
  1633. uart_hal_set_mode(&(uart_context[uart_num].hal), mode);
  1634. if (mode == UART_MODE_RS485_COLLISION_DETECT) {
  1635. // This mode allows read while transmitting that allows collision detection
  1636. p_uart_obj[uart_num]->coll_det_flg = false;
  1637. // Enable collision detection interrupts
  1638. uart_hal_ena_intr_mask(&(uart_context[uart_num].hal), UART_INTR_RXFIFO_TOUT
  1639. | UART_INTR_RXFIFO_FULL
  1640. | UART_INTR_RS485_CLASH
  1641. | UART_INTR_RS485_FRM_ERR
  1642. | UART_INTR_RS485_PARITY_ERR);
  1643. }
  1644. p_uart_obj[uart_num]->uart_mode = mode;
  1645. UART_EXIT_CRITICAL(&(uart_context[uart_num].spinlock));
  1646. return ESP_OK;
  1647. }
  1648. esp_err_t uart_set_rx_full_threshold(uart_port_t uart_num, int threshold)
  1649. {
  1650. ESP_RETURN_ON_FALSE((uart_num < UART_NUM_MAX), ESP_ERR_INVALID_ARG, UART_TAG, "uart_num error");
  1651. ESP_RETURN_ON_FALSE((threshold < UART_THRESHOLD_NUM(uart_num, UART_RXFIFO_FULL_THRHD_V)) && (threshold > 0), ESP_ERR_INVALID_ARG, UART_TAG,
  1652. "rx fifo full threshold value error");
  1653. if (p_uart_obj[uart_num] == NULL) {
  1654. ESP_LOGE(UART_TAG, "call uart_driver_install API first");
  1655. return ESP_ERR_INVALID_STATE;
  1656. }
  1657. UART_ENTER_CRITICAL(&(uart_context[uart_num].spinlock));
  1658. if (uart_hal_get_intr_ena_status(&(uart_context[uart_num].hal)) & UART_INTR_RXFIFO_FULL) {
  1659. uart_hal_set_rxfifo_full_thr(&(uart_context[uart_num].hal), threshold);
  1660. }
  1661. UART_EXIT_CRITICAL(&(uart_context[uart_num].spinlock));
  1662. return ESP_OK;
  1663. }
  1664. esp_err_t uart_set_tx_empty_threshold(uart_port_t uart_num, int threshold)
  1665. {
  1666. ESP_RETURN_ON_FALSE((uart_num < UART_NUM_MAX), ESP_ERR_INVALID_ARG, UART_TAG, "uart_num error");
  1667. ESP_RETURN_ON_FALSE((threshold < UART_THRESHOLD_NUM(uart_num, UART_TXFIFO_EMPTY_THRHD_V)) && (threshold > 0), ESP_ERR_INVALID_ARG, UART_TAG,
  1668. "tx fifo empty threshold value error");
  1669. if (p_uart_obj[uart_num] == NULL) {
  1670. ESP_LOGE(UART_TAG, "call uart_driver_install API first");
  1671. return ESP_ERR_INVALID_STATE;
  1672. }
  1673. UART_ENTER_CRITICAL(&(uart_context[uart_num].spinlock));
  1674. if (uart_hal_get_intr_ena_status(&(uart_context[uart_num].hal)) & UART_INTR_TXFIFO_EMPTY) {
  1675. uart_hal_set_txfifo_empty_thr(&(uart_context[uart_num].hal), threshold);
  1676. }
  1677. UART_EXIT_CRITICAL(&(uart_context[uart_num].spinlock));
  1678. return ESP_OK;
  1679. }
  1680. esp_err_t uart_set_rx_timeout(uart_port_t uart_num, const uint8_t tout_thresh)
  1681. {
  1682. ESP_RETURN_ON_FALSE((uart_num < UART_NUM_MAX), ESP_ERR_INVALID_ARG, UART_TAG, "uart_num error");
  1683. // get maximum timeout threshold
  1684. uint16_t tout_max_thresh = uart_hal_get_max_rx_timeout_thrd(&(uart_context[uart_num].hal));
  1685. if (tout_thresh > tout_max_thresh) {
  1686. ESP_LOGE(UART_TAG, "tout_thresh = %d > maximum value = %d", tout_thresh, tout_max_thresh);
  1687. return ESP_ERR_INVALID_ARG;
  1688. }
  1689. UART_ENTER_CRITICAL(&(uart_context[uart_num].spinlock));
  1690. uart_hal_set_rx_timeout(&(uart_context[uart_num].hal), tout_thresh);
  1691. UART_EXIT_CRITICAL(&(uart_context[uart_num].spinlock));
  1692. return ESP_OK;
  1693. }
  1694. esp_err_t uart_get_collision_flag(uart_port_t uart_num, bool *collision_flag)
  1695. {
  1696. ESP_RETURN_ON_FALSE((uart_num < UART_NUM_MAX), ESP_ERR_INVALID_ARG, UART_TAG, "uart_num error");
  1697. ESP_RETURN_ON_FALSE((p_uart_obj[uart_num]), ESP_FAIL, UART_TAG, "uart driver error");
  1698. ESP_RETURN_ON_FALSE((collision_flag != NULL), ESP_ERR_INVALID_ARG, UART_TAG, "wrong parameter pointer");
  1699. ESP_RETURN_ON_FALSE((UART_IS_MODE_SET(uart_num, UART_MODE_RS485_HALF_DUPLEX) || UART_IS_MODE_SET(uart_num, UART_MODE_RS485_COLLISION_DETECT)),
  1700. ESP_ERR_INVALID_ARG, UART_TAG, "wrong mode");
  1701. *collision_flag = p_uart_obj[uart_num]->coll_det_flg;
  1702. return ESP_OK;
  1703. }
  1704. esp_err_t uart_set_wakeup_threshold(uart_port_t uart_num, int wakeup_threshold)
  1705. {
  1706. ESP_RETURN_ON_FALSE((uart_num < UART_NUM_MAX), ESP_ERR_INVALID_ARG, UART_TAG, "uart_num error");
  1707. ESP_RETURN_ON_FALSE((wakeup_threshold <= UART_THRESHOLD_NUM(uart_num, UART_ACTIVE_THRESHOLD_V) && wakeup_threshold > UART_MIN_WAKEUP_THRESH), ESP_ERR_INVALID_ARG, UART_TAG,
  1708. "wakeup_threshold out of bounds");
  1709. UART_ENTER_CRITICAL(&(uart_context[uart_num].spinlock));
  1710. uart_hal_set_wakeup_thrd(&(uart_context[uart_num].hal), wakeup_threshold);
  1711. UART_EXIT_CRITICAL(&(uart_context[uart_num].spinlock));
  1712. return ESP_OK;
  1713. }
  1714. esp_err_t uart_get_wakeup_threshold(uart_port_t uart_num, int *out_wakeup_threshold)
  1715. {
  1716. ESP_RETURN_ON_FALSE((uart_num < UART_NUM_MAX), ESP_ERR_INVALID_ARG, UART_TAG, "uart_num error");
  1717. ESP_RETURN_ON_FALSE((out_wakeup_threshold != NULL), ESP_ERR_INVALID_ARG, UART_TAG, "argument is NULL");
  1718. uart_hal_get_wakeup_thrd(&(uart_context[uart_num].hal), (uint32_t *)out_wakeup_threshold);
  1719. return ESP_OK;
  1720. }
  1721. esp_err_t uart_wait_tx_idle_polling(uart_port_t uart_num)
  1722. {
  1723. ESP_RETURN_ON_FALSE((uart_num < UART_NUM_MAX), ESP_ERR_INVALID_ARG, UART_TAG, "uart_num error");
  1724. while (!uart_hal_is_tx_idle(&(uart_context[uart_num].hal)));
  1725. return ESP_OK;
  1726. }
  1727. esp_err_t uart_set_loop_back(uart_port_t uart_num, bool loop_back_en)
  1728. {
  1729. ESP_RETURN_ON_FALSE((uart_num < UART_NUM_MAX), ESP_ERR_INVALID_ARG, UART_TAG, "uart_num error");
  1730. uart_hal_set_loop_back(&(uart_context[uart_num].hal), loop_back_en);
  1731. return ESP_OK;
  1732. }
  1733. void uart_set_always_rx_timeout(uart_port_t uart_num, bool always_rx_timeout)
  1734. {
  1735. uint16_t rx_tout = uart_hal_get_rx_tout_thr(&(uart_context[uart_num].hal));
  1736. if (rx_tout) {
  1737. p_uart_obj[uart_num]->rx_always_timeout_flg = always_rx_timeout;
  1738. } else {
  1739. p_uart_obj[uart_num]->rx_always_timeout_flg = false;
  1740. }
  1741. }