Added beginnings of a new AVRISP-MKII clone project.
[pub/USBasp.git] / Demos / Device / ClassDriver / CDC / CDC.c
index 86d5c36..f09d3f1 100644 (file)
  *  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
 \r
+#if 0\r
+/* NOTE: Here you can set up a standard stream using the created virtual serial port, so that the standard stream functions in\r
+ *       <stdio.h> can be used on the virtual serial port (e.g. fprintf(&USBSerial, "Test"); to print a string).\r
+ */\r
+\r
+static int CDC_putchar(char c, FILE *stream)\r
+{\r
+       CDC_Device_SendByte(&VirtualSerial_CDC_Interface, c);\r
+       return 0;\r
+}\r
+\r
+static int CDC_getchar(FILE *stream)\r
+{\r
+       if (!(CDC_Device_BytesReceived(&VirtualSerial_CDC_Interface)))\r
+         return -1;\r
+\r
+       return CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface);\r
+}\r
+\r
+static FILE USBSerial = FDEV_SETUP_STREAM(CDC_putchar, CDC_getchar, _FDEV_SETUP_RW);\r
+#endif\r
+\r
 /** Main program entry point. This routine contains the overall program flow, including initial\r
  *  setup of all components and the main program loop.\r
  */\r
@@ -66,9 +91,9 @@ int main(void)
        for (;;)\r
        {\r
                CheckJoystickMovement();\r
-               \r
-               uint16_t BytesToDiscard = CDC_Device_BytesReceived(&VirtualSerial_CDC_Interface);\r
-               while (BytesToDiscard--)\r
+                \r
+               /* Must throw away unused bytes from the host, or it will lock up while waiting for the device */\r
+               while (CDC_Device_BytesReceived(&VirtualSerial_CDC_Interface))\r
                  CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface);\r
 \r
                CDC_Device_USBTask(&VirtualSerial_CDC_Interface);\r
@@ -99,25 +124,16 @@ void CheckJoystickMovement(void)
        char*       ReportString  = NULL;\r
        static bool ActionSent    = false;\r
        \r
-       char* JoystickStrings[] =\r
-               {\r
-                       "Joystick Up\r\n",\r
-                       "Joystick Down\r\n",\r
-                       "Joystick Left\r\n",\r
-                       "Joystick Right\r\n",\r
-                       "Joystick Pressed\r\n",\r
-               };\r
-\r
        if (JoyStatus_LCL & JOY_UP)\r
-         ReportString = JoystickStrings[0];\r
+         ReportString = "Joystick Up\r\n";\r
        else if (JoyStatus_LCL & JOY_DOWN)\r
-         ReportString = JoystickStrings[1];\r
+         ReportString = "Joystick Down\r\n";\r
        else if (JoyStatus_LCL & JOY_LEFT)\r
-         ReportString = JoystickStrings[2];\r
+         ReportString = "Joystick Left\r\n";\r
        else if (JoyStatus_LCL & JOY_RIGHT)\r
-         ReportString = JoystickStrings[3];\r
+         ReportString = "Joystick Right\r\n";\r
        else if (JoyStatus_LCL & JOY_PRESS)\r
-         ReportString = JoystickStrings[4];\r
+         ReportString = "Joystick Pressed\r\n";\r
        else\r
          ActionSent = false;\r
          \r
@@ -130,19 +146,19 @@ void CheckJoystickMovement(void)
 }\r
 \r
 /** Event handler for the library USB Connection event. */\r
-void EVENT_USB_Connect(void)\r
+void EVENT_USB_Device_Connect(void)\r
 {\r
        LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);\r
 }\r
 \r
 /** Event handler for the library USB Disconnection event. */\r
-void EVENT_USB_Disconnect(void)\r
+void EVENT_USB_Device_Disconnect(void)\r
 {\r
        LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);\r
 }\r
 \r
 /** Event handler for the library USB Configuration Changed event. */\r
-void EVENT_USB_ConfigurationChanged(void)\r
+void EVENT_USB_Device_ConfigurationChanged(void)\r
 {\r
        LEDs_SetAllLEDs(LEDMASK_USB_READY);\r
 \r
@@ -150,8 +166,8 @@ void EVENT_USB_ConfigurationChanged(void)
          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
+/** Event handler for the library USB Unhandled Control Request event. */\r
+void EVENT_USB_Device_UnhandledControlRequest(void)\r
 {\r
-       CDC_Device_ProcessControlPacket(&VirtualSerial_CDC_Interface);\r
+       CDC_Device_ProcessControlRequest(&VirtualSerial_CDC_Interface);\r
 }\r