uart.c 86 KB

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