uart.c 81 KB

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