uart.c 80 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767
  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. ESP_RETURN_ON_FALSE((uart_num < UART_NUM_MAX), ESP_FAIL, UART_TAG, "uart_num error");
  517. ESP_RETURN_ON_FALSE((thresh < SOC_UART_FIFO_LEN), ESP_FAIL, UART_TAG, "empty intr threshold error");
  518. uart_hal_clr_intsts_mask(&(uart_context[uart_num].hal), UART_INTR_TXFIFO_EMPTY);
  519. UART_ENTER_CRITICAL(&(uart_context[uart_num].spinlock));
  520. uart_hal_set_txfifo_empty_thr(&(uart_context[uart_num].hal), thresh);
  521. uart_hal_ena_intr_mask(&(uart_context[uart_num].hal), UART_INTR_TXFIFO_EMPTY);
  522. UART_EXIT_CRITICAL(&(uart_context[uart_num].spinlock));
  523. return ESP_OK;
  524. }
  525. 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)
  526. {
  527. int ret;
  528. ESP_RETURN_ON_FALSE((uart_num < UART_NUM_MAX), ESP_FAIL, UART_TAG, "uart_num error");
  529. UART_ENTER_CRITICAL(&(uart_context[uart_num].spinlock));
  530. ret = esp_intr_alloc(uart_periph_signal[uart_num].irq, intr_alloc_flags, fn, arg, handle);
  531. UART_EXIT_CRITICAL(&(uart_context[uart_num].spinlock));
  532. return ret;
  533. }
  534. esp_err_t uart_isr_free(uart_port_t uart_num)
  535. {
  536. esp_err_t ret;
  537. ESP_RETURN_ON_FALSE((uart_num < UART_NUM_MAX), ESP_FAIL, UART_TAG, "uart_num error");
  538. ESP_RETURN_ON_FALSE((p_uart_obj[uart_num]), ESP_FAIL, UART_TAG, "uart driver error");
  539. ESP_RETURN_ON_FALSE((p_uart_obj[uart_num]->intr_handle != NULL), ESP_ERR_INVALID_ARG, UART_TAG, "uart driver error");
  540. UART_ENTER_CRITICAL(&(uart_context[uart_num].spinlock));
  541. ret = esp_intr_free(p_uart_obj[uart_num]->intr_handle);
  542. p_uart_obj[uart_num]->intr_handle = NULL;
  543. UART_EXIT_CRITICAL(&(uart_context[uart_num].spinlock));
  544. return ret;
  545. }
  546. static bool uart_try_set_iomux_pin(uart_port_t uart_num, int io_num, uint32_t idx)
  547. {
  548. /* Store a pointer to the default pin, to optimize access to its fields. */
  549. const uart_periph_sig_t *upin = &uart_periph_signal[uart_num].pins[idx];
  550. /* In theory, if default_gpio is -1, iomux_func should also be -1, but
  551. * let's be safe and test both. */
  552. if (upin->iomux_func == -1 || upin->default_gpio == -1 || upin->default_gpio != io_num) {
  553. return false;
  554. }
  555. /* Assign the correct funct to the GPIO. */
  556. assert (upin->iomux_func != -1);
  557. gpio_iomux_out(io_num, upin->iomux_func, false);
  558. /* If the pin is input, we also have to redirect the signal,
  559. * in order to bypasse the GPIO matrix. */
  560. if (upin->input) {
  561. gpio_iomux_in(io_num, upin->signal);
  562. }
  563. return true;
  564. }
  565. //internal signal can be output to multiple GPIO pads
  566. //only one GPIO pad can connect with input signal
  567. 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)
  568. {
  569. ESP_RETURN_ON_FALSE((uart_num >= 0), ESP_FAIL, UART_TAG, "uart_num error");
  570. ESP_RETURN_ON_FALSE((uart_num < UART_NUM_MAX), ESP_FAIL, UART_TAG, "uart_num error");
  571. ESP_RETURN_ON_FALSE((tx_io_num < 0 || (GPIO_IS_VALID_OUTPUT_GPIO(tx_io_num))), ESP_FAIL, UART_TAG, "tx_io_num error");
  572. ESP_RETURN_ON_FALSE((rx_io_num < 0 || (GPIO_IS_VALID_GPIO(rx_io_num))), ESP_FAIL, UART_TAG, "rx_io_num error");
  573. ESP_RETURN_ON_FALSE((rts_io_num < 0 || (GPIO_IS_VALID_OUTPUT_GPIO(rts_io_num))), ESP_FAIL, UART_TAG, "rts_io_num error");
  574. ESP_RETURN_ON_FALSE((cts_io_num < 0 || (GPIO_IS_VALID_GPIO(cts_io_num))), ESP_FAIL, UART_TAG, "cts_io_num error");
  575. /* In the following statements, if the io_num is negative, no need to configure anything. */
  576. if (tx_io_num >= 0 && !uart_try_set_iomux_pin(uart_num, tx_io_num, SOC_UART_TX_PIN_IDX)) {
  577. gpio_hal_iomux_func_sel(GPIO_PIN_MUX_REG[tx_io_num], PIN_FUNC_GPIO);
  578. gpio_set_level(tx_io_num, 1);
  579. esp_rom_gpio_connect_out_signal(tx_io_num, UART_PERIPH_SIGNAL(uart_num, SOC_UART_TX_PIN_IDX), 0, 0);
  580. }
  581. if (rx_io_num >= 0 && !uart_try_set_iomux_pin(uart_num, rx_io_num, SOC_UART_RX_PIN_IDX)) {
  582. gpio_hal_iomux_func_sel(GPIO_PIN_MUX_REG[rx_io_num], PIN_FUNC_GPIO);
  583. gpio_set_pull_mode(rx_io_num, GPIO_PULLUP_ONLY);
  584. gpio_set_direction(rx_io_num, GPIO_MODE_INPUT);
  585. esp_rom_gpio_connect_in_signal(rx_io_num, UART_PERIPH_SIGNAL(uart_num, SOC_UART_RX_PIN_IDX), 0);
  586. }
  587. if (rts_io_num >= 0 && !uart_try_set_iomux_pin(uart_num, rts_io_num, SOC_UART_RTS_PIN_IDX)) {
  588. gpio_hal_iomux_func_sel(GPIO_PIN_MUX_REG[rts_io_num], PIN_FUNC_GPIO);
  589. gpio_set_direction(rts_io_num, GPIO_MODE_OUTPUT);
  590. esp_rom_gpio_connect_out_signal(rts_io_num, UART_PERIPH_SIGNAL(uart_num, SOC_UART_RTS_PIN_IDX), 0, 0);
  591. }
  592. if (cts_io_num >= 0 && !uart_try_set_iomux_pin(uart_num, cts_io_num, SOC_UART_CTS_PIN_IDX)) {
  593. gpio_hal_iomux_func_sel(GPIO_PIN_MUX_REG[cts_io_num], PIN_FUNC_GPIO);
  594. gpio_set_pull_mode(cts_io_num, GPIO_PULLUP_ONLY);
  595. gpio_set_direction(cts_io_num, GPIO_MODE_INPUT);
  596. esp_rom_gpio_connect_in_signal(cts_io_num, UART_PERIPH_SIGNAL(uart_num, SOC_UART_CTS_PIN_IDX), 0);
  597. }
  598. return ESP_OK;
  599. }
  600. esp_err_t uart_set_rts(uart_port_t uart_num, int level)
  601. {
  602. ESP_RETURN_ON_FALSE((uart_num < UART_NUM_MAX), ESP_FAIL, UART_TAG, "uart_num error");
  603. 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");
  604. UART_ENTER_CRITICAL(&(uart_context[uart_num].spinlock));
  605. uart_hal_set_rts(&(uart_context[uart_num].hal), level);
  606. UART_EXIT_CRITICAL(&(uart_context[uart_num].spinlock));
  607. return ESP_OK;
  608. }
  609. esp_err_t uart_set_dtr(uart_port_t uart_num, int level)
  610. {
  611. ESP_RETURN_ON_FALSE((uart_num < UART_NUM_MAX), ESP_FAIL, UART_TAG, "uart_num error");
  612. UART_ENTER_CRITICAL(&(uart_context[uart_num].spinlock));
  613. uart_hal_set_dtr(&(uart_context[uart_num].hal), level);
  614. UART_EXIT_CRITICAL(&(uart_context[uart_num].spinlock));
  615. return ESP_OK;
  616. }
  617. esp_err_t uart_set_tx_idle_num(uart_port_t uart_num, uint16_t idle_num)
  618. {
  619. ESP_RETURN_ON_FALSE((uart_num < UART_NUM_MAX), ESP_FAIL, UART_TAG, "uart_num error");
  620. ESP_RETURN_ON_FALSE((idle_num <= UART_TX_IDLE_NUM_V), ESP_FAIL, UART_TAG, "uart idle num error");
  621. UART_ENTER_CRITICAL(&(uart_context[uart_num].spinlock));
  622. uart_hal_set_tx_idle_num(&(uart_context[uart_num].hal), idle_num);
  623. UART_EXIT_CRITICAL(&(uart_context[uart_num].spinlock));
  624. return ESP_OK;
  625. }
  626. esp_err_t uart_param_config(uart_port_t uart_num, const uart_config_t *uart_config)
  627. {
  628. ESP_RETURN_ON_FALSE((uart_num < UART_NUM_MAX), ESP_FAIL, UART_TAG, "uart_num error");
  629. ESP_RETURN_ON_FALSE((uart_config), ESP_FAIL, UART_TAG, "param null");
  630. ESP_RETURN_ON_FALSE((uart_config->rx_flow_ctrl_thresh < SOC_UART_FIFO_LEN), ESP_FAIL, UART_TAG, "rx flow thresh error");
  631. ESP_RETURN_ON_FALSE((uart_config->flow_ctrl < UART_HW_FLOWCTRL_MAX), ESP_FAIL, UART_TAG, "hw_flowctrl mode error");
  632. ESP_RETURN_ON_FALSE((uart_config->data_bits < UART_DATA_BITS_MAX), ESP_FAIL, UART_TAG, "data bit error");
  633. uart_module_enable(uart_num);
  634. #if SOC_UART_SUPPORT_RTC_CLK
  635. if (uart_config->source_clk == UART_SCLK_RTC) {
  636. rtc_clk_enable(uart_num);
  637. }
  638. #endif
  639. UART_ENTER_CRITICAL(&(uart_context[uart_num].spinlock));
  640. uart_hal_init(&(uart_context[uart_num].hal), uart_num);
  641. uart_hal_set_sclk(&(uart_context[uart_num].hal), uart_config->source_clk);
  642. uart_hal_set_baudrate(&(uart_context[uart_num].hal), uart_config->baud_rate);
  643. uart_hal_set_parity(&(uart_context[uart_num].hal), uart_config->parity);
  644. uart_hal_set_data_bit_num(&(uart_context[uart_num].hal), uart_config->data_bits);
  645. uart_hal_set_stop_bits(&(uart_context[uart_num].hal), uart_config->stop_bits);
  646. uart_hal_set_tx_idle_num(&(uart_context[uart_num].hal), UART_TX_IDLE_NUM_DEFAULT);
  647. uart_hal_set_hw_flow_ctrl(&(uart_context[uart_num].hal), uart_config->flow_ctrl, uart_config->rx_flow_ctrl_thresh);
  648. UART_EXIT_CRITICAL(&(uart_context[uart_num].spinlock));
  649. uart_hal_rxfifo_rst(&(uart_context[uart_num].hal));
  650. uart_hal_txfifo_rst(&(uart_context[uart_num].hal));
  651. return ESP_OK;
  652. }
  653. esp_err_t uart_intr_config(uart_port_t uart_num, const uart_intr_config_t *intr_conf)
  654. {
  655. ESP_RETURN_ON_FALSE((uart_num < UART_NUM_MAX), ESP_FAIL, UART_TAG, "uart_num error");
  656. ESP_RETURN_ON_FALSE((intr_conf), ESP_FAIL, UART_TAG, "param null");
  657. uart_hal_clr_intsts_mask(&(uart_context[uart_num].hal), UART_LL_INTR_MASK);
  658. UART_ENTER_CRITICAL(&(uart_context[uart_num].spinlock));
  659. if (intr_conf->intr_enable_mask & UART_INTR_RXFIFO_TOUT) {
  660. uart_hal_set_rx_timeout(&(uart_context[uart_num].hal), intr_conf->rx_timeout_thresh);
  661. } else {
  662. //Disable rx_tout intr
  663. uart_hal_set_rx_timeout(&(uart_context[uart_num].hal), 0);
  664. }
  665. if (intr_conf->intr_enable_mask & UART_INTR_RXFIFO_FULL) {
  666. uart_hal_set_rxfifo_full_thr(&(uart_context[uart_num].hal), intr_conf->rxfifo_full_thresh);
  667. }
  668. if (intr_conf->intr_enable_mask & UART_INTR_TXFIFO_EMPTY) {
  669. uart_hal_set_txfifo_empty_thr(&(uart_context[uart_num].hal), intr_conf->txfifo_empty_intr_thresh);
  670. }
  671. uart_hal_ena_intr_mask(&(uart_context[uart_num].hal), intr_conf->intr_enable_mask);
  672. UART_EXIT_CRITICAL(&(uart_context[uart_num].spinlock));
  673. return ESP_OK;
  674. }
  675. static int UART_ISR_ATTR uart_find_pattern_from_last(uint8_t *buf, int length, uint8_t pat_chr, uint8_t pat_num)
  676. {
  677. int cnt = 0;
  678. int len = length;
  679. while (len >= 0) {
  680. if (buf[len] == pat_chr) {
  681. cnt++;
  682. } else {
  683. cnt = 0;
  684. }
  685. if (cnt >= pat_num) {
  686. break;
  687. }
  688. len --;
  689. }
  690. return len;
  691. }
  692. //internal isr handler for default driver code.
  693. static void UART_ISR_ATTR uart_rx_intr_handler_default(void *param)
  694. {
  695. uart_obj_t *p_uart = (uart_obj_t *) param;
  696. uint8_t uart_num = p_uart->uart_num;
  697. int rx_fifo_len = 0;
  698. uint32_t uart_intr_status = 0;
  699. uart_event_t uart_event;
  700. portBASE_TYPE HPTaskAwoken = 0;
  701. static uint8_t pat_flg = 0;
  702. while (1) {
  703. // The `continue statement` may cause the interrupt to loop infinitely
  704. // we exit the interrupt here
  705. uart_intr_status = uart_hal_get_intsts_mask(&(uart_context[uart_num].hal));
  706. //Exit form while loop
  707. if (uart_intr_status == 0) {
  708. break;
  709. }
  710. uart_event.type = UART_EVENT_MAX;
  711. if (uart_intr_status & UART_INTR_TXFIFO_EMPTY) {
  712. UART_ENTER_CRITICAL_ISR(&(uart_context[uart_num].spinlock));
  713. uart_hal_disable_intr_mask(&(uart_context[uart_num].hal), UART_INTR_TXFIFO_EMPTY);
  714. UART_EXIT_CRITICAL_ISR(&(uart_context[uart_num].spinlock));
  715. uart_hal_clr_intsts_mask(&(uart_context[uart_num].hal), UART_INTR_TXFIFO_EMPTY);
  716. if (p_uart->tx_waiting_brk) {
  717. continue;
  718. }
  719. //TX semaphore will only be used when tx_buf_size is zero.
  720. if (p_uart->tx_waiting_fifo == true && p_uart->tx_buf_size == 0) {
  721. p_uart->tx_waiting_fifo = false;
  722. xSemaphoreGiveFromISR(p_uart->tx_fifo_sem, &HPTaskAwoken);
  723. } else {
  724. //We don't use TX ring buffer, because the size is zero.
  725. if (p_uart->tx_buf_size == 0) {
  726. continue;
  727. }
  728. bool en_tx_flg = false;
  729. uint32_t tx_fifo_rem = uart_hal_get_txfifo_len(&(uart_context[uart_num].hal));
  730. //We need to put a loop here, in case all the buffer items are very short.
  731. //That would cause a watch_dog reset because empty interrupt happens so often.
  732. //Although this is a loop in ISR, this loop will execute at most 128 turns.
  733. while (tx_fifo_rem) {
  734. if (p_uart->tx_len_tot == 0 || p_uart->tx_ptr == NULL || p_uart->tx_len_cur == 0) {
  735. size_t size;
  736. p_uart->tx_head = (uart_tx_data_t *) xRingbufferReceiveFromISR(p_uart->tx_ring_buf, &size);
  737. if (p_uart->tx_head) {
  738. //The first item is the data description
  739. //Get the first item to get the data information
  740. if (p_uart->tx_len_tot == 0) {
  741. p_uart->tx_ptr = NULL;
  742. p_uart->tx_len_tot = p_uart->tx_head->tx_data.size;
  743. if (p_uart->tx_head->type == UART_DATA_BREAK) {
  744. p_uart->tx_brk_flg = 1;
  745. p_uart->tx_brk_len = p_uart->tx_head->tx_data.brk_len;
  746. }
  747. //We have saved the data description from the 1st item, return buffer.
  748. vRingbufferReturnItemFromISR(p_uart->tx_ring_buf, p_uart->tx_head, &HPTaskAwoken);
  749. } else if (p_uart->tx_ptr == NULL) {
  750. //Update the TX item pointer, we will need this to return item to buffer.
  751. p_uart->tx_ptr = (uint8_t *)p_uart->tx_head;
  752. en_tx_flg = true;
  753. p_uart->tx_len_cur = size;
  754. }
  755. } else {
  756. //Can not get data from ring buffer, return;
  757. break;
  758. }
  759. }
  760. if (p_uart->tx_len_tot > 0 && p_uart->tx_ptr && p_uart->tx_len_cur > 0) {
  761. //To fill the TX FIFO.
  762. uint32_t send_len = 0;
  763. // Set RS485 RTS pin before transmission if the half duplex mode is enabled
  764. if (UART_IS_MODE_SET(uart_num, UART_MODE_RS485_HALF_DUPLEX)) {
  765. UART_ENTER_CRITICAL_ISR(&(uart_context[uart_num].spinlock));
  766. uart_hal_set_rts(&(uart_context[uart_num].hal), 0);
  767. uart_hal_ena_intr_mask(&(uart_context[uart_num].hal), UART_INTR_TX_DONE);
  768. UART_EXIT_CRITICAL_ISR(&(uart_context[uart_num].spinlock));
  769. }
  770. uart_hal_write_txfifo(&(uart_context[uart_num].hal),
  771. (const uint8_t *)p_uart->tx_ptr,
  772. (p_uart->tx_len_cur > tx_fifo_rem) ? tx_fifo_rem : p_uart->tx_len_cur,
  773. &send_len);
  774. p_uart->tx_ptr += send_len;
  775. p_uart->tx_len_tot -= send_len;
  776. p_uart->tx_len_cur -= send_len;
  777. tx_fifo_rem -= send_len;
  778. if (p_uart->tx_len_cur == 0) {
  779. //Return item to ring buffer.
  780. vRingbufferReturnItemFromISR(p_uart->tx_ring_buf, p_uart->tx_head, &HPTaskAwoken);
  781. p_uart->tx_head = NULL;
  782. p_uart->tx_ptr = NULL;
  783. //Sending item done, now we need to send break if there is a record.
  784. //Set TX break signal after FIFO is empty
  785. if (p_uart->tx_len_tot == 0 && p_uart->tx_brk_flg == 1) {
  786. uart_hal_clr_intsts_mask(&(uart_context[uart_num].hal), UART_INTR_TX_BRK_DONE);
  787. UART_ENTER_CRITICAL_ISR(&(uart_context[uart_num].spinlock));
  788. uart_hal_tx_break(&(uart_context[uart_num].hal), p_uart->tx_brk_len);
  789. uart_hal_ena_intr_mask(&(uart_context[uart_num].hal), UART_INTR_TX_BRK_DONE);
  790. UART_EXIT_CRITICAL_ISR(&(uart_context[uart_num].spinlock));
  791. p_uart->tx_waiting_brk = 1;
  792. //do not enable TX empty interrupt
  793. en_tx_flg = false;
  794. } else {
  795. //enable TX empty interrupt
  796. en_tx_flg = true;
  797. }
  798. } else {
  799. //enable TX empty interrupt
  800. en_tx_flg = true;
  801. }
  802. }
  803. }
  804. if (en_tx_flg) {
  805. uart_hal_clr_intsts_mask(&(uart_context[uart_num].hal), UART_INTR_TXFIFO_EMPTY);
  806. UART_ENTER_CRITICAL_ISR(&(uart_context[uart_num].spinlock));
  807. uart_hal_ena_intr_mask(&(uart_context[uart_num].hal), UART_INTR_TXFIFO_EMPTY);
  808. UART_EXIT_CRITICAL_ISR(&(uart_context[uart_num].spinlock));
  809. }
  810. }
  811. } else if ((uart_intr_status & UART_INTR_RXFIFO_TOUT)
  812. || (uart_intr_status & UART_INTR_RXFIFO_FULL)
  813. || (uart_intr_status & UART_INTR_CMD_CHAR_DET)
  814. ) {
  815. if (pat_flg == 1) {
  816. uart_intr_status |= UART_INTR_CMD_CHAR_DET;
  817. pat_flg = 0;
  818. }
  819. if (p_uart->rx_buffer_full_flg == false) {
  820. rx_fifo_len = uart_hal_get_rxfifo_len(&(uart_context[uart_num].hal));
  821. if ((p_uart_obj[uart_num]->rx_always_timeout_flg) && !(uart_intr_status & UART_INTR_RXFIFO_TOUT)) {
  822. rx_fifo_len--; // leave one byte in the fifo in order to trigger uart_intr_rxfifo_tout
  823. }
  824. uart_hal_read_rxfifo(&(uart_context[uart_num].hal), p_uart->rx_data_buf, &rx_fifo_len);
  825. uint8_t pat_chr = 0;
  826. uint8_t pat_num = 0;
  827. int pat_idx = -1;
  828. uart_hal_get_at_cmd_char(&(uart_context[uart_num].hal), &pat_chr, &pat_num);
  829. //Get the buffer from the FIFO
  830. if (uart_intr_status & UART_INTR_CMD_CHAR_DET) {
  831. uart_hal_clr_intsts_mask(&(uart_context[uart_num].hal), UART_INTR_CMD_CHAR_DET);
  832. uart_event.type = UART_PATTERN_DET;
  833. uart_event.size = rx_fifo_len;
  834. pat_idx = uart_find_pattern_from_last(p_uart->rx_data_buf, rx_fifo_len - 1, pat_chr, pat_num);
  835. } else {
  836. //After Copying the Data From FIFO ,Clear intr_status
  837. uart_hal_clr_intsts_mask(&(uart_context[uart_num].hal), UART_INTR_RXFIFO_TOUT | UART_INTR_RXFIFO_FULL);
  838. uart_event.type = UART_DATA;
  839. uart_event.size = rx_fifo_len;
  840. uart_event.timeout_flag = (uart_intr_status & UART_INTR_RXFIFO_TOUT) ? true : false;
  841. UART_ENTER_CRITICAL_ISR(&uart_selectlock);
  842. if (p_uart->uart_select_notif_callback) {
  843. p_uart->uart_select_notif_callback(uart_num, UART_SELECT_READ_NOTIF, &HPTaskAwoken);
  844. }
  845. UART_EXIT_CRITICAL_ISR(&uart_selectlock);
  846. }
  847. p_uart->rx_stash_len = rx_fifo_len;
  848. //If we fail to push data to ring buffer, we will have to stash the data, and send next time.
  849. //Mainly for applications that uses flow control or small ring buffer.
  850. if (pdFALSE == xRingbufferSendFromISR(p_uart->rx_ring_buf, p_uart->rx_data_buf, p_uart->rx_stash_len, &HPTaskAwoken)) {
  851. p_uart->rx_buffer_full_flg = true;
  852. UART_ENTER_CRITICAL_ISR(&(uart_context[uart_num].spinlock));
  853. uart_hal_disable_intr_mask(&(uart_context[uart_num].hal), UART_INTR_RXFIFO_TOUT | UART_INTR_RXFIFO_FULL);
  854. UART_EXIT_CRITICAL_ISR(&(uart_context[uart_num].spinlock));
  855. if (uart_event.type == UART_PATTERN_DET) {
  856. UART_ENTER_CRITICAL_ISR(&(uart_context[uart_num].spinlock));
  857. if (rx_fifo_len < pat_num) {
  858. //some of the characters are read out in last interrupt
  859. uart_pattern_enqueue(uart_num, p_uart->rx_buffered_len - (pat_num - rx_fifo_len));
  860. } else {
  861. uart_pattern_enqueue(uart_num,
  862. pat_idx <= -1 ?
  863. //can not find the pattern in buffer,
  864. p_uart->rx_buffered_len + p_uart->rx_stash_len :
  865. // find the pattern in buffer
  866. p_uart->rx_buffered_len + pat_idx);
  867. }
  868. UART_EXIT_CRITICAL_ISR(&(uart_context[uart_num].spinlock));
  869. if ((p_uart->event_queue != NULL) && (pdFALSE == xQueueSendFromISR(p_uart->event_queue, (void * )&uart_event, &HPTaskAwoken))) {
  870. #ifndef CONFIG_UART_ISR_IN_IRAM //Only log if ISR is not in IRAM
  871. ESP_EARLY_LOGV(UART_TAG, "UART event queue full");
  872. #endif
  873. }
  874. }
  875. uart_event.type = UART_BUFFER_FULL;
  876. } else {
  877. UART_ENTER_CRITICAL_ISR(&(uart_context[uart_num].spinlock));
  878. if (uart_intr_status & UART_INTR_CMD_CHAR_DET) {
  879. if (rx_fifo_len < pat_num) {
  880. //some of the characters are read out in last interrupt
  881. uart_pattern_enqueue(uart_num, p_uart->rx_buffered_len - (pat_num - rx_fifo_len));
  882. } else if (pat_idx >= 0) {
  883. // find the pattern in stash buffer.
  884. uart_pattern_enqueue(uart_num, p_uart->rx_buffered_len + pat_idx);
  885. }
  886. }
  887. p_uart->rx_buffered_len += p_uart->rx_stash_len;
  888. UART_EXIT_CRITICAL_ISR(&(uart_context[uart_num].spinlock));
  889. }
  890. } else {
  891. UART_ENTER_CRITICAL_ISR(&(uart_context[uart_num].spinlock));
  892. uart_hal_disable_intr_mask(&(uart_context[uart_num].hal), UART_INTR_RXFIFO_FULL | UART_INTR_RXFIFO_TOUT);
  893. UART_EXIT_CRITICAL_ISR(&(uart_context[uart_num].spinlock));
  894. uart_hal_clr_intsts_mask(&(uart_context[uart_num].hal), UART_INTR_RXFIFO_FULL | UART_INTR_RXFIFO_TOUT);
  895. if (uart_intr_status & UART_INTR_CMD_CHAR_DET) {
  896. uart_hal_clr_intsts_mask(&(uart_context[uart_num].hal), UART_INTR_CMD_CHAR_DET);
  897. uart_event.type = UART_PATTERN_DET;
  898. uart_event.size = rx_fifo_len;
  899. pat_flg = 1;
  900. }
  901. }
  902. } else if (uart_intr_status & UART_INTR_RXFIFO_OVF) {
  903. // When fifo overflows, we reset the fifo.
  904. UART_ENTER_CRITICAL_ISR(&(uart_context[uart_num].spinlock));
  905. uart_hal_rxfifo_rst(&(uart_context[uart_num].hal));
  906. UART_EXIT_CRITICAL_ISR(&(uart_context[uart_num].spinlock));
  907. UART_ENTER_CRITICAL_ISR(&uart_selectlock);
  908. if (p_uart->uart_select_notif_callback) {
  909. p_uart->uart_select_notif_callback(uart_num, UART_SELECT_ERROR_NOTIF, &HPTaskAwoken);
  910. }
  911. UART_EXIT_CRITICAL_ISR(&uart_selectlock);
  912. uart_hal_clr_intsts_mask(&(uart_context[uart_num].hal), UART_INTR_RXFIFO_OVF);
  913. uart_event.type = UART_FIFO_OVF;
  914. } else if (uart_intr_status & UART_INTR_BRK_DET) {
  915. uart_hal_clr_intsts_mask(&(uart_context[uart_num].hal), UART_INTR_BRK_DET);
  916. uart_event.type = UART_BREAK;
  917. } else if (uart_intr_status & UART_INTR_FRAM_ERR) {
  918. UART_ENTER_CRITICAL_ISR(&uart_selectlock);
  919. if (p_uart->uart_select_notif_callback) {
  920. p_uart->uart_select_notif_callback(uart_num, UART_SELECT_ERROR_NOTIF, &HPTaskAwoken);
  921. }
  922. UART_EXIT_CRITICAL_ISR(&uart_selectlock);
  923. uart_hal_clr_intsts_mask(&(uart_context[uart_num].hal), UART_INTR_FRAM_ERR);
  924. uart_event.type = UART_FRAME_ERR;
  925. } else if (uart_intr_status & UART_INTR_PARITY_ERR) {
  926. UART_ENTER_CRITICAL_ISR(&uart_selectlock);
  927. if (p_uart->uart_select_notif_callback) {
  928. p_uart->uart_select_notif_callback(uart_num, UART_SELECT_ERROR_NOTIF, &HPTaskAwoken);
  929. }
  930. UART_EXIT_CRITICAL_ISR(&uart_selectlock);
  931. uart_hal_clr_intsts_mask(&(uart_context[uart_num].hal), UART_INTR_PARITY_ERR);
  932. uart_event.type = UART_PARITY_ERR;
  933. } else if (uart_intr_status & UART_INTR_TX_BRK_DONE) {
  934. UART_ENTER_CRITICAL_ISR(&(uart_context[uart_num].spinlock));
  935. uart_hal_tx_break(&(uart_context[uart_num].hal), 0);
  936. uart_hal_disable_intr_mask(&(uart_context[uart_num].hal), UART_INTR_TX_BRK_DONE);
  937. if (p_uart->tx_brk_flg == 1) {
  938. uart_hal_ena_intr_mask(&(uart_context[uart_num].hal), UART_INTR_TXFIFO_EMPTY);
  939. }
  940. UART_EXIT_CRITICAL_ISR(&(uart_context[uart_num].spinlock));
  941. uart_hal_clr_intsts_mask(&(uart_context[uart_num].hal), UART_INTR_TX_BRK_DONE);
  942. if (p_uart->tx_brk_flg == 1) {
  943. p_uart->tx_brk_flg = 0;
  944. p_uart->tx_waiting_brk = 0;
  945. } else {
  946. xSemaphoreGiveFromISR(p_uart->tx_brk_sem, &HPTaskAwoken);
  947. }
  948. } else if (uart_intr_status & UART_INTR_TX_BRK_IDLE) {
  949. UART_ENTER_CRITICAL_ISR(&(uart_context[uart_num].spinlock));
  950. uart_hal_disable_intr_mask(&(uart_context[uart_num].hal), UART_INTR_TX_BRK_IDLE);
  951. UART_EXIT_CRITICAL_ISR(&(uart_context[uart_num].spinlock));
  952. uart_hal_clr_intsts_mask(&(uart_context[uart_num].hal), UART_INTR_TX_BRK_IDLE);
  953. } else if (uart_intr_status & UART_INTR_CMD_CHAR_DET) {
  954. uart_hal_clr_intsts_mask(&(uart_context[uart_num].hal), UART_INTR_CMD_CHAR_DET);
  955. uart_event.type = UART_PATTERN_DET;
  956. } else if ((uart_intr_status & UART_INTR_RS485_PARITY_ERR)
  957. || (uart_intr_status & UART_INTR_RS485_FRM_ERR)
  958. || (uart_intr_status & UART_INTR_RS485_CLASH)) {
  959. // RS485 collision or frame error interrupt triggered
  960. UART_ENTER_CRITICAL_ISR(&(uart_context[uart_num].spinlock));
  961. uart_hal_rxfifo_rst(&(uart_context[uart_num].hal));
  962. // Set collision detection flag
  963. p_uart_obj[uart_num]->coll_det_flg = true;
  964. UART_EXIT_CRITICAL_ISR(&(uart_context[uart_num].spinlock));
  965. uart_hal_clr_intsts_mask(&(uart_context[uart_num].hal), UART_INTR_RS485_CLASH | UART_INTR_RS485_FRM_ERR | UART_INTR_RS485_PARITY_ERR);
  966. uart_event.type = UART_EVENT_MAX;
  967. } else if (uart_intr_status & UART_INTR_TX_DONE) {
  968. if (UART_IS_MODE_SET(uart_num, UART_MODE_RS485_HALF_DUPLEX) && uart_hal_is_tx_idle(&(uart_context[uart_num].hal)) != true) {
  969. // The TX_DONE interrupt is triggered but transmit is active
  970. // then postpone interrupt processing for next interrupt
  971. uart_event.type = UART_EVENT_MAX;
  972. } else {
  973. // Workaround for RS485: If the RS485 half duplex mode is active
  974. // and transmitter is in idle state then reset received buffer and reset RTS pin
  975. // skip this behavior for other UART modes
  976. UART_ENTER_CRITICAL_ISR(&(uart_context[uart_num].spinlock));
  977. uart_hal_disable_intr_mask(&(uart_context[uart_num].hal), UART_INTR_TX_DONE);
  978. if (UART_IS_MODE_SET(uart_num, UART_MODE_RS485_HALF_DUPLEX)) {
  979. uart_hal_rxfifo_rst(&(uart_context[uart_num].hal));
  980. uart_hal_set_rts(&(uart_context[uart_num].hal), 1);
  981. }
  982. UART_EXIT_CRITICAL_ISR(&(uart_context[uart_num].spinlock));
  983. uart_hal_clr_intsts_mask(&(uart_context[uart_num].hal), UART_INTR_TX_DONE);
  984. xSemaphoreGiveFromISR(p_uart_obj[uart_num]->tx_done_sem, &HPTaskAwoken);
  985. }
  986. } else {
  987. uart_hal_clr_intsts_mask(&(uart_context[uart_num].hal), uart_intr_status); /*simply clear all other intr status*/
  988. uart_event.type = UART_EVENT_MAX;
  989. }
  990. if (uart_event.type != UART_EVENT_MAX && p_uart->event_queue) {
  991. if (pdFALSE == xQueueSendFromISR(p_uart->event_queue, (void * )&uart_event, &HPTaskAwoken)) {
  992. #ifndef CONFIG_UART_ISR_IN_IRAM //Only log if ISR is not in IRAM
  993. ESP_EARLY_LOGV(UART_TAG, "UART event queue full");
  994. #endif
  995. }
  996. }
  997. }
  998. if (HPTaskAwoken == pdTRUE) {
  999. portYIELD_FROM_ISR();
  1000. }
  1001. }
  1002. /**************************************************************/
  1003. esp_err_t uart_wait_tx_done(uart_port_t uart_num, TickType_t ticks_to_wait)
  1004. {
  1005. ESP_RETURN_ON_FALSE((uart_num < UART_NUM_MAX), ESP_FAIL, UART_TAG, "uart_num error");
  1006. ESP_RETURN_ON_FALSE((p_uart_obj[uart_num]), ESP_FAIL, UART_TAG, "uart driver error");
  1007. BaseType_t res;
  1008. portTickType ticks_start = xTaskGetTickCount();
  1009. //Take tx_mux
  1010. res = xSemaphoreTake(p_uart_obj[uart_num]->tx_mux, (portTickType)ticks_to_wait);
  1011. if (res == pdFALSE) {
  1012. return ESP_ERR_TIMEOUT;
  1013. }
  1014. xSemaphoreTake(p_uart_obj[uart_num]->tx_done_sem, 0);
  1015. if (uart_hal_is_tx_idle(&(uart_context[uart_num].hal))) {
  1016. xSemaphoreGive(p_uart_obj[uart_num]->tx_mux);
  1017. return ESP_OK;
  1018. }
  1019. UART_ENTER_CRITICAL(&(uart_context[uart_num].spinlock));
  1020. uart_hal_ena_intr_mask(&(uart_context[uart_num].hal), UART_INTR_TX_DONE);
  1021. UART_EXIT_CRITICAL(&(uart_context[uart_num].spinlock));
  1022. TickType_t ticks_end = xTaskGetTickCount();
  1023. if (ticks_end - ticks_start > ticks_to_wait) {
  1024. ticks_to_wait = 0;
  1025. } else {
  1026. ticks_to_wait = ticks_to_wait - (ticks_end - ticks_start);
  1027. }
  1028. //take 2nd tx_done_sem, wait given from ISR
  1029. res = xSemaphoreTake(p_uart_obj[uart_num]->tx_done_sem, (portTickType)ticks_to_wait);
  1030. if (res == pdFALSE) {
  1031. // The TX_DONE interrupt will be disabled in ISR
  1032. xSemaphoreGive(p_uart_obj[uart_num]->tx_mux);
  1033. return ESP_ERR_TIMEOUT;
  1034. }
  1035. xSemaphoreGive(p_uart_obj[uart_num]->tx_mux);
  1036. return ESP_OK;
  1037. }
  1038. int uart_tx_chars(uart_port_t uart_num, const char *buffer, uint32_t len)
  1039. {
  1040. ESP_RETURN_ON_FALSE((uart_num < UART_NUM_MAX), (-1), UART_TAG, "uart_num error");
  1041. ESP_RETURN_ON_FALSE((p_uart_obj[uart_num]), (-1), UART_TAG, "uart driver error");
  1042. ESP_RETURN_ON_FALSE(buffer, (-1), UART_TAG, "buffer null");
  1043. if (len == 0) {
  1044. return 0;
  1045. }
  1046. int tx_len = 0;
  1047. xSemaphoreTake(p_uart_obj[uart_num]->tx_mux, (portTickType)portMAX_DELAY);
  1048. if (UART_IS_MODE_SET(uart_num, UART_MODE_RS485_HALF_DUPLEX)) {
  1049. UART_ENTER_CRITICAL(&(uart_context[uart_num].spinlock));
  1050. uart_hal_set_rts(&(uart_context[uart_num].hal), 0);
  1051. uart_hal_ena_intr_mask(&(uart_context[uart_num].hal), UART_INTR_TX_DONE);
  1052. UART_EXIT_CRITICAL(&(uart_context[uart_num].spinlock));
  1053. }
  1054. uart_hal_write_txfifo(&(uart_context[uart_num].hal), (const uint8_t *) buffer, len, (uint32_t *)&tx_len);
  1055. xSemaphoreGive(p_uart_obj[uart_num]->tx_mux);
  1056. return tx_len;
  1057. }
  1058. static int uart_tx_all(uart_port_t uart_num, const char *src, size_t size, bool brk_en, int brk_len)
  1059. {
  1060. if (size == 0) {
  1061. return 0;
  1062. }
  1063. size_t original_size = size;
  1064. //lock for uart_tx
  1065. xSemaphoreTake(p_uart_obj[uart_num]->tx_mux, (portTickType)portMAX_DELAY);
  1066. p_uart_obj[uart_num]->coll_det_flg = false;
  1067. if (p_uart_obj[uart_num]->tx_buf_size > 0) {
  1068. size_t max_size = xRingbufferGetMaxItemSize(p_uart_obj[uart_num]->tx_ring_buf);
  1069. int offset = 0;
  1070. uart_tx_data_t evt;
  1071. evt.tx_data.size = size;
  1072. evt.tx_data.brk_len = brk_len;
  1073. if (brk_en) {
  1074. evt.type = UART_DATA_BREAK;
  1075. } else {
  1076. evt.type = UART_DATA;
  1077. }
  1078. xRingbufferSend(p_uart_obj[uart_num]->tx_ring_buf, (void *) &evt, sizeof(uart_tx_data_t), portMAX_DELAY);
  1079. while (size > 0) {
  1080. size_t send_size = size > max_size / 2 ? max_size / 2 : size;
  1081. xRingbufferSend(p_uart_obj[uart_num]->tx_ring_buf, (void *) (src + offset), send_size, portMAX_DELAY);
  1082. size -= send_size;
  1083. offset += send_size;
  1084. uart_enable_tx_intr(uart_num, 1, UART_EMPTY_THRESH_DEFAULT);
  1085. }
  1086. } else {
  1087. while (size) {
  1088. //semaphore for tx_fifo available
  1089. if (pdTRUE == xSemaphoreTake(p_uart_obj[uart_num]->tx_fifo_sem, (portTickType)portMAX_DELAY)) {
  1090. uint32_t sent = 0;
  1091. if (UART_IS_MODE_SET(uart_num, UART_MODE_RS485_HALF_DUPLEX)) {
  1092. UART_ENTER_CRITICAL(&(uart_context[uart_num].spinlock));
  1093. uart_hal_set_rts(&(uart_context[uart_num].hal), 0);
  1094. uart_hal_ena_intr_mask(&(uart_context[uart_num].hal), UART_INTR_TX_DONE);
  1095. UART_EXIT_CRITICAL(&(uart_context[uart_num].spinlock));
  1096. }
  1097. uart_hal_write_txfifo(&(uart_context[uart_num].hal), (const uint8_t *)src, size, &sent);
  1098. if (sent < size) {
  1099. p_uart_obj[uart_num]->tx_waiting_fifo = true;
  1100. uart_enable_tx_intr(uart_num, 1, UART_EMPTY_THRESH_DEFAULT);
  1101. }
  1102. size -= sent;
  1103. src += sent;
  1104. }
  1105. }
  1106. if (brk_en) {
  1107. uart_hal_clr_intsts_mask(&(uart_context[uart_num].hal), UART_INTR_TX_BRK_DONE);
  1108. UART_ENTER_CRITICAL(&(uart_context[uart_num].spinlock));
  1109. uart_hal_tx_break(&(uart_context[uart_num].hal), brk_len);
  1110. uart_hal_ena_intr_mask(&(uart_context[uart_num].hal), UART_INTR_TX_BRK_DONE);
  1111. UART_EXIT_CRITICAL(&(uart_context[uart_num].spinlock));
  1112. xSemaphoreTake(p_uart_obj[uart_num]->tx_brk_sem, (portTickType)portMAX_DELAY);
  1113. }
  1114. xSemaphoreGive(p_uart_obj[uart_num]->tx_fifo_sem);
  1115. }
  1116. xSemaphoreGive(p_uart_obj[uart_num]->tx_mux);
  1117. return original_size;
  1118. }
  1119. int uart_write_bytes(uart_port_t uart_num, const void *src, size_t size)
  1120. {
  1121. ESP_RETURN_ON_FALSE((uart_num < UART_NUM_MAX), (-1), UART_TAG, "uart_num error");
  1122. ESP_RETURN_ON_FALSE((p_uart_obj[uart_num] != NULL), (-1), UART_TAG, "uart driver error");
  1123. ESP_RETURN_ON_FALSE(src, (-1), UART_TAG, "buffer null");
  1124. return uart_tx_all(uart_num, src, size, 0, 0);
  1125. }
  1126. int uart_write_bytes_with_break(uart_port_t uart_num, const void *src, size_t size, int brk_len)
  1127. {
  1128. ESP_RETURN_ON_FALSE((uart_num < UART_NUM_MAX), (-1), UART_TAG, "uart_num error");
  1129. ESP_RETURN_ON_FALSE((p_uart_obj[uart_num]), (-1), UART_TAG, "uart driver error");
  1130. ESP_RETURN_ON_FALSE((size > 0), (-1), UART_TAG, "uart size error");
  1131. ESP_RETURN_ON_FALSE((src), (-1), UART_TAG, "uart data null");
  1132. ESP_RETURN_ON_FALSE((brk_len > 0 && brk_len < 256), (-1), UART_TAG, "break_num error");
  1133. return uart_tx_all(uart_num, src, size, 1, brk_len);
  1134. }
  1135. static bool uart_check_buf_full(uart_port_t uart_num)
  1136. {
  1137. if (p_uart_obj[uart_num]->rx_buffer_full_flg) {
  1138. 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);
  1139. if (res == pdTRUE) {
  1140. UART_ENTER_CRITICAL(&(uart_context[uart_num].spinlock));
  1141. p_uart_obj[uart_num]->rx_buffered_len += p_uart_obj[uart_num]->rx_stash_len;
  1142. p_uart_obj[uart_num]->rx_buffer_full_flg = false;
  1143. UART_EXIT_CRITICAL(&(uart_context[uart_num].spinlock));
  1144. uart_enable_rx_intr(p_uart_obj[uart_num]->uart_num);
  1145. return true;
  1146. }
  1147. }
  1148. return false;
  1149. }
  1150. int uart_read_bytes(uart_port_t uart_num, void *buf, uint32_t length, TickType_t ticks_to_wait)
  1151. {
  1152. ESP_RETURN_ON_FALSE((uart_num < UART_NUM_MAX), (-1), UART_TAG, "uart_num error");
  1153. ESP_RETURN_ON_FALSE((buf), (-1), UART_TAG, "uart data null");
  1154. ESP_RETURN_ON_FALSE((p_uart_obj[uart_num]), (-1), UART_TAG, "uart driver error");
  1155. uint8_t *data = NULL;
  1156. size_t size;
  1157. size_t copy_len = 0;
  1158. int len_tmp;
  1159. if (xSemaphoreTake(p_uart_obj[uart_num]->rx_mux, (portTickType)ticks_to_wait) != pdTRUE) {
  1160. return -1;
  1161. }
  1162. while (length) {
  1163. if (p_uart_obj[uart_num]->rx_cur_remain == 0) {
  1164. data = (uint8_t *) xRingbufferReceive(p_uart_obj[uart_num]->rx_ring_buf, &size, (portTickType) ticks_to_wait);
  1165. if (data) {
  1166. p_uart_obj[uart_num]->rx_head_ptr = data;
  1167. p_uart_obj[uart_num]->rx_ptr = data;
  1168. p_uart_obj[uart_num]->rx_cur_remain = size;
  1169. } else {
  1170. //When using dual cores, `rx_buffer_full_flg` may read and write on different cores at same time,
  1171. //which may lose synchronization. So we also need to call `uart_check_buf_full` once when ringbuffer is empty
  1172. //to solve the possible asynchronous issues.
  1173. if (uart_check_buf_full(uart_num)) {
  1174. //This condition will never be true if `uart_read_bytes`
  1175. //and `uart_rx_intr_handler_default` are scheduled on the same core.
  1176. continue;
  1177. } else {
  1178. xSemaphoreGive(p_uart_obj[uart_num]->rx_mux);
  1179. return copy_len;
  1180. }
  1181. }
  1182. }
  1183. if (p_uart_obj[uart_num]->rx_cur_remain > length) {
  1184. len_tmp = length;
  1185. } else {
  1186. len_tmp = p_uart_obj[uart_num]->rx_cur_remain;
  1187. }
  1188. memcpy((uint8_t *)buf + copy_len, p_uart_obj[uart_num]->rx_ptr, len_tmp);
  1189. UART_ENTER_CRITICAL(&(uart_context[uart_num].spinlock));
  1190. p_uart_obj[uart_num]->rx_buffered_len -= len_tmp;
  1191. uart_pattern_queue_update(uart_num, len_tmp);
  1192. p_uart_obj[uart_num]->rx_ptr += len_tmp;
  1193. UART_EXIT_CRITICAL(&(uart_context[uart_num].spinlock));
  1194. p_uart_obj[uart_num]->rx_cur_remain -= len_tmp;
  1195. copy_len += len_tmp;
  1196. length -= len_tmp;
  1197. if (p_uart_obj[uart_num]->rx_cur_remain == 0) {
  1198. vRingbufferReturnItem(p_uart_obj[uart_num]->rx_ring_buf, p_uart_obj[uart_num]->rx_head_ptr);
  1199. p_uart_obj[uart_num]->rx_head_ptr = NULL;
  1200. p_uart_obj[uart_num]->rx_ptr = NULL;
  1201. uart_check_buf_full(uart_num);
  1202. }
  1203. }
  1204. xSemaphoreGive(p_uart_obj[uart_num]->rx_mux);
  1205. return copy_len;
  1206. }
  1207. esp_err_t uart_get_buffered_data_len(uart_port_t uart_num, size_t *size)
  1208. {
  1209. ESP_RETURN_ON_FALSE((uart_num < UART_NUM_MAX), ESP_FAIL, UART_TAG, "uart_num error");
  1210. ESP_RETURN_ON_FALSE((p_uart_obj[uart_num]), ESP_FAIL, UART_TAG, "uart driver error");
  1211. UART_ENTER_CRITICAL(&(uart_context[uart_num].spinlock));
  1212. *size = p_uart_obj[uart_num]->rx_buffered_len;
  1213. UART_EXIT_CRITICAL(&(uart_context[uart_num].spinlock));
  1214. return ESP_OK;
  1215. }
  1216. esp_err_t uart_flush(uart_port_t uart_num) __attribute__((alias("uart_flush_input")));
  1217. static esp_err_t uart_disable_intr_mask_and_return_prev(uart_port_t uart_num, uint32_t disable_mask, uint32_t *prev_mask)
  1218. {
  1219. ESP_RETURN_ON_FALSE((uart_num < UART_NUM_MAX), ESP_FAIL, UART_TAG, "uart_num error");
  1220. UART_ENTER_CRITICAL(&(uart_context[uart_num].spinlock));
  1221. *prev_mask = uart_hal_get_intr_ena_status(&uart_context[uart_num].hal) & disable_mask;
  1222. uart_hal_disable_intr_mask(&(uart_context[uart_num].hal), disable_mask);
  1223. UART_EXIT_CRITICAL(&(uart_context[uart_num].spinlock));
  1224. return ESP_OK;
  1225. }
  1226. esp_err_t uart_flush_input(uart_port_t uart_num)
  1227. {
  1228. ESP_RETURN_ON_FALSE((uart_num < UART_NUM_MAX), ESP_FAIL, UART_TAG, "uart_num error");
  1229. ESP_RETURN_ON_FALSE((p_uart_obj[uart_num]), ESP_FAIL, UART_TAG, "uart driver error");
  1230. uart_obj_t *p_uart = p_uart_obj[uart_num];
  1231. uint8_t *data;
  1232. size_t size;
  1233. uint32_t prev_mask;
  1234. //rx sem protect the ring buffer read related functions
  1235. xSemaphoreTake(p_uart->rx_mux, (portTickType)portMAX_DELAY);
  1236. uart_disable_intr_mask_and_return_prev(uart_num, UART_INTR_RXFIFO_FULL | UART_INTR_RXFIFO_TOUT, &prev_mask);
  1237. while (true) {
  1238. if (p_uart->rx_head_ptr) {
  1239. vRingbufferReturnItem(p_uart->rx_ring_buf, p_uart->rx_head_ptr);
  1240. UART_ENTER_CRITICAL(&(uart_context[uart_num].spinlock));
  1241. p_uart_obj[uart_num]->rx_buffered_len -= p_uart->rx_cur_remain;
  1242. uart_pattern_queue_update(uart_num, p_uart->rx_cur_remain);
  1243. UART_EXIT_CRITICAL(&(uart_context[uart_num].spinlock));
  1244. p_uart->rx_ptr = NULL;
  1245. p_uart->rx_cur_remain = 0;
  1246. p_uart->rx_head_ptr = NULL;
  1247. }
  1248. data = (uint8_t*) xRingbufferReceive(p_uart->rx_ring_buf, &size, (portTickType) 0);
  1249. if(data == NULL) {
  1250. bool error = false;
  1251. UART_ENTER_CRITICAL(&(uart_context[uart_num].spinlock));
  1252. if( p_uart_obj[uart_num]->rx_buffered_len != 0 ) {
  1253. p_uart_obj[uart_num]->rx_buffered_len = 0;
  1254. error = true;
  1255. }
  1256. //We also need to clear the `rx_buffer_full_flg` here.
  1257. p_uart_obj[uart_num]->rx_buffer_full_flg = false;
  1258. UART_EXIT_CRITICAL(&(uart_context[uart_num].spinlock));
  1259. if (error) {
  1260. // this must be called outside the critical section
  1261. ESP_LOGE(UART_TAG, "rx_buffered_len error");
  1262. }
  1263. break;
  1264. }
  1265. UART_ENTER_CRITICAL(&(uart_context[uart_num].spinlock));
  1266. p_uart_obj[uart_num]->rx_buffered_len -= size;
  1267. uart_pattern_queue_update(uart_num, size);
  1268. UART_EXIT_CRITICAL(&(uart_context[uart_num].spinlock));
  1269. vRingbufferReturnItem(p_uart->rx_ring_buf, data);
  1270. if (p_uart_obj[uart_num]->rx_buffer_full_flg) {
  1271. 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);
  1272. if (res == pdTRUE) {
  1273. UART_ENTER_CRITICAL(&(uart_context[uart_num].spinlock));
  1274. p_uart_obj[uart_num]->rx_buffered_len += p_uart_obj[uart_num]->rx_stash_len;
  1275. p_uart_obj[uart_num]->rx_buffer_full_flg = false;
  1276. UART_EXIT_CRITICAL(&(uart_context[uart_num].spinlock));
  1277. }
  1278. }
  1279. }
  1280. p_uart->rx_ptr = NULL;
  1281. p_uart->rx_cur_remain = 0;
  1282. p_uart->rx_head_ptr = NULL;
  1283. uart_hal_rxfifo_rst(&(uart_context[uart_num].hal));
  1284. uart_enable_intr_mask(uart_num, prev_mask);
  1285. xSemaphoreGive(p_uart->rx_mux);
  1286. return ESP_OK;
  1287. }
  1288. static void uart_free_driver_obj(uart_obj_t *uart_obj)
  1289. {
  1290. if (uart_obj->tx_fifo_sem) {
  1291. vSemaphoreDelete(uart_obj->tx_fifo_sem);
  1292. }
  1293. if (uart_obj->tx_done_sem) {
  1294. vSemaphoreDelete(uart_obj->tx_done_sem);
  1295. }
  1296. if (uart_obj->tx_brk_sem) {
  1297. vSemaphoreDelete(uart_obj->tx_brk_sem);
  1298. }
  1299. if (uart_obj->tx_mux) {
  1300. vSemaphoreDelete(uart_obj->tx_mux);
  1301. }
  1302. if (uart_obj->rx_mux) {
  1303. vSemaphoreDelete(uart_obj->rx_mux);
  1304. }
  1305. if (uart_obj->event_queue) {
  1306. vQueueDelete(uart_obj->event_queue);
  1307. }
  1308. if (uart_obj->rx_ring_buf) {
  1309. vRingbufferDelete(uart_obj->rx_ring_buf);
  1310. }
  1311. if (uart_obj->tx_ring_buf) {
  1312. vRingbufferDelete(uart_obj->tx_ring_buf);
  1313. }
  1314. #if CONFIG_UART_ISR_IN_IRAM
  1315. free(uart_obj->event_queue_storage);
  1316. free(uart_obj->event_queue_struct);
  1317. free(uart_obj->tx_ring_buf_storage);
  1318. free(uart_obj->tx_ring_buf_struct);
  1319. free(uart_obj->rx_ring_buf_storage);
  1320. free(uart_obj->rx_ring_buf_struct);
  1321. free(uart_obj->rx_mux_struct);
  1322. free(uart_obj->tx_mux_struct);
  1323. free(uart_obj->tx_brk_sem_struct);
  1324. free(uart_obj->tx_done_sem_struct);
  1325. free(uart_obj->tx_fifo_sem_struct);
  1326. #endif
  1327. free(uart_obj);
  1328. }
  1329. static uart_obj_t *uart_alloc_driver_obj(int event_queue_size, int tx_buffer_size, int rx_buffer_size)
  1330. {
  1331. uart_obj_t *uart_obj = heap_caps_calloc(1, sizeof(uart_obj_t), UART_MALLOC_CAPS);
  1332. if (!uart_obj) {
  1333. return NULL;
  1334. }
  1335. #if CONFIG_UART_ISR_IN_IRAM
  1336. if (event_queue_size > 0) {
  1337. uart_obj->event_queue_storage = heap_caps_calloc(event_queue_size, sizeof(uart_event_t), UART_MALLOC_CAPS);
  1338. uart_obj->event_queue_struct = heap_caps_calloc(1, sizeof(StaticQueue_t), UART_MALLOC_CAPS);
  1339. if (!uart_obj->event_queue_storage || !uart_obj->event_queue_struct) {
  1340. goto err;
  1341. }
  1342. }
  1343. if (tx_buffer_size > 0) {
  1344. uart_obj->tx_ring_buf_storage = heap_caps_calloc(1, tx_buffer_size, UART_MALLOC_CAPS);
  1345. uart_obj->tx_ring_buf_struct = heap_caps_calloc(1, sizeof(StaticRingbuffer_t), UART_MALLOC_CAPS);
  1346. if (!uart_obj->tx_ring_buf_storage || !uart_obj->tx_ring_buf_struct) {
  1347. goto err;
  1348. }
  1349. }
  1350. uart_obj->rx_ring_buf_storage = heap_caps_calloc(1, rx_buffer_size, UART_MALLOC_CAPS);
  1351. uart_obj->rx_ring_buf_struct = heap_caps_calloc(1, sizeof(StaticRingbuffer_t), UART_MALLOC_CAPS);
  1352. uart_obj->rx_mux_struct = heap_caps_calloc(1, sizeof(StaticSemaphore_t), UART_MALLOC_CAPS);
  1353. uart_obj->tx_mux_struct = heap_caps_calloc(1, sizeof(StaticSemaphore_t), UART_MALLOC_CAPS);
  1354. uart_obj->tx_brk_sem_struct = heap_caps_calloc(1, sizeof(StaticSemaphore_t), UART_MALLOC_CAPS);
  1355. uart_obj->tx_done_sem_struct = heap_caps_calloc(1, sizeof(StaticSemaphore_t), UART_MALLOC_CAPS);
  1356. uart_obj->tx_fifo_sem_struct = heap_caps_calloc(1, sizeof(StaticSemaphore_t), UART_MALLOC_CAPS);
  1357. if (!uart_obj->rx_ring_buf_storage || !uart_obj->rx_ring_buf_struct || !uart_obj->rx_mux_struct ||
  1358. !uart_obj->tx_mux_struct || !uart_obj->tx_brk_sem_struct || !uart_obj->tx_done_sem_struct ||
  1359. !uart_obj->tx_fifo_sem_struct) {
  1360. goto err;
  1361. }
  1362. if (event_queue_size > 0) {
  1363. uart_obj->event_queue = xQueueCreateStatic(event_queue_size, sizeof(uart_event_t),
  1364. uart_obj->event_queue_storage, uart_obj->event_queue_struct);
  1365. if (!uart_obj->event_queue) {
  1366. goto err;
  1367. }
  1368. }
  1369. if (tx_buffer_size > 0) {
  1370. uart_obj->tx_ring_buf = xRingbufferCreateStatic(tx_buffer_size, RINGBUF_TYPE_NOSPLIT,
  1371. uart_obj->tx_ring_buf_storage, uart_obj->tx_ring_buf_struct);
  1372. if (!uart_obj->tx_ring_buf) {
  1373. goto err;
  1374. }
  1375. }
  1376. uart_obj->rx_ring_buf = xRingbufferCreateStatic(rx_buffer_size, RINGBUF_TYPE_BYTEBUF,
  1377. uart_obj->rx_ring_buf_storage, uart_obj->rx_ring_buf_struct);
  1378. uart_obj->rx_mux = xSemaphoreCreateMutexStatic(uart_obj->rx_mux_struct);
  1379. uart_obj->tx_mux = xSemaphoreCreateMutexStatic(uart_obj->tx_mux_struct);
  1380. uart_obj->tx_brk_sem = xSemaphoreCreateBinaryStatic(uart_obj->tx_brk_sem_struct);
  1381. uart_obj->tx_done_sem = xSemaphoreCreateBinaryStatic(uart_obj->tx_done_sem_struct);
  1382. uart_obj->tx_fifo_sem = xSemaphoreCreateBinaryStatic(uart_obj->tx_fifo_sem_struct);
  1383. if (!uart_obj->rx_ring_buf || !uart_obj->rx_mux || !uart_obj->tx_mux || !uart_obj->tx_brk_sem ||
  1384. !uart_obj->tx_done_sem || !uart_obj->tx_fifo_sem) {
  1385. goto err;
  1386. }
  1387. #else
  1388. if (event_queue_size > 0) {
  1389. uart_obj->event_queue = xQueueCreate(event_queue_size, sizeof(uart_event_t));
  1390. if (!uart_obj->event_queue) {
  1391. goto err;
  1392. }
  1393. }
  1394. if (tx_buffer_size > 0) {
  1395. uart_obj->tx_ring_buf = xRingbufferCreate(tx_buffer_size, RINGBUF_TYPE_NOSPLIT);
  1396. if (!uart_obj->tx_ring_buf) {
  1397. goto err;
  1398. }
  1399. }
  1400. uart_obj->rx_ring_buf = xRingbufferCreate(rx_buffer_size, RINGBUF_TYPE_BYTEBUF);
  1401. uart_obj->tx_mux = xSemaphoreCreateMutex();
  1402. uart_obj->rx_mux = xSemaphoreCreateMutex();
  1403. uart_obj->tx_brk_sem = xSemaphoreCreateBinary();
  1404. uart_obj->tx_done_sem = xSemaphoreCreateBinary();
  1405. uart_obj->tx_fifo_sem = xSemaphoreCreateBinary();
  1406. if (!uart_obj->rx_ring_buf || !uart_obj->rx_mux || !uart_obj->tx_mux || !uart_obj->tx_brk_sem ||
  1407. !uart_obj->tx_done_sem || !uart_obj->tx_fifo_sem) {
  1408. goto err;
  1409. }
  1410. #endif
  1411. return uart_obj;
  1412. err:
  1413. uart_free_driver_obj(uart_obj);
  1414. return NULL;
  1415. }
  1416. 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)
  1417. {
  1418. esp_err_t r;
  1419. #ifdef CONFIG_ESP_SYSTEM_GDBSTUB_RUNTIME
  1420. 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.");
  1421. #endif // CONFIG_ESP_SYSTEM_GDBSTUB_RUNTIME
  1422. ESP_RETURN_ON_FALSE((uart_num < UART_NUM_MAX), ESP_FAIL, UART_TAG, "uart_num error");
  1423. ESP_RETURN_ON_FALSE((rx_buffer_size > SOC_UART_FIFO_LEN), ESP_FAIL, UART_TAG, "uart rx buffer length error");
  1424. ESP_RETURN_ON_FALSE((tx_buffer_size > SOC_UART_FIFO_LEN) || (tx_buffer_size == 0), ESP_FAIL, UART_TAG, "uart tx buffer length error");
  1425. #if CONFIG_UART_ISR_IN_IRAM
  1426. if ((intr_alloc_flags & ESP_INTR_FLAG_IRAM) == 0) {
  1427. ESP_LOGI(UART_TAG, "ESP_INTR_FLAG_IRAM flag not set while CONFIG_UART_ISR_IN_IRAM is enabled, flag updated");
  1428. intr_alloc_flags |= ESP_INTR_FLAG_IRAM;
  1429. }
  1430. #else
  1431. if ((intr_alloc_flags & ESP_INTR_FLAG_IRAM) != 0) {
  1432. ESP_LOGW(UART_TAG, "ESP_INTR_FLAG_IRAM flag is set while CONFIG_UART_ISR_IN_IRAM is not enabled, flag updated");
  1433. intr_alloc_flags &= ~ESP_INTR_FLAG_IRAM;
  1434. }
  1435. #endif
  1436. if (p_uart_obj[uart_num] == NULL) {
  1437. p_uart_obj[uart_num] = uart_alloc_driver_obj(event_queue_size, tx_buffer_size, rx_buffer_size);
  1438. if (p_uart_obj[uart_num] == NULL) {
  1439. ESP_LOGE(UART_TAG, "UART driver malloc error");
  1440. return ESP_FAIL;
  1441. }
  1442. p_uart_obj[uart_num]->uart_num = uart_num;
  1443. p_uart_obj[uart_num]->uart_mode = UART_MODE_UART;
  1444. p_uart_obj[uart_num]->coll_det_flg = false;
  1445. p_uart_obj[uart_num]->rx_always_timeout_flg = false;
  1446. p_uart_obj[uart_num]->event_queue_size = event_queue_size;
  1447. p_uart_obj[uart_num]->tx_ptr = NULL;
  1448. p_uart_obj[uart_num]->tx_head = NULL;
  1449. p_uart_obj[uart_num]->tx_len_tot = 0;
  1450. p_uart_obj[uart_num]->tx_brk_flg = 0;
  1451. p_uart_obj[uart_num]->tx_brk_len = 0;
  1452. p_uart_obj[uart_num]->tx_waiting_brk = 0;
  1453. p_uart_obj[uart_num]->rx_buffered_len = 0;
  1454. p_uart_obj[uart_num]->rx_buffer_full_flg = false;
  1455. p_uart_obj[uart_num]->tx_waiting_fifo = false;
  1456. p_uart_obj[uart_num]->rx_ptr = NULL;
  1457. p_uart_obj[uart_num]->rx_cur_remain = 0;
  1458. p_uart_obj[uart_num]->rx_head_ptr = NULL;
  1459. p_uart_obj[uart_num]->tx_buf_size = tx_buffer_size;
  1460. p_uart_obj[uart_num]->uart_select_notif_callback = NULL;
  1461. xSemaphoreGive(p_uart_obj[uart_num]->tx_fifo_sem);
  1462. uart_pattern_queue_reset(uart_num, UART_PATTERN_DET_QLEN_DEFAULT);
  1463. if (uart_queue) {
  1464. *uart_queue = p_uart_obj[uart_num]->event_queue;
  1465. ESP_LOGI(UART_TAG, "queue free spaces: %d", uxQueueSpacesAvailable(p_uart_obj[uart_num]->event_queue));
  1466. }
  1467. } else {
  1468. ESP_LOGE(UART_TAG, "UART driver already installed");
  1469. return ESP_FAIL;
  1470. }
  1471. uart_intr_config_t uart_intr = {
  1472. .intr_enable_mask = UART_INTR_CONFIG_FLAG,
  1473. .rxfifo_full_thresh = UART_FULL_THRESH_DEFAULT,
  1474. .rx_timeout_thresh = UART_TOUT_THRESH_DEFAULT,
  1475. .txfifo_empty_intr_thresh = UART_EMPTY_THRESH_DEFAULT,
  1476. };
  1477. uart_module_enable(uart_num);
  1478. uart_hal_disable_intr_mask(&(uart_context[uart_num].hal), UART_LL_INTR_MASK);
  1479. uart_hal_clr_intsts_mask(&(uart_context[uart_num].hal), UART_LL_INTR_MASK);
  1480. 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);
  1481. if (r != ESP_OK) {
  1482. goto err;
  1483. }
  1484. r = uart_intr_config(uart_num, &uart_intr);
  1485. if (r != ESP_OK) {
  1486. goto err;
  1487. }
  1488. return r;
  1489. err:
  1490. uart_driver_delete(uart_num);
  1491. return r;
  1492. }
  1493. //Make sure no other tasks are still using UART before you call this function
  1494. esp_err_t uart_driver_delete(uart_port_t uart_num)
  1495. {
  1496. ESP_RETURN_ON_FALSE((uart_num < UART_NUM_MAX), ESP_FAIL, UART_TAG, "uart_num error");
  1497. if (p_uart_obj[uart_num] == NULL) {
  1498. ESP_LOGI(UART_TAG, "ALREADY NULL");
  1499. return ESP_OK;
  1500. }
  1501. esp_intr_free(p_uart_obj[uart_num]->intr_handle);
  1502. uart_disable_rx_intr(uart_num);
  1503. uart_disable_tx_intr(uart_num);
  1504. uart_pattern_link_free(uart_num);
  1505. uart_free_driver_obj(p_uart_obj[uart_num]);
  1506. p_uart_obj[uart_num] = NULL;
  1507. #if SOC_UART_SUPPORT_RTC_CLK
  1508. uart_sclk_t sclk = 0;
  1509. uart_hal_get_sclk(&(uart_context[uart_num].hal), &sclk);
  1510. if (sclk == UART_SCLK_RTC) {
  1511. rtc_clk_disable(uart_num);
  1512. }
  1513. #endif
  1514. uart_module_disable(uart_num);
  1515. return ESP_OK;
  1516. }
  1517. bool uart_is_driver_installed(uart_port_t uart_num)
  1518. {
  1519. return uart_num < UART_NUM_MAX && (p_uart_obj[uart_num] != NULL);
  1520. }
  1521. void uart_set_select_notif_callback(uart_port_t uart_num, uart_select_notif_callback_t uart_select_notif_callback)
  1522. {
  1523. if (uart_num < UART_NUM_MAX && p_uart_obj[uart_num]) {
  1524. p_uart_obj[uart_num]->uart_select_notif_callback = (uart_select_notif_callback_t) uart_select_notif_callback;
  1525. }
  1526. }
  1527. portMUX_TYPE *uart_get_selectlock(void)
  1528. {
  1529. return &uart_selectlock;
  1530. }
  1531. // Set UART mode
  1532. esp_err_t uart_set_mode(uart_port_t uart_num, uart_mode_t mode)
  1533. {
  1534. ESP_RETURN_ON_FALSE((uart_num < UART_NUM_MAX), ESP_ERR_INVALID_ARG, UART_TAG, "uart_num error");
  1535. ESP_RETURN_ON_FALSE((p_uart_obj[uart_num]), ESP_ERR_INVALID_STATE, UART_TAG, "uart driver error");
  1536. if ((mode == UART_MODE_RS485_COLLISION_DETECT) || (mode == UART_MODE_RS485_APP_CTRL)
  1537. || (mode == UART_MODE_RS485_HALF_DUPLEX)) {
  1538. ESP_RETURN_ON_FALSE((!uart_hal_is_hw_rts_en(&(uart_context[uart_num].hal))), ESP_ERR_INVALID_ARG, UART_TAG,
  1539. "disable hw flowctrl before using RS485 mode");
  1540. }
  1541. UART_ENTER_CRITICAL(&(uart_context[uart_num].spinlock));
  1542. uart_hal_set_mode(&(uart_context[uart_num].hal), mode);
  1543. if (mode == UART_MODE_RS485_COLLISION_DETECT) {
  1544. // This mode allows read while transmitting that allows collision detection
  1545. p_uart_obj[uart_num]->coll_det_flg = false;
  1546. // Enable collision detection interrupts
  1547. uart_hal_ena_intr_mask(&(uart_context[uart_num].hal), UART_INTR_RXFIFO_TOUT
  1548. | UART_INTR_RXFIFO_FULL
  1549. | UART_INTR_RS485_CLASH
  1550. | UART_INTR_RS485_FRM_ERR
  1551. | UART_INTR_RS485_PARITY_ERR);
  1552. }
  1553. p_uart_obj[uart_num]->uart_mode = mode;
  1554. UART_EXIT_CRITICAL(&(uart_context[uart_num].spinlock));
  1555. return ESP_OK;
  1556. }
  1557. esp_err_t uart_set_rx_full_threshold(uart_port_t uart_num, int threshold)
  1558. {
  1559. ESP_RETURN_ON_FALSE((uart_num < UART_NUM_MAX), ESP_ERR_INVALID_ARG, UART_TAG, "uart_num error");
  1560. ESP_RETURN_ON_FALSE((threshold < UART_RXFIFO_FULL_THRHD_V) && (threshold > 0), ESP_ERR_INVALID_ARG, UART_TAG,
  1561. "rx fifo full threshold value error");
  1562. if (p_uart_obj[uart_num] == NULL) {
  1563. ESP_LOGE(UART_TAG, "call uart_driver_install API first");
  1564. return ESP_ERR_INVALID_STATE;
  1565. }
  1566. UART_ENTER_CRITICAL(&(uart_context[uart_num].spinlock));
  1567. if (uart_hal_get_intr_ena_status(&(uart_context[uart_num].hal)) & UART_INTR_RXFIFO_FULL) {
  1568. uart_hal_set_rxfifo_full_thr(&(uart_context[uart_num].hal), threshold);
  1569. }
  1570. UART_EXIT_CRITICAL(&(uart_context[uart_num].spinlock));
  1571. return ESP_OK;
  1572. }
  1573. esp_err_t uart_set_tx_empty_threshold(uart_port_t uart_num, int threshold)
  1574. {
  1575. ESP_RETURN_ON_FALSE((uart_num < UART_NUM_MAX), ESP_ERR_INVALID_ARG, UART_TAG, "uart_num error");
  1576. ESP_RETURN_ON_FALSE((threshold < UART_TXFIFO_EMPTY_THRHD_V) && (threshold > 0), ESP_ERR_INVALID_ARG, UART_TAG,
  1577. "tx fifo empty threshold value error");
  1578. if (p_uart_obj[uart_num] == NULL) {
  1579. ESP_LOGE(UART_TAG, "call uart_driver_install API first");
  1580. return ESP_ERR_INVALID_STATE;
  1581. }
  1582. UART_ENTER_CRITICAL(&(uart_context[uart_num].spinlock));
  1583. if (uart_hal_get_intr_ena_status(&(uart_context[uart_num].hal)) & UART_INTR_TXFIFO_EMPTY) {
  1584. uart_hal_set_txfifo_empty_thr(&(uart_context[uart_num].hal), threshold);
  1585. }
  1586. UART_EXIT_CRITICAL(&(uart_context[uart_num].spinlock));
  1587. return ESP_OK;
  1588. }
  1589. esp_err_t uart_set_rx_timeout(uart_port_t uart_num, const uint8_t tout_thresh)
  1590. {
  1591. ESP_RETURN_ON_FALSE((uart_num < UART_NUM_MAX), ESP_ERR_INVALID_ARG, UART_TAG, "uart_num error");
  1592. // get maximum timeout threshold
  1593. uint16_t tout_max_thresh = uart_hal_get_max_rx_timeout_thrd(&(uart_context[uart_num].hal));
  1594. if (tout_thresh > tout_max_thresh) {
  1595. ESP_LOGE(UART_TAG, "tout_thresh = %d > maximum value = %d", tout_thresh, tout_max_thresh);
  1596. return ESP_ERR_INVALID_ARG;
  1597. }
  1598. UART_ENTER_CRITICAL(&(uart_context[uart_num].spinlock));
  1599. uart_hal_set_rx_timeout(&(uart_context[uart_num].hal), tout_thresh);
  1600. UART_EXIT_CRITICAL(&(uart_context[uart_num].spinlock));
  1601. return ESP_OK;
  1602. }
  1603. esp_err_t uart_get_collision_flag(uart_port_t uart_num, bool *collision_flag)
  1604. {
  1605. ESP_RETURN_ON_FALSE((uart_num < UART_NUM_MAX), ESP_ERR_INVALID_ARG, UART_TAG, "uart_num error");
  1606. ESP_RETURN_ON_FALSE((p_uart_obj[uart_num]), ESP_FAIL, UART_TAG, "uart driver error");
  1607. ESP_RETURN_ON_FALSE((collision_flag != NULL), ESP_ERR_INVALID_ARG, UART_TAG, "wrong parameter pointer");
  1608. 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)),
  1609. ESP_ERR_INVALID_ARG, UART_TAG, "wrong mode");
  1610. *collision_flag = p_uart_obj[uart_num]->coll_det_flg;
  1611. return ESP_OK;
  1612. }
  1613. esp_err_t uart_set_wakeup_threshold(uart_port_t uart_num, int wakeup_threshold)
  1614. {
  1615. ESP_RETURN_ON_FALSE((uart_num < UART_NUM_MAX), ESP_ERR_INVALID_ARG, UART_TAG, "uart_num error");
  1616. ESP_RETURN_ON_FALSE((wakeup_threshold <= UART_ACTIVE_THRESHOLD_V && wakeup_threshold > UART_MIN_WAKEUP_THRESH), ESP_ERR_INVALID_ARG, UART_TAG,
  1617. "wakeup_threshold out of bounds");
  1618. UART_ENTER_CRITICAL(&(uart_context[uart_num].spinlock));
  1619. uart_hal_set_wakeup_thrd(&(uart_context[uart_num].hal), wakeup_threshold);
  1620. UART_EXIT_CRITICAL(&(uart_context[uart_num].spinlock));
  1621. return ESP_OK;
  1622. }
  1623. esp_err_t uart_get_wakeup_threshold(uart_port_t uart_num, int *out_wakeup_threshold)
  1624. {
  1625. ESP_RETURN_ON_FALSE((uart_num < UART_NUM_MAX), ESP_ERR_INVALID_ARG, UART_TAG, "uart_num error");
  1626. ESP_RETURN_ON_FALSE((out_wakeup_threshold != NULL), ESP_ERR_INVALID_ARG, UART_TAG, "argument is NULL");
  1627. uart_hal_get_wakeup_thrd(&(uart_context[uart_num].hal), (uint32_t *)out_wakeup_threshold);
  1628. return ESP_OK;
  1629. }
  1630. esp_err_t uart_wait_tx_idle_polling(uart_port_t uart_num)
  1631. {
  1632. ESP_RETURN_ON_FALSE((uart_num < UART_NUM_MAX), ESP_ERR_INVALID_ARG, UART_TAG, "uart_num error");
  1633. while (!uart_hal_is_tx_idle(&(uart_context[uart_num].hal)));
  1634. return ESP_OK;
  1635. }
  1636. esp_err_t uart_set_loop_back(uart_port_t uart_num, bool loop_back_en)
  1637. {
  1638. ESP_RETURN_ON_FALSE((uart_num < UART_NUM_MAX), ESP_ERR_INVALID_ARG, UART_TAG, "uart_num error");
  1639. uart_hal_set_loop_back(&(uart_context[uart_num].hal), loop_back_en);
  1640. return ESP_OK;
  1641. }
  1642. void uart_set_always_rx_timeout(uart_port_t uart_num, bool always_rx_timeout)
  1643. {
  1644. uint16_t rx_tout = uart_hal_get_rx_tout_thr(&(uart_context[uart_num].hal));
  1645. if (rx_tout) {
  1646. p_uart_obj[uart_num]->rx_always_timeout_flg = always_rx_timeout;
  1647. } else {
  1648. p_uart_obj[uart_num]->rx_always_timeout_flg = false;
  1649. }
  1650. }