uart.c 72 KB

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