Added USB Virtual Serial support

master
Jason Green 2016-08-01 05:02:52 +00:00
parent d8c5041f0a
commit 80d10bef07
6 changed files with 276 additions and 4 deletions

View File

@ -190,6 +190,10 @@ ifeq ($(strip $(MIDI_ENABLE)), yes)
SRC += $(QUANTUM_DIR)/process_keycode/process_midi.c SRC += $(QUANTUM_DIR)/process_keycode/process_midi.c
endif endif
ifeq ($(strip $(VIRTSER_ENABLE)), yes)
OPT_DEFS += -DVIRTSER_ENABLE
endif
ifeq ($(strip $(AUDIO_ENABLE)), yes) ifeq ($(strip $(AUDIO_ENABLE)), yes)
OPT_DEFS += -DAUDIO_ENABLE OPT_DEFS += -DAUDIO_ENABLE
SRC += $(QUANTUM_DIR)/process_keycode/process_music.c SRC += $(QUANTUM_DIR)/process_keycode/process_music.c

View File

@ -0,0 +1,10 @@
#ifndef _VIRTSER_H_
#define _VIRTSER_H_
/* Define this function in your code to process incoming bytes */
void virtser_recv(const uint8_t ch);
/* Call this to send a character over the Virtual Serial Device */
void virtser_send(const uint8_t byte);
#endif

View File

@ -26,6 +26,10 @@ ifeq ($(strip $(BLUETOOTH_ENABLE)), yes)
$(TMK_DIR)/protocol/serial_uart.c $(TMK_DIR)/protocol/serial_uart.c
endif endif
ifeq ($(strip $(VIRTSER_ENABLE)), yes)
LUFA_SRC += $(LUFA_ROOT_PATH)/Drivers/USB/Class/Device/CDCClassDevice.c
endif
SRC += $(LUFA_SRC) SRC += $(LUFA_SRC)
# Search Path # Search Path

View File

@ -231,9 +231,15 @@ const USB_Descriptor_Device_t PROGMEM DeviceDescriptor =
.Header = {.Size = sizeof(USB_Descriptor_Device_t), .Type = DTYPE_Device}, .Header = {.Size = sizeof(USB_Descriptor_Device_t), .Type = DTYPE_Device},
.USBSpecification = VERSION_BCD(1,1,0), .USBSpecification = VERSION_BCD(1,1,0),
#if VIRTSER_ENABLE
.Class = USB_CSCP_IADDeviceClass,
.SubClass = USB_CSCP_IADDeviceSubclass,
.Protocol = USB_CSCP_IADDeviceProtocol,
#else
.Class = USB_CSCP_NoDeviceClass, .Class = USB_CSCP_NoDeviceClass,
.SubClass = USB_CSCP_NoDeviceSubclass, .SubClass = USB_CSCP_NoDeviceSubclass,
.Protocol = USB_CSCP_NoDeviceProtocol, .Protocol = USB_CSCP_NoDeviceProtocol,
#endif
.Endpoint0Size = FIXED_CONTROL_ENDPOINT_SIZE, .Endpoint0Size = FIXED_CONTROL_ENDPOINT_SIZE,
@ -643,8 +649,112 @@ const USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor =
.TotalEmbeddedJacks = 0x01, .TotalEmbeddedJacks = 0x01,
.AssociatedJackID = {0x03} .AssociatedJackID = {0x03}
} },
#endif #endif
#ifdef VIRTSER_ENABLE
.CDC_Interface_Association =
{
.Header = {.Size = sizeof(USB_Descriptor_Interface_Association_t), .Type = DTYPE_InterfaceAssociation},
.FirstInterfaceIndex = CCI_INTERFACE,
.TotalInterfaces = 2,
.Class = CDC_CSCP_CDCClass,
.SubClass = CDC_CSCP_ACMSubclass,
.Protocol = CDC_CSCP_ATCommandProtocol,
.IADStrIndex = NO_DESCRIPTOR,
},
.CDC_CCI_Interface =
{
.Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface},
.InterfaceNumber = CCI_INTERFACE,
.AlternateSetting = 0,
.TotalEndpoints = 1,
.Class = CDC_CSCP_CDCClass,
.SubClass = CDC_CSCP_ACMSubclass,
.Protocol = CDC_CSCP_ATCommandProtocol,
.InterfaceStrIndex = NO_DESCRIPTOR
},
.CDC_Functional_Header =
{
.Header = {.Size = sizeof(USB_CDC_Descriptor_FunctionalHeader_t), .Type = DTYPE_CSInterface},
.Subtype = 0x00,
.CDCSpecification = VERSION_BCD(1,1,0),
},
.CDC_Functional_ACM =
{
.Header = {.Size = sizeof(USB_CDC_Descriptor_FunctionalACM_t), .Type = DTYPE_CSInterface},
.Subtype = 0x02,
.Capabilities = 0x02,
},
.CDC_Functional_Union =
{
.Header = {.Size = sizeof(USB_CDC_Descriptor_FunctionalUnion_t), .Type = DTYPE_CSInterface},
.Subtype = 0x06,
.MasterInterfaceNumber = CCI_INTERFACE,
.SlaveInterfaceNumber = CDI_INTERFACE,
},
.CDC_NotificationEndpoint =
{
.Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
.EndpointAddress = CDC_NOTIFICATION_EPADDR,
.Attributes = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
.EndpointSize = CDC_NOTIFICATION_EPSIZE,
.PollingIntervalMS = 0xFF
},
.CDC_DCI_Interface =
{
.Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface},
.InterfaceNumber = CDI_INTERFACE,
.AlternateSetting = 0,
.TotalEndpoints = 2,
.Class = CDC_CSCP_CDCDataClass,
.SubClass = CDC_CSCP_NoDataSubclass,
.Protocol = CDC_CSCP_NoDataProtocol,
.InterfaceStrIndex = NO_DESCRIPTOR
},
.CDC_DataOutEndpoint =
{
.Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
.EndpointAddress = CDC_OUT_EPADDR,
.Attributes = (EP_TYPE_BULK | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
.EndpointSize = CDC_EPSIZE,
.PollingIntervalMS = 0x05
},
.CDC_DataInEndpoint =
{
.Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
.EndpointAddress = CDC_IN_EPADDR,
.Attributes = (EP_TYPE_BULK | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
.EndpointSize = CDC_EPSIZE,
.PollingIntervalMS = 0x05
},
#endif
}; };

