uart.c 82 KB

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