uart.c 80 KB

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