1 /*
2 * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 // The LL layer for UART register operations.
8 // Note that most of the register operations in this layer are non-atomic operations.
9
10
11 #pragma once
12
13 #include <stdlib.h>
14 #include "hal/misc.h"
15 #include "hal/uart_types.h"
16 #include "soc/uart_reg.h"
17 #include "soc/uart_struct.h"
18 #include "soc/system_struct.h"
19 #include "soc/system_reg.h"
20 #include "soc/dport_access.h"
21 #include "esp_attr.h"
22
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26
27 // The default fifo depth
28 #define UART_LL_FIFO_DEF_LEN (SOC_UART_FIFO_LEN)
29 // Get UART hardware instance with giving uart num
30 #define UART_LL_GET_HW(num) (((num) == 0) ? (&UART0) : (((num) == 1) ? (&UART1) : (&UART2)))
31
32 #define UART_LL_MIN_WAKEUP_THRESH (3)
33 #define UART_LL_INTR_MASK (0x7ffff) //All interrupt mask
34
35 #define UART_LL_FSM_IDLE (0x0)
36 #define UART_LL_FSM_TX_WAIT_SEND (0xf)
37
38 // Define UART interrupts
39 typedef enum {
40 UART_INTR_RXFIFO_FULL = (0x1 << 0),
41 UART_INTR_TXFIFO_EMPTY = (0x1 << 1),
42 UART_INTR_PARITY_ERR = (0x1 << 2),
43 UART_INTR_FRAM_ERR = (0x1 << 3),
44 UART_INTR_RXFIFO_OVF = (0x1 << 4),
45 UART_INTR_DSR_CHG = (0x1 << 5),
46 UART_INTR_CTS_CHG = (0x1 << 6),
47 UART_INTR_BRK_DET = (0x1 << 7),
48 UART_INTR_RXFIFO_TOUT = (0x1 << 8),
49 UART_INTR_SW_XON = (0x1 << 9),
50 UART_INTR_SW_XOFF = (0x1 << 10),
51 UART_INTR_GLITCH_DET = (0x1 << 11),
52 UART_INTR_TX_BRK_DONE = (0x1 << 12),
53 UART_INTR_TX_BRK_IDLE = (0x1 << 13),
54 UART_INTR_TX_DONE = (0x1 << 14),
55 UART_INTR_RS485_PARITY_ERR = (0x1 << 15),
56 UART_INTR_RS485_FRM_ERR = (0x1 << 16),
57 UART_INTR_RS485_CLASH = (0x1 << 17),
58 UART_INTR_CMD_CHAR_DET = (0x1 << 18),
59 UART_INTR_WAKEUP = (0x1 << 19),
60 } uart_intr_t;
61
62 /**
63 * @brief Check if UART is enabled or disabled.
64 *
65 * @param uart_num UART port number, the max port number is (UART_NUM_MAX -1).
66 *
67 * @return true: enabled; false: disabled
68 */
uart_ll_is_enabled(uint32_t uart_num)69 FORCE_INLINE_ATTR bool uart_ll_is_enabled(uint32_t uart_num)
70 {
71 uint32_t uart_rst_bit = ((uart_num == 0) ? SYSTEM_UART_RST :
72 (uart_num == 1) ? SYSTEM_UART1_RST :
73 (uart_num == 2) ? SYSTEM_UART2_RST : 0);
74 uint32_t uart_en_bit = ((uart_num == 0) ? SYSTEM_UART_CLK_EN :
75 (uart_num == 1) ? SYSTEM_UART1_CLK_EN :
76 (uart_num == 2) ? SYSTEM_UART2_CLK_EN : 0);
77 return DPORT_REG_GET_BIT(SYSTEM_PERIP_RST_EN0_REG, uart_rst_bit) == 0 &&
78 DPORT_REG_GET_BIT(SYSTEM_PERIP_CLK_EN0_REG, uart_en_bit) != 0;
79 }
80
81 /**
82 * @brief Configure the UART core reset.
83 *
84 * @param hw Beginning address of the peripheral registers.
85 * @param core_rst_en True to enable the core reset, otherwise set it false.
86 *
87 * @return None.
88 */
uart_ll_set_reset_core(uart_dev_t * hw,bool core_rst_en)89 static inline void uart_ll_set_reset_core(uart_dev_t *hw, bool core_rst_en)
90 {
91 hw->clk_conf.rst_core = core_rst_en;
92 }
93
94 /**
95 * @brief Set the UART source clock.
96 *
97 * @param hw Beginning address of the peripheral registers.
98 * @param source_clk The UART source clock. The source clock can be APB clock, RTC clock or XTAL clock.
99 * If the source clock is RTC/XTAL, the UART can still work when the APB changes.
100 *
101 * @return None.
102 */
uart_ll_set_sclk(uart_dev_t * hw,uart_sclk_t source_clk)103 FORCE_INLINE_ATTR void uart_ll_set_sclk(uart_dev_t *hw, uart_sclk_t source_clk)
104 {
105 switch (source_clk) {
106 default:
107 case UART_SCLK_APB:
108 hw->clk_conf.sclk_sel = 1;
109 break;
110 case UART_SCLK_RTC:
111 hw->clk_conf.sclk_sel = 2;
112 break;
113 case UART_SCLK_XTAL:
114 hw->clk_conf.sclk_sel = 3;
115 break;
116 }
117 }
118
119 /**
120 * @brief Get the UART source clock type.
121 *
122 * @param hw Beginning address of the peripheral registers.
123 * @param source_clk The pointer to accept the UART source clock type.
124 *
125 * @return None.
126 */
uart_ll_get_sclk(uart_dev_t * hw,uart_sclk_t * source_clk)127 FORCE_INLINE_ATTR void uart_ll_get_sclk(uart_dev_t *hw, uart_sclk_t *source_clk)
128 {
129 switch (hw->clk_conf.sclk_sel) {
130 default:
131 case 1:
132 *source_clk = UART_SCLK_APB;
133 break;
134 case 2:
135 *source_clk = UART_SCLK_RTC;
136 break;
137 case 3:
138 *source_clk = UART_SCLK_XTAL;
139 break;
140 }
141 }
142
143 /**
144 * @brief Configure the baud-rate.
145 *
146 * @param hw Beginning address of the peripheral registers.
147 * @param baud The baud rate to be set.
148 * @param sclk_freq Frequency of the clock source of UART, in Hz.
149 *
150 * @return None
151 */
uart_ll_set_baudrate(uart_dev_t * hw,uint32_t baud,uint32_t sclk_freq)152 FORCE_INLINE_ATTR void uart_ll_set_baudrate(uart_dev_t *hw, uint32_t baud, uint32_t sclk_freq)
153 {
154 #define DIV_UP(a, b) (((a) + (b) - 1) / (b))
155 const uint32_t max_div = BIT(12) - 1; // UART divider integer part only has 12 bits
156 uint32_t sclk_div = DIV_UP(sclk_freq, (uint64_t)max_div * baud);
157
158 if (sclk_div == 0) abort();
159
160 uint32_t clk_div = ((sclk_freq) << 4) / (baud * sclk_div);
161 // The baud rate configuration register is divided into
162 // an integer part and a fractional part.
163 hw->clkdiv.clkdiv = clk_div >> 4;
164 hw->clkdiv.clkdiv_frag = clk_div & 0xf;
165 HAL_FORCE_MODIFY_U32_REG_FIELD(hw->clk_conf, sclk_div_num, sclk_div - 1);
166 #undef DIV_UP
167 }
168
169 /**
170 * @brief Get the current baud-rate.
171 *
172 * @param hw Beginning address of the peripheral registers.
173 * @param sclk_freq Frequency of the clock source of UART, in Hz.
174 *
175 * @return The current baudrate
176 */
uart_ll_get_baudrate(uart_dev_t * hw,uint32_t sclk_freq)177 FORCE_INLINE_ATTR uint32_t uart_ll_get_baudrate(uart_dev_t *hw, uint32_t sclk_freq)
178 {
179 uart_clkdiv_reg_t div_reg;
180 div_reg.val = hw->clkdiv.val;
181 return ((sclk_freq << 4)) /
182 (((div_reg.clkdiv << 4) | div_reg.clkdiv_frag) * (HAL_FORCE_READ_U32_REG_FIELD(hw->clk_conf, sclk_div_num) + 1));
183 }
184
185 /**
186 * @brief Enable the UART interrupt based on the given mask.
187 *
188 * @param hw Beginning address of the peripheral registers.
189 * @param mask The bitmap of the interrupts need to be enabled.
190 *
191 * @return None
192 */
uart_ll_ena_intr_mask(uart_dev_t * hw,uint32_t mask)193 FORCE_INLINE_ATTR void uart_ll_ena_intr_mask(uart_dev_t *hw, uint32_t mask)
194 {
195 hw->int_ena.val |= mask;
196 }
197
198 /**
199 * @brief Disable the UART interrupt based on the given mask.
200 *
201 * @param hw Beginning address of the peripheral registers.
202 * @param mask The bitmap of the interrupts need to be disabled.
203 *
204 * @return None
205 */
uart_ll_disable_intr_mask(uart_dev_t * hw,uint32_t mask)206 FORCE_INLINE_ATTR void uart_ll_disable_intr_mask(uart_dev_t *hw, uint32_t mask)
207 {
208 hw->int_ena.val &= (~mask);
209 }
210
211 /**
212 * @brief Get the UART raw interrupt status.
213 *
214 * @param hw Beginning address of the peripheral registers.
215 *
216 * @return The UART interrupt status.
217 */
uart_ll_get_intraw_mask(uart_dev_t * hw)218 static inline uint32_t uart_ll_get_intraw_mask(uart_dev_t *hw)
219 {
220 return hw->int_raw.val;
221 }
222
223 /**
224 * @brief Get the UART interrupt status.
225 *
226 * @param hw Beginning address of the peripheral registers.
227 *
228 * @return The UART interrupt status.
229 */
uart_ll_get_intsts_mask(uart_dev_t * hw)230 FORCE_INLINE_ATTR uint32_t uart_ll_get_intsts_mask(uart_dev_t *hw)
231 {
232 return hw->int_st.val;
233 }
234
235 /**
236 * @brief Clear the UART interrupt status based on the given mask.
237 *
238 * @param hw Beginning address of the peripheral registers.
239 * @param mask The bitmap of the interrupts need to be cleared.
240 *
241 * @return None
242 */
uart_ll_clr_intsts_mask(uart_dev_t * hw,uint32_t mask)243 FORCE_INLINE_ATTR void uart_ll_clr_intsts_mask(uart_dev_t *hw, uint32_t mask)
244 {
245 hw->int_clr.val = mask;
246 }
247
248 /**
249 * @brief Get status of enabled interrupt.
250 *
251 * @param hw Beginning address of the peripheral registers.
252 *
253 * @return interrupt enable value
254 */
uart_ll_get_intr_ena_status(uart_dev_t * hw)255 FORCE_INLINE_ATTR uint32_t uart_ll_get_intr_ena_status(uart_dev_t *hw)
256 {
257 return hw->int_ena.val;
258 }
259
260 /**
261 * @brief Read the UART rxfifo.
262 *
263 * @param hw Beginning address of the peripheral registers.
264 * @param buf The data buffer. The buffer size should be large than 128 byts.
265 * @param rd_len The data length needs to be read.
266 *
267 * @return None.
268 */
uart_ll_read_rxfifo(uart_dev_t * hw,uint8_t * buf,uint32_t rd_len)269 FORCE_INLINE_ATTR void uart_ll_read_rxfifo(uart_dev_t *hw, uint8_t *buf, uint32_t rd_len)
270 {
271 for (int i = 0; i < (int)rd_len; i++) {
272 buf[i] = hw->fifo.rxfifo_rd_byte;
273 }
274 }
275
276 /**
277 * @brief Write byte to the UART txfifo.
278 *
279 * @param hw Beginning address of the peripheral registers.
280 * @param buf The data buffer.
281 * @param wr_len The data length needs to be writen.
282 *
283 * @return None
284 */
uart_ll_write_txfifo(uart_dev_t * hw,const uint8_t * buf,uint32_t wr_len)285 FORCE_INLINE_ATTR void uart_ll_write_txfifo(uart_dev_t *hw, const uint8_t *buf, uint32_t wr_len)
286 {
287 for (int i = 0; i < (int)wr_len; i++) {
288 hw->fifo.rxfifo_rd_byte = buf[i];
289 }
290 }
291
292 /**
293 * @brief Reset the UART hw rxfifo.
294 *
295 * @param hw Beginning address of the peripheral registers.
296 *
297 * @return None
298 */
uart_ll_rxfifo_rst(uart_dev_t * hw)299 FORCE_INLINE_ATTR void uart_ll_rxfifo_rst(uart_dev_t *hw)
300 {
301 hw->conf0.rxfifo_rst = 1;
302 hw->conf0.rxfifo_rst = 0;
303 }
304
305 /**
306 * @brief Reset the UART hw txfifo.
307 *
308 * @param hw Beginning address of the peripheral registers.
309 *
310 * @return None
311 */
uart_ll_txfifo_rst(uart_dev_t * hw)312 FORCE_INLINE_ATTR void uart_ll_txfifo_rst(uart_dev_t *hw)
313 {
314 hw->conf0.txfifo_rst = 1;
315 hw->conf0.txfifo_rst = 0;
316 }
317
318 /**
319 * @brief Get the length of readable data in UART rxfifo.
320 *
321 * @param hw Beginning address of the peripheral registers.
322 *
323 * @return The readable data length in rxfifo.
324 */
uart_ll_get_rxfifo_len(uart_dev_t * hw)325 FORCE_INLINE_ATTR uint32_t uart_ll_get_rxfifo_len(uart_dev_t *hw)
326 {
327 return hw->status.rxfifo_cnt;
328 }
329
330 /**
331 * @brief Get the writable data length of UART txfifo.
332 *
333 * @param hw Beginning address of the peripheral registers.
334 *
335 * @return The data length of txfifo can be written.
336 */
uart_ll_get_txfifo_len(uart_dev_t * hw)337 FORCE_INLINE_ATTR uint32_t uart_ll_get_txfifo_len(uart_dev_t *hw)
338 {
339 return UART_LL_FIFO_DEF_LEN - hw->status.txfifo_cnt;
340 }
341
342 /**
343 * @brief Configure the UART stop bit.
344 *
345 * @param hw Beginning address of the peripheral registers.
346 * @param stop_bit The stop bit number to be set.
347 *
348 * @return None.
349 */
uart_ll_set_stop_bits(uart_dev_t * hw,uart_stop_bits_t stop_bit)350 FORCE_INLINE_ATTR void uart_ll_set_stop_bits(uart_dev_t *hw, uart_stop_bits_t stop_bit)
351 {
352 hw->conf0.stop_bit_num = stop_bit;
353 }
354
355 /**
356 * @brief Get the configuration of the UART stop bit.
357 *
358 * @param hw Beginning address of the peripheral registers.
359 * @param stop_bit The pointer to accept the stop bit configuration
360 *
361 * @return None.
362 */
uart_ll_get_stop_bits(uart_dev_t * hw,uart_stop_bits_t * stop_bit)363 FORCE_INLINE_ATTR void uart_ll_get_stop_bits(uart_dev_t *hw, uart_stop_bits_t *stop_bit)
364 {
365 *stop_bit = (uart_stop_bits_t)hw->conf0.stop_bit_num;
366 }
367
368 /**
369 * @brief Configure the UART parity check mode.
370 *
371 * @param hw Beginning address of the peripheral registers.
372 * @param parity_mode The parity check mode to be set.
373 *
374 * @return None.
375 */
uart_ll_set_parity(uart_dev_t * hw,uart_parity_t parity_mode)376 FORCE_INLINE_ATTR void uart_ll_set_parity(uart_dev_t *hw, uart_parity_t parity_mode)
377 {
378 if (parity_mode != UART_PARITY_DISABLE) {
379 hw->conf0.parity = parity_mode & 0x1;
380 }
381 hw->conf0.parity_en = (parity_mode >> 1) & 0x1;
382 }
383
384 /**
385 * @brief Get the UART parity check mode configuration.
386 *
387 * @param hw Beginning address of the peripheral registers.
388 * @param parity_mode The pointer to accept the parity check mode configuration.
389 *
390 * @return None.
391 */
uart_ll_get_parity(uart_dev_t * hw,uart_parity_t * parity_mode)392 FORCE_INLINE_ATTR void uart_ll_get_parity(uart_dev_t *hw, uart_parity_t *parity_mode)
393 {
394 if (hw->conf0.parity_en) {
395 *parity_mode = (uart_parity_t)(0x2 | hw->conf0.parity);
396 } else {
397 *parity_mode = UART_PARITY_DISABLE;
398 }
399 }
400
401 /**
402 * @brief Set the UART rxfifo full threshold value. When the data in rxfifo is more than the threshold value,
403 * it will produce rxfifo_full_int_raw interrupt.
404 *
405 * @param hw Beginning address of the peripheral registers.
406 * @param full_thrhd The full threshold value of the rxfifo. `full_thrhd` should be less than `UART_LL_FIFO_DEF_LEN`.
407 *
408 * @return None.
409 */
uart_ll_set_rxfifo_full_thr(uart_dev_t * hw,uint16_t full_thrhd)410 FORCE_INLINE_ATTR void uart_ll_set_rxfifo_full_thr(uart_dev_t *hw, uint16_t full_thrhd)
411 {
412 hw->conf1.rxfifo_full_thrhd = full_thrhd;
413 }
414
415 /**
416 * @brief Set the txfifo empty threshold. when the data length in txfifo is less than threshold value,
417 * it will produce txfifo_empty_int_raw interrupt.
418 *
419 * @param hw Beginning address of the peripheral registers.
420 * @param empty_thrhd The empty threshold of txfifo.
421 *
422 * @return None.
423 */
uart_ll_set_txfifo_empty_thr(uart_dev_t * hw,uint16_t empty_thrhd)424 FORCE_INLINE_ATTR void uart_ll_set_txfifo_empty_thr(uart_dev_t *hw, uint16_t empty_thrhd)
425 {
426 hw->conf1.txfifo_empty_thrhd = empty_thrhd;
427 }
428
429 /**
430 * @brief Set the UART rx-idle threshold value. when receiver takes more time than rx_idle_thrhd to receive a byte data,
431 * it will produce frame end signal for uhci to stop receiving data.
432 *
433 * @param hw Beginning address of the peripheral registers.
434 * @param rx_idle_thr The rx-idle threshold to be set.
435 *
436 * @return None.
437 */
uart_ll_set_rx_idle_thr(uart_dev_t * hw,uint32_t rx_idle_thr)438 FORCE_INLINE_ATTR void uart_ll_set_rx_idle_thr(uart_dev_t *hw, uint32_t rx_idle_thr)
439 {
440 hw->idle_conf.rx_idle_thrhd = rx_idle_thr;
441 }
442
443 /**
444 * @brief Configure the duration time between transfers.
445 *
446 * @param hw Beginning address of the peripheral registers.
447 * @param idle_num the duration time between transfers.
448 *
449 * @return None.
450 */
uart_ll_set_tx_idle_num(uart_dev_t * hw,uint32_t idle_num)451 FORCE_INLINE_ATTR void uart_ll_set_tx_idle_num(uart_dev_t *hw, uint32_t idle_num)
452 {
453 hw->idle_conf.tx_idle_num = idle_num;
454 }
455
456 /**
457 * @brief Configure the transmiter to send break chars.
458 *
459 * @param hw Beginning address of the peripheral registers.
460 * @param break_num The number of the break chars need to be send.
461 *
462 * @return None.
463 */
uart_ll_tx_break(uart_dev_t * hw,uint32_t break_num)464 FORCE_INLINE_ATTR void uart_ll_tx_break(uart_dev_t *hw, uint32_t break_num)
465 {
466 if (break_num > 0) {
467 HAL_FORCE_MODIFY_U32_REG_FIELD(hw->txbrk_conf, tx_brk_num, break_num);
468 hw->conf0.txd_brk = 1;
469 } else {
470 hw->conf0.txd_brk = 0;
471 }
472 }
473 /**
474 * @brief Configure the UART hardware flow control.
475 *
476 * @param hw Beginning address of the peripheral registers.
477 * @param flow_ctrl The hw flow control configuration.
478 * @param rx_thrs The rx flow control signal will be active if the data length in rxfifo is more than this value.
479 *
480 * @return None.
481 */
uart_ll_set_hw_flow_ctrl(uart_dev_t * hw,uart_hw_flowcontrol_t flow_ctrl,uint32_t rx_thrs)482 FORCE_INLINE_ATTR void uart_ll_set_hw_flow_ctrl(uart_dev_t *hw, uart_hw_flowcontrol_t flow_ctrl, uint32_t rx_thrs)
483 {
484 //only when UART_HW_FLOWCTRL_RTS is set , will the rx_thresh value be set.
485 if (flow_ctrl & UART_HW_FLOWCTRL_RTS) {
486 hw->mem_conf.rx_flow_thrhd = rx_thrs;
487 hw->conf1.rx_flow_en = 1;
488 } else {
489 hw->conf1.rx_flow_en = 0;
490 }
491 if (flow_ctrl & UART_HW_FLOWCTRL_CTS) {
492 hw->conf0.tx_flow_en = 1;
493 } else {
494 hw->conf0.tx_flow_en = 0;
495 }
496 }
497
498 /**
499 * @brief Configure the hardware flow control.
500 *
501 * @param hw Beginning address of the peripheral registers.
502 * @param flow_ctrl A pointer to accept the hw flow control configuration.
503 *
504 * @return None.
505 */
uart_ll_get_hw_flow_ctrl(uart_dev_t * hw,uart_hw_flowcontrol_t * flow_ctrl)506 FORCE_INLINE_ATTR void uart_ll_get_hw_flow_ctrl(uart_dev_t *hw, uart_hw_flowcontrol_t *flow_ctrl)
507 {
508 *flow_ctrl = UART_HW_FLOWCTRL_DISABLE;
509 if (hw->conf1.rx_flow_en) {
510 *flow_ctrl = (uart_hw_flowcontrol_t)((unsigned int)(*flow_ctrl) | (unsigned int)UART_HW_FLOWCTRL_RTS);
511 }
512 if (hw->conf0.tx_flow_en) {
513 *flow_ctrl = (uart_hw_flowcontrol_t)((unsigned int)(*flow_ctrl) | (unsigned int)UART_HW_FLOWCTRL_CTS);
514 }
515 }
516
517 /**
518 * @brief Configure the software flow control.
519 *
520 * @param hw Beginning address of the peripheral registers.
521 * @param flow_ctrl The UART sofware flow control settings.
522 * @param sw_flow_ctrl_en Set true to enable software flow control, otherwise set it false.
523 *
524 * @return None.
525 */
uart_ll_set_sw_flow_ctrl(uart_dev_t * hw,uart_sw_flowctrl_t * flow_ctrl,bool sw_flow_ctrl_en)526 FORCE_INLINE_ATTR void uart_ll_set_sw_flow_ctrl(uart_dev_t *hw, uart_sw_flowctrl_t *flow_ctrl, bool sw_flow_ctrl_en)
527 {
528 if (sw_flow_ctrl_en) {
529 hw->flow_conf.xonoff_del = 1;
530 hw->flow_conf.sw_flow_con_en = 1;
531 hw->swfc_conf1.xon_threshold = flow_ctrl->xon_thrd;
532 hw->swfc_conf0.xoff_threshold = flow_ctrl->xoff_thrd;
533 HAL_FORCE_MODIFY_U32_REG_FIELD(hw->swfc_conf1, xon_char, flow_ctrl->xon_char);
534 HAL_FORCE_MODIFY_U32_REG_FIELD(hw->swfc_conf0, xoff_char, flow_ctrl->xoff_char);
535 } else {
536 hw->flow_conf.sw_flow_con_en = 0;
537 hw->flow_conf.xonoff_del = 0;
538 }
539 }
540
541 /**
542 * @brief Configure the AT cmd char. When the receiver receives a continuous AT cmd char, it will produce at_cmd_char_det interrupt.
543 *
544 * @param hw Beginning address of the peripheral registers.
545 * @param cmd_char The AT cmd char configuration.The configuration member is:
546 * - cmd_char The AT cmd character
547 * - char_num The number of received AT cmd char must be equal to or greater than this value
548 * - gap_tout The interval between each AT cmd char, when the duration is less than this value, it will not take this data as AT cmd char
549 * - pre_idle The idle time before the first AT cmd char, when the duration is less than this value, it will not take the previous data as the last AT cmd char
550 * - post_idle The idle time after the last AT cmd char, when the duration is less than this value, it will not take this data as the first AT cmd char
551 *
552 * @return None.
553 */
uart_ll_set_at_cmd_char(uart_dev_t * hw,uart_at_cmd_t * cmd_char)554 FORCE_INLINE_ATTR void uart_ll_set_at_cmd_char(uart_dev_t *hw, uart_at_cmd_t *cmd_char)
555 {
556 HAL_FORCE_MODIFY_U32_REG_FIELD(hw->at_cmd_char, at_cmd_char, cmd_char->cmd_char);
557 HAL_FORCE_MODIFY_U32_REG_FIELD(hw->at_cmd_char, char_num, cmd_char->char_num);
558 HAL_FORCE_MODIFY_U32_REG_FIELD(hw->at_cmd_postcnt, post_idle_num, cmd_char->post_idle);
559 HAL_FORCE_MODIFY_U32_REG_FIELD(hw->at_cmd_precnt, pre_idle_num, cmd_char->pre_idle);
560 HAL_FORCE_MODIFY_U32_REG_FIELD(hw->at_cmd_gaptout, rx_gap_tout, cmd_char->gap_tout);
561 }
562
563 /**
564 * @brief Set the UART data bit mode.
565 *
566 * @param hw Beginning address of the peripheral registers.
567 * @param data_bit The data bit mode to be set.
568 *
569 * @return None.
570 */
uart_ll_set_data_bit_num(uart_dev_t * hw,uart_word_length_t data_bit)571 FORCE_INLINE_ATTR void uart_ll_set_data_bit_num(uart_dev_t *hw, uart_word_length_t data_bit)
572 {
573 hw->conf0.bit_num = data_bit;
574 }
575
576 /**
577 FORCE_INLINE_ATTR void uart_ll_get_sclk(uart_dev_t *hw, uart_sclk_t *source_clk)
578 * @brief Set the rts active level.
579 *
580 * @param hw Beginning address of the peripheral registers.
581 * @param level The rts active level, 0 or 1.
582 *
583 * @return None.
584 */
uart_ll_set_rts_active_level(uart_dev_t * hw,int level)585 FORCE_INLINE_ATTR void uart_ll_set_rts_active_level(uart_dev_t *hw, int level)
586 {
587 hw->conf0.sw_rts = level & 0x1;
588 }
589
590 /**
591 * @brief Set the dtr active level.
592 *
593 * @param hw Beginning address of the peripheral registers.
594 * @param level The dtr active level, 0 or 1.
595 *
596 * @return None.
597 */
uart_ll_set_dtr_active_level(uart_dev_t * hw,int level)598 FORCE_INLINE_ATTR void uart_ll_set_dtr_active_level(uart_dev_t *hw, int level)
599 {
600 hw->conf0.sw_dtr = level & 0x1;
601 }
602
603 /**
604 * @brief Set the UART wakeup threshold.
605 *
606 * @param hw Beginning address of the peripheral registers.
607 * @param wakeup_thrd The wakeup threshold value to be set. When the input rx edge changes more than this value,
608 * the UART will active from light sleeping mode.
609 *
610 * @return None.
611 */
uart_ll_set_wakeup_thrd(uart_dev_t * hw,uint32_t wakeup_thrd)612 FORCE_INLINE_ATTR void uart_ll_set_wakeup_thrd(uart_dev_t *hw, uint32_t wakeup_thrd)
613 {
614 // System would wakeup when the number of positive edges of RxD signal is larger than or equal to (UART_ACTIVE_THRESHOLD+3)
615 hw->sleep_conf.active_threshold = wakeup_thrd - UART_LL_MIN_WAKEUP_THRESH;
616 }
617
618 /**
619 * @brief Configure the UART work in normal mode.
620 *
621 * @param hw Beginning address of the peripheral registers.
622 *
623 * @return None.
624 */
uart_ll_set_mode_normal(uart_dev_t * hw)625 FORCE_INLINE_ATTR void uart_ll_set_mode_normal(uart_dev_t *hw)
626 {
627 hw->rs485_conf.rs485_en = 0;
628 hw->rs485_conf.rs485tx_rx_en= 0;
629 hw->rs485_conf.rs485rxby_tx_en = 0;
630 hw->conf0.irda_en = 0;
631 }
632
633 /**
634 * @brief Configure the UART work in rs485_app_ctrl mode.
635 *
636 * @param hw Beginning address of the peripheral registers.
637 *
638 * @return None.
639 */
uart_ll_set_mode_rs485_app_ctrl(uart_dev_t * hw)640 FORCE_INLINE_ATTR void uart_ll_set_mode_rs485_app_ctrl(uart_dev_t *hw)
641 {
642 // Application software control, remove echo
643 hw->rs485_conf.rs485rxby_tx_en = 1;
644 hw->conf0.irda_en = 0;
645 hw->conf0.sw_rts = 0;
646 hw->conf0.irda_en = 0;
647 hw->rs485_conf.rs485_en = 1;
648 }
649
650 /**
651 * @brief Configure the UART work in rs485_half_duplex mode.
652 *
653 * @param hw Beginning address of the peripheral registers.
654 *
655 * @return None.
656 */
uart_ll_set_mode_rs485_half_duplex(uart_dev_t * hw)657 FORCE_INLINE_ATTR void uart_ll_set_mode_rs485_half_duplex(uart_dev_t *hw)
658 {
659 // Enable receiver, sw_rts = 1 generates low level on RTS pin
660 hw->conf0.sw_rts = 1;
661 // Must be set to 0 to automatically remove echo
662 hw->rs485_conf.rs485tx_rx_en = 0;
663 // This is to void collision
664 hw->rs485_conf.rs485rxby_tx_en = 1;
665 hw->conf0.irda_en = 0;
666 hw->rs485_conf.rs485_en= 1;
667 }
668
669 /**
670 * @brief Get the rs485_half_duplex mode.
671 *
672 * @param hw Beginning address of the peripheral registers.
673 *
674 * @return True if RS485 half duplex mode enabled.
675 */
uart_ll_is_mode_rs485_half_duplex(uart_dev_t * hw)676 FORCE_INLINE_ATTR bool uart_ll_is_mode_rs485_half_duplex(uart_dev_t *hw)
677 {
678 return (!hw->rs485_conf.rs485rxby_tx_en && hw->rs485_conf.rs485_en);
679 }
680
681 /**
682 * @brief Configure the UART work in collision_detect mode.
683 *
684 * @param hw Beginning address of the peripheral registers.
685 *
686 * @return None.
687 */
uart_ll_set_mode_collision_detect(uart_dev_t * hw)688 FORCE_INLINE_ATTR void uart_ll_set_mode_collision_detect(uart_dev_t *hw)
689 {
690 hw->conf0.irda_en = 0;
691 // Transmitters output signal loop back to the receivers input signal
692 hw->rs485_conf.rs485tx_rx_en = 1 ;
693 // Transmitter should send data when the receiver is busy
694 hw->rs485_conf.rs485rxby_tx_en = 1;
695 hw->conf0.sw_rts = 0;
696 hw->rs485_conf.rs485_en = 1;
697 }
698
699 /**
700 * @brief Configure the UART work in irda mode.
701 *
702 * @param hw Beginning address of the peripheral registers.
703 *
704 * @return None.
705 */
uart_ll_set_mode_irda(uart_dev_t * hw)706 FORCE_INLINE_ATTR void uart_ll_set_mode_irda(uart_dev_t *hw)
707 {
708 hw->rs485_conf.rs485_en = 0;
709 hw->rs485_conf.rs485tx_rx_en = 0;
710 hw->rs485_conf.rs485rxby_tx_en = 0;
711 hw->conf0.sw_rts = 0;
712 hw->conf0.irda_en = 1;
713 }
714
715 /**
716 * @brief Set uart mode.
717 *
718 * @param hw Beginning address of the peripheral registers.
719 * @param mode The UART mode to be set.
720 *
721 * @return None.
722 */
uart_ll_set_mode(uart_dev_t * hw,uart_mode_t mode)723 FORCE_INLINE_ATTR void uart_ll_set_mode(uart_dev_t *hw, uart_mode_t mode)
724 {
725 switch (mode) {
726 default:
727 case UART_MODE_UART:
728 uart_ll_set_mode_normal(hw);
729 break;
730 case UART_MODE_RS485_COLLISION_DETECT:
731 uart_ll_set_mode_collision_detect(hw);
732 break;
733 case UART_MODE_RS485_APP_CTRL:
734 uart_ll_set_mode_rs485_app_ctrl(hw);
735 break;
736 case UART_MODE_RS485_HALF_DUPLEX:
737 uart_ll_set_mode_rs485_half_duplex(hw);
738 break;
739 case UART_MODE_IRDA:
740 uart_ll_set_mode_irda(hw);
741 break;
742 }
743 }
744
745 /**
746 * @brief Get the UART AT cmd char configuration.
747 *
748 * @param hw Beginning address of the peripheral registers.
749 * @param cmd_char The Pointer to accept value of UART AT cmd char.
750 * @param char_num Pointer to accept the repeat number of UART AT cmd char.
751 *
752 * @return None.
753 */
uart_ll_get_at_cmd_char(uart_dev_t * hw,uint8_t * cmd_char,uint8_t * char_num)754 FORCE_INLINE_ATTR void uart_ll_get_at_cmd_char(uart_dev_t *hw, uint8_t *cmd_char, uint8_t *char_num)
755 {
756 *cmd_char = HAL_FORCE_READ_U32_REG_FIELD(hw->at_cmd_char, at_cmd_char);
757 *char_num = HAL_FORCE_READ_U32_REG_FIELD(hw->at_cmd_char, char_num);
758 }
759
760 /**
761 * @brief Get the UART wakeup threshold value.
762 *
763 * @param hw Beginning address of the peripheral registers.
764 *
765 * @return The UART wakeup threshold value.
766 */
uart_ll_get_wakeup_thrd(uart_dev_t * hw)767 FORCE_INLINE_ATTR uint32_t uart_ll_get_wakeup_thrd(uart_dev_t *hw)
768 {
769 return hw->sleep_conf.active_threshold + UART_LL_MIN_WAKEUP_THRESH;
770 }
771
772 /**
773 * @brief Get the UART data bit configuration.
774 *
775 * @param hw Beginning address of the peripheral registers.
776 * @param data_bit The pointer to accept the UART data bit configuration.
777 *
778 * @return The bit mode.
779 */
uart_ll_get_data_bit_num(uart_dev_t * hw,uart_word_length_t * data_bit)780 FORCE_INLINE_ATTR void uart_ll_get_data_bit_num(uart_dev_t *hw, uart_word_length_t *data_bit)
781 {
782 *data_bit = (uart_word_length_t)hw->conf0.bit_num;
783 }
784
785 /**
786 * @brief Check if the UART sending state machine is in the IDLE state.
787 *
788 * @param hw Beginning address of the peripheral registers.
789 *
790 * @return True if the state machine is in the IDLE state, otherwise false is returned.
791 */
uart_ll_is_tx_idle(uart_dev_t * hw)792 FORCE_INLINE_ATTR bool uart_ll_is_tx_idle(uart_dev_t *hw)
793 {
794 return ((hw->status.txfifo_cnt == 0) && (hw->fsm_status.st_utx_out == 0));
795 }
796
797 /**
798 * @brief Check if the UART rts flow control is enabled.
799 *
800 * @param hw Beginning address of the peripheral registers.
801 *
802 * @return True if hw rts flow control is enabled, otherwise false is returned.
803 */
uart_ll_is_hw_rts_en(uart_dev_t * hw)804 FORCE_INLINE_ATTR bool uart_ll_is_hw_rts_en(uart_dev_t *hw)
805 {
806 return hw->conf1.rx_flow_en;
807 }
808
809 /**
810 * @brief Check if the UART cts flow control is enabled.
811 *
812 * @param hw Beginning address of the peripheral registers.
813 *
814 * @return True if hw cts flow control is enabled, otherwise false is returned.
815 */
uart_ll_is_hw_cts_en(uart_dev_t * hw)816 FORCE_INLINE_ATTR bool uart_ll_is_hw_cts_en(uart_dev_t *hw)
817 {
818 return hw->conf0.tx_flow_en;
819 }
820
821 /**
822 * @brief Configure TX signal loop back to RX module, just for the testing purposes
823 *
824 * @param hw Beginning address of the peripheral registers.
825 * @param loop_back_en Set ture to enable the loop back function, else set it false.
826 *
827 * @return None
828 */
uart_ll_set_loop_back(uart_dev_t * hw,bool loop_back_en)829 FORCE_INLINE_ATTR void uart_ll_set_loop_back(uart_dev_t *hw, bool loop_back_en)
830 {
831 hw->conf0.loopback = loop_back_en;
832 }
833
834 /**
835 * @brief Inverse the UART signal with the given mask.
836 *
837 * @param hw Beginning address of the peripheral registers.
838 * @param inv_mask The UART signal bitmap needs to be inversed.
839 * Use the ORred mask of `uart_signal_inv_t`;
840 *
841 * @return None.
842 */
uart_ll_inverse_signal(uart_dev_t * hw,uint32_t inv_mask)843 FORCE_INLINE_ATTR void uart_ll_inverse_signal(uart_dev_t *hw, uint32_t inv_mask)
844 {
845 uart_conf0_reg_t conf0_reg;
846 conf0_reg.val = hw->conf0.val;
847 conf0_reg.irda_tx_inv = (inv_mask & UART_SIGNAL_IRDA_TX_INV) ? 1 : 0;
848 conf0_reg.irda_rx_inv = (inv_mask & UART_SIGNAL_IRDA_RX_INV) ? 1 : 0;
849 conf0_reg.rxd_inv = (inv_mask & UART_SIGNAL_RXD_INV) ? 1 : 0;
850 conf0_reg.cts_inv = (inv_mask & UART_SIGNAL_CTS_INV) ? 1 : 0;
851 conf0_reg.dsr_inv = (inv_mask & UART_SIGNAL_DSR_INV) ? 1 : 0;
852 conf0_reg.txd_inv = (inv_mask & UART_SIGNAL_TXD_INV) ? 1 : 0;
853 conf0_reg.rts_inv = (inv_mask & UART_SIGNAL_RTS_INV) ? 1 : 0;
854 conf0_reg.dtr_inv = (inv_mask & UART_SIGNAL_DTR_INV) ? 1 : 0;
855 hw->conf0.val = conf0_reg.val;
856 }
857
858 /**
859 * @brief Configure the timeout value for receiver receiving a byte, and enable rx timeout function.
860 *
861 * @param hw Beginning address of the peripheral registers.
862 * @param tout_thrd The timeout value as UART bit time. The rx timeout function will be disabled if `tout_thrd == 0`.
863 *
864 * @return None.
865 */
uart_ll_set_rx_tout(uart_dev_t * hw,uint16_t tout_thrd)866 FORCE_INLINE_ATTR void uart_ll_set_rx_tout(uart_dev_t *hw, uint16_t tout_thrd)
867 {
868 uint16_t tout_val = tout_thrd;
869 if(tout_thrd > 0) {
870 hw->mem_conf.rx_tout_thrhd = tout_val;
871 hw->conf1.rx_tout_en = 1;
872 } else {
873 hw->conf1.rx_tout_en = 0;
874 }
875 }
876
877 /**
878 * @brief Get the timeout value for receiver receiving a byte.
879 *
880 * @param hw Beginning address of the peripheral registers.
881 *
882 * @return tout_thr The timeout threshold value. If timeout feature is disabled returns 0.
883 */
uart_ll_get_rx_tout_thr(uart_dev_t * hw)884 FORCE_INLINE_ATTR uint16_t uart_ll_get_rx_tout_thr(uart_dev_t *hw)
885 {
886 uint16_t tout_thrd = 0;
887 if(hw->conf1.rx_tout_en > 0) {
888 tout_thrd = hw->mem_conf.rx_tout_thrhd;
889 }
890 return tout_thrd;
891 }
892
893 /**
894 * @brief Get UART maximum timeout threshold.
895 *
896 * @param hw Beginning address of the peripheral registers.
897 *
898 * @return maximum timeout threshold.
899 */
uart_ll_max_tout_thrd(uart_dev_t * hw)900 FORCE_INLINE_ATTR uint16_t uart_ll_max_tout_thrd(uart_dev_t *hw)
901 {
902 return UART_RX_TOUT_THRHD_V;
903 }
904
905 /**
906 * @brief Configure the auto baudrate.
907 *
908 * @param hw Beginning address of the peripheral registers.
909 * @param enable Boolean marking whether the auto baudrate should be enabled or not.
910 */
uart_ll_set_autobaud_en(uart_dev_t * hw,bool enable)911 FORCE_INLINE_ATTR void uart_ll_set_autobaud_en(uart_dev_t *hw, bool enable)
912 {
913 hw->conf0.autobaud_en = enable ? 1 : 0;
914 }
915
916 /**
917 * @brief Get the RXD edge count.
918 *
919 * @param hw Beginning address of the peripheral registers.
920 */
uart_ll_get_rxd_edge_cnt(uart_dev_t * hw)921 FORCE_INLINE_ATTR uint32_t uart_ll_get_rxd_edge_cnt(uart_dev_t *hw)
922 {
923 return hw->rxd_cnt.rxd_edge_cnt;
924 }
925
926 /**
927 * @brief Get the positive pulse minimum count.
928 *
929 * @param hw Beginning address of the peripheral registers.
930 */
uart_ll_get_pos_pulse_cnt(uart_dev_t * hw)931 FORCE_INLINE_ATTR uint32_t uart_ll_get_pos_pulse_cnt(uart_dev_t *hw)
932 {
933 return hw->pospulse.posedge_min_cnt;
934 }
935
936 /**
937 * @brief Get the negative pulse minimum count.
938 *
939 * @param hw Beginning address of the peripheral registers.
940 */
uart_ll_get_neg_pulse_cnt(uart_dev_t * hw)941 FORCE_INLINE_ATTR uint32_t uart_ll_get_neg_pulse_cnt(uart_dev_t *hw)
942 {
943 return hw->negpulse.negedge_min_cnt;
944 }
945
946 /**
947 * @brief Get the high pulse minimum count.
948 *
949 * @param hw Beginning address of the peripheral registers.
950 */
uart_ll_get_high_pulse_cnt(uart_dev_t * hw)951 FORCE_INLINE_ATTR uint32_t uart_ll_get_high_pulse_cnt(uart_dev_t *hw)
952 {
953 return hw->highpulse.highpulse_min_cnt;
954 }
955
956 /**
957 * @brief Get the low pulse minimum count.
958 *
959 * @param hw Beginning address of the peripheral registers.
960 */
uart_ll_get_low_pulse_cnt(uart_dev_t * hw)961 FORCE_INLINE_ATTR uint32_t uart_ll_get_low_pulse_cnt(uart_dev_t *hw)
962 {
963 return hw->lowpulse.lowpulse_min_cnt;
964 }
965
966 /**
967 * @brief Force UART xoff.
968 *
969 * @param uart_num UART port number, the max port number is (UART_NUM_MAX -1).
970 *
971 * @return None.
972 */
uart_ll_force_xoff(uart_port_t uart_num)973 static inline void uart_ll_force_xoff(uart_port_t uart_num)
974 {
975 REG_CLR_BIT(UART_FLOW_CONF_REG(uart_num), UART_FORCE_XON);
976 REG_SET_BIT(UART_FLOW_CONF_REG(uart_num), UART_SW_FLOW_CON_EN | UART_FORCE_XOFF);
977 }
978
979 /**
980 * @brief Force UART xon.
981 *
982 * @param uart_num UART port number, the max port number is (UART_NUM_MAX -1).
983 *
984 * @return None.
985 */
uart_ll_force_xon(uart_port_t uart_num)986 FORCE_INLINE_ATTR void uart_ll_force_xon(uart_port_t uart_num)
987 {
988 REG_CLR_BIT(UART_FLOW_CONF_REG(uart_num), UART_FORCE_XOFF);
989 REG_SET_BIT(UART_FLOW_CONF_REG(uart_num), UART_FORCE_XON);
990 REG_CLR_BIT(UART_FLOW_CONF_REG(uart_num), UART_SW_FLOW_CON_EN | UART_FORCE_XON);
991 }
992
993 /**
994 * @brief Get UART finite-state machine status.
995 *
996 * @param uart_num UART port number, the max port number is (UART_NUM_MAX -1).
997 *
998 * @return UART module FSM status.
999 */
uart_ll_get_fsm_status(uart_port_t uart_num)1000 FORCE_INLINE_ATTR uint32_t uart_ll_get_fsm_status(uart_port_t uart_num)
1001 {
1002 return REG_GET_FIELD(UART_FSM_STATUS_REG(uart_num), UART_ST_UTX_OUT);
1003 }
1004
1005 /**
1006 * @brief Configure UART whether to discard when receiving wrong data
1007 *
1008 * @param hw Beginning address of the peripheral registers.
1009 * @param discard true: Receiver stops storing data into FIFO when data is wrong
1010 * false: Receiver continue storing data into FIFO when data is wrong
1011 */
uart_ll_discard_error_data(uart_dev_t * hw,bool discard)1012 FORCE_INLINE_ATTR void uart_ll_discard_error_data(uart_dev_t *hw, bool discard)
1013 {
1014 hw->conf0.err_wr_mask = discard ? 1 : 0;
1015 }
1016
1017 #ifdef __cplusplus
1018 }
1019 #endif
1020