View File

@ -104,6 +104,21 @@ typedef struct
USB_MIDI_Descriptor_Jack_Endpoint_t MIDI_Out_Jack_Endpoint_SPC; USB_MIDI_Descriptor_Jack_Endpoint_t MIDI_Out_Jack_Endpoint_SPC;
#endif #endif
#ifdef VIRTSER_ENABLE
USB_Descriptor_Interface_Association_t CDC_Interface_Association;
// CDC Control Interface
USB_Descriptor_Interface_t CDC_CCI_Interface;
USB_CDC_Descriptor_FunctionalHeader_t CDC_Functional_Header;
USB_CDC_Descriptor_FunctionalACM_t CDC_Functional_ACM;
USB_CDC_Descriptor_FunctionalUnion_t CDC_Functional_Union;
USB_Descriptor_Endpoint_t CDC_NotificationEndpoint;
// CDC Data Interface
USB_Descriptor_Interface_t CDC_DCI_Interface;
USB_Descriptor_Endpoint_t CDC_DataOutEndpoint;
USB_Descriptor_Endpoint_t CDC_DataInEndpoint;
#endif
} USB_Descriptor_Configuration_t; } USB_Descriptor_Configuration_t;
@ -141,8 +156,15 @@ typedef struct
# define AS_INTERFACE NKRO_INTERFACE # define AS_INTERFACE NKRO_INTERFACE
#endif #endif
#ifdef VIRTSER_ENABLE
# define CCI_INTERFACE (AS_INTERFACE + 1)
# define CDI_INTERFACE (AS_INTERFACE + 2)
#else
# define CDI_INTERFACE AS_INTERFACE
#endif
/* nubmer of interfaces */ /* nubmer of interfaces */
#define TOTAL_INTERFACES AS_INTERFACE + 1 #define TOTAL_INTERFACES (CDI_INTERFACE + 1)
// Endopoint number and size // Endopoint number and size
@ -180,11 +202,24 @@ typedef struct
# define MIDI_STREAM_OUT_EPNUM (NKRO_IN_EPNUM + 2) # define MIDI_STREAM_OUT_EPNUM (NKRO_IN_EPNUM + 2)
# define MIDI_STREAM_IN_EPADDR (ENDPOINT_DIR_IN | MIDI_STREAM_IN_EPNUM) # define MIDI_STREAM_IN_EPADDR (ENDPOINT_DIR_IN | MIDI_STREAM_IN_EPNUM)
# define MIDI_STREAM_OUT_EPADDR (ENDPOINT_DIR_OUT | MIDI_STREAM_OUT_EPNUM) # define MIDI_STREAM_OUT_EPADDR (ENDPOINT_DIR_OUT | MIDI_STREAM_OUT_EPNUM)
#else
# define MIDI_STREAM_OUT_EPNUM NKRO_IN_EPNUM
#endif
#ifdef VIRTSER_ENABLE
# define CDC_NOTIFICATION_EPNUM (MIDI_STREAM_OUT_EPNUM + 1)
# define CDC_IN_EPNUM (MIDI_STREAM_OUT_EPNUM + 2)
# define CDC_OUT_EPNUM (MIDI_STREAM_OUT_EPNUM + 3)
# define CDC_NOTIFICATION_EPADDR (ENDPOINT_DIR_IN | CDC_NOTIFICATION_EPNUM)
# define CDC_IN_EPADDR (ENDPOINT_DIR_IN | CDC_IN_EPNUM)
# define CDC_OUT_EPADDR (ENDPOINT_DIR_OUT | CDC_OUT_EPNUM)
#else
# define CDC_OUT_EPNUM MIDI_STREAM_OUT_EPNUM
#endif #endif
#if defined(__AVR_ATmega32U2__) && MIDI_STREAM_OUT_EPADDR > 4 #if defined(__AVR_ATmega32U2__) && CDC_OUT_EPNUM > 4
# error "Endpoints are not available enough to support all functions. Remove some in Makefile.(MOUSEKEY, EXTRAKEY, CONSOLE, NKRO, MIDI)" # error "Endpoints are not available enough to support all functions. Remove some in Makefile.(MOUSEKEY, EXTRAKEY, CONSOLE, NKRO, MIDI, SERIAL)"
#endif #endif
#define KEYBOARD_EPSIZE 8 #define KEYBOARD_EPSIZE 8
@ -193,6 +228,8 @@ typedef struct
#define CONSOLE_EPSIZE 32 #define CONSOLE_EPSIZE 32
#define NKRO_EPSIZE 16 #define NKRO_EPSIZE 16
#define MIDI_STREAM_EPSIZE 64 #define MIDI_STREAM_EPSIZE 64
#define CDC_NOTIFICATION_EPSIZE 8
#define CDC_EPSIZE 16
uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue, uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue,

