uart.c 83 KB

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