uart.c 83 KB

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