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