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