uart.c 75 KB

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