--- /dev/null
+/*\r
+ LUFA Library\r
+ Copyright (C) Dean Camera, 2009.\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
+ software without specific, written prior permission.\r
+\r
+ The author disclaim all warranties with regard to this\r
+ software, including all implied warranties of merchantability\r
+ and fitness. In no event shall the author be liable for any\r
+ special, indirect or consequential damages or any damages\r
+ whatsoever resulting from loss of use, data or profits, whether\r
+ in an action of contract, negligence or other tortious action,\r
+ arising out of or in connection with the use or performance of\r
+ this software.\r
+*/\r
+\r
+/*\r
+ USB Printer host demo application.\r
+ \r
+ ** NOT CURRENTLY FUNCTIONAL - DO NOT USE **\r
+*/\r
+\r
+#include "PrinterHost.h"\r
+\r
+/* Globals */\r
+uint8_t PrinterProtocol;\r
+\r
+\r
+int main(void)\r
+{\r
+ SetupHardware();\r
+\r
+ LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);\r
+\r
+ puts_P(PSTR(ESC_RESET ESC_BG_WHITE ESC_INVERSE_ON ESC_ERASE_DISPLAY\r
+ "Printer Host Demo running.\r\n" ESC_INVERSE_OFF));\r
+ \r
+ for (;;)\r
+ {\r
+ USB_Printer_Host();\r
+ USB_USBTask();\r
+ }\r
+}\r
+\r
+void SetupHardware(void)\r
+{\r
+ /* Disable watchdog if enabled by bootloader/fuses */\r
+ MCUSR &= ~(1 << WDRF);\r
+ wdt_disable();\r
+\r
+ /* Disable clock division */\r
+ clock_prescale_set(clock_div_1);\r
+\r
+ /* Hardware Initialization */\r
+ SerialStream_Init(9600, false);\r
+ LEDs_Init();\r
+ USB_Init();\r
+}\r
+\r
+void EVENT_USB_DeviceAttached(void)\r
+{\r
+ puts_P(PSTR("Device Attached.\r\n"));\r
+ LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);\r
+}\r
+\r
+void EVENT_USB_DeviceUnattached(void)\r
+{\r
+ puts_P(PSTR("\r\nDevice Unattached.\r\n"));\r
+ LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);\r
+}\r
+\r
+void EVENT_USB_HostError(uint8_t ErrorCode)\r
+{\r
+ USB_ShutDown();\r
+\r
+ puts_P(PSTR(ESC_BG_RED "Host Mode Error\r\n"));\r
+ printf_P(PSTR(" -- Error Code %d\r\n"), ErrorCode);\r
+\r
+ LEDs_SetAllLEDs(LEDMASK_USB_ERROR);\r
+ for(;;);\r
+}\r
+\r
+void EVENT_USB_DeviceEnumerationFailed(uint8_t ErrorCode, uint8_t SubErrorCode)\r
+{\r
+ puts_P(PSTR(ESC_BG_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"), USB_HostState);\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
+void USB_Printer_Host(void)\r
+{\r
+ uint8_t ErrorCode;\r
+\r
+ switch (USB_HostState)\r
+ {\r
+ case HOST_STATE_Addressed:\r
+ /* Standard request to set the device configuration to configuration 1 */\r
+ USB_ControlRequest = (USB_Request_Header_t)\r
+ {\r
+ bmRequestType: (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_DEVICE),\r
+ bRequest: REQ_SetConfiguration,\r
+ wValue: 1,\r
+ wIndex: 0,\r
+ wLength: 0,\r
+ };\r
+ \r
+ /* Send the request, display error and wait for device detatch if request fails */\r
+ if ((ErrorCode = USB_Host_SendControlRequest(NULL)) != HOST_SENDCONTROL_Successful)\r
+ {\r
+ puts_P(PSTR("Control Error (Set Configuration).\r\n"));\r
+ printf_P(PSTR(" -- Error Code: %d\r\n"), ErrorCode);\r
+\r
+ /* Indicate error via status LEDs */\r
+ LEDs_SetAllLEDs(LEDS_LED1);\r
+\r
+ /* Wait until USB device disconnected */\r
+ while (USB_IsConnected);\r
+ break;\r
+ }\r
+ \r
+ USB_HostState = HOST_STATE_Configured;\r
+ break;\r
+ case HOST_STATE_Configured:\r
+ puts_P(PSTR("Getting Config Data.\r\n"));\r
+ \r
+ /* Get and process the configuration descriptor data */\r
+ if ((ErrorCode = ProcessConfigurationDescriptor()) != SuccessfulConfigRead)\r
+ {\r
+ if (ErrorCode == ControlError)\r
+ puts_P(PSTR("Control Error (Get Configuration).\r\n"));\r
+ else\r
+ puts_P(PSTR("Invalid Device.\r\n"));\r
+\r
+ printf_P(PSTR(" -- Error Code: %d\r\n"), ErrorCode);\r
+ \r
+ /* Indicate error via status LEDs */\r
+ LEDs_SetAllLEDs(LEDS_LED1);\r
+\r
+ /* Wait until USB device disconnected */\r
+ while (USB_IsConnected);\r
+ break;\r
+ }\r
+\r
+ puts_P(PSTR("Printer Enumerated.\r\n"));\r
+ \r
+ USB_HostState = HOST_STATE_Ready;\r
+ break;\r
+ case HOST_STATE_Ready:\r
+ /* Indicate device busy via the status LEDs */\r
+ LEDs_SetAllLEDs(LEDS_LED3 | LEDS_LED4);\r
+ \r
+ if (!(GetDeviceID()))\r
+ {\r
+ /* Indicate error via status LEDs */\r
+ LEDs_SetAllLEDs(LEDS_LED1);\r
+\r
+ /* Wait until USB device disconnected */\r
+ while (USB_IsConnected);\r
+ break;\r
+ }\r
+ \r
+ /* Indicate device no longer busy */\r
+ LEDs_SetAllLEDs(LEDS_LED4);\r
+ \r
+ /* Wait until USB device disconnected */\r
+ while (USB_IsConnected);\r
+ \r
+ break;\r
+ }\r
+}\r
+\r
+bool GetDeviceID(void)\r
+{\r
+ Device_ID_String_t DeviceIDString;\r
+\r
+ /* Request to retrieve the device ID string */\r
+ USB_ControlRequest = (USB_Request_Header_t)\r
+ {\r
+ bmRequestType: (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE),\r
+ bRequest: GET_DEVICE_ID,\r
+ wValue: 0,\r
+ wIndex: 0,\r
+ wLength: sizeof(DeviceIDString),\r
+ };\r
+\r
+ printf("Error Code: %d", USB_Host_SendControlRequest(&DeviceIDString));\r
+\r
+ /* Send the request, display error and wait for device detatch if request fails */\r
+ if (USB_Host_SendControlRequest(&DeviceIDString) != HOST_SENDCONTROL_Successful)\r
+ return false;\r
+\r
+ /* Reverse the order of the string length as it is sent in big-endian format */\r
+ DeviceIDString.Length = SwapEndian_16(DeviceIDString.Length);\r
+ \r
+ printf("%s", DeviceIDString.String);\r
+ \r
+ return true;\r
+}\r