refactor debug UART to console UART; get working on ESP32

This commit is contained in:
Dan Halbert 2022-06-30 20:42:30 -04:00
parent b0efd130c9
commit 8bb369cac5
21 changed files with 110 additions and 98 deletions

View File

@ -60,7 +60,7 @@
#define MICROPY_PORT_ROOT_POINTERS \
CIRCUITPY_COMMON_ROOT_POINTERS
#define CIRCUITPY_DEBUG_UART_TX (&pin_GPIO14)
#define CIRCUITPY_DEBUG_UART_RX (&pin_GPIO15)
#define CIRCUITPY_CONSOLE_UART_TX (&pin_GPIO14)
#define CIRCUITPY_CONSOLE_UART_RX (&pin_GPIO15)
#endif // __INCLUDED_MPCONFIGPORT_H

View File

@ -47,5 +47,6 @@
// Explanation of how a user got into safe mode
#define BOARD_USER_SAFE_MODE_ACTION translate("pressing SW38 button at start up.\n")
#define CIRCUITPY_DEBUG_UART_TX (&pin_GPIO8)
#define CIRCUITPY_DEBUG_UART_RX (&pin_GPIO7)
// UART pins attached to the USB-serial converter chip
#define CIRCUITPY_CONSOLE_UART_TX (&pin_GPIO1)
#define CIRCUITPY_CONSOLE_UART_RX (&pin_GPIO3)

View File

@ -37,5 +37,5 @@
#define DEFAULT_UART_BUS_TX (&pin_GPIO21)
// Serial over UART
#define CIRCUITPY_DEBUG_UART_RX DEFAULT_UART_BUS_RX
#define CIRCUITPY_DEBUG_UART_TX DEFAULT_UART_BUS_TX
#define CIRCUITPY_CONSOLE_UART_RX DEFAULT_UART_BUS_RX
#define CIRCUITPY_CONSOLE_UART_TX DEFAULT_UART_BUS_TX

View File

@ -37,5 +37,5 @@
#define DEFAULT_UART_BUS_TX (&pin_GPIO21)
// Serial over UART
#define CIRCUITPY_DEBUG_UART_RX DEFAULT_UART_BUS_RX
#define CIRCUITPY_DEBUG_UART_TX DEFAULT_UART_BUS_TX
#define CIRCUITPY_CONSOLE_UART_RX DEFAULT_UART_BUS_RX
#define CIRCUITPY_CONSOLE_UART_TX DEFAULT_UART_BUS_TX

View File

@ -37,5 +37,5 @@
#define DEFAULT_UART_BUS_TX (&pin_GPIO21)
// Serial over UART
#define CIRCUITPY_DEBUG_UART_RX DEFAULT_UART_BUS_RX
#define CIRCUITPY_DEBUG_UART_TX DEFAULT_UART_BUS_TX
#define CIRCUITPY_CONSOLE_UART_RX DEFAULT_UART_BUS_RX
#define CIRCUITPY_CONSOLE_UART_TX DEFAULT_UART_BUS_TX

View File

@ -6,5 +6,5 @@
#define DEFAULT_UART_BUS_RX (&pin_GPIO20)
#define DEFAULT_UART_BUS_TX (&pin_GPIO21)
#define CIRCUITPY_DEBUG_UART_RX DEFAULT_UART_BUS_RX
#define CIRCUITPY_DEBUG_UART_TX DEFAULT_UART_BUS_TX
#define CIRCUITPY_CONSOLE_UART_RX DEFAULT_UART_BUS_RX
#define CIRCUITPY_CONSOLE_UART_TX DEFAULT_UART_BUS_TX

View File

@ -6,5 +6,5 @@
#define DEFAULT_UART_BUS_RX (&pin_GPIO20)
#define DEFAULT_UART_BUS_TX (&pin_GPIO21)
#define CIRCUITPY_DEBUG_UART_RX DEFAULT_UART_BUS_RX
#define CIRCUITPY_DEBUG_UART_TX DEFAULT_UART_BUS_TX
#define CIRCUITPY_CONSOLE_UART_RX DEFAULT_UART_BUS_RX
#define CIRCUITPY_CONSOLE_UART_TX DEFAULT_UART_BUS_TX

View File

