uart.c 78 KB

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