1515#include "esp_intr_alloc.h"
1616#include "soc/periph_defs.h"
1717
18+ #include "esp_log.h"
19+ static const char * TAG = "usb_serial_jtag" ;
1820
1921#define USB_SERIAL_JTAG_BUF_SIZE (64)
2022
@@ -33,6 +35,7 @@ static void _copy_out_of_fifo(void) {
3335 if (req_len == 0 ) {
3436 // Disable the interrupt so that CircuitPython can run and process the ringbuf. It will
3537 // re-enable the interrupt once the ringbuf is empty.
38+ ESP_EARLY_LOGW (TAG , "Disable interrupt" );
3639 usb_serial_jtag_ll_disable_intr_mask (USB_SERIAL_JTAG_INTR_SERIAL_OUT_RECV_PKT );
3740 }
3841 if (req_len > USB_SERIAL_JTAG_BUF_SIZE ) {
@@ -42,9 +45,15 @@ static void _copy_out_of_fifo(void) {
4245
4346 // Read up to req_len bytes. Does not block.
4447 size_t len = usb_serial_jtag_ll_read_rxfifo (rx_buf , req_len );
48+ if (len == req_len ) {
49+ ESP_EARLY_LOGW (TAG , "Buffer full" );
50+ } else if (len > 0 ) {
51+ ESP_EARLY_LOGW (TAG , "Read %d bytes" , len );
52+ }
4553
4654 for (size_t i = 0 ; i < len ; ++ i ) {
4755 if (rx_buf [i ] == mp_interrupt_char ) {
56+ ESP_LOGI (TAG , "usb_serial_jtag_read_char: interrupt char received" );
4857 mp_sched_keyboard_interrupt ();
4958 ringbuf_clear (& ringbuf );
5059 } else {
@@ -72,10 +81,20 @@ static void usb_serial_jtag_isr_handler(void *arg) {
7281 _copy_out_of_fifo ();
7382 port_wake_main_task_from_isr ();
7483 }
84+
85+ // Clear everything else
86+ usb_serial_jtag_ll_clr_intsts_mask (~(USB_SERIAL_JTAG_INTR_SOF | USB_SERIAL_JTAG_INTR_SERIAL_OUT_RECV_PKT | USB_SERIAL_JTAG_INTR_TOKEN_REC_IN_EP1 ));
7587}
7688
7789void usb_serial_jtag_init (void ) {
7890 ringbuf_init (& ringbuf , buf , sizeof (buf ));
91+ #ifdef USB_SERIAL_JTAG_USB_UART_CHIP_RST_DIS
92+ // Disable UART triggered reset because the host may set the serial lines.
93+ USB_SERIAL_JTAG .chip_rst .usb_uart_chip_rst_dis = 1 ;
94+ ESP_LOGI (TAG , "UART reset disabled" );
95+ #else
96+ #error "USB_SERIAL_JTAG_USB_UART_CHIP_RST_DIS must be defined"
97+ #endif
7998 usb_serial_jtag_ll_clr_intsts_mask (USB_SERIAL_JTAG_INTR_SOF | USB_SERIAL_JTAG_INTR_SERIAL_OUT_RECV_PKT | USB_SERIAL_JTAG_INTR_TOKEN_REC_IN_EP1 );
8099 usb_serial_jtag_ll_ena_intr_mask (USB_SERIAL_JTAG_INTR_SOF | USB_SERIAL_JTAG_INTR_SERIAL_OUT_RECV_PKT | USB_SERIAL_JTAG_INTR_TOKEN_REC_IN_EP1 );
81100 ESP_ERROR_CHECK (esp_intr_alloc (ETS_USB_SERIAL_JTAG_INTR_SOURCE , ESP_INTR_FLAG_LEVEL1 ,
@@ -90,6 +109,7 @@ char usb_serial_jtag_read_char(void) {
90109 uint32_t num_filled = ringbuf_num_filled (& ringbuf );
91110
92111 if (num_filled == 0 && !usb_serial_jtag_ll_rxfifo_data_available ()) {
112+ ESP_LOGI (TAG , "usb_serial_jtag_read_char: no data available" );
93113 return -1 ;
94114 }
95115 char c = -1 ;
@@ -113,6 +133,7 @@ char usb_serial_jtag_read_char(void) {
113133 }
114134 usb_serial_jtag_ll_ena_intr_mask (USB_SERIAL_JTAG_INTR_SERIAL_OUT_RECV_PKT );
115135 }
136+ ESP_LOGI (TAG , "c: %c" , c );
116137 return c ;
117138}
118139
@@ -121,13 +142,15 @@ uint32_t usb_serial_jtag_bytes_available(void) {
121142 common_hal_mcu_disable_interrupts ();
122143 const uint32_t count = ringbuf_num_filled (& ringbuf ) + usb_serial_jtag_ll_rxfifo_data_available ();
123144 common_hal_mcu_enable_interrupts ();
145+ ESP_LOGI (TAG , "usb_serial_jtag_bytes_available: %d" , count );
124146 return count ;
125147}
126148
127149void usb_serial_jtag_write (const char * text , uint32_t length ) {
128150 if (!usb_serial_jtag_connected ()) {
129151 return ;
130152 }
153+ ESP_LOGI (TAG , "Writing '%s'" , text );
131154 size_t total_written = 0 ;
132155 while (total_written < length ) {
133156 uint32_t start_time = supervisor_ticks_ms32 ();
@@ -144,4 +167,5 @@ void usb_serial_jtag_write(const char *text, uint32_t length) {
144167 RUN_BACKGROUND_TASKS ;
145168 }
146169 usb_serial_jtag_ll_txfifo_flush ();
170+ ESP_LOGI (TAG , "Written %d bytes" , total_written );
147171}
0 commit comments