Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add USB to Serial/JTAG support for REPL #6181

Merged
merged 7 commits into from
Mar 23, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 5 additions & 139 deletions ports/espressif/supervisor/serial.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,104 +24,20 @@
* THE SOFTWARE.
*/

// This file will only be used when CIRCUITPY_USB is 0. See
// supervisor/supervisor.mk for the rule that applies.

#include <stdarg.h>
#include <string.h>

#include "py/mpconfig.h"
#include "supervisor/shared/cpu.h"
#include "supervisor/shared/display.h"
#include "shared-bindings/terminalio/Terminal.h"
#include "supervisor/serial.h"
#include "shared-bindings/microcontroller/Pin.h"

#if CIRCUITPY_SERIAL_BLE
#include "supervisor/shared/bluetooth/serial.h"
#endif

#if defined(CIRCUITPY_DEBUG_UART_TX) || defined(CIRCUITPY_DEBUG_UART_RX)
#include "py/mpprint.h"
#include "shared-bindings/busio/UART.h"
busio_uart_obj_t debug_uart;
byte buf_array[64];
#endif

#if CIRCUITPY_ESP_USB_SERIAL_JTAG
#include "supervisor/usb_serial_jtag.h"
#endif

#if defined(CIRCUITPY_DEBUG_UART_TX)
STATIC void debug_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);
}

const mp_print_t debug_uart_print = {NULL, debug_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
// crashing.
if (common_hal_busio_uart_deinited(&debug_uart)) {
return 0;
}
va_list ap;
va_start(ap, fmt);
int ret = mp_vprintf(&debug_uart_print, fmt, ap);
va_end(ap);
return ret;
#else
return 0;
#endif
}

void serial_early_init(void) {
#if defined(CIRCUITPY_DEBUG_UART_TX) || defined(CIRCUITPY_DEBUG_UART_RX)
debug_uart.base.type = &busio_uart_type;

#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 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

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);

// Do an initial print so that we can confirm the serial output is working.
debug_uart_printf("Serial debug setup\r\n");
#endif
}

