uart.c 80 KB

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