uart.c 82 KB

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