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

Skip to content

Commit 3be5b7c

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

File tree

4 files changed

+51
-19
lines changed

4 files changed

+51
-19
lines changed

ports/esp32/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ void mp_task(void *pvParameter) {
9393
#elif CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG
9494
usb_serial_jtag_init();
9595
#else
96-
uart_init();
96+
uart_stdout_init();
9797
#endif
9898
machine_init();
9999

ports/esp32/mphalport.c

Lines changed: 2 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,7 @@
5444
#include "mphalport.h"
5545
#include "usb.h"
5646
#include "usb_serial_jtag.h"
47+
#include "uart.h"
5748

5849
TaskHandle_t mp_main_task_handle;
5950

@@ -122,9 +113,7 @@ void mp_hal_stdout_tx_strn(const char *str, size_t len) {
122113
#elif CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG
123114
usb_serial_jtag_tx_strn(str, len);
124115
#else
125-
for (uint32_t i = 0; i < len; ++i) {
126-
uart_tx_one_char(str[i]);
127-
}
116+
uart_stdout_tx_strn(str, len);
128117
#endif
129118
if (release_gil) {
130119
MP_THREAD_GIL_ENTER();

ports/esp32/uart.c

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,61 @@
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
38+
#define MICROPY_HW_UART_REPL UART_NUM_0
39+
#endif
40+
41+
#ifndef MICROPY_HW_UART_REPL_BAUD
42+
#define MICROPY_HW_UART_REPL_BAUD 115200
43+
#endif
44+
3745
STATIC void uart_irq_handler(void *arg);
3846

39-
void uart_init(void) {
47+
void uart_stdout_init(void) {
48+
uart_config_t uartcfg = {
49+
.baud_rate = MICROPY_HW_UART_REPL_BAUD,
50+
.data_bits = UART_DATA_8_BITS,
51+
.parity = UART_PARITY_DISABLE,
52+
.stop_bits = UART_STOP_BITS_1,
53+
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
54+
.rx_flow_ctrl_thresh = 0
55+
};
56+
uart_param_config(MICROPY_HW_UART_REPL, &uartcfg);
57+
58+
const uint32_t rxbuf = 129; // IDF requires > 128 min
59+
const uint32_t txbuf = 0;
60+
61+
uart_driver_install(MICROPY_HW_UART_REPL, rxbuf, txbuf, 0, NULL, 0);
62+
4063
uart_isr_handle_t handle;
41-
uart_isr_register(UART_NUM_0, uart_irq_handler, NULL, ESP_INTR_FLAG_LOWMED | ESP_INTR_FLAG_IRAM, &handle);
42-
uart_enable_rx_intr(UART_NUM_0);
64+
uart_isr_free(MICROPY_HW_UART_REPL);
65+
uart_isr_register(MICROPY_HW_UART_REPL, uart_irq_handler, NULL, ESP_INTR_FLAG_LOWMED | ESP_INTR_FLAG_IRAM, &handle);
66+
uart_enable_rx_intr(MICROPY_HW_UART_REPL);
67+
}
68+
69+
int uart_stdout_tx_strn(const char *str, size_t len) {
70+
size_t remaining = len;
71+
// TODO add a timeout
72+
while (1) {
73+
int ret = uart_tx_chars(MICROPY_HW_UART_REPL, str, remaining);
74+
if (ret == -1) {
75+
return -1;
76+
}
77+
remaining -= ret;
78+
if (remaining <= 0) {
79+
break;
80+
}
81+
str += ret;
82+
ulTaskNotifyTake(pdFALSE, 1);
83+
}
84+
return len - remaining;
4385
}
4486

4587
// all code executed in ISR must be in IRAM, and any const data must be in DRAM

ports/esp32/uart.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#ifndef MICROPY_INCLUDED_ESP32_UART_H
2929
#define MICROPY_INCLUDED_ESP32_UART_H
3030

31-
void uart_init(void);
31+
void uart_stdout_init(void);
32+
int uart_stdout_tx_strn(const char *str, size_t len);
3233

3334
#endif // MICROPY_INCLUDED_ESP32_UART_H

0 commit comments

Comments
 (0)
0