@ -44,5 +44,5 @@
#define DEFAULT_UART_BUS_TX (&pin_GPIO21)
// Serial over UART
#define CIRCUITPY_DEBUG_UART_RX DEFAULT_UART_BUS_RX
#define CIRCUITPY_DEBUG_UART_TX DEFAULT_UART_BUS_TX
#define CIRCUITPY_CONSOLE_UART_RX DEFAULT_UART_BUS_RX
#define CIRCUITPY_CONSOLE_UART_TX DEFAULT_UART_BUS_TX

View File

@ -50,8 +50,8 @@ static void uart_event_task(void *param) {
if (xQueueReceive(self->event_queue, &event, portMAX_DELAY)) {
switch (event.type) {
case UART_PATTERN_DET:
// When the debug uart receives CTRL+C, wake the main task and schedule a keyboard interrupt
if (self->is_debug) {
// When the console uart receives CTRL+C, wake the main task and schedule a keyboard interrupt
if (self->is_console) {
port_wake_main_task();
if (mp_interrupt_char == CHAR_CTRL_C) {
uart_flush(self->uart_num);
@ -60,8 +60,8 @@ static void uart_event_task(void *param) {
}
break;
case UART_DATA:
// When the debug uart receives any key, wake the main task
if (self->is_debug) {
// When the console uart receives any key, wake the main task
if (self->is_console) {
port_wake_main_task();
}
break;
@ -162,13 +162,15 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
uart_set_mode(self->uart_num, mode) != ESP_OK) {
mp_raise_RuntimeError(translate("UART init"));
}
// On the debug uart, enable pattern detection to look for CTRL+C
#ifdef CIRCUITPY_DEBUG_UART_RX
if (rx == CIRCUITPY_DEBUG_UART_RX) {
self->is_debug = true;
// On the console uart, enable pattern detection to look for CTRL+C
#if CIRCUITPY_CONSOLE_UART
if (rx == CIRCUITPY_CONSOLE_UART_RX) {
self->is_console = true;
uart_enable_pattern_det_baud_intr(self->uart_num, CHAR_CTRL_C, 1, 1, 0, 0);
}
#endif
// Start a task to listen for uart events
xTaskCreatePinnedToCore(
uart_event_task,

View File

@ -46,7 +46,7 @@ typedef struct {
uint8_t character_bits;
bool rx_error;
uint32_t timeout_ms;
bool is_debug;
bool is_console;
QueueHandle_t event_queue;
TaskHandle_t event_task;
} busio_uart_obj_t;

View File

@ -196,7 +196,7 @@ void common_hal_mcu_pin_claim(const mcu_pin_obj_t *pin) {
}
bool pin_number_is_free(gpio_num_t pin_number) {
return in_use & PIN_BIT(pin_number);
return !(in_use & PIN_BIT(pin_number));
}
bool common_hal_mcu_pin_is_free(const mcu_pin_obj_t *pin) {

View File

@ -27,6 +27,10 @@
#include "py/mpconfig.h"
#include "supervisor/serial.h"
#if CIRCUITPY_ESP_USB_SERIAL_JTAG && CIRCUITPY_CONSOLE_UART
#error CIRCUITPY_ESP_USB_SERIAL_JTAG and CIRCUITPY_CONSOLE_UART cannot both be enabled.
#endif
#if CIRCUITPY_ESP_USB_SERIAL_JTAG
#include "supervisor/usb_serial_jtag.h"
#endif
@ -37,14 +41,13 @@ void port_serial_init(void) {
#endif
}
bool port_serial_connected(void) {
#if CIRCUITPY_ESP_USB_SERIAL_JTAG
if (usb_serial_jtag_connected()) {
return true;
}
#endif
return usb_serial_jtag_connected();
#else
return false;
#endif
}
char port_serial_read(void) {
@ -58,12 +61,10 @@ char port_serial_read(void) {
bool port_serial_bytes_available(void) {
#if CIRCUITPY_ESP_USB_SERIAL_JTAG
if (usb_serial_jtag_bytes_available()) {
return true;
}
#endif
return usb_serial_jtag_bytes_available();
#else
return false;
#endif
}
void port_serial_write_substring(const char *text, uint32_t length) {

View File

@ -16,9 +16,8 @@
#define DEFAULT_UART_BUS_RX (&pin_GPIO_AD_B1_07)
#define DEFAULT_UART_BUS_TX (&pin_GPIO_AD_B1_06)
#define CIRCUITPY_DEBUG_UART_TX (&pin_GPIO_AD_B0_12)
#define CIRCUITPY_DEBUG_UART_RX (&pin_GPIO_AD_B0_13)
#define CIRCUITPY_CONSOLE_UART_TX (&pin_GPIO_AD_B0_12)
#define CIRCUITPY_CONSOLE_UART_RX (&pin_GPIO_AD_B0_13)
// Put host on the first USB so that right angle OTG adapters can fit. This is
// the right port when looking at the board.

View File

@ -33,9 +33,7 @@
#include "fsl_clock.h"
#include "fsl_lpuart.h"
// TODO: Switch this to using DEBUG_UART.
// If the board defined a debug uart tx or rx pin then we enable this code
#if defined(CIRCUITPY_DEBUG_UART_TX) || defined(CIRCUITPY_DEBUG_UART_RX)
#if defined(CIRCUITPY_CONSOLE_UART)
// static LPUART_Type *uart_instance = LPUART1; // evk
static LPUART_Type *uart_instance = LPUART4; // feather 1011
// static LPUART_Type *uart_instance = LPUART2; // feather 1062
@ -89,4 +87,4 @@ void port_serial_write_substring(const char *text, uint32_t len) {
LPUART_WriteBlocking(uart_instance, (uint8_t *)text, len);
}
#endif // USE_DEBUG_PORT_CODE
#endif // CIRCUITPY_CONSOLE_UART

View File

@ -52,5 +52,6 @@
#define BOOTLOADER_SETTING_SIZE (0)
#define BOARD_HAS_32KHZ_XTAL (0)
#define CIRCUITPY_DEBUG_UART_TX (&pin_P0_06)
#define CIRCUITPY_DEBUG_UART_RX (&pin_P1_08)
#define CIRCUITPY_CONSOLE_UART_TX (&pin_P0_06)
#define CIRCUITPY_CONSOLE_UART_RX (&pin_P1_08)

View File

@ -46,5 +46,5 @@
#define BOARD_HSE_SOURCE (RCC_HSE_BYPASS) // ST boards use the STLink clock signal
#define BOARD_HAS_LOW_SPEED_CRYSTAL (1)
#define CIRCUITPY_DEBUG_UART_TX (&pin_PD08)
#define CIRCUITPY_DEBUG_UART_RX (&pin_PD09)
#define CIRCUITPY_CONSOLE_UART_TX (&pin_PD08)
#define CIRCUITPY_CONSOLE_UART_RX (&pin_PD09)

View File

@ -198,10 +198,11 @@ typedef long mp_off_t;
// extra built in names to add to the global namespace
// Not indented so as not to confused the editor.
#define MICROPY_PORT_BUILTINS \
{ MP_OBJ_NEW_QSTR(MP_QSTR_help), (mp_obj_t)&mp_builtin_help_obj }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_input), (mp_obj_t)&mp_builtin_input_obj }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_open), (mp_obj_t)&mp_builtin_open_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_help), (mp_obj_t)&mp_builtin_help_obj }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_input), (mp_obj_t)&mp_builtin_input_obj }, \
{ MP_OBJ_NEW_QSTR(MP_QSTR_open), (mp_obj_t)&mp_builtin_open_obj }, \
//////////////////////////////////////////////////////////////////////////////////////////////////
// board-specific definitions, which control and may override definitions below.
@ -296,6 +297,18 @@ typedef long mp_off_t;
#define BOARD_UART_ROOT_POINTER mp_obj_t board_uart_bus;
#endif
#if defined(CIRCUITPY_CONSOLE_UART_RX) || defined(CIRCUITPY_CONSOLE_UART_TX)
#if !(defined(CIRCUITPY_CONSOLE_UART_RX) && defined(CIRCUITPY_CONSOLE_UART_TX))
#error Both CIRCUITPY_CONSOLE_UART_RX and CIRCUITPY_CONSOLE_UART_TX must be defined if one is defined.
#endif
#define CIRCUITPY_CONSOLE_UART (1)
#ifndef CIRCUITPY_CONSOLE_UART_BAUDRATE
#define CIRCUITPY_CONSOLE_UART_BAUDRATE (115200)
#endif
#else
#define CIRCUITPY_CONSOLE_UART (0)
#endif
// These CIRCUITPY_xxx values should all be defined in the *.mk files as being on or off.
// So if any are not defined in *.mk, they'll throw an error here.

View File

@ -168,9 +168,6 @@ CFLAGS += -DCIRCUITPY_OPT_LOAD_ATTR_FAST_PATH=$(CIRCUITPY_OPT_LOAD_ATTR_FAST_PAT
CIRCUITPY_OPT_MAP_LOOKUP_CACHE ?= $(CIRCUITPY_FULL_BUILD)
CFLAGS += -DCIRCUITPY_OPT_MAP_LOOKUP_CACHE=$(CIRCUITPY_OPT_MAP_LOOKUP_CACHE)
CIRCUITPY_CONSOLE_UART ?= 0
CFLAGS += -DCIRCUITPY_CONSOLE_UART=$(CIRCUITPY_CONSOLE_UART)
CIRCUITPY_COUNTIO ?= $(CIRCUITPY_FULL_BUILD)
CFLAGS += -DCIRCUITPY_COUNTIO=$(CIRCUITPY_COUNTIO)

View File

@ -51,12 +51,13 @@ bool serial_connected(void);
// These have no-op versions that are weak and the port can override. They work
// in tandem with the cross-port mechanics like USB and BLE.
void port_serial_early_init(void);
void port_serial_init(void);
bool port_serial_connected(void);
char port_serial_read(void);
bool port_serial_bytes_available(void);
void port_serial_write_substring(const char *text, uint32_t length);
int debug_uart_printf(const char *fmt, ...) __attribute__((format(printf, 1, 2)));
int console_uart_printf(const char *fmt, ...) __attribute__((format(printf, 1, 2)));
#endif // MICROPY_INCLUDED_SUPERVISOR_SERIAL_H

View File

@ -45,42 +45,43 @@
#include "tusb.h"
#endif
/*
* Note: DEBUG_UART currently only works on STM32 and nRF.
* Enabling on another platform will cause a crash.
*/
#if defined(CIRCUITPY_DEBUG_UART_TX) || defined(CIRCUITPY_DEBUG_UART_RX)
#if CIRCUITPY_CONSOLE_UART
#include "py/mpprint.h"
#include "shared-bindings/busio/UART.h"
busio_uart_obj_t debug_uart;
byte buf_array[256];
busio_uart_obj_t console_uart;
// on Espressif, the receive buffer must be larger than the hardware FIFO length. See uart_driver_install().
#if defined(SOC_UART_FIFO_LEN)
byte console_uart_rx_buf[SOC_UART_FIFO_LEN + 1];
#else
byte console_uart_rx_buf[64];
#endif
#endif
#if CIRCUITPY_USB_VENDOR
bool tud_vendor_connected(void);
#endif
#if defined(CIRCUITPY_DEBUG_UART_TX)
STATIC void debug_uart_print_strn(void *env, const char *str, size_t len) {
#if CIRCUITPY_CONSOLE_UART
STATIC void console_uart_print_strn(void *env, const char *str, size_t len) {
(void)env;
int uart_errcode;
common_hal_busio_uart_write(&debug_uart, (const uint8_t *)str, len, &uart_errcode);
common_hal_busio_uart_write(&console_uart, (const uint8_t *)str, len, &uart_errcode);
}
const mp_print_t debug_uart_print = {NULL, debug_uart_print_strn};
const mp_print_t console_uart_print = {NULL, console_uart_print_strn};
#endif
int debug_uart_printf(const char *fmt, ...) {
#if defined(CIRCUITPY_DEBUG_UART_TX)
// Skip prints that occur before debug serial is started. It's better than
int console_uart_printf(const char *fmt, ...) {
#if CIRCUITPY_CONSOLE_UART
// Skip prints that occur before console serial is started. It's better than
// crashing.
if (common_hal_busio_uart_deinited(&debug_uart)) {
if (common_hal_busio_uart_deinited(&console_uart)) {
return 0;
}
va_list ap;
va_start(ap, fmt);
int ret = mp_vprintf(&debug_uart_print, fmt, ap);
int ret = mp_vprintf(&console_uart_print, fmt, ap);
va_end(ap);
return ret;
#else
@ -88,6 +89,9 @@ int debug_uart_printf(const char *fmt, ...) {
#endif
}
MP_WEAK void port_serial_early_init(void) {
}
MP_WEAK void port_serial_init(void) {
}
@ -109,29 +113,24 @@ MP_WEAK void port_serial_write_substring(const char *text, uint32_t length) {
}
void serial_early_init(void) {
#if defined(CIRCUITPY_DEBUG_UART_TX) || defined(CIRCUITPY_DEBUG_UART_RX)
debug_uart.base.type = &busio_uart_type;
// Set up console UART, if enabled.
#if defined(CIRCUITPY_DEBUG_UART_RX)
const mcu_pin_obj_t *rx = MP_OBJ_TO_PTR(CIRCUITPY_DEBUG_UART_RX);
#else
const mcu_pin_obj_t *rx = NULL;
#endif
#if CIRCUITPY_CONSOLE_UART
console_uart.base.type = &busio_uart_type;
#if defined(CIRCUITPY_DEBUG_UART_TX)
const mcu_pin_obj_t *tx = MP_OBJ_TO_PTR(CIRCUITPY_DEBUG_UART_TX);
#else
const mcu_pin_obj_t *tx = NULL;
#endif
const mcu_pin_obj_t *console_rx = MP_OBJ_TO_PTR(CIRCUITPY_CONSOLE_UART_RX);
const mcu_pin_obj_t *console_tx = MP_OBJ_TO_PTR(CIRCUITPY_CONSOLE_UART_TX);
common_hal_busio_uart_construct(&debug_uart, tx, rx, NULL, NULL, NULL,
false, 115200, 8, BUSIO_UART_PARITY_NONE, 1, 1.0f, 64,
buf_array, true);
common_hal_busio_uart_never_reset(&debug_uart);
common_hal_busio_uart_construct(&console_uart, console_tx, console_rx, NULL, NULL, NULL,
false, 115200, 8, BUSIO_UART_PARITY_NONE, 1, 1.0f, sizeof(console_uart_rx_buf),
console_uart_rx_buf, true);
common_hal_busio_uart_never_reset(&console_uart);
// Do an initial print so that we can confirm the serial output is working.
debug_uart_printf("Serial debug setup\r\n");
console_uart_printf("Serial console setup\r\n");
#endif
port_serial_early_init();
}
void serial_init(void) {
@ -145,7 +144,7 @@ bool serial_connected(void) {
}
#endif
#if defined(CIRCUITPY_DEBUG_UART_TX) && defined(CIRCUITPY_DEBUG_UART_RX)
#if CIRCUITPY_CONSOLE_UART
return true;
#endif
@ -180,11 +179,11 @@ char serial_read(void) {
}
#endif
#if defined(CIRCUITPY_DEBUG_UART_RX)
if (common_hal_busio_uart_rx_characters_available(&debug_uart)) {
#if CIRCUITPY_CONSOLE_UART
if (common_hal_busio_uart_rx_characters_available(&console_uart)) {
int uart_errcode;
char text;
common_hal_busio_uart_read(&debug_uart, (uint8_t *)&text, 1, &uart_errcode);
common_hal_busio_uart_read(&console_uart, (uint8_t *)&text, 1, &uart_errcode);
return text;
}
#endif
@ -217,8 +216,8 @@ bool serial_bytes_available(void) {
}
#endif
#if defined(CIRCUITPY_DEBUG_UART_RX)
if (common_hal_busio_uart_rx_characters_available(&debug_uart)) {
#if CIRCUITPY_CONSOLE_UART
if (common_hal_busio_uart_rx_characters_available(&console_uart)) {
return true;
}
#endif
@ -261,10 +260,10 @@ void serial_write_substring(const char *text, uint32_t length) {
}
#endif
#if defined(CIRCUITPY_DEBUG_UART_TX)
#if CIRCUITPY_CONSOLE_UART
int uart_errcode;
common_hal_busio_uart_write(&debug_uart, (const uint8_t *)text, length, &uart_errcode);
common_hal_busio_uart_write(&console_uart, (const uint8_t *)text, length, &uart_errcode);
#endif
#if CIRCUITPY_SERIAL_BLE

View File

@ -48,8 +48,8 @@ extern "C" {
// COMMON CONFIGURATION
// --------------------------------------------------------------------+
// When debugging TinyUSB, only output to the UART debug link.
#if CIRCUITPY_DEBUG_TINYUSB > 0 && defined(CIRCUITPY_DEBUG_UART_TX)
// When debugging TinyUSB, only output to the console UART link.
#if CIRCUITPY_DEBUG_TINYUSB > 0 && defined(CIRCUITPY_CONSOLE_UART)
#define CFG_TUSB_DEBUG CIRCUITPY_DEBUG_TINYUSB
#define CFG_TUSB_DEBUG_PRINTF debug_uart_printf
#endif