Skip to content

Commit 14ab3c7

Browse files
committed
ESP32 USE uart_param_config
Use this function instead of several individual configuration functions to configure such things as Baud rate, transfer size, stop bits, parity... This function also resets both the RX and TX Hardware Fifo reset functions are called to setup the hardware.
1 parent ebe442c commit 14ab3c7

File tree

1 file changed

+31
-21
lines changed
  • ports/espressif/common-hal/busio

1 file changed

+31
-21
lines changed

ports/espressif/common-hal/busio/UART.c

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
108108
bool have_rx = rx != NULL;
109109
bool have_rts = rts != NULL;
110110
bool have_cts = cts != NULL;
111+
112+
uart_config_t uart_config = {0};
111113
bool have_rs485_dir = rs485_dir != NULL;
112114
if (!have_tx && !have_rx) {
113115
mp_raise_ValueError(translate("tx and rx cannot both be None"));
@@ -135,25 +137,26 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
135137
}
136138

137139
uart_mode_t mode = UART_MODE_UART;
138-
uart_hw_flowcontrol_t flow_control = UART_HW_FLOWCTRL_DISABLE;
140+
uart_config.flow_ctrl = UART_HW_FLOWCTRL_DISABLE;
139141
if (have_rs485_dir) {
140142
mode = UART_MODE_RS485_HALF_DUPLEX;
141143
if (!rs485_invert) {
144+
// This one is not in the set
142145
uart_set_line_inverse(self->uart_num, UART_SIGNAL_DTR_INV);
143146
}
144147
} else if (have_rts && have_cts) {
145-
flow_control = UART_HW_FLOWCTRL_CTS_RTS;
148+
uart_config.flow_ctrl = UART_HW_FLOWCTRL_CTS_RTS;
146149
} else if (have_rts) {
147-
flow_control = UART_HW_FLOWCTRL_RTS;
150+
uart_config.flow_ctrl = UART_HW_FLOWCTRL_RTS;
148151
} else if (have_rts) {
149-
flow_control = UART_HW_FLOWCTRL_CTS;
152+
uart_config.flow_ctrl = UART_HW_FLOWCTRL_CTS;
150153
}
151154

152155
if (receiver_buffer_size <= UART_FIFO_LEN) {
153156
receiver_buffer_size = UART_FIFO_LEN + 8;
154157
}
155158

156-
uint8_t rx_threshold = UART_FIFO_LEN - 8;
159+
uart_config.rx_flow_ctrl_thresh = UART_FIFO_LEN - 8;
157160
// Install the driver before we change the settings.
158161
if (uart_driver_install(self->uart_num, receiver_buffer_size, 0, 20, &self->event_queue, 0) != ESP_OK ||
159162
uart_set_mode(self->uart_num, mode) != ESP_OK) {
@@ -175,55 +178,62 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
175178
CONFIG_PTHREAD_TASK_PRIO_DEFAULT,
176179
&self->event_task,
177180
xPortGetCoreID());
178-
uart_set_hw_flow_ctrl(self->uart_num, flow_control, rx_threshold);
181+
// uart_set_hw_flow_ctrl(self->uart_num, uart_config.flow_control, uart_config.rx_flow_ctrl_thresh);
179182

180183
// Set baud rate
181-
common_hal_busio_uart_set_baudrate(self, baudrate);
184+
// common_hal_busio_uart_set_baudrate(self, baudrate);
185+
uart_config.baud_rate = baudrate;
182186

183-
uart_word_length_t word_length = UART_DATA_8_BITS;
187+
uart_config.data_bits = UART_DATA_8_BITS;
184188
switch (bits) {
185189
// Shared bindings prevents data < 7 bits.
186190
// case 5:
187-
// word_length = UART_DATA_5_BITS;
191+
// uart_config.data_bits = UART_DATA_5_BITS;
188192
// break;
189193
// case 6:
190-
// word_length = UART_DATA_6_BITS;
194+
// uart_config.data_bits = UART_DATA_6_BITS;
191195
// break;
192196
case 7:
193-
word_length = UART_DATA_7_BITS;
197+
uart_config.data_bits = UART_DATA_7_BITS;
194198
break;
195199
case 8:
196-
word_length = UART_DATA_8_BITS;
200+
uart_config.data_bits = UART_DATA_8_BITS;
197201
break;
198202
default:
199203
// Won't hit this because shared-bindings limits to 7-9 bits. We error on 9 above.
200204
break;
201205
}
202-
uart_set_word_length(self->uart_num, word_length);
206+
// uart_set_word_length(self->uart_num, uart_config.data_bits);
203207

204-
uart_parity_t parity_mode = UART_PARITY_DISABLE;
208+
uart_config.parity = UART_PARITY_DISABLE;
205209
switch (parity) {
206210
case BUSIO_UART_PARITY_NONE:
207-
parity_mode = UART_PARITY_DISABLE;
211+
uart_config.parity = UART_PARITY_DISABLE;
208212
break;
209213
case BUSIO_UART_PARITY_EVEN:
210-
parity_mode = UART_PARITY_EVEN;
214+
uart_config.parity = UART_PARITY_EVEN;
211215
break;
212216
case BUSIO_UART_PARITY_ODD:
213-
parity_mode = UART_PARITY_ODD;
217+
uart_config.parity = UART_PARITY_ODD;
214218
break;
215219
default:
216220
// Won't reach here because the input is an enum that is completely handled.
217221
break;
218222
}
219-
uart_set_parity(self->uart_num, parity_mode);
223+
// uart_set_parity(self->uart_num, uart_config.parity);
220224

221225
// Stop is 1 or 2 always.
222-
uart_stop_bits_t stop_bits = UART_STOP_BITS_1;
226+
uart_config.stop_bits = UART_STOP_BITS_1;
223227
if (stop == 2) {
224-
stop_bits = UART_STOP_BITS_2;
228+
uart_config.stop_bits = UART_STOP_BITS_2;
229+
}
230+
// uart_set_stop_bits(self->uart_num, stop_bits);
231+
uart_config.source_clk = UART_SCLK_APB; // guessing here...
232+
233+
// config all in one?
234+
if (uart_param_config(self->uart_num, &uart_config) != ESP_OK) {
235+
mp_raise_RuntimeError(translate("UART init"));
225236
}
226-
uart_set_stop_bits(self->uart_num, stop_bits);
227237

228238
self->tx_pin = NULL;
229239
self->rx_pin = NULL;

0 commit comments

Comments
 (0)