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