Added new RNDISHost Host LowLevel demo. Fixed misnamed Pipe_SetPipeToken() macro...
[pub/USBasp.git] / Demos / Device / ClassDriver / CDC / CDC.c
index 6f15393..8940ae0 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
  *  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
        {\r
-               .ControlInterfaceNumber     = 0,\r
-\r
-               .DataINEndpointNumber       = CDC_TX_EPNUM,\r
-               .DataINEndpointSize         = CDC_TXRX_EPSIZE,\r
-\r
-               .DataOUTEndpointNumber      = CDC_RX_EPNUM,\r
-               .DataOUTEndpointSize        = CDC_TXRX_EPSIZE,\r
-\r
-               .NotificationEndpointNumber = CDC_NOTIFICATION_EPNUM,\r
-               .NotificationEndpointSize   = CDC_NOTIFICATION_EPSIZE,\r
+               .Config =\r
+                       {\r
+                               .ControlInterfaceNumber         = 0,\r
+\r
+                               .DataINEndpointNumber           = CDC_TX_EPNUM,\r
+                               .DataINEndpointSize             = CDC_TXRX_EPSIZE,\r
+                               .DataINEndpointDoubleBank       = false,\r
+\r
+                               .DataOUTEndpointNumber          = CDC_RX_EPNUM,\r
+                               .DataOUTEndpointSize            = CDC_TXRX_EPSIZE,\r
+                               .DataOUTEndpointDoubleBank      = false,\r
+\r
+                               .NotificationEndpointNumber     = CDC_NOTIFICATION_EPNUM,\r
+                               .NotificationEndpointSize       = CDC_NOTIFICATION_EPSIZE,\r
+                               .NotificationEndpointDoubleBank = false,\r
+                       },\r
        };\r
 \r
        };\r
 \r
+/** Standard file stream for the CDC interface when set up, so that the virtual CDC COM port can be\r
+ *  used like any regular character stream in the C APIs\r
+ */\r
+static FILE USBSerialStream;\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
 /** 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
@@ -61,17 +72,20 @@ int main(void)
 {\r
        SetupHardware();\r
        \r
 {\r
        SetupHardware();\r
        \r
+       /* Create a regular character stream for the interface so that it can be used with the stdio.h functions */\r
+       CDC_Device_CreateStream(&VirtualSerial_CDC_Interface, &USBSerialStream);\r
+\r
        LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);\r
 \r
        for (;;)\r
        {\r
                CheckJoystickMovement();\r
        LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);\r
 \r
        for (;;)\r
        {\r
                CheckJoystickMovement();\r
-               \r
-               uint16_t BytesToDiscard = USB_CDC_BytesReceived(&VirtualSerial_CDC_Interface);\r
-               while (BytesToDiscard--)\r
-                 USB_CDC_ReceiveByte(&VirtualSerial_CDC_Interface);\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
 \r
-               USB_CDC_USBTask(&VirtualSerial_CDC_Interface);\r
+               CDC_Device_USBTask(&VirtualSerial_CDC_Interface);\r
                USB_USBTask();\r
        }\r
 }\r
                USB_USBTask();\r
        }\r
 }\r
@@ -99,59 +113,54 @@ void CheckJoystickMovement(void)
        char*       ReportString  = NULL;\r
        static bool ActionSent    = false;\r
        \r
        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
        if (JoyStatus_LCL & JOY_UP)\r
-         ReportString = JoystickStrings[0];\r
+         ReportString = "Joystick Up\r\n";\r
        else if (JoyStatus_LCL & JOY_DOWN)\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
        else if (JoyStatus_LCL & JOY_LEFT)\r
-         ReportString = JoystickStrings[2];\r
+         ReportString = "Joystick Left\r\n";\r
        else if (JoyStatus_LCL & JOY_RIGHT)\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
        else if (JoyStatus_LCL & JOY_PRESS)\r
-         ReportString = JoystickStrings[4];\r
+         ReportString = "Joystick Pressed\r\n";\r
        else\r
          ActionSent = false;\r
          \r
        if ((ReportString != NULL) && (ActionSent == false))\r
        {\r
                ActionSent = true;\r
        else\r
          ActionSent = false;\r
          \r
        if ((ReportString != NULL) && (ActionSent == false))\r
        {\r
                ActionSent = true;\r
-               \r
-               USB_CDC_SendString(&VirtualSerial_CDC_Interface, ReportString, strlen(ReportString));           \r
+\r
+               /* Write the string to the virtual COM port via the created character stream */\r
+               fputs(ReportString, &USBSerialStream);\r
+\r
+               /* Alternatively, without the stream: */\r
+               // CDC_Device_SendString(&VirtualSerial_CDC_Interface, ReportString, strlen(ReportString));\r
        }\r
 }\r
 \r
 /** Event handler for the library USB Connection event. */\r
        }\r
 }\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
 {\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
 {\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
 {\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
          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
 {\r
-       USB_CDC_ProcessControlPacket(&VirtualSerial_CDC_Interface);\r
+       CDC_Device_ProcessControlRequest(&VirtualSerial_CDC_Interface);\r
 }\r
 }\r