Skip to content

Commit

Permalink
esp32: Support JTAG console, free up UART.
Browse files Browse the repository at this point in the history
CONFIG_USB_OTG_SUPPORTED is automatically set by the ESP-IDF when the chip
supports USB-OTG, which is the case for the ESP32-S2 and ESP32-S3.

When trying to use the JTAG console with these chips, it would not work
because our USB implementation will take over control over the USB port,
breaking the JTAG console in the process.

Thus, when the board is configured to use the JTAG console, we should not
enable our USB console support.

Additionally, this change also frees up UART0 when an USB-based console is
configured, since there's no reason to prevent (re)configuration of UART0
for other uses in that case.

Signed-off-by: Daniël van de Giessen <daniel@dvdgiessen.nl>
  • Loading branch information
DvdGiessen authored and dpgeorge committed Sep 1, 2023
1 parent ba8aad3 commit f8bd677
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 13 deletions.
7 changes: 6 additions & 1 deletion ports/esp32/machine_uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,11 @@ STATIC void machine_uart_init_helper(machine_uart_obj_t *self, size_t n_args, co

if (args[ARG_txbuf].u_int >= 0 || args[ARG_rxbuf].u_int >= 0) {
// must reinitialise driver to change the tx/rx buffer size
#if MICROPY_HW_ENABLE_UART_REPL
if (self->uart_num == MICROPY_HW_UART_REPL) {
mp_raise_ValueError(MP_ERROR_TEXT("UART buffer size is fixed"));
}
#endif

if (args[ARG_txbuf].u_int >= 0) {
self->txbuf = args[ARG_txbuf].u_int;
Expand Down Expand Up @@ -353,8 +355,11 @@ STATIC mp_obj_t machine_uart_make_new(const mp_obj_type_t *type, size_t n_args,
#endif
}

#if MICROPY_HW_ENABLE_UART_REPL
// Only reset the driver if it's not the REPL UART.
if (uart_num != MICROPY_HW_UART_REPL) {
if (uart_num != MICROPY_HW_UART_REPL)
#endif
{
// Remove any existing configuration
uart_driver_delete(self->uart_num);

Expand Down
6 changes: 3 additions & 3 deletions ports/esp32/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ void mp_task(void *pvParameter) {
#if MICROPY_PY_THREAD
mp_thread_init(pxTaskGetStackStart(NULL), MP_TASK_STACK_SIZE / sizeof(uintptr_t));
#endif
#if CONFIG_USB_OTG_SUPPORTED
usb_init();
#elif CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG
#if CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG
usb_serial_jtag_init();
#elif CONFIG_USB_OTG_SUPPORTED
usb_init();
#endif
#if MICROPY_HW_ENABLE_UART_REPL
uart_stdout_init();
Expand Down
6 changes: 3 additions & 3 deletions ports/esp32/mphalport.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,10 @@ void mp_hal_stdout_tx_strn(const char *str, size_t len) {
if (release_gil) {
MP_THREAD_GIL_EXIT();
}
#if CONFIG_USB_OTG_SUPPORTED
usb_tx_strn(str, len);
#elif CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG
#if CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG
usb_serial_jtag_tx_strn(str, len);
#elif CONFIG_USB_OTG_SUPPORTED
usb_tx_strn(str, len);
#endif
#if MICROPY_HW_ENABLE_UART_REPL
uart_stdout_tx_strn(str, len);
Expand Down
10 changes: 7 additions & 3 deletions ports/esp32/uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,16 @@
* THE SOFTWARE.
*/

#include <stdio.h>

#include "hal/uart_hal.h"

#include "py/runtime.h"
#include "py/mphal.h"
#include "uart.h"

#if MICROPY_HW_ENABLE_UART_REPL

#include <stdio.h>
#include "hal/uart_hal.h"

// Backwards compatibility for when MICROPY_HW_UART_REPL was a ESP-IDF UART
// driver enum. Only UART_NUM_0 was supported with that version of the driver.
#define UART_NUM_0 0
Expand Down Expand Up @@ -118,3 +120,5 @@ STATIC void IRAM_ATTR uart_irq_handler(void *arg) {
}
}
}

#endif // MICROPY_HW_ENABLE_UART_REPL
6 changes: 5 additions & 1 deletion ports/esp32/uart.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@

// Whether to enable the REPL on a UART.
#ifndef MICROPY_HW_ENABLE_UART_REPL
#define MICROPY_HW_ENABLE_UART_REPL (!CONFIG_USB_OTG_SUPPORTED && !CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG)
#define MICROPY_HW_ENABLE_UART_REPL (!CONFIG_USB_OTG_SUPPORTED && !CONFIG_ESP_CONSOLE_USB_CDC && !CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG)
#endif

#if MICROPY_HW_ENABLE_UART_REPL

#ifndef MICROPY_HW_UART_REPL
#define MICROPY_HW_UART_REPL (0)
#endif
Expand All @@ -44,4 +46,6 @@
void uart_stdout_init(void);
int uart_stdout_tx_strn(const char *str, size_t len);

#endif // MICROPY_HW_ENABLE_UART_REPL

#endif // MICROPY_INCLUDED_ESP32_UART_H
4 changes: 2 additions & 2 deletions ports/esp32/usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#include "py/mphal.h"
#include "usb.h"

#if CONFIG_USB_OTG_SUPPORTED
#if CONFIG_USB_OTG_SUPPORTED && !CONFIG_ESP_CONSOLE_USB_CDC && !CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG

#include "esp_timer.h"
#ifndef NO_QSTR
Expand Down Expand Up @@ -97,4 +97,4 @@ void usb_tx_strn(const char *str, size_t len) {
}
}

#endif // CONFIG_USB_OTG_SUPPORTED
#endif // CONFIG_USB_OTG_SUPPORTED && !CONFIG_ESP_CONSOLE_USB_CDC && !CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG

0 comments on commit f8bd677

Please sign in to comment.