uart.c 75 KB

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