uart.c 82 KB

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