uart.c 72 KB

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