uart.c 72 KB

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