uart.c 81 KB

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