X-Git-Url: http://git.linex4red.de/pub/USBasp.git/blobdiff_plain/b9b03aadb219d06fbad9d110e508db93e45461af..8c6c27d88bb40ecf55f369fc4499ec990d2d93d2:/Demos/Device/ClassDriver/CDC/CDC.c diff --git a/Demos/Device/ClassDriver/CDC/CDC.c b/Demos/Device/ClassDriver/CDC/CDC.c index 6f1539334..8940ae042 100644 --- a/Demos/Device/ClassDriver/CDC/CDC.c +++ b/Demos/Device/ClassDriver/CDC/CDC.c @@ -40,20 +40,31 @@ * passed to all CDC Class driver functions, so that multiple instances of the same class * within a device can be differentiated from one another. */ -USB_ClassInfo_CDC_t VirtualSerial_CDC_Interface = +USB_ClassInfo_CDC_Device_t VirtualSerial_CDC_Interface = { - .ControlInterfaceNumber = 0, - - .DataINEndpointNumber = CDC_TX_EPNUM, - .DataINEndpointSize = CDC_TXRX_EPSIZE, - - .DataOUTEndpointNumber = CDC_RX_EPNUM, - .DataOUTEndpointSize = CDC_TXRX_EPSIZE, - - .NotificationEndpointNumber = CDC_NOTIFICATION_EPNUM, - .NotificationEndpointSize = CDC_NOTIFICATION_EPSIZE, + .Config = + { + .ControlInterfaceNumber = 0, + + .DataINEndpointNumber = CDC_TX_EPNUM, + .DataINEndpointSize = CDC_TXRX_EPSIZE, + .DataINEndpointDoubleBank = false, + + .DataOUTEndpointNumber = CDC_RX_EPNUM, + .DataOUTEndpointSize = CDC_TXRX_EPSIZE, + .DataOUTEndpointDoubleBank = false, + + .NotificationEndpointNumber = CDC_NOTIFICATION_EPNUM, + .NotificationEndpointSize = CDC_NOTIFICATION_EPSIZE, + .NotificationEndpointDoubleBank = false, + }, }; +/** 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; + /** Main program entry point. This routine contains the overall program flow, including initial * setup of all components and the main program loop. */ @@ -61,17 +72,20 @@ int main(void) { SetupHardware(); + /* 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); + LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY); for (;;) { CheckJoystickMovement(); - - uint16_t BytesToDiscard = USB_CDC_BytesReceived(&VirtualSerial_CDC_Interface); - while (BytesToDiscard--) - USB_CDC_ReceiveByte(&VirtualSerial_CDC_Interface); + + /* Must throw away unused bytes from the host, or it will lock up while waiting for the device */ + while (CDC_Device_BytesReceived(&VirtualSerial_CDC_Interface)) + CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface); - USB_CDC_USBTask(&VirtualSerial_CDC_Interface); + CDC_Device_USBTask(&VirtualSerial_CDC_Interface); USB_USBTask(); } } @@ -99,59 +113,54 @@ void CheckJoystickMovement(void) char* ReportString = NULL; static bool ActionSent = false; - char* JoystickStrings[] = - { - "Joystick Up\r\n", - "Joystick Down\r\n", - "Joystick Left\r\n", - "Joystick Right\r\n", - "Joystick Pressed\r\n", - }; - if (JoyStatus_LCL & JOY_UP) - ReportString = JoystickStrings[0]; + ReportString = "Joystick Up\r\n"; else if (JoyStatus_LCL & JOY_DOWN) - ReportString = JoystickStrings[1]; + ReportString = "Joystick Down\r\n"; else if (JoyStatus_LCL & JOY_LEFT) - ReportString = JoystickStrings[2]; + ReportString = "Joystick Left\r\n"; else if (JoyStatus_LCL & JOY_RIGHT) - ReportString = JoystickStrings[3]; + ReportString = "Joystick Right\r\n"; else if (JoyStatus_LCL & JOY_PRESS) - ReportString = JoystickStrings[4]; + ReportString = "Joystick Pressed\r\n"; else ActionSent = false; if ((ReportString != NULL) && (ActionSent == false)) { ActionSent = true; - - USB_CDC_SendString(&VirtualSerial_CDC_Interface, ReportString, strlen(ReportString)); + + /* Write the string to the virtual COM port via the created character stream */ + fputs(ReportString, &USBSerialStream); + + /* Alternatively, without the stream: */ + // CDC_Device_SendString(&VirtualSerial_CDC_Interface, ReportString, strlen(ReportString)); } } /** Event handler for the library USB Connection event. */ -void EVENT_USB_Connect(void) +void EVENT_USB_Device_Connect(void) { LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING); } /** Event handler for the library USB Disconnection event. */ -void EVENT_USB_Disconnect(void) +void EVENT_USB_Device_Disconnect(void) { LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY); } /** Event handler for the library USB Configuration Changed event. */ -void EVENT_USB_ConfigurationChanged(void) +void EVENT_USB_Device_ConfigurationChanged(void) { LEDs_SetAllLEDs(LEDMASK_USB_READY); - if (!(USB_CDC_ConfigureEndpoints(&VirtualSerial_CDC_Interface))) + if (!(CDC_Device_ConfigureEndpoints(&VirtualSerial_CDC_Interface))) LEDs_SetAllLEDs(LEDMASK_USB_ERROR); } -/** Event handler for the library USB Unhandled Control Packet event. */ -void EVENT_USB_UnhandledControlPacket(void) +/** Event handler for the library USB Unhandled Control Request event. */ +void EVENT_USB_Device_UnhandledControlRequest(void) { - USB_CDC_ProcessControlPacket(&VirtualSerial_CDC_Interface); + CDC_Device_ProcessControlRequest(&VirtualSerial_CDC_Interface); }