From 362d012effc634bd8dffa902a1757b9d1080ab29 Mon Sep 17 00:00:00 2001 From: Alex Voinea Date: Thu, 30 Sep 2021 17:59:39 +0200 Subject: [PATCH] USB cdc stdout stream --- lib/Descriptors.h | 2 +- src/main.cpp | 1 + src/modules/usb_cdc.cpp | 12 ++++++++++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/Descriptors.h b/lib/Descriptors.h index bbf506f..b6789d6 100644 --- a/lib/Descriptors.h +++ b/lib/Descriptors.h @@ -55,7 +55,7 @@ #define CDC_NOTIFICATION_EPSIZE 8 /** Size in bytes of the CDC data IN and OUT endpoints. */ -#define CDC_TXRX_EPSIZE 16 +#define CDC_TXRX_EPSIZE 64 /* Type Defines: */ /** Type define for the device configuration descriptor structure. This must be defined in the diff --git a/src/main.cpp b/src/main.cpp index 63f0244..ebd19da 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -10,6 +10,7 @@ #include "pins.h" #include #include +#include #include "modules/buttons.h" #include "modules/finda.h" diff --git a/src/modules/usb_cdc.cpp b/src/modules/usb_cdc.cpp index 913ae66..1b4fd88 100644 --- a/src/modules/usb_cdc.cpp +++ b/src/modules/usb_cdc.cpp @@ -8,6 +8,12 @@ extern "C" { #include "Descriptors.h" #include "lufa/LUFA/Drivers/USB/USB.h" + +/** Standard file stream for the CDC interface when set up, so that the virtual CDC COM port can be + * used like any regular character stream in the C APIs. + */ +static FILE USBSerialStream; + /** LUFA CDC Class driver interface configuration and state information. This structure is * passed to all CDC Class driver functions, so that multiple instances of the same class * within a device can be differentiated from one another. @@ -104,10 +110,16 @@ CDC cdc; void CDC::Init() { USB_Init(); + + /* Create a regular character stream for the interface so that it can be used with the stdio.h functions */ + CDC_Device_CreateStream(&VirtualSerial_CDC_Interface, &USBSerialStream); + stdout = &USBSerialStream; } void CDC::Step() { + /* Must throw away unused bytes from the host, or it will lock up while waiting for the device */ CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface); + CDC_Device_USBTask(&VirtualSerial_CDC_Interface); USB_USBTask(); }