Update manual pages to reference unfinished AVR32 port, add port status to the Future...
[pub/USBasp.git] / Demos / Host / LowLevel / PrinterHost / PrinterHost.c
index 79e6f9e..6840138 100644 (file)
@@ -1,21 +1,21 @@
 /*\r
              LUFA Library\r
 /*\r
              LUFA Library\r
-     Copyright (C) Dean Camera, 2009.\r
+     Copyright (C) Dean Camera, 2010.\r
               \r
   dean [at] fourwalledcubicle [dot] com\r
       www.fourwalledcubicle.com\r
 */\r
 \r
 /*\r
               \r
   dean [at] fourwalledcubicle [dot] com\r
       www.fourwalledcubicle.com\r
 */\r
 \r
 /*\r
-  Copyright 2009  Dean Camera (dean [at] fourwalledcubicle [dot] com)\r
-\r
-  Permission to use, copy, modify, and distribute this software\r
-  and its documentation for any purpose and without fee is hereby\r
-  granted, provided that the above copyright notice appear in all\r
-  copies and that both that the copyright notice and this\r
-  permission notice and warranty disclaimer appear in supporting\r
-  documentation, and that the name of the author not be used in\r
-  advertising or publicity pertaining to distribution of the\r
+  Copyright 2010  Dean Camera (dean [at] fourwalledcubicle [dot] com)\r
+\r
+  Permission to use, copy, modify, distribute, and sell this \r
+  software and its documentation for any purpose is hereby granted\r
+  without fee, provided that the above copyright notice appear in \r
+  all copies and that both that the copyright notice and this\r
+  permission notice and warranty disclaimer appear in supporting \r
+  documentation, and that the name of the author not be used in \r
+  advertising or publicity pertaining to distribution of the \r
   software without specific, written prior permission.\r
 \r
   The author disclaim all warranties with regard to this\r
   software without specific, written prior permission.\r
 \r
   The author disclaim all warranties with regard to this\r
   this software.\r
 */\r
 \r
   this software.\r
 */\r
 \r
-/*\r
-       USB Printer host demo application.\r
-       \r
-       ** NOT CURRENTLY FUNCTIONAL - DO NOT USE **\r
-*/\r
+/** \file\r
+ *\r
+ *  Main source file for the PrinterHost demo. This file contains the main tasks of\r
+ *  the demo and is responsible for the initial application hardware configuration.\r
+ */\r
 \r
 #include "PrinterHost.h"\r
 \r
 \r
 #include "PrinterHost.h"\r
 \r
-\r
+/** Main program entry point. This routine configures the hardware required by the application, then\r
+ *  enters a loop to run the application tasks in sequence.\r
+ */\r
 int main(void)\r
 {\r
        SetupHardware();\r
 int main(void)\r
 {\r
        SetupHardware();\r
@@ -52,6 +54,7 @@ int main(void)
        }\r
 }\r
 \r
        }\r
 }\r
 \r
+/** Configures the board hardware and chip peripherals for the demo's functionality. */\r
 void SetupHardware(void)\r
 {\r
        /* Disable watchdog if enabled by bootloader/fuses */\r
 void SetupHardware(void)\r
 {\r
        /* Disable watchdog if enabled by bootloader/fuses */\r
@@ -67,43 +70,60 @@ void SetupHardware(void)
        USB_Init();\r
 }\r
 \r
        USB_Init();\r
 }\r
 \r
-void EVENT_USB_DeviceAttached(void)\r
+/** Event handler for the USB_DeviceAttached event. This indicates that a device has been attached to the host, and\r
+ *  starts the library USB task to begin the enumeration and USB management process.\r
+ */\r
+void EVENT_USB_Host_DeviceAttached(void)\r
 {\r
        puts_P(PSTR(ESC_FG_GREEN "Device Attached.\r\n" ESC_FG_WHITE));\r
        LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);\r
 }\r
 \r
 {\r
        puts_P(PSTR(ESC_FG_GREEN "Device Attached.\r\n" ESC_FG_WHITE));\r
        LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);\r
 }\r
 \r
