8000 esp32/uart: Correctly init uart driver for repl. · micropython/micropython@4ec8210 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4ec8210

Browse files
committed
esp32/uart: Correctly init uart driver for repl.
1 parent 5f50f4a commit 4ec8210

File tree

3 files changed

+43
-14
lines changed

3 files changed

+43
-14
lines changed

ports/esp32/mphalport.c

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,6 @@
3333
#include "freertos/FreeRTOS.h"
3434
#include "freertos/task.h"
3535

36-
#if CONFIG_IDF_TARGET_ESP32
37-
#include "esp32/rom/uart.h"
38-
#elif CONFIG_IDF_TARGET_ESP32C3
39-
#include "esp32c3/rom/uart.h"
40-
#elif CONFIG_IDF_TARGET_ESP32S2
41-
#include "esp32s2/rom/uart.h"
42-
#elif CONFIG_IDF_TARGET_ESP32S3
43-
#include "esp32s3/rom/uart.h"
44-
#endif
45-
4636
#include "py/obj.h"
4737
#include "py/objstr.h"
4838
#include "py/stream.h"
@@ -54,6 +44,8 @@
5444
#include "mphalport.h"
5545
#include "usb.h"
5646
#include "usb_serial_jtag.h"
47+
#include "uart.h"
48+
#include "driver/uart.h"
5749

5850
TaskHandle_t mp_main_task_handle;
5951

@@ -122,9 +114,7 @@ void mp_hal_stdout_tx_strn(const char *str, size_t len) {
122114
#elif CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG
123115
usb_serial_jtag_tx_strn(str, len);
124116
#else
125-
for (uint32_t i = 0; i < len; ++i) {
126-
uart_tx_one_char(str[i]);
127-
}
117+
uart_repl_write(str, len);
128118
#endif
129119
if (release_gil) {
130120
MP_THREAD_GIL_ENTER();

ports/esp32/uart.c

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,59 @@
2727
*/
2828

2929
#include <stdio.h>
30-
30+
#include "uart.h"
3131
#include "driver/uart.h"
3232
#include "soc/uart_periph.h"
3333

3434
#include "py/runtime.h"
3535
#include "py/mphal.h"
3636

37+
#ifndef MICROPY_HW_UART_REPL_BAUD
38+
#define MICROPY_HW_UART_REPL_BAUD 115200
39+
#endif
40+
3741
STATIC void uart_irq_handler(void *arg);
3842

3943
void uart_init(void) {
44+
uart_config_t uartcfg = {
45+
.baud_rate = MICROPY_HW_UART_REPL_BAUD,
46+
.data_bits = UART_DATA_8_BITS,
47+
.parity = UART_PARITY_DISABLE,
48+
.stop_bits = UART_STOP_BITS_1,
49+
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
50+
.rx_flow_ctrl_thresh = 0
51+
};
52+
uart_param_config(UART_NUM_0, &uartcfg);
53+
54+
const uint32_t rxbuf = 129; // IDF requires > 128 min
55+
const uint32_t txbuf = 0;
56+
57+
uart_driver_install(UART_NUM_0, rxbuf, txbuf, 0, NULL, 0);
58+
4059
uart_isr_handle_t handle;
60+
uart_isr_free(UART_NUM_0);
4161
uart_isr_register(UART_NUM_0, uart_irq_handler, NULL, ESP_INTR_FLAG_LOWMED | ESP_INTR_FLAG_IRAM, &handle);
4262
uart_enable_rx_intr(UART_NUM_0);
4363
}
4464

65+
int uart_repl_write(const char *str, size_t len) {
66+
size_t remaining = len;
67+
// TODO add a timeout
68+
while (1) {
69+
int ret = uart_tx_chars(UART_NUM_0, str, remaining);
70+
if (ret == -1) {
71+
return -1;
72+
}
73+
remaining -= ret;
74+
if (remaining <= 0) {
75+
break;
76+
}
77+
str += ret;
78+
ulTaskNotifyTake(pdFALSE, 1);
79+
}
80+
return len - remaining;
81+
}
82+
4583
// all code executed in ISR must be in IRAM, and any const data must be in DRAM
4684
STATIC void IRAM_ATTR uart_irq_handler(void *arg) {
4785
volatile uart_dev_t *uart = &UART0;

ports/esp32/uart.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,6 @@
2929
#define MICROPY_INCLUDED_ESP32_UART_H
3030

3131
void uart_init(void);
32+
int uart_repl_write(const char *str, size_t len);
3233

3334
#endif // MICROPY_INCLUDED_ESP32_UART_H

0 commit comments

Comments
 (0)
0