forked from espressif/arduino-esp32
Add debug output to CDC
This commit is contained in:
@ -607,9 +607,6 @@ int uartGetDebug()
|
|||||||
|
|
||||||
int log_printf(const char *format, ...)
|
int log_printf(const char *format, ...)
|
||||||
{
|
{
|
||||||
if(s_uart_debug_nr < 0){
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
static char loc_buf[64];
|
static char loc_buf[64];
|
||||||
char * temp = loc_buf;
|
char * temp = loc_buf;
|
||||||
int len;
|
int len;
|
||||||
@ -627,7 +624,7 @@ int log_printf(const char *format, ...)
|
|||||||
}
|
}
|
||||||
vsnprintf(temp, len+1, format, arg);
|
vsnprintf(temp, len+1, format, arg);
|
||||||
#if !CONFIG_DISABLE_HAL_LOCKS
|
#if !CONFIG_DISABLE_HAL_LOCKS
|
||||||
if(_uart_bus_array[s_uart_debug_nr].lock){
|
if(s_uart_debug_nr != -1 && _uart_bus_array[s_uart_debug_nr].lock){
|
||||||
xSemaphoreTake(_uart_bus_array[s_uart_debug_nr].lock, portMAX_DELAY);
|
xSemaphoreTake(_uart_bus_array[s_uart_debug_nr].lock, portMAX_DELAY);
|
||||||
ets_printf("%s", temp);
|
ets_printf("%s", temp);
|
||||||
xSemaphoreGive(_uart_bus_array[s_uart_debug_nr].lock);
|
xSemaphoreGive(_uart_bus_array[s_uart_debug_nr].lock);
|
||||||
|
@ -64,13 +64,47 @@ void tud_cdc_rx_cb(uint8_t itf)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static size_t tinyusb_cdc_write(uint8_t itf, const uint8_t *buffer, size_t size){
|
||||||
|
if(itf >= MAX_USB_CDC_DEVICES){
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if(!tud_cdc_n_connected(itf)){
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
size_t tosend = size, sofar = 0;
|
||||||
|
while(tosend){
|
||||||
|
uint32_t space = tud_cdc_n_write_available(itf);
|
||||||
|
if(!space){
|
||||||
|
delay(1);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if(tosend < space){
|
||||||
|
space = tosend;
|
||||||
|
}
|
||||||
|
uint32_t sent = tud_cdc_n_write(itf, buffer + sofar, space);
|
||||||
|
if(!sent){
|
||||||
|
return sofar;
|
||||||
|
}
|
||||||
|
sofar += sent;
|
||||||
|
tosend -= sent;
|
||||||
|
tud_cdc_n_write_flush(itf);
|
||||||
|
}
|
||||||
|
return sofar;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void ARDUINO_ISR_ATTR cdc0_write_char(char c)
|
||||||
|
{
|
||||||
|
tinyusb_cdc_write(0, (const uint8_t *)&c, 1);
|
||||||
|
}
|
||||||
|
|
||||||
//void tud_cdc_rx_wanted_cb(uint8_t itf, char wanted_char);
|
//void tud_cdc_rx_wanted_cb(uint8_t itf, char wanted_char);
|
||||||
|
|
||||||
static void usb_unplugged_cb(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data){
|
static void usb_unplugged_cb(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data){
|
||||||
((USBCDC*)arg)->_onUnplugged();
|
((USBCDC*)arg)->_onUnplugged();
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
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) {
|
||||||
tinyusb_enable_interface(USB_INTERFACE_CDC, TUD_CDC_DESC_LEN, load_cdc_descriptor);
|
tinyusb_enable_interface(USB_INTERFACE_CDC, TUD_CDC_DESC_LEN, load_cdc_descriptor);
|
||||||
if(itf < MAX_USB_CDC_DEVICES){
|
if(itf < MAX_USB_CDC_DEVICES){
|
||||||
devices[itf] = this;
|
devices[itf] = this;
|
||||||
@ -94,15 +128,6 @@ void USBCDC::begin(size_t rx_queue_len)
|
|||||||
if(!rx_queue){
|
if(!rx_queue){
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if(connected){
|
|
||||||
arduino_usb_cdc_event_data_t p = {0};
|
|
||||||
arduino_usb_event_post(ARDUINO_USB_CDC_EVENTS, ARDUINO_USB_CDC_CONNECTED_EVENT, &p, sizeof(arduino_usb_cdc_event_data_t), portMAX_DELAY);
|
|
||||||
|
|
||||||
arduino_usb_cdc_event_data_t l = {0};
|
|
||||||
l.line_state.dtr = true;
|
|
||||||
l.line_state.rts = true;
|
|
||||||
arduino_usb_event_post(ARDUINO_USB_CDC_EVENTS, ARDUINO_USB_CDC_LINE_STATE_EVENT, &l, sizeof(arduino_usb_cdc_event_data_t), portMAX_DELAY);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void USBCDC::end()
|
void USBCDC::end()
|
||||||
@ -167,8 +192,6 @@ void USBCDC::_onLineState(bool _dtr, bool _rts){
|
|||||||
l.line_state.dtr = dtr;
|
l.line_state.dtr = dtr;
|
||||||
l.line_state.rts = rts;
|
l.line_state.rts = rts;
|
||||||
arduino_usb_event_post(ARDUINO_USB_CDC_EVENTS, ARDUINO_USB_CDC_LINE_STATE_EVENT, &l, sizeof(arduino_usb_cdc_event_data_t), portMAX_DELAY);
|
arduino_usb_event_post(ARDUINO_USB_CDC_EVENTS, ARDUINO_USB_CDC_LINE_STATE_EVENT, &l, sizeof(arduino_usb_cdc_event_data_t), portMAX_DELAY);
|
||||||
} else {
|
|
||||||
log_d("CDC RESET: itf: %u, dtr: %u, rts: %u, state: %u", itf, dtr, rts, lineState);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -193,7 +216,6 @@ void USBCDC::_onRX(){
|
|||||||
uint32_t count = tud_cdc_n_read(itf, buf, CONFIG_USB_CDC_RX_BUFSIZE);
|
uint32_t count = tud_cdc_n_read(itf, buf, CONFIG_USB_CDC_RX_BUFSIZE);
|
||||||
for(uint32_t i=0; i<count; i++){
|
for(uint32_t i=0; i<count; i++){
|
||||||
if(rx_queue == NULL || !xQueueSend(rx_queue, buf+i, 0)){
|
if(rx_queue == NULL || !xQueueSend(rx_queue, buf+i, 0)){
|
||||||
log_e("read failed");
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -271,29 +293,7 @@ int USBCDC::availableForWrite(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
size_t USBCDC::write(const uint8_t *buffer, size_t size){
|
size_t USBCDC::write(const uint8_t *buffer, size_t size){
|
||||||
if(itf >= MAX_USB_CDC_DEVICES){
|
return tinyusb_cdc_write(itf, buffer, size);
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
size_t tosend = size, sofar = 0;
|
|
||||||
while(tosend){
|
|
||||||
uint32_t space = tud_cdc_n_write_available(itf);
|
|
||||||
if(!space){
|
|
||||||
delay(1);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if(tosend < space){
|
|
||||||
space = tosend;
|
|
||||||
}
|
|
||||||
uint32_t sent = tud_cdc_n_write(itf, buffer + sofar, space);
|
|
||||||
if(!sent){
|
|
||||||
log_e("tud_cdc_n_write failed!");
|
|
||||||
return sofar;
|
|
||||||
}
|
|
||||||
sofar += sent;
|
|
||||||
tosend -= sent;
|
|
||||||
tud_cdc_n_write_flush(itf);
|
|
||||||
}
|
|
||||||
return sofar;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t USBCDC::write(uint8_t c)
|
size_t USBCDC::write(uint8_t c)
|
||||||
@ -309,6 +309,15 @@ uint32_t USBCDC::baudRate()
|
|||||||
return bit_rate;
|
return bit_rate;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void USBCDC::setDebugOutput(bool en)
|
||||||
|
{
|
||||||
|
if(en) {
|
||||||
|
ets_install_putc1((void (*)(char)) &cdc0_write_char);
|
||||||
|
} else {
|
||||||
|
ets_install_putc1(NULL);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
USBCDC::operator bool() const
|
USBCDC::operator bool() const
|
||||||
{
|
{
|
||||||
if(itf >= MAX_USB_CDC_DEVICES){
|
if(itf >= MAX_USB_CDC_DEVICES){
|
||||||
|
@ -98,6 +98,7 @@ public:
|
|||||||
return write((uint8_t) n);
|
return write((uint8_t) n);
|
||||||
}
|
}
|
||||||
uint32_t baudRate();
|
uint32_t baudRate();
|
||||||
|
void setDebugOutput(bool);
|
||||||
operator bool() const;
|
operator bool() const;
|
||||||
|
|
||||||
void enableReboot(bool enable);
|
void enableReboot(bool enable);
|
||||||
|
Reference in New Issue
Block a user