Added CDC functional descriptor structs to the Low Level CDC demos and CDC class...
[pub/USBasp.git] / Bootloaders / CDC / BootloaderCDC.c
index cfc040f..234b5ce 100644 (file)
 #define  INCLUDE_FROM_BOOTLOADERCDC_C
 #include "BootloaderCDC.h"
 
+/** Contains the current baud rate and other settings of the first virtual serial port. This must be retained as some
+ *  operating systems will not open the port unless the settings can be set successfully.
+ */
+CDC_Line_Coding_t LineEncoding = { .BaudRateBPS = 0,
+                                   .CharFormat  = OneStopBit,
+                                   .ParityType  = Parity_None,
+                                   .DataBits    = 8            };
+
 /** Current address counter. This stores the current address of the FLASH or EEPROM as set by the host,
  *  and is used when reading or writing to the AVRs memory (either FLASH or EEPROM depending on the issued
  *  command.)
@@ -113,6 +121,40 @@ void EVENT_USB_Device_ConfigurationChanged(void)
                                   ENDPOINT_BANK_SINGLE);
 }
 
+/** Event handler for the USB_UnhandledControlRequest event. This is used to catch standard and class specific
+ *  control requests that are not handled internally by the USB library (including the CDC control commands,
+ *  which are all issued via the control endpoint), so that they can be handled appropriately for the application.
+ */
+void EVENT_USB_Device_UnhandledControlRequest(void)
+{
+       /* Process CDC specific control requests */
+       switch (USB_ControlRequest.bRequest)
+       {
+               case REQ_GetLineEncoding:
+                       if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
+                       {       
+                               Endpoint_ClearSETUP();
+
+                               /* Write the line coding data to the control endpoint */
+                               Endpoint_Write_Control_Stream_LE(&LineEncoding, sizeof(CDC_Line_Coding_t));
+                               Endpoint_ClearOUT();
+                       }
+                       
+                       break;
+               case REQ_SetLineEncoding:
+                       if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
+                       {
+                               Endpoint_ClearSETUP();
+
+                               /* Read the line coding data in from the host into the global struct */
+                               Endpoint_Read_Control_Stream_LE(&LineEncoding, sizeof(CDC_Line_Coding_t));
+                               Endpoint_ClearIN();
+                       }
+       
+                       break;
+       }
+}
+
 /** Reads or writes a block of EEPROM or FLASH memory to or from the appropriate CDC data endpoint, depending
  *  on the AVR910 protocol command issued.
  *