uart.c 83 KB

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