View File

@ -60,6 +60,10 @@
#include "bluetooth.h" #include "bluetooth.h"
#endif #endif
#ifdef VIRTSER_ENABLE
#include "virtser.h"
#endif
uint8_t keyboard_idle = 0; uint8_t keyboard_idle = 0;
/* 0: Boot Protocol, 1: Report Protocol(default) */ /* 0: Boot Protocol, 1: Report Protocol(default) */
uint8_t keyboard_protocol = 1; uint8_t keyboard_protocol = 1;
@ -127,6 +131,34 @@ USB_ClassInfo_MIDI_Device_t USB_MIDI_Interface =
#define SYS_COMMON_3 0x30 #define SYS_COMMON_3 0x30
#endif #endif
#ifdef VIRTSER_ENABLE
USB_ClassInfo_CDC_Device_t cdc_device =
{
.Config =
{
.ControlInterfaceNumber = CCI_INTERFACE,
.DataINEndpoint =
{
.Address = CDC_IN_EPADDR,
.Size = CDC_EPSIZE,
.Banks = 1,
},
.DataOUTEndpoint =
{
.Address = CDC_OUT_EPADDR,
.Size = CDC_EPSIZE,
.Banks = 1,
},
.NotificationEndpoint =
{
.Address = CDC_NOTIFICATION_EPADDR,
.Size = CDC_NOTIFICATION_EPSIZE,
.Banks = 1,
},
},
};
#endif
/******************************************************************************* /*******************************************************************************
* Console * Console
@ -311,6 +343,12 @@ void EVENT_USB_Device_ConfigurationChanged(void)
ConfigSuccess &= Endpoint_ConfigureEndpoint(MIDI_STREAM_IN_EPADDR, EP_TYPE_BULK, MIDI_STREAM_EPSIZE, ENDPOINT_BANK_SINGLE); ConfigSuccess &= Endpoint_ConfigureEndpoint(MIDI_STREAM_IN_EPADDR, EP_TYPE_BULK, MIDI_STREAM_EPSIZE, ENDPOINT_BANK_SINGLE);
ConfigSuccess &= Endpoint_ConfigureEndpoint(MIDI_STREAM_OUT_EPADDR, EP_TYPE_BULK, MIDI_STREAM_EPSIZE, ENDPOINT_BANK_SINGLE); ConfigSuccess &= Endpoint_ConfigureEndpoint(MIDI_STREAM_OUT_EPADDR, EP_TYPE_BULK, MIDI_STREAM_EPSIZE, ENDPOINT_BANK_SINGLE);
#endif #endif
#ifdef VIRTSER_ENABLE
ConfigSuccess &= Endpoint_ConfigureEndpoint(CDC_NOTIFICATION_EPADDR, EP_TYPE_INTERRUPT, CDC_NOTIFICATION_EPSIZE, ENDPOINT_BANK_SINGLE);
ConfigSuccess &= Endpoint_ConfigureEndpoint(CDC_OUT_EPADDR, EP_TYPE_BULK, CDC_EPSIZE, ENDPOINT_BANK_SINGLE);
ConfigSuccess &= Endpoint_ConfigureEndpoint(CDC_IN_EPADDR, EP_TYPE_BULK, CDC_EPSIZE, ENDPOINT_BANK_SINGLE);
#endif
} }
/* /*
@ -432,10 +470,15 @@ void EVENT_USB_Device_ControlRequest(void)
break; break;
} }
#ifdef VIRTSER_ENABLE
CDC_Device_ProcessControlRequest(&cdc_device);
#endif
} }
/******************************************************************************* /*******************************************************************************
* Host driver * Host driver
p
******************************************************************************/ ******************************************************************************/
static uint8_t keyboard_leds(void) static uint8_t keyboard_leds(void)
{ {
@ -827,6 +870,61 @@ void MIDI_Task(void)
#endif #endif
/*******************************************************************************
* VIRTUAL SERIAL
******************************************************************************/
#ifdef VIRTSER_ENABLE
void virtser_init(void)
{
cdc_device.State.ControlLineStates.DeviceToHost = CDC_CONTROL_LINE_IN_DSR ;
CDC_Device_SendControlLineStateChange(&cdc_device);
}
__attribute__ ((weak))
void virtser_recv(uint8_t c)
{
// Ignore by default
}
void virtser_task(void)
{
uint16_t count = CDC_Device_BytesReceived(&cdc_device);
uint8_t ch;
if (count)
{
ch = CDC_Device_ReceiveByte(&cdc_device);
virtser_recv(ch);
}
}
void virtser_send(const uint8_t byte)
{
uint8_t timeout = 255;
uint8_t ep = Endpoint_GetCurrentEndpoint();
if (cdc_device.State.ControlLineStates.HostToDevice & CDC_CONTROL_LINE_OUT_DTR)
{
/* IN packet */
Endpoint_SelectEndpoint(cdc_device.Config.DataINEndpoint.Address);
if (!Endpoint_IsEnabled() || !Endpoint_IsConfigured()) {
Endpoint_SelectEndpoint(ep);
return;
}
while (timeout-- && !Endpoint_IsReadWriteAllowed()) _delay_us(40);
Endpoint_Write_8(byte);
CDC_Device_Flush(&cdc_device);
if (Endpoint_IsINReady()) {
Endpoint_ClearIN();
}
Endpoint_SelectEndpoint(ep);
}
}
#endif
/******************************************************************************* /*******************************************************************************
* main * main
@ -918,6 +1016,10 @@ int main(void)
sleep_led_init(); sleep_led_init();
#endif #endif
#ifdef VIRTSER_ENABLE
virtser_init();
#endif
print("Keyboard start.\n"); print("Keyboard start.\n");
while (1) { while (1) {
#ifndef BLUETOOTH_ENABLE #ifndef BLUETOOTH_ENABLE
@ -936,6 +1038,11 @@ int main(void)
#endif #endif
keyboard_task(); keyboard_task();
#ifdef VIRTSER_ENABLE
virtser_task();
CDC_Device_USBTask(&cdc_device);
#endif
#if !defined(INTERRUPT_CONTROL_ENDPOINT) #if !defined(INTERRUPT_CONTROL_ENDPOINT)
USB_USBTask(); USB_USBTask();
#endif #endif