8000 Add debug output to CDC · silverchris/arduino-esp32@ea04207 · GitHub
[go: up one dir, main page]

Skip to content

Commit ea04207

Browse files
committed
Add debug output to CDC
1 parent e06f9b7 commit ea04207

File tree

3 files changed

+47
-40
lines changed

3 files changed

+47
-40
lines changed

cores/esp32/esp32-hal-uart.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -595,9 +595,6 @@ int uartGetDebug()
595595

596596
int log_printf(const char *format, ...)
597597
{
598-
if(s_uart_debug_nr < 0){
599-
return 0;
600-
}
601598
static char loc_buf[64];
602599
char * temp = loc_buf;
603600
int len;
@@ -615,7 +612,7 @@ int log_printf(const char *format, ...)
615612
}
616613
vsnprintf(temp, len+1, format, arg);
617614
#if !CONFIG_DISABLE_HAL_LOCKS
618-
if(_uart_bus_array[s_uart_debug_nr].lock){
615+
if(s_uart_debug_nr != -1 && _uart_bus_array[s_uart_debug_nr].lock){
619616
xSemaphoreTake(_uart_bus_array[s_uart_debug_nr].lock, portMAX_DELAY);
620617
ets_printf("%s", temp);
621618
xSemaphoreGive(_uart_bus_array[s_uart_debug_nr].lock);

libraries/USB/src/USBCDC.cpp

Lines changed: 45 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,47 @@ void tud_cdc_rx_cb(uint8_t itf)
6464
}
6565
}
6666

67+
68+
static size_t tinyusb_cdc_write(uint8_t itf, const uint8_t *buffer, size_t size){
69+
if(itf >= MAX_USB_CDC_DEVICES){
70+
return 0;
71+
}
72+
if(!tud_cdc_n_connected(itf)){
73+
return 0;
74+
}
75+
size_t tosend = size, sofar = 0;
76+
while(tosend){
77+
uint32_t space = tud_cdc_n_write_available(itf);
78+
if(!space){
79+
delay(1);
80+
continue;
81+
}
82+
if(tosend < space){
83+
space = tosend;
84+
}
85+
uint32_t sent = tud_cdc_n_write(itf, buffer + sofar, space);
86+
if(!sent){
87+
return sofar;
88+
}
89+
sofar += sent;
90+
tosend -= sent;
91+
tud_cdc_n_write_flush(itf);
92+
}
93+
return sofar;
94+
}
95+
96+
static void ARDUINO_ISR_ATTR cdc0_write_char(char c)
97+
{
98+
tinyusb_cdc_write(0, (const uint8_t *)&c, 1);
99+
}
100+
67101
//void tud_cdc_rx_wanted_cb(uint8_t itf, char wanted_char);
68102

69103
static void usb_unplugged_cb(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data){
70104
((USBCDC*)arg)->_onUnplugged();
71105
}
72106

73-
USBCDC::USBCDC(uint8_t itfn) : itf(itfn), bit_rate(0), stop_bits(0), parity(0), data_bits(0), dtr(false), rts(false), connected(usb_persist_this_boot()), reboot_enable(true), rx_queue(NULL) {
107+
USBCDC::USBCDC(uint8_t itfn) : itf(itfn), bit_rate(0), stop_bits(0), parity(0), data_bits(0), dtr(false), rts(false), connected(false), reboot_enable(true), rx_queue(NULL) {
74108
tinyusb_enable_interface(USB_INTERFACE_CDC, TUD_CDC_DESC_LEN, load_cdc_descriptor);
75109
if(itf < MAX_USB_CDC_DEVICES){
76110
devices[itf] = this;
@@ -94,15 +128,6 @@ void USBCDC::begin(size_t rx_queue_len)
94128
if(!rx_queue){
95129
return;
96130
}
97-
if(connected){
98-
arduino_usb_cdc_event_data_t p = {0};
99-
arduino_usb_event_post(ARDUINO_USB_CDC_EVENTS, ARDUINO_USB_CDC_CONNECTED_EVENT, &p, sizeof(arduino_usb_cdc_event_data_t), portMAX_DELAY);
100-
101-
arduino_usb_cdc_event_data_t l = {0};
102-
l.line_state.dtr = true;
103-
l.line_state.rts = true;
104-
arduino_usb_event_post(ARDUINO_USB_CDC_EVENTS, ARDUINO_USB_CDC_LINE_STATE_EVENT, &l, sizeof(arduino_usb_cdc_event_data_t), portMAX_DELAY);
105-
}
106131
}
107132

108133
void USBCDC::end()
@@ -167,8 +192,6 @@ void USBCDC::_onLineState(bool _dtr, bool _rts){
167192
l.line_state.dtr = dtr;
168193
l.line_state.rts = rts;
169194
arduino_usb_event_post(ARDUINO_USB_CDC_EVENTS, ARDUINO_USB_CDC_LINE_STATE_EVENT, &l, sizeof(arduino_usb_cdc_event_data_t), portMAX_DELAY);
170-
} else {
171-
log_d("CDC RESET: itf: %u, dtr: %u, rts: %u, state: %u", itf, dtr, rts, lineState);
172195
}
173196

174197
}
@@ -193,7 +216,6 @@ void USBCDC::_onRX(){
193216
uint32_t count = tud_cdc_n_read(itf, buf, CONFIG_USB_CDC_RX_BUFSIZE);
194217
for(uint32_t i=0; i<count; i++){
195218
if(rx_queue == NULL || !xQueueSend(rx_queue, buf+i, 0)){
196-
log_e("read failed");
197219
return;
198220
}
199221
}
@@ -271,29 +293,7 @@ int USBCDC::availableForWrite(void)
271293
}
272294

273295
size_t USBCDC::write(const uint8_t *buffer, size_t size){
274-
if(itf >= MAX_USB_CDC_DEVICES){
275-
return 0;
276-
}
277-
size_t tosend = size, sofar = 0;
278-
while(tosend){
279-
uint32_t space = tud_cdc_n_write_available(itf);
280-
if(!space){
281-
delay(1);
282-
continue;
283-
}
284-
if(tosend < space){
285-
space = tosend;
286-
}
287-
uint32_t sent = tud_cdc_n_write(itf, buffer + sofar, space);
288-
if(!sent){
289-
log_e("tud_cdc_n_write failed!");
290-
return sofar;
291-
}
292-
sofar += sent;
293-
tosend -= sent;
294-
tud_cdc_n_write_flush(itf);
295-
}
296-
return sofar;
296+
return tinyusb_cdc_write(itf, buffer, size);
297297
}
298298

299299
size_t USBCDC::write(uint8_t c)
@@ -309,6 +309,15 @@ uint32_t USBCDC::baudRate()
309309
return bit_rate;
310310
}
311311

312+
void USBCDC::setDebugOutput(bool en)
313+
{
314+
if(en) {
315+
ets_install_putc1((void (*)(char)) &cdc0_write_char);
316+
} else {
317+
ets_install_putc1(NULL);
318+
}
319+
}
320+
312321
USBCDC::operator bool() const
313322
{
314323
if(itf >= MAX_USB_CDC_DEVICES){

libraries/USB/src/USBCDC.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ class USBCDC: public Stream
9898
return write((uint8_t) n);
9999
}
100100
uint32_t baudRate();
101+
void setDebugOutput(bool);
101102
operator bool() const;
102103

103104
void enableReboot(bool enable);

0 commit comments

Comments
 (0)
0