void serial_init(void) {
void port_serial_init(void) {
#if CIRCUITPY_ESP_USB_SERIAL_JTAG
usb_serial_jtag_init();
#endif
}

bool serial_connected(void) {
#if defined(CIRCUITPY_DEBUG_UART_TX) && defined(CIRCUITPY_DEBUG_UART_RX)
return true;
#endif

#if CIRCUITPY_SERIAL_BLE
if (ble_serial_connected()) {
return true;
}
#endif

bool port_serial_connected(void) {
#if CIRCUITPY_ESP_USB_SERIAL_JTAG
if (usb_serial_jtag_connected()) {
return true;
Expand All @@ -131,23 +47,7 @@ bool serial_connected(void) {
return false;
}

char serial_read(void) {

#if defined(CIRCUITPY_DEBUG_UART_RX)
if (common_hal_busio_uart_rx_characters_available(&debug_uart)) {
int uart_errcode;
char text;
common_hal_busio_uart_read(&debug_uart, (uint8_t *)&text, 1, &uart_errcode);
return text;
}
#endif

#if CIRCUITPY_SERIAL_BLE
if (ble_serial_available() > 0) {
return ble_serial_read_char();
}
#endif

char port_serial_read(void) {
#if CIRCUITPY_ESP_USB_SERIAL_JTAG
if (usb_serial_jtag_bytes_available() > 0) {
return usb_serial_jtag_read_char();
Expand All @@ -156,19 +56,7 @@ char serial_read(void) {
return -1;
}

bool serial_bytes_available(void) {
#if defined(CIRCUITPY_DEBUG_UART_RX)
if (common_hal_busio_uart_rx_characters_available(&debug_uart)) {
return true;
}
#endif

#if CIRCUITPY_SERIAL_BLE
if (ble_serial_available()) {
return true;
}
#endif

bool port_serial_bytes_available(void) {
#if CIRCUITPY_ESP_USB_SERIAL_JTAG
if (usb_serial_jtag_bytes_available()) {
return true;
Expand All @@ -178,30 +66,8 @@ bool serial_bytes_available(void) {
return false;
}

void serial_write_substring(const char *text, uint32_t length) {
if (length == 0) {
return;
}
#if CIRCUITPY_TERMINALIO
int errcode;
common_hal_terminalio_terminal_write(&supervisor_terminal, (const uint8_t *)text, length, &errcode);
#endif

#if defined(CIRCUITPY_DEBUG_UART_TX)
int uart_errcode;

common_hal_busio_uart_write(&debug_uart, (const uint8_t *)text, length, &uart_errcode);
#endif

#if CIRCUITPY_SERIAL_BLE
ble_serial_write(text, length);
#endif

void port_serial_write_substring(const char *text, uint32_t length) {
#if CIRCUITPY_ESP_USB_SERIAL_JTAG
usb_serial_jtag_write(text, length);
#endif
}

void serial_write(const char *text) {
serial_write_substring(text, strlen(text));
}
43 changes: 0 additions & 43 deletions ports/espressif/supervisor/workflow.c

This file was deleted.

16 changes: 7 additions & 9 deletions ports/mimxrt10xx/supervisor/serial.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
#include "fsl_clock.h"
#include "fsl_lpuart.h"

// TODO: Switch this to using DEBUG_UART.

// static LPUART_Type *uart_instance = LPUART1; // evk
static LPUART_Type *uart_instance = LPUART4; // feather 1011
// static LPUART_Type *uart_instance = LPUART2; // feather 1062
Expand All @@ -52,7 +54,7 @@ static uint32_t UartSrcFreq(void) {
return freq;
}

void serial_init(void) {
void port_serial_init(void) {
lpuart_config_t config;

LPUART_GetDefaultConfig(&config);
Expand All @@ -63,27 +65,23 @@ void serial_init(void) {
LPUART_Init(uart_instance, &config, UartSrcFreq());
}

bool serial_connected(void) {
bool port_serial_connected(void) {
return true;
}

char serial_read(void) {
char port_serial_read(void) {
uint8_t data;

LPUART_ReadBlocking(uart_instance, &data, sizeof(data));

return data;
}

bool serial_bytes_available(void) {
bool port_serial_bytes_available(void) {
return LPUART_GetStatusFlags(uart_instance) & kLPUART_RxDataRegFullFlag;
}

void serial_write(const char *text) {
LPUART_WriteBlocking(uart_instance, (uint8_t *)text, strlen(text));
}

void serial_write_substring(const char *text, uint32_t len) {
void port_serial_write_substring(const char *text, uint32_t len) {
if (len == 0) {
return;
}
Expand Down
16 changes: 7 additions & 9 deletions ports/stm/supervisor/serial.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@
#include "stm32f4xx_hal.h"
#include "stm32f4/gpio.h"

// TODO: Switch this to using DEBUG_UART.

UART_HandleTypeDef huart2;

void serial_init(void) {
void port_serial_init(void) {
huart2.Instance = USART2;
huart2.Init.BaudRate = 115200;
huart2.Init.WordLength = UART_WORDLENGTH_8B;
Expand All @@ -47,25 +49,21 @@ void serial_init(void) {
}
}

bool serial_connected(void) {
bool port_serial_connected(void) {
return true;
}

char serial_read(void) {
char port_serial_read(void) {
uint8_t data;
HAL_UART_Receive(&huart2, &data, 1,500);
return data;
}

bool serial_bytes_available(void) {
bool port_serial_bytes_available(void) {
return __HAL_UART_GET_FLAG(&huart2, UART_FLAG_RXNE);
}

void serial_write(const char *text) {
serial_write_substring(text, strlen(text));
}

void serial_write_substring(const char *text, uint32_t len) {
void port_serial_write_substring(const char *text, uint32_t len) {
if (len == 0) {
return;
}
Expand Down
9 changes: 9 additions & 0 deletions supervisor/serial.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
extern vstr_t *boot_output;
#endif


void serial_early_init(void);
void serial_init(void);
void serial_write(const char *text);
Expand All @@ -48,6 +49,14 @@ char serial_read(void);
bool serial_bytes_available(void);
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_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)));

#endif // MICROPY_INCLUDED_SUPERVISOR_SERIAL_H
Loading