uart.c 82 KB

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