1 /*
2 * SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 // The HAL layer for UART (common part)
8 #include "hal/uart_hal.h"
9 #include "soc/soc_caps.h"
10
uart_hal_get_sclk(uart_hal_context_t * hal,uart_sclk_t * sclk)11 void uart_hal_get_sclk(uart_hal_context_t *hal, uart_sclk_t *sclk)
12 {
13 uart_ll_get_sclk(hal->dev, sclk);
14 }
15
uart_hal_get_baudrate(uart_hal_context_t * hal,uint32_t * baud_rate,uint32_t sclk_freq)16 void uart_hal_get_baudrate(uart_hal_context_t *hal, uint32_t *baud_rate, uint32_t sclk_freq)
17 {
18 *baud_rate = uart_ll_get_baudrate(hal->dev, sclk_freq);
19 }
20
uart_hal_set_stop_bits(uart_hal_context_t * hal,uart_stop_bits_t stop_bit)21 void uart_hal_set_stop_bits(uart_hal_context_t *hal, uart_stop_bits_t stop_bit)
22 {
23 uart_ll_set_stop_bits(hal->dev, stop_bit);
24 }
25
uart_hal_get_stop_bits(uart_hal_context_t * hal,uart_stop_bits_t * stop_bit)26 void uart_hal_get_stop_bits(uart_hal_context_t *hal, uart_stop_bits_t *stop_bit)
27 {
28 uart_ll_get_stop_bits(hal->dev, stop_bit);
29 }
30
uart_hal_set_data_bit_num(uart_hal_context_t * hal,uart_word_length_t data_bit)31 void uart_hal_set_data_bit_num(uart_hal_context_t *hal, uart_word_length_t data_bit)
32 {
33 uart_ll_set_data_bit_num(hal->dev, data_bit);
34 }
35
uart_hal_get_data_bit_num(uart_hal_context_t * hal,uart_word_length_t * data_bit)36 void uart_hal_get_data_bit_num(uart_hal_context_t *hal, uart_word_length_t *data_bit)
37 {
38 uart_ll_get_data_bit_num(hal->dev, data_bit);
39 }
40
uart_hal_set_parity(uart_hal_context_t * hal,uart_parity_t parity_mode)41 void uart_hal_set_parity(uart_hal_context_t *hal, uart_parity_t parity_mode)
42 {
43 uart_ll_set_parity(hal->dev, parity_mode);
44 }
45
uart_hal_get_parity(uart_hal_context_t * hal,uart_parity_t * parity_mode)46 void uart_hal_get_parity(uart_hal_context_t *hal, uart_parity_t *parity_mode)
47 {
48 uart_ll_get_parity(hal->dev, parity_mode);
49 }
50
uart_hal_set_hw_flow_ctrl(uart_hal_context_t * hal,uart_hw_flowcontrol_t flow_ctrl,uint8_t rx_thresh)51 void uart_hal_set_hw_flow_ctrl(uart_hal_context_t *hal, uart_hw_flowcontrol_t flow_ctrl, uint8_t rx_thresh)
52 {
53 uart_ll_set_hw_flow_ctrl(hal->dev, flow_ctrl, rx_thresh);
54 }
55
uart_hal_get_hw_flow_ctrl(uart_hal_context_t * hal,uart_hw_flowcontrol_t * flow_ctrl)56 void uart_hal_get_hw_flow_ctrl(uart_hal_context_t *hal, uart_hw_flowcontrol_t *flow_ctrl)
57 {
58 uart_ll_get_hw_flow_ctrl(hal->dev, flow_ctrl);
59 }
60
uart_hal_set_sw_flow_ctrl(uart_hal_context_t * hal,uart_sw_flowctrl_t * flow_ctrl,bool sw_flow_ctrl_en)61 void uart_hal_set_sw_flow_ctrl(uart_hal_context_t *hal, uart_sw_flowctrl_t *flow_ctrl, bool sw_flow_ctrl_en)
62 {
63 uart_ll_set_sw_flow_ctrl(hal->dev, flow_ctrl, sw_flow_ctrl_en);
64 }
65
uart_hal_set_at_cmd_char(uart_hal_context_t * hal,uart_at_cmd_t * at_cmd)66 void uart_hal_set_at_cmd_char(uart_hal_context_t *hal, uart_at_cmd_t *at_cmd)
67 {
68 uart_ll_set_at_cmd_char(hal->dev, at_cmd);
69 }
70
uart_hal_set_tx_idle_num(uart_hal_context_t * hal,uint16_t idle_num)71 void uart_hal_set_tx_idle_num(uart_hal_context_t *hal, uint16_t idle_num)
72 {
73 uart_ll_set_tx_idle_num(hal->dev, idle_num);
74 }
75
uart_hal_set_dtr(uart_hal_context_t * hal,int active_level)76 void uart_hal_set_dtr(uart_hal_context_t *hal, int active_level)
77 {
78 uart_ll_set_dtr_active_level(hal->dev, active_level);
79 }
80
uart_hal_set_rxfifo_full_thr(uart_hal_context_t * hal,uint32_t full_thrhd)81 void uart_hal_set_rxfifo_full_thr(uart_hal_context_t *hal, uint32_t full_thrhd)
82 {
83 uart_ll_set_rxfifo_full_thr(hal->dev, full_thrhd);
84 }
85
uart_hal_set_txfifo_empty_thr(uart_hal_context_t * hal,uint32_t empty_thrhd)86 void uart_hal_set_txfifo_empty_thr(uart_hal_context_t *hal, uint32_t empty_thrhd)
87 {
88 uart_ll_set_txfifo_empty_thr(hal->dev, empty_thrhd);
89 }
90
uart_hal_set_wakeup_thrd(uart_hal_context_t * hal,uint32_t wakeup_thrd)91 void uart_hal_set_wakeup_thrd(uart_hal_context_t *hal, uint32_t wakeup_thrd)
92 {
93 uart_ll_set_wakeup_thrd(hal->dev, wakeup_thrd);
94 }
95
uart_hal_get_wakeup_thrd(uart_hal_context_t * hal,uint32_t * wakeup_thrd)96 void uart_hal_get_wakeup_thrd(uart_hal_context_t *hal, uint32_t *wakeup_thrd)
97 {
98 *wakeup_thrd = uart_ll_get_wakeup_thrd(hal->dev);
99 }
100
uart_hal_set_mode(uart_hal_context_t * hal,uart_mode_t mode)101 void uart_hal_set_mode(uart_hal_context_t *hal, uart_mode_t mode)
102 {
103 uart_ll_set_mode(hal->dev, mode);
104 }
105
uart_hal_is_mode_rs485_half_duplex(uart_hal_context_t * hal)106 bool uart_hal_is_mode_rs485_half_duplex(uart_hal_context_t *hal)
107 {
108 return uart_ll_is_mode_rs485_half_duplex(hal->dev);
109 }
110
uart_hal_is_hw_rts_en(uart_hal_context_t * hal)111 bool uart_hal_is_hw_rts_en(uart_hal_context_t *hal)
112 {
113 return uart_ll_is_hw_rts_en(hal->dev);
114 }
115
uart_hal_inverse_signal(uart_hal_context_t * hal,uint32_t inv_mask)116 void uart_hal_inverse_signal(uart_hal_context_t *hal, uint32_t inv_mask)
117 {
118 uart_ll_inverse_signal(hal->dev, inv_mask);
119 }
120
uart_hal_set_loop_back(uart_hal_context_t * hal,bool loop_back_en)121 void uart_hal_set_loop_back(uart_hal_context_t *hal, bool loop_back_en)
122 {
123 uart_ll_set_loop_back(hal->dev, loop_back_en);
124 }
125
uart_hal_init(uart_hal_context_t * hal,uart_port_t uart_num)126 void uart_hal_init(uart_hal_context_t *hal, uart_port_t uart_num)
127 {
128 // Set UART mode.
129 uart_ll_set_mode(hal->dev, UART_MODE_UART);
130 // Disable UART parity
131 uart_ll_set_parity(hal->dev, UART_PARITY_DISABLE);
132 // 8-bit world
133 uart_ll_set_data_bit_num(hal->dev, UART_DATA_8_BITS);
134 // 1-bit stop bit
135 uart_ll_set_stop_bits(hal->dev, UART_STOP_BITS_1);
136 // Set tx idle
137 uart_ll_set_tx_idle_num(hal->dev, 0);
138 // Disable hw-flow control
139 uart_ll_set_hw_flow_ctrl(hal->dev, UART_HW_FLOWCTRL_DISABLE, 100);
140 }
141
uart_hal_get_symb_len(uart_hal_context_t * hal)142 uint8_t uart_hal_get_symb_len(uart_hal_context_t *hal)
143 {
144 uint8_t symbol_len = 1; // number of bits per symbol including start
145 uart_parity_t parity_mode;
146 uart_stop_bits_t stop_bit;
147 uart_word_length_t data_bit;
148 uart_ll_get_data_bit_num(hal->dev, &data_bit);
149 uart_ll_get_stop_bits(hal->dev, &stop_bit);
150 uart_ll_get_parity(hal->dev, &parity_mode);
151 symbol_len += (data_bit < UART_DATA_BITS_MAX) ? (uint8_t)data_bit + 5 : 8;
152 symbol_len += (stop_bit > UART_STOP_BITS_1) ? 2 : 1;
153 symbol_len += (parity_mode > UART_PARITY_DISABLE) ? 1 : 0;
154 return symbol_len;
155 }
156
uart_hal_set_rx_timeout(uart_hal_context_t * hal,const uint8_t tout)157 void uart_hal_set_rx_timeout(uart_hal_context_t *hal, const uint8_t tout)
158 {
159 uint8_t symb_len = uart_hal_get_symb_len(hal);
160 uart_ll_set_rx_tout(hal->dev, symb_len * tout);
161 }
162
uart_hal_get_max_rx_timeout_thrd(uart_hal_context_t * hal)163 uint16_t uart_hal_get_max_rx_timeout_thrd(uart_hal_context_t *hal)
164 {
165 uint8_t symb_len = uart_hal_get_symb_len(hal);
166 uint16_t max_tout_thresh = uart_ll_max_tout_thrd(hal->dev);
167 return (max_tout_thresh / symb_len);
168 }
169
uart_hal_get_port_num(uart_hal_context_t * hal)170 int8_t uart_hal_get_port_num(uart_hal_context_t *hal)
171 {
172 if (hal->dev == (uart_dev_t *)&UART0) {
173 return 0;
174 } else if (hal->dev == (uart_dev_t *)&UART1) {
175 return 1;
176 #if CONFIG_SOC_SERIES_ESP32 || CONFIG_SOC_SERIES_ESP32S3
177 } else if (hal->dev == (uart_dev_t *)&UART2) {
178 return 2;
179 #endif
180 }
181 return -1;
182 }
183