uart.c 81 KB

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