uart.c 73 KB

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