uart.c 73 KB

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