Fixed incorrect PIPE_EPNUM_MASK mask causing pipe failures on devices with endpoint...
[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(LEDMASK_USB_ERROR);
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(LEDMASK_USB_ERROR);
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 printf_P(PSTR("Printer Protocol: %d\r\n"), PrinterProtocol);
158
159 puts_P(PSTR("Retrieving Device ID...\r\n"));
160
161 Device_ID_String_t DeviceIDString;
162 if ((ErrorCode = Printer_GetDeviceID(&DeviceIDString)) != HOST_SENDCONTROL_Successful)
163 {
164 puts_P(PSTR("Control Error (Get DeviceID).\r\n"));
165 printf_P(PSTR(" -- Error Code: %d\r\n"), ErrorCode);
166
167 /* Indicate error via status LEDs */
168 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
169
170 /* Wait until USB device disconnected */
171 USB_HostState = HOST_STATE_WaitForDeviceRemoval;
172 break;
173 }
174
175 printf_P(PSTR("Printer Device ID: %s\r\n"), DeviceIDString.String);
176
177 puts_P(PSTR("Printer Enumerated.\r\n"));
178
179 USB_HostState = HOST_STATE_Ready;
180 break;
181 case HOST_STATE_Ready:
182 /* Indicate device busy via the status LEDs */
183 LEDs_SetAllLEDs(LEDMASK_USB_BUSY);
184
185 //--------------------------------------------------------------
186 #define TEST_TEXT_PAGE "\033%-12345X\033E LUFA PCL Test Page \033E\033%-12345X"
187 // #define TEST_TEXT_PAGE "\033@\033i\001\033X\001\060\000\r\nLUFA ESCP/2 Test Page\r\n"
188 #define PAGE_SIZE (sizeof(TEST_TEXT_PAGE) - 1)
189
190 Pipe_SelectPipe(PRINTER_DATA_OUT_PIPE);
191 Pipe_Unfreeze();
192
193 puts_P(PSTR("Waiting for Printer to Become Ready...\r\n"));
194
195 while (!(Pipe_IsReadWriteAllowed()));
196
197 printf_P(PSTR("Printer Write Allowed, Sending Page (%d bytes)...\r\n"), PAGE_SIZE);
198
199 Pipe_Write_Stream_LE(TEST_TEXT_PAGE, PAGE_SIZE);
200 Pipe_ClearOUT();
201
202 puts_P(PSTR("Page Sent, Waiting for Pipe...\r\n"));
203
204 while (!(Pipe_IsReadWriteAllowed()));
205 Pipe_Freeze();
206
207 puts_P(PSTR("Pipe Frozen.\r\n"));
208 //--------------------------------------------------------------
209
210 /* Indicate device no longer busy */
211 LEDs_SetAllLEDs(LEDMASK_USB_READY);
212
213 USB_HostState = HOST_STATE_WaitForDeviceRemoval;
214 break;
215 }
216 }