Minor updates to the Benito programmer - remove redundant PORT register manipulations.
[pub/USBasp.git] / Demos / Device / ClassDriver / USBtoSerial / USBtoSerial.c
index 85826c2..637071a 100644 (file)
@@ -46,18 +46,26 @@ RingBuff_t Tx_Buffer;
  *  passed to all CDC Class driver functions, so that multiple instances of the same class\r
  *  within a device can be differentiated from one another.\r
  */\r
-USB_ClassInfo_CDC_t VirtualSerial_CDC_Interface =\r
+USB_ClassInfo_CDC_Device_t VirtualSerial_CDC_Interface =\r
        {\r
-               .ControlInterfaceNumber     = 0,\r
+               .Config = \r
+                       {\r
+                               .ControlInterfaceNumber     = 0,\r
 \r
-               .DataINEndpointNumber       = CDC_TX_EPNUM,\r
-               .DataINEndpointSize         = CDC_TXRX_EPSIZE,\r
+                               .DataINEndpointNumber       = CDC_TX_EPNUM,\r
+                               .DataINEndpointSize         = CDC_TXRX_EPSIZE,\r
 \r
-               .DataOUTEndpointNumber      = CDC_RX_EPNUM,\r
-               .DataOUTEndpointSize        = CDC_TXRX_EPSIZE,\r
+                               .DataOUTEndpointNumber      = CDC_RX_EPNUM,\r
+                               .DataOUTEndpointSize        = CDC_TXRX_EPSIZE,\r
 \r
-               .NotificationEndpointNumber = CDC_NOTIFICATION_EPNUM,\r
-               .NotificationEndpointSize   = CDC_NOTIFICATION_EPSIZE,\r
+                               .NotificationEndpointNumber = CDC_NOTIFICATION_EPNUM,\r
+                               .NotificationEndpointSize   = CDC_NOTIFICATION_EPSIZE,\r
+                       },\r
+               \r
+               .State =\r
+                       {\r
+                               // Leave all state values to their defaults\r
+                       }\r
        };\r
 \r
 /** Main program entry point. This routine contains the overall program flow, including initial\r
@@ -74,21 +82,24 @@ int main(void)
 \r
        for (;;)\r
        {\r
-               for (uint8_t DataBytesRem = USB_CDC_BytesReceived(&VirtualSerial_CDC_Interface); DataBytesRem != 0; DataBytesRem--)\r
+               /* Read bytes from the USB OUT endpoint into the USART transmit buffer */\r
+               for (uint8_t DataBytesRem = CDC_Device_BytesReceived(&VirtualSerial_CDC_Interface); DataBytesRem != 0; DataBytesRem--)\r
                {\r
                        if (!(BUFF_STATICSIZE - Rx_Buffer.Elements))\r
                          break;\r
                          \r
-                       Buffer_StoreElement(&Rx_Buffer, USB_CDC_ReceiveByte(&VirtualSerial_CDC_Interface));\r
+                       Buffer_StoreElement(&Rx_Buffer, CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface));\r
                }\r
                \r
+               /* Read bytes from the USART receive buffer into the USB IN endpoint */\r
                if (Tx_Buffer.Elements)\r
-                 USB_CDC_SendByte(&VirtualSerial_CDC_Interface, Buffer_GetElement(&Rx_Buffer));\r
-                 \r
+                 CDC_Device_SendByte(&VirtualSerial_CDC_Interface, Buffer_GetElement(&Tx_Buffer));\r
+               \r
+               /* Load bytes from the USART transmit buffer into the USART */\r
                if (Rx_Buffer.Elements)\r
                  Serial_TxByte(Buffer_GetElement(&Rx_Buffer));\r
                \r
-               USB_CDC_USBTask(&VirtualSerial_CDC_Interface);\r
+               CDC_Device_USBTask(&VirtualSerial_CDC_Interface);\r
                USB_USBTask();\r
        }\r
 }\r
@@ -126,14 +137,14 @@ void EVENT_USB_ConfigurationChanged(void)
 {\r
        LEDs_SetAllLEDs(LEDMASK_USB_READY);\r
 \r
-       if (!(USB_CDC_ConfigureEndpoints(&VirtualSerial_CDC_Interface)))\r
+       if (!(CDC_Device_ConfigureEndpoints(&VirtualSerial_CDC_Interface)))\r
          LEDs_SetAllLEDs(LEDMASK_USB_ERROR);\r
 }\r
 \r
 /** Event handler for the library USB Unhandled Control Packet event. */\r
 void EVENT_USB_UnhandledControlPacket(void)\r
 {\r
-       USB_CDC_ProcessControlPacket(&VirtualSerial_CDC_Interface);\r
+       CDC_Device_ProcessControlPacket(&VirtualSerial_CDC_Interface);\r
 }\r
 \r
 /** ISR to manage the reception of data from the serial port, placing received bytes into a circular buffer\r
@@ -141,35 +152,46 @@ void EVENT_USB_UnhandledControlPacket(void)
  */\r
 ISR(USART1_RX_vect, ISR_BLOCK)\r
 {\r
-       if (USB_IsConnected)\r
+       if (USB_DeviceState == DEVICE_STATE_Configured)\r
          Buffer_StoreElement(&Tx_Buffer, UDR1);\r
 }\r
 \r
 /** Event handler for the CDC Class driver Line Encoding Changed event.\r
  *\r
- *  \param CDCInterfaceInfo  Pointer to the CDC class interface configuration structure being referenced\r
+ *  \param[in] CDCInterfaceInfo  Pointer to the CDC class interface configuration structure being referenced\r
  */\r
-void EVENT_USB_CDC_LineEncodingChanged(USB_ClassInfo_CDC_t* CDCInterfaceInfo)\r
+void EVENT_CDC_Device_LineEncodingChanged(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo)\r
 {\r
        uint8_t ConfigMask = 0;\r
 \r
-       if (CDCInterfaceInfo->LineEncoding.ParityType == CDC_PARITY_Odd)\r
-         ConfigMask = ((1 << UPM11) | (1 << UPM10));\r
-       else if (CDCInterfaceInfo->LineEncoding.ParityType == CDC_PARITY_Even)\r
-         ConfigMask = (1 << UPM11);\r
+       switch (CDCInterfaceInfo->State.LineEncoding.ParityType)\r
+       {\r
+               case CDC_PARITY_Odd:\r
+                       ConfigMask = ((1 << UPM11) | (1 << UPM10));             \r
+                       break;\r
+               case CDC_PARITY_Even:\r
+                       ConfigMask = (1 << UPM11);              \r
+                       break;\r
+       }\r
 \r
-       if (CDCInterfaceInfo->LineEncoding.CharFormat == CDC_LINEENCODING_TwoStopBits)\r
+       if (CDCInterfaceInfo->State.LineEncoding.CharFormat == CDC_LINEENCODING_TwoStopBits)\r
          ConfigMask |= (1 << USBS1);\r
 \r
-       if (CDCInterfaceInfo->LineEncoding.DataBits == 6)\r
-         ConfigMask |= (1 << UCSZ10);\r
-       else if (CDCInterfaceInfo->LineEncoding.DataBits == 7)\r
-         ConfigMask |= (1 << UCSZ11);\r
-       else if (CDCInterfaceInfo->LineEncoding.DataBits == 8)\r
-         ConfigMask |= ((1 << UCSZ11) | (1 << UCSZ10));\r
+       switch (CDCInterfaceInfo->State.LineEncoding.DataBits)\r
+       {\r
+               case 6:\r
+                       ConfigMask |= (1 << UCSZ10);\r
+                       break;\r
+               case 7:\r
+                       ConfigMask |= (1 << UCSZ11);\r
+                       break;\r
+               case 8:\r
+                       ConfigMask |= ((1 << UCSZ11) | (1 << UCSZ10));\r
+                       break;\r
+       }\r
        \r
        UCSR1A = (1 << U2X1);   \r
        UCSR1B = ((1 << RXCIE1) | (1 << TXEN1) | (1 << RXEN1));\r
        UCSR1C = ConfigMask;    \r
-       UBRR1  = SERIAL_2X_UBBRVAL((uint16_t)CDCInterfaceInfo->LineEncoding.BaudRateBPS);\r
+       UBRR1  = SERIAL_2X_UBBRVAL((uint16_t)CDCInterfaceInfo->State.LineEncoding.BaudRateBPS);\r
 }\r