uart.c 82 KB

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