X-Git-Url: http://git.linex4red.de/pub/USBasp.git/blobdiff_plain/33a018474913701fa9ef8e962acf58accd1184d2..b9dd51cd632c1a8cff18f77b4f09c33bdf804768:/Demos/Device/LowLevel/CDC/CDC.c diff --git a/Demos/Device/LowLevel/CDC/CDC.c b/Demos/Device/LowLevel/CDC/CDC.c index 3db4c1e55..e63257dbd 100644 --- a/Demos/Device/LowLevel/CDC/CDC.c +++ b/Demos/Device/LowLevel/CDC/CDC.c @@ -50,6 +50,58 @@ CDC_Line_Coding_t LineCoding = { .BaudRateBPS = 9600, .ParityType = Parity_None, .DataBits = 8 }; +#if 0 +/* NOTE: Here you can set up a standard stream using the created virtual serial port, so that the standard stream functions in + * can be used on the virtual serial port (e.g. fprintf(&USBSerial, "Test"); to print a string). + */ + +static int CDC_putchar (char c, FILE *stream) +{ + Endpoint_SelectEndpoint(CDC_TX_EPNUM); + + while (!(Endpoint_IsReadWriteAllowed())) + { + if (USB_DeviceState != DEVICE_STATE_Configured) + return -1; + } + + Endpoint_Write_Byte(c); + Endpoint_ClearIN(); + + return 0; +} + +static int CDC_getchar (FILE *stream) +{ + int c; + + Endpoint_SelectEndpoint(CDC_RX_EPNUM); + + for (;;) + { + while (!(Endpoint_IsReadWriteAllowed())) + { + if (USB_DeviceState != DEVICE_STATE_Configured) + return -1; + } + + if (!(Endpoint_BytesInEndpoint())) + { + Endpoint_ClearOUT(); + } + else + { + c = Endpoint_Read_Byte(); + break; + } + } + + return c; +} + +static FILE USBSerial = FDEV_SETUP_STREAM(CDC_putchar, CDC_getchar, _FDEV_SETUP_RW); +#endif + /** Main program entry point. This routine contains the overall program flow, including initial * setup of all components and the main program loop. */ @@ -105,21 +157,30 @@ void EVENT_USB_Disconnect(void) */ void EVENT_USB_ConfigurationChanged(void) { - /* Setup CDC Notification, Rx and Tx Endpoints */ - Endpoint_ConfigureEndpoint(CDC_NOTIFICATION_EPNUM, EP_TYPE_INTERRUPT, - ENDPOINT_DIR_IN, CDC_NOTIFICATION_EPSIZE, - ENDPOINT_BANK_SINGLE); - - Endpoint_ConfigureEndpoint(CDC_TX_EPNUM, EP_TYPE_BULK, - ENDPOINT_DIR_IN, CDC_TXRX_EPSIZE, - ENDPOINT_BANK_SINGLE); - - Endpoint_ConfigureEndpoint(CDC_RX_EPNUM, EP_TYPE_BULK, - ENDPOINT_DIR_OUT, CDC_TXRX_EPSIZE, - ENDPOINT_BANK_SINGLE); - /* Indicate USB connected and ready */ LEDs_SetAllLEDs(LEDMASK_USB_READY); + + /* Setup CDC Notification, Rx and Tx Endpoints */ + if (!(Endpoint_ConfigureEndpoint(CDC_NOTIFICATION_EPNUM, EP_TYPE_INTERRUPT, + ENDPOINT_DIR_IN, CDC_NOTIFICATION_EPSIZE, + ENDPOINT_BANK_SINGLE))) + { + LEDs_SetAllLEDs(LEDMASK_USB_ERROR); + } + + if (!(Endpoint_ConfigureEndpoint(CDC_TX_EPNUM, EP_TYPE_BULK, + ENDPOINT_DIR_IN, CDC_TXRX_EPSIZE, + ENDPOINT_BANK_SINGLE))) + { + LEDs_SetAllLEDs(LEDMASK_USB_ERROR); + } + + if (!(Endpoint_ConfigureEndpoint(CDC_RX_EPNUM, EP_TYPE_BULK, + ENDPOINT_DIR_OUT, CDC_TXRX_EPSIZE, + ENDPOINT_BANK_SINGLE))) + { + LEDs_SetAllLEDs(LEDMASK_USB_ERROR); + } } /** Event handler for the USB_UnhandledControlPacket event. This is used to catch standard and class specific @@ -172,9 +233,7 @@ void EVENT_USB_UnhandledControlPacket(void) CONTROL_LINE_OUT_* masks to determine the RTS and DTR line states using the following code: */ - /* Acknowledge status stage */ - while (!(Endpoint_IsINReady())); - Endpoint_ClearIN(); + Endpoint_ClearStatusStage(); } break; @@ -187,20 +246,23 @@ void CDC_Task(void) char* ReportString = NULL; uint8_t JoyStatus_LCL = Joystick_GetStatus(); 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", - }; + char* JoystickStrings[] = + { + "Joystick Up\r\n", + "Joystick Down\r\n", + "Joystick Left\r\n", + "Joystick Right\r\n", + "Joystick Pressed\r\n", + }; + /* Device must be connected and configured for the task to run */ + if (USB_DeviceState != DEVICE_STATE_Configured) + return; + #if 0 /* NOTE: Here you can use the notification endpoint to send back line state changes to the host, for the special RS-232 - handshake signal lines (and some error states), via the CONTROL_LINE_IN_* masks and the following code: - */ + * handshake signal lines (and some error states), via the CONTROL_LINE_IN_* masks and the following code: + */ USB_Notification_Header_t Notification = (USB_Notification_Header_t) { .NotificationType = (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE), @@ -258,7 +320,11 @@ void CDC_Task(void) if (IsFull) { /* Wait until the endpoint is ready for another packet */ - while (!(Endpoint_IsINReady())); + while (!(Endpoint_IsINReady())) + { + if (USB_DeviceState == DEVICE_STATE_Unattached) + return; + } /* Send an empty packet to ensure that the host does not buffer data sent to it */ Endpoint_ClearIN();