7b98542d09109abf97c8484c1cb04016e17f3bda
[pub/USBasp.git] / Demos / Host / Incomplete / PrinterHost / PrinterHost.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2009.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
12 Permission to use, copy, modify, and distribute this software
13 and its documentation for any purpose and without fee is hereby
14 granted, provided that the above copyright notice appear in all
15 copies and that both that the copyright notice and this
16 permission notice and warranty disclaimer appear in supporting
17 documentation, and that the name of the author not be used in
18 advertising or publicity pertaining to distribution of the
19 software without specific, written prior permission.
20
21 The author disclaim all warranties with regard to this
22 software, including all implied warranties of merchantability
23 and fitness. In no event shall the author be liable for any
24 special, indirect or consequential damages or any damages
25 whatsoever resulting from loss of use, data or profits, whether
26 in an action of contract, negligence or other tortious action,
27 arising out of or in connection with the use or performance of
28 this software.
29 */
30
31 /*
32 USB Printer host demo application.
33
34 ** NOT CURRENTLY FUNCTIONAL - DO NOT USE **
35 */
36
37 #include "PrinterHost.h"
38
39 uint8_t PrinterProtocol;
40
41
42 int main(void)
43 {
44 SetupHardware();
45
46 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
47
48 puts_P(PSTR(ESC_RESET ESC_BG_WHITE ESC_INVERSE_ON ESC_ERASE_DISPLAY
49 "Printer Host Demo running.\r\n" ESC_INVERSE_OFF));
50
51 for (;;)
52 {
53 USB_Printer_Host();
54 USB_USBTask();
55 }
56 }
57
58 void SetupHardware(void)
59 {
60 /* Disable watchdog if enabled by bootloader/fuses */
61 MCUSR &= ~(1 << WDRF);
62 wdt_disable();
63
64 /* Disable clock division */
65 clock_prescale_set(clock_div_1);
66
67 /* Hardware Initialization */
68 SerialStream_Init(9600, false);
69 LEDs_Init();
70 USB_Init();
71 }
72
73 void EVENT_USB_DeviceAttached(void)
74 {
75 puts_P(PSTR("Device Attached.\r\n"));
76 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
77 }
78
79 void EVENT_USB_DeviceUnattached(void)
80 {
81 puts_P(PSTR("\r\nDevice Unattached.\r\n"));
82 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
83 }
84
85 void EVENT_USB_HostError(uint8_t ErrorCode)
86 {
87 USB_ShutDown();
88
89 puts_P(PSTR(ESC_BG_RED "Host Mode Error\r\n"));
90 printf_P(PSTR(" -- Error Code %d\r\n"), ErrorCode);
91
92 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
93 for(;;);
94 }
95
96 void EVENT_USB_DeviceEnumerationFailed(uint8_t ErrorCode, uint8_t SubErrorCode)
97 {
98 puts_P(PSTR(ESC_BG_RED "Dev Enum Error\r\n"));
99 printf_P(PSTR(" -- Error Code %d\r\n"), ErrorCode);
100 printf_P(PSTR(" -- In State %d\r\n"), USB_HostState);
101
102 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
103 }
104
105 void EVENT_USB_DeviceEnumerationComplete(void)
106 {
107 LEDs_SetAllLEDs(LEDMASK_USB_READY);
108 }
109
110 void USB_Printer_Host(void)
111 {
112 uint8_t ErrorCode;
113
114 switch (USB_HostState)
115 {
116 case HOST_STATE_Addressed:
117 puts_P(PSTR("Getting Config Data.\r\n"));
118
119 /* Select the control pipe for the request transfer */
120 Pipe_SelectPipe(PIPE_CONTROLPIPE);
121
122 /* Get and process the configuration descriptor data */
123 if ((ErrorCode = ProcessConfigurationDescriptor()) != SuccessfulConfigRead)
124 {
125 if (ErrorCode == ControlError)
126 puts_P(PSTR("Control Error (Get Configuration).\r\n"));
127 else
128 puts_P(PSTR("Invalid Device.\r\n"));
129
130 printf_P(PSTR(" -- Error Code: %d\r\n"), ErrorCode);
131
132 /* Indicate error via status LEDs */
133 LEDs_SetAllLEDs(LEDS_LED1);
134
135 /* Wait until USB device disconnected */
136 USB_HostState = HOST_STATE_WaitForDeviceRemoval;
137 break;
138 }
139
140 /* Set the device configuration to the first configuration (rarely do devices use multiple configurations) */
141 if ((ErrorCode = USB_Host_SetDeviceConfiguration(1)) != HOST_SENDCONTROL_Successful)
142 {
143 puts_P(PSTR("Control Error (Set Configuration).\r\n"));
144 printf_P(PSTR(" -- Error Code: %d\r\n"), ErrorCode);
145
146 /* Indicate error via status LEDs */
147 LEDs_SetAllLEDs(LEDS_LED1);
148
149 /* Wait until USB device disconnected */
150 USB_HostState = HOST_STATE_WaitForDeviceRemoval;
151 break;
152 }
153
154 USB_HostState = HOST_STATE_Configured;
155 break;
156 case HOST_STATE_Configured:
157 puts_P(PSTR("Printer Enumerated.\r\n"));
158
159 USB_HostState = HOST_STATE_Ready;
160 break;
161 case HOST_STATE_Ready:
162 /* Indicate device busy via the status LEDs */
163 LEDs_SetAllLEDs(LEDS_LED3 | LEDS_LED4);
164
165 printf_P(PSTR("Printer Protocol: %d\r\n"), PrinterProtocol);
166
167 puts_P(PSTR("Retrieving Device ID...\r\n"));
168
169 Device_ID_String_t DeviceIDString;
170 if (Printer_GetDeviceID(&DeviceIDString) != HOST_SENDCONTROL_Successful)
171 {
172 /* Indicate error via status LEDs */
173 LEDs_SetAllLEDs(LEDS_LED1);
174
175 /* Wait until USB device disconnected */
176 USB_HostState = HOST_STATE_WaitForDeviceRemoval;
177 break;
178 }
179
180 printf_P(PSTR("Printer Device ID: %s\r\n"), DeviceIDString.String);
181
182 /* Indicate device no longer busy */
183 LEDs_SetAllLEDs(LEDS_LED4);
184
185 USB_HostState = HOST_STATE_WaitForDeviceRemoval;
186 break;
187 }
188 }