Skip to content

Commit

Permalink
writing to the debugger over serial_jtag would sometimes hang. #1299
Browse files Browse the repository at this point in the history
  • Loading branch information
mkellner committed Feb 10, 2024
1 parent a3108f5 commit f45de9f
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 34 deletions.
34 changes: 26 additions & 8 deletions build/devices/esp32/xsProj-esp32c3/main/main.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2023 Moddable Tech, Inc.
* Copyright (c) 2016-2024 Moddable Tech, Inc.
*
* This file is part of the Moddable SDK Runtime.
*
Expand Down Expand Up @@ -45,6 +45,7 @@
#if USE_USB
#if USE_USB == 2
#include "driver/usb_serial_jtag.h"
#include "hal/usb_serial_jtag_ll.h"
#else
#error esp32c3 doesnt support TinyUSB
#endif
Expand Down Expand Up @@ -115,7 +116,7 @@ static xsMachine *gThe; // the main XS virtual machine running
#if USE_USB
static void debug_task(void *pvParameter)
{
const usb_serial_jtag_driver_config_t cfg = { .rx_buffer_size = 4096, .tx_buffer_size = 2048 };
const usb_serial_jtag_driver_config_t cfg = { .rx_buffer_size = 2048, .tx_buffer_size = 64 };
usb_serial_jtag_driver_install(&cfg);

while (true) {
Expand Down Expand Up @@ -199,23 +200,40 @@ void modLog_transmit(const char *msg)
}
}

#define WRITE_CHUNK 64
#define FAIL_RETRY 2
void ESP_put(uint8_t *c, int count) {
#if USE_USB
int sent = 0;
while (count > 0) {
sent = usb_serial_jtag_write_bytes(c, count, 10);
c += sent;
count -= sent;
int len = count > WRITE_CHUNK ? WRITE_CHUNK : count;
int wrote;
int fail = FAIL_RETRY;
while ((wrote = usb_serial_jtag_ll_write_txfifo(c, len)) < 1) {
if (0 == --fail)
goto done;
modDelayMilliseconds(1);
continue;
}
usb_serial_jtag_ll_txfifo_flush();
c += wrote;
count -= wrote;
}
done:
#else
uart_write_bytes(USE_UART, (char *)c, count);
#endif
}

void ESP_putc(int c) {
char cx = c;
uint8_t cx = c;
#if USE_USB
usb_serial_jtag_write_bytes(&cx, 1, 1);
int fail = FAIL_RETRY;
while (usb_serial_jtag_ll_write_txfifo(&cx, 1) < 1) {
if (0 == --fail)
break;
modDelayMilliseconds(1);
}
usb_serial_jtag_ll_txfifo_flush();
#else
uart_write_bytes(USE_UART, &cx, 1);
#endif
Expand Down
36 changes: 27 additions & 9 deletions build/devices/esp32/xsProj-esp32c6/main/main.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2023 Moddable Tech, Inc.
* Copyright (c) 2016-2024 Moddable Tech, Inc.
*
* This file is part of the Moddable SDK Runtime.
*
Expand Down Expand Up @@ -70,6 +70,7 @@
#include "tusb_cdc_acm.h"
#elif (USE_USB == 2)
#include "driver/usb_serial_jtag.h"
#include "hal/usb_serial_jtag_ll.h"
#endif
#else
#include "driver/uart.h"
Expand Down Expand Up @@ -197,7 +198,7 @@ printf("fifo_init - bad size: %d\r\n", size);
static void debug_task(void *pvParameter)
{
#if (USE_USB == 2)
const usb_serial_jtag_driver_config_t cfg = { .rx_buffer_size = 4096, .tx_buffer_size = 2048 };
const usb_serial_jtag_driver_config_t cfg = { .rx_buffer_size = 2048, .tx_buffer_size = 64 };
usb_serial_jtag_driver_install(&cfg);
#endif

Expand Down Expand Up @@ -404,23 +405,40 @@ uint8_t ESP_setBaud(int baud) {

#else

#define WRITE_CHUNK 64
#define FAIL_RETRY 2
void ESP_put(uint8_t *c, int count) {
#if (USE_USB == 2)
int sent = 0;
while (count > 0) {
sent = usb_serial_jtag_write_bytes(c, count, 10);
c += sent;
count -= sent;
}
int len = count > WRITE_CHUNK ? WRITE_CHUNK : count;
int wrote;
int fail = FAIL_RETRY;
while ((wrote = usb_serial_jtag_ll_write_txfifo(c, len)) < 1) {
if (0 == --fail)
goto done;
modDelayMilliseconds(1);
continue;
}
usb_serial_jtag_ll_txfifo_flush();
c += wrote;
count -= wrote;
}
done:
#else
uart_write_bytes(USE_UART, (char *)c, count);
#endif
}

void ESP_putc(int c) {
char cx = c;
uint8_t cx = c;
#if (USE_USB == 2)
usb_serial_jtag_write_bytes(&cx, 1, 1);
int fail = FAIL_RETRY;
while (usb_serial_jtag_ll_write_txfifo(&cx, 1) < 1) {
if (0 == --fail)
break;
modDelayMilliseconds(1);
}
usb_serial_jtag_ll_txfifo_flush();
#else
uart_write_bytes(USE_UART, &cx, 1);
#endif
Expand Down
22 changes: 14 additions & 8 deletions build/devices/esp32/xsProj-esp32h2/main/main.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2023 Moddable Tech, Inc.
* Copyright (c) 2016-2024 Moddable Tech, Inc.
*
* This file is part of the Moddable SDK Runtime.
*
Expand Down Expand Up @@ -70,6 +70,7 @@
#include "tusb_cdc_acm.h"
#elif (USE_USB == 2)
#include "driver/usb_serial_jtag.h"
#include "hal/usb_serial_jtag_ll.h"
#endif
#else
#include "driver/uart.h"
Expand Down Expand Up @@ -197,7 +198,7 @@ printf("fifo_init - bad size: %d\r\n", size);
static void debug_task(void *pvParameter)
{
#if (USE_USB == 2)
const usb_serial_jtag_driver_config_t cfg = { .rx_buffer_size = 4096, .tx_buffer_size = 2048 };
const usb_serial_jtag_driver_config_t cfg = { .rx_buffer_size = 2048, .tx_buffer_size = 64 };
usb_serial_jtag_driver_install(&cfg);
#endif

Expand Down Expand Up @@ -404,23 +405,28 @@ uint8_t ESP_setBaud(int baud) {

#else

#define WRITE_CHUNK 64
void ESP_put(uint8_t *c, int count) {
#if (USE_USB == 2)
int sent = 0;
while (count > 0) {
sent = usb_serial_jtag_write_bytes(c, count, 10);
c += sent;
count -= sent;
int len = count > WRITE_CHUNK ? WRITE_CHUNK : count;
int wrote;
wrote = usb_serial_jtag_ll_write_txfifo(c, len);
usb_serial_jtag_ll_txfifo_flush();
c += wrote;
count -= wrote;
}
#else
uart_write_bytes(USE_UART, (char *)c, count);
#endif
}

void ESP_putc(int c) {
char cx = c;
uint8_t cx = c;
#if (USE_USB == 2)
usb_serial_jtag_write_bytes(&cx, 1, 1);
while (usb_serial_jtag_ll_write_txfifo(&cx, 1) < 1)
;
usb_serial_jtag_ll_txfifo_flush();
#else
uart_write_bytes(USE_UART, &cx, 1);
#endif
Expand Down
36 changes: 27 additions & 9 deletions build/devices/esp32/xsProj-esp32s3/main/main.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2023 Moddable Tech, Inc.
* Copyright (c) 2016-2024 Moddable Tech, Inc.
*
* This file is part of the Moddable SDK Runtime.
*
Expand Down Expand Up @@ -69,6 +69,7 @@
#include "tusb_cdc_acm.h"
#elif (USE_USB == 2)
#include "driver/usb_serial_jtag.h"
#include "hal/usb_serial_jtag_ll.h"
#endif
#else
#include "driver/uart.h"
Expand Down Expand Up @@ -196,7 +197,7 @@ printf("fifo_init - bad size: %ld\r\n", size);
static void debug_task(void *pvParameter)
{
#if (USE_USB == 2)
const usb_serial_jtag_driver_config_t cfg = { .rx_buffer_size = 4096, .tx_buffer_size = 2048 };
const usb_serial_jtag_driver_config_t cfg = { .rx_buffer_size = 2048, .tx_buffer_size = 64 };
usb_serial_jtag_driver_install(&cfg);
#endif

Expand Down Expand Up @@ -403,23 +404,40 @@ uint8_t ESP_setBaud(int baud) {

#else

#define WRITE_CHUNK 64
#define FAIL_RETRY 2
WEAK void ESP_put(uint8_t *c, int count) {
#if (USE_USB == 2)
int sent = 0;
while (count > 0) {
sent = usb_serial_jtag_write_bytes(c, count, 10);
c += sent;
count -= sent;
}
int len = count > WRITE_CHUNK ? WRITE_CHUNK : count;
int wrote;
int fail = FAIL_RETRY;
while ((wrote = usb_serial_jtag_ll_write_txfifo(c, len)) < 1) {
if (0 == --fail)
goto done;
modDelayMilliseconds(1);
continue;
}
usb_serial_jtag_ll_txfifo_flush();
c += wrote;
count -= wrote;
}
done:
#else
uart_write_bytes(USE_UART, (char *)c, count);
#endif
}

WEAK void ESP_putc(int c) {
char cx = c;
uint8_t cx = c;
#if (USE_USB == 2)
usb_serial_jtag_write_bytes(&cx, 1, 1);
int fail = FAIL_RETRY;
while (usb_serial_jtag_ll_write_txfifo(&cx, 1) < 1) {
if (0 == --fail)
break;
modDelayMilliseconds(1);
}
usb_serial_jtag_ll_txfifo_flush();
#else
uart_write_bytes(USE_UART, &cx, 1);
#endif
Expand Down

0 comments on commit f45de9f

Please sign in to comment.