uart.c 80 KB

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