Added incomplete PrinterHost demo application.
[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 /* Globals */
40 uint8_t PrinterProtocol;
41
42
43 int main(void)
44 {
45 SetupHardware();
46
47 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
48
49 puts_P(PSTR(ESC_RESET ESC_BG_WHITE ESC_INVERSE_ON ESC_ERASE_DISPLAY
50 "Printer Host Demo running.\r\n" ESC_INVERSE_OFF));
51
52 for (;;)
53 {
54 USB_Printer_Host();
55 USB_USBTask();
56 }
57 }
58
59 void SetupHardware(void)
60 {
61 /* Disable watchdog if enabled by bootloader/fuses */
62 MCUSR &= ~(1 << WDRF);
63 wdt_disable();
64
65 /* Disable clock division */
66 clock_prescale_set(clock_div_1);
67
68 /* Hardware Initialization */
69 SerialStream_Init(9600, false);
70 LEDs_Init();
71 USB_Init();
72 }
73
74 void EVENT_USB_DeviceAttached(void)
75 {
76 puts_P(PSTR("Device Attached.\r\n"));
77 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
78 }
79
80 void EVENT_USB_DeviceUnattached(void)
81 {
82 puts_P(PSTR("\r\nDevice Unattached.\r\n"));
83 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
84 }
85
86 void EVENT_USB_HostError(uint8_t ErrorCode)
87 {
88 USB_ShutDown();
89
90 puts_P(PSTR(ESC_BG_RED "Host Mode Error\r\n"));
91 printf_P(PSTR(" -- Error Code %d\r\n"), ErrorCode);
92
93 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
94 for(;;);
95 }
96
97 void EVENT_USB_DeviceEnumerationFailed(uint8_t ErrorCode, uint8_t SubErrorCode)
98 {
99 puts_P(PSTR(ESC_BG_RED "Dev Enum Error\r\n"));
100 printf_P(PSTR(" -- Error Code %d\r\n"), ErrorCode);
101 printf_P(PSTR(" -- In State %d\r\n"), USB_HostState);
102
103 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
104 }
105
106 void EVENT_USB_DeviceEnumerationComplete(void)
107 {
108 LEDs_SetAllLEDs(LEDMASK_USB_READY);
109 }
110
111 void USB_Printer_Host(void)
112 {
113 uint8_t ErrorCode;
114
115 switch (USB_HostState)
116 {
117 case HOST_STATE_Addressed:
118 /* Standard request to set the device configuration to configuration 1 */
119 USB_ControlRequest = (USB_Request_Header_t)
120 {
121 bmRequestType: (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_DEVICE),
122 bRequest: REQ_SetConfiguration,
123 wValue: 1,
124 wIndex: 0,
125 wLength: 0,
126 };
127
128 /* Send the request, display error and wait for device detatch if request fails */
129 if ((ErrorCode = USB_Host_SendControlRequest(NULL)) != HOST_SENDCONTROL_Successful)
130 {
131 puts_P(PSTR("Control Error (Set Configuration).\r\n"));
132 printf_P(PSTR(" -- Error Code: %d\r\n"), ErrorCode);
133
134 /* Indicate error via status LEDs */
135 LEDs_SetAllLEDs(LEDS_LED1);
136
137 /* Wait until USB device disconnected */
138 while (USB_IsConnected);
139 break;
140 }
141
142 USB_HostState = HOST_STATE_Configured;
143 break;
144 case HOST_STATE_Configured:
145 puts_P(PSTR("Getting Config Data.\r\n"));
146
147 /* Get and process the configuration descriptor data */
148 if ((ErrorCode = ProcessConfigurationDescriptor()) != SuccessfulConfigRead)
149 {
150 if (ErrorCode == ControlError)
151 puts_P(PSTR("Control Error (Get Configuration).\r\n"));
152 else
153 puts_P(PSTR("Invalid Device.\r\n"));
154
155 printf_P(PSTR(" -- Error Code: %d\r\n"), ErrorCode);
156
157 /* Indicate error via status LEDs */
158 LEDs_SetAllLEDs(LEDS_LED1);
159
160 /* Wait until USB device disconnected */
161 while (USB_IsConnected);
162 break;
163 }
164
165 puts_P(PSTR("Printer Enumerated.\r\n"));
166
167 USB_HostState = HOST_STATE_Ready;
168 break;
169 case HOST_STATE_Ready:
170 /* Indicate device busy via the status LEDs */
171 LEDs_SetAllLEDs(LEDS_LED3 | LEDS_LED4);
172
173 if (!(GetDeviceID()))
174 {
175 /* Indicate error via status LEDs */
176 LEDs_SetAllLEDs(LEDS_LED1);
177
178 /* Wait until USB device disconnected */
179 while (USB_IsConnected);
180 break;
181 }
182
183 /* Indicate device no longer busy */
184 LEDs_SetAllLEDs(LEDS_LED4);
185
186 /* Wait until USB device disconnected */
187 while (USB_IsConnected);
188
189 break;
190 }
191 }
192
193 bool GetDeviceID(void)
194 {
195 Device_ID_String_t DeviceIDString;
196
197 /* Request to retrieve the device ID string */
198 USB_ControlRequest = (USB_Request_Header_t)
199 {
200 bmRequestType: (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE),
201 bRequest: GET_DEVICE_ID,
202 wValue: 0,
203 wIndex: 0,
204 wLength: sizeof(DeviceIDString),
205 };
206
207 printf("Error Code: %d", USB_Host_SendControlRequest(&DeviceIDString));
208
209 /* Send the request, display error and wait for device detatch if request fails */
210 if (USB_Host_SendControlRequest(&DeviceIDString) != HOST_SENDCONTROL_Successful)
211 return false;
212
213 /* Reverse the order of the string length as it is sent in big-endian format */
214 DeviceIDString.Length = SwapEndian_16(DeviceIDString.Length);
215
216 printf("%s", DeviceIDString.String);
217
218 return true;
219 }