-void EVENT_USB_DeviceUnattached(void)\r
+/** Event handler for the USB_DeviceUnattached event. This indicates that a device has been removed from the host, and\r
+ *  stops the library USB task management process.\r
+ */\r
+void EVENT_USB_Host_DeviceUnattached(void)\r
 {\r
        puts_P(PSTR(ESC_FG_GREEN "\r\nDevice Unattached.\r\n" ESC_FG_WHITE));\r
        LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);\r
 }\r
 \r
 {\r
        puts_P(PSTR(ESC_FG_GREEN "\r\nDevice Unattached.\r\n" ESC_FG_WHITE));\r
        LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);\r
 }\r
 \r
-void EVENT_USB_HostError(uint8_t ErrorCode)\r
+/** Event handler for the USB_DeviceEnumerationComplete event. This indicates that a device has been successfully\r
+ *  enumerated by the host and is now ready to be used by the application.\r
+ */\r
+void EVENT_USB_Host_DeviceEnumerationComplete(void)\r
+{\r
+       LEDs_SetAllLEDs(LEDMASK_USB_READY);\r
+}\r
+\r
+/** Event handler for the USB_HostError event. This indicates that a hardware error occurred while in host mode. */\r
+void EVENT_USB_Host_HostError(uint8_t ErrorCode)\r
 {\r
        USB_ShutDown();\r
 \r
 {\r
        USB_ShutDown();\r
 \r
-       puts_P(PSTR(ESC_FG_RED "Host Mode Error\r\n"));\r
-       printf_P(PSTR(" -- Error Code %d\r\n" ESC_FG_WHITE), ErrorCode);\r
+       printf_P(PSTR(ESC_FG_RED "Host Mode Error\r\n"\r
+                                " -- Error Code %d\r\n" ESC_FG_WHITE), ErrorCode);\r
 \r
        LEDs_SetAllLEDs(LEDMASK_USB_ERROR);\r
        for(;;);\r
 }\r
 \r
 \r
        LEDs_SetAllLEDs(LEDMASK_USB_ERROR);\r
        for(;;);\r
 }\r
 \r
-void EVENT_USB_DeviceEnumerationFailed(uint8_t ErrorCode, uint8_t SubErrorCode)\r
+/** Event handler for the USB_DeviceEnumerationFailed event. This indicates that a problem occurred while\r
+ *  enumerating an attached USB device.\r
+ */\r
+void EVENT_USB_Host_DeviceEnumerationFailed(uint8_t ErrorCode, uint8_t SubErrorCode)\r
 {\r
 {\r
-       puts_P(PSTR(ESC_FG_RED "Dev Enum Error\r\n"));\r
-       printf_P(PSTR(" -- Error Code %d\r\n"), ErrorCode);\r
-       printf_P(PSTR(" -- In State %d\r\n" ESC_FG_WHITE), USB_HostState);\r
+       printf_P(PSTR(ESC_FG_RED "Dev Enum Error\r\n"\r
+                                " -- Error Code %d\r\n"\r
+                                " -- Sub Error Code %d\r\n"\r
+                                " -- In State %d\r\n" ESC_FG_WHITE), ErrorCode, SubErrorCode, USB_HostState);\r
 \r
        LEDs_SetAllLEDs(LEDMASK_USB_ERROR);\r
 }\r
 \r
 \r
        LEDs_SetAllLEDs(LEDMASK_USB_ERROR);\r
 }\r
 \r
-void EVENT_USB_DeviceEnumerationComplete(void)\r
-{\r
-       LEDs_SetAllLEDs(LEDMASK_USB_READY);\r
-}\r
-\r
+/** Task to set the configuration of the attached device after it has been enumerated, and to send some test page\r
+ *  data to the attached printer.\r
+ */\r
 void USB_Printer_Host(void)\r
 {\r
        uint8_t ErrorCode;\r
 void USB_Printer_Host(void)\r
 {\r
        uint8_t ErrorCode;\r
@@ -137,8 +157,8 @@ void USB_Printer_Host(void)
                        /* Set the device configuration to the first configuration (rarely do devices use multiple configurations) */\r
                        if ((ErrorCode = USB_Host_SetDeviceConfiguration(1)) != HOST_SENDCONTROL_Successful)\r
                        {\r
                        /* Set the device configuration to the first configuration (rarely do devices use multiple configurations) */\r
                        if ((ErrorCode = USB_Host_SetDeviceConfiguration(1)) != HOST_SENDCONTROL_Successful)\r
                        {\r
-                               puts_P(PSTR(ESC_FG_RED "Control Error (Set Configuration).\r\n"));\r
-                               printf_P(PSTR(" -- Error Code: %d\r\n" ESC_FG_WHITE), ErrorCode);\r
+                               printf_P(PSTR(ESC_FG_RED "Control Error (Set Configuration).\r\n"\r
+                                                        " -- Error Code: %d\r\n" ESC_FG_WHITE), ErrorCode);\r
 \r
                                /* Indicate error via status LEDs */\r
                                LEDs_SetAllLEDs(LEDMASK_USB_ERROR);\r
 \r
                                /* Indicate error via status LEDs */\r
                                LEDs_SetAllLEDs(LEDMASK_USB_ERROR);\r
@@ -154,17 +174,17 @@ void USB_Printer_Host(void)
                        {\r
                                USB_ControlRequest = (USB_Request_Header_t)\r
                                        {\r
                        {\r
                                USB_ControlRequest = (USB_Request_Header_t)\r
                                        {\r
-                                               bmRequestType: (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_INTERFACE),\r
-                                               bRequest:      REQ_SetInterface,\r
-                                               wValue:        PrinterAltSetting,\r
-                                               wIndex:        PrinterInterfaceNumber,\r
-                                               wLength:       0,\r
+                                               .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_INTERFACE),\r
+                                               .bRequest      = REQ_SetInterface,\r
+                                               .wValue        = PrinterAltSetting,\r
+                                               .wIndex        = PrinterInterfaceNumber,\r
+                                               .wLength       = 0,\r
                                        };\r
                                        \r
                                if ((ErrorCode = USB_Host_SendControlRequest(NULL)) != HOST_SENDCONTROL_Successful)\r
                                {\r
                                        };\r
                                        \r
                                if ((ErrorCode = USB_Host_SendControlRequest(NULL)) != HOST_SENDCONTROL_Successful)\r
                                {\r
-                                       puts_P(PSTR(ESC_FG_RED "Control Error (Set Interface).\r\n"));\r
-                                       printf_P(PSTR(" -- Error Code: %d\r\n" ESC_FG_WHITE), ErrorCode);\r
+                                       printf_P(PSTR(ESC_FG_RED "Control Error (Set Interface).\r\n"\r
+                                                                " -- Error Code: %d\r\n" ESC_FG_WHITE), ErrorCode);\r
 \r
                                        /* Indicate error via status LEDs */\r
                                        LEDs_SetAllLEDs(LEDMASK_USB_ERROR);\r
 \r
                                        /* Indicate error via status LEDs */\r
                                        LEDs_SetAllLEDs(LEDMASK_USB_ERROR);\r
@@ -175,16 +195,13 @@ void USB_Printer_Host(void)
                                }\r
                        }\r
                        \r
                                }\r
                        }\r
                        \r
-                       USB_HostState = HOST_STATE_Configured;\r
-                       break;\r
-               case HOST_STATE_Configured:\r
                        puts_P(PSTR("Retrieving Device ID...\r\n"));\r
                \r
                        puts_P(PSTR("Retrieving Device ID...\r\n"));\r
                \r
-                       char DeviceIDString[256];\r
+                       char DeviceIDString[300];\r
                        if ((ErrorCode = Printer_GetDeviceID(DeviceIDString, sizeof(DeviceIDString))) != HOST_SENDCONTROL_Successful)\r
                        {\r
                        if ((ErrorCode = Printer_GetDeviceID(DeviceIDString, sizeof(DeviceIDString))) != HOST_SENDCONTROL_Successful)\r
                        {\r
-                               puts_P(PSTR(ESC_FG_RED "Control Error (Get DeviceID).\r\n"));\r
-                               printf_P(PSTR(" -- Error Code: %d\r\n" ESC_FG_WHITE), ErrorCode);\r
+                               printf_P(PSTR(ESC_FG_RED "Control Error (Get Device ID).\r\n"\r
+                                                        " -- Error Code: %d\r\n" ESC_FG_WHITE), ErrorCode);\r
 \r
                                /* Indicate error via status LEDs */\r
                                LEDs_SetAllLEDs(LEDMASK_USB_ERROR);\r
 \r
                                /* Indicate error via status LEDs */\r
                                LEDs_SetAllLEDs(LEDMASK_USB_ERROR);\r
@@ -197,26 +214,22 @@ void USB_Printer_Host(void)
                        printf_P(PSTR("Printer Device ID: %s\r\n"), DeviceIDString);\r
 \r
                        puts_P(PSTR("Printer Enumerated.\r\n"));\r
                        printf_P(PSTR("Printer Device ID: %s\r\n"), DeviceIDString);\r
 \r
                        puts_P(PSTR("Printer Enumerated.\r\n"));\r
-                                       \r
-                       USB_HostState = HOST_STATE_Ready;\r
+\r
+                       USB_HostState = HOST_STATE_Configured;\r
                        break;\r
                        break;\r
-               case HOST_STATE_Ready:\r
+               case HOST_STATE_Configured:\r
                        /* Indicate device busy via the status LEDs */\r
                        LEDs_SetAllLEDs(LEDMASK_USB_BUSY);\r
                \r
                        /* Indicate device busy via the status LEDs */\r
                        LEDs_SetAllLEDs(LEDMASK_USB_BUSY);\r
                \r
-                       Printer_Data_t TestPageData =\r
-                               {\r
-//                                     "\033%-12345X\033E LUFA PCL Test Page \033E\033%-12345X",\r
-                                       "\033@\033i\001\033X\001\060\000\r\nLUFA ESCP/2 Test Page\r\n",\r
-                                       (sizeof(TestPageData.Data) - 1)\r
-                               };\r
+                       char  TestPageData[]    = "\033%-12345X\033E" "LUFA PCL Test Page" "\033E\033%-12345X";\r
+                       uint16_t TestPageLength = strlen(TestPageData);\r
                \r
                \r
-                       printf_P(PSTR("Sending Test Page (%d bytes)...\r\n"), TestPageData.Length);\r
+                       printf_P(PSTR("Sending Test Page (%d bytes)...\r\n"), TestPageLength);\r
 \r
 \r
-                       if ((ErrorCode = Printer_SendData(&TestPageData)) != PIPE_RWSTREAM_NoError)\r
+                       if ((ErrorCode = Printer_SendData(&TestPageData, TestPageLength)) != PIPE_RWSTREAM_NoError)\r
                        {\r
                        {\r
-                               puts_P(PSTR(ESC_FG_RED "Error Sending Test Page.\r\n"));\r
-                               printf_P(PSTR(" -- Error Code: %d\r\n" ESC_FG_WHITE), ErrorCode);\r
+                               printf_P(PSTR(ESC_FG_RED "Error Sending Test Page.\r\n"\r
+                                                        " -- Error Code: %d\r\n" ESC_FG_WHITE), ErrorCode);\r
 \r
                                /* Indicate error via status LEDs */\r
                                LEDs_SetAllLEDs(LEDMASK_USB_ERROR);\r
 \r
                                /* Indicate error via status LEDs */\r
                                LEDs_SetAllLEDs(LEDMASK_USB_ERROR);\r