Minor documentation enhancements.
[pub/USBasp.git] / Bootloaders / CDC / BootloaderCDC.c
index 0b2eb81..234b5ce 100644 (file)
 #define  INCLUDE_FROM_BOOTLOADERCDC_C
 #include "BootloaderCDC.h"
 
-/** Line coding options for the virtual serial port. Although the virtual serial port data is never
- *  sent through a physical serial port, the line encoding data must still be read and preserved from
- *  the host, or the host will detect a problem and fail to open the port. This structure contains the
- *  current encoding options, including baud rate, character format, parity mode and total number of 
- *  bits in each data chunk.
+/** 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 LineCoding = { .BaudRateBPS = 9600,
-                                 .CharFormat  = OneStopBit,
-                                 .ParityType  = Parity_None,
-                                 .DataBits    = 8            };
+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
@@ -112,40 +109,35 @@ void EVENT_USB_Device_ConfigurationChanged(void)
 {
        /* Setup CDC Notification, Rx and Tx Endpoints */
        Endpoint_ConfigureEndpoint(CDC_NOTIFICATION_EPNUM, EP_TYPE_INTERRUPT,
-                                      ENDPOINT_DIR_IN, CDC_NOTIFICATION_EPSIZE,
+                                  ENDPOINT_DIR_IN, CDC_NOTIFICATION_EPSIZE,
                                   ENDPOINT_BANK_SINGLE);
 
        Endpoint_ConfigureEndpoint(CDC_TX_EPNUM, EP_TYPE_BULK,
-                                      ENDPOINT_DIR_IN, CDC_TXRX_EPSIZE,
+                                  ENDPOINT_DIR_IN, CDC_TXRX_EPSIZE,
                                   ENDPOINT_BANK_SINGLE);
 
        Endpoint_ConfigureEndpoint(CDC_RX_EPNUM, EP_TYPE_BULK,
-                                      ENDPOINT_DIR_OUT, CDC_TXRX_EPSIZE,
+                                  ENDPOINT_DIR_OUT, CDC_TXRX_EPSIZE,
                                   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, so that they can be handled appropriately
- *  for the application.
+ *  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)
 {
-       uint8_t* LineCodingData = (uint8_t*)&LineCoding;
-
        /* Process CDC specific control requests */
        switch (USB_ControlRequest.bRequest)
        {
                case REQ_GetLineEncoding:
                        if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
-                       {
+                       {       
                                Endpoint_ClearSETUP();
 
-                               for (uint8_t i = 0; i < sizeof(LineCoding); i++)
-                                 Endpoint_Write_Byte(*(LineCodingData++));     
-                               
-                               Endpoint_ClearIN();
-                               
-                               Endpoint_ClearStatusStage();
+                               /* Write the line coding data to the control endpoint */
+                               Endpoint_Write_Control_Stream_LE(&LineEncoding, sizeof(CDC_Line_Coding_t));
+                               Endpoint_ClearOUT();
                        }
                        
                        break;
@@ -154,27 +146,9 @@ void EVENT_USB_Device_UnhandledControlRequest(void)
                        {
                                Endpoint_ClearSETUP();
 
-                               while (!(Endpoint_IsOUTReceived()))
-                               {                               
-                                       if (USB_DeviceState == DEVICE_STATE_Unattached)
-                                         return;
-                               }
-                       
-                               for (uint8_t i = 0; i < sizeof(LineCoding); i++)
-                                 *(LineCodingData++) = Endpoint_Read_Byte();
-
-                               Endpoint_ClearOUT();
-
-                               Endpoint_ClearStatusStage();
-                       }
-       
-                       break;
-               case REQ_SetControlLineState:
-                       if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
-                       {
-                               Endpoint_ClearSETUP();
-                               
-                               Endpoint_ClearStatusStage();
+                               /* 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;
@@ -339,7 +313,7 @@ static void WriteNextResponseByte(const uint8_t Response)
                Endpoint_ClearIN();
                
                while (!(Endpoint_IsINReady()))
-               {                               
+               {
                        if (USB_DeviceState == DEVICE_STATE_Unattached)
                          return;
                }
@@ -367,7 +341,7 @@ void CDC_Task(void)
                {
                        if (Command == 'E')
                          RunBootloader = false;
-                       if (Command == 'T')
+                       else if (Command == 'T')
                          FetchNextCommandByte();
 
                        /* Send confirmation byte back to the host */
@@ -377,7 +351,6 @@ void CDC_Task(void)
                {
                        /* Return ATMEGA128 part code - this is only to allow AVRProg to use the bootloader */
                        WriteNextResponseByte(0x44);
-
                        WriteNextResponseByte(0x00);
                }
                else if (Command == 'a')