3 Copyright (C) Dean Camera, 2009.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
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.
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
32 USB Printer host demo application.
34 ** NOT CURRENTLY FUNCTIONAL - DO NOT USE **
37 #include "PrinterHost.h"
44 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
);
46 puts_P(PSTR(ESC_RESET ESC_BG_WHITE ESC_INVERSE_ON ESC_ERASE_DISPLAY
47 "Printer Host Demo running.\r\n" ESC_INVERSE_OFF
));
56 void SetupHardware(void)
58 /* Disable watchdog if enabled by bootloader/fuses */
59 MCUSR
&= ~(1 << WDRF
);
62 /* Disable clock division */
63 clock_prescale_set(clock_div_1
);
65 /* Hardware Initialization */
66 SerialStream_Init(9600, false);
71 void EVENT_USB_DeviceAttached(void)
73 puts_P(PSTR("Device Attached.\r\n"));
74 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING
);
77 void EVENT_USB_DeviceUnattached(void)
79 puts_P(PSTR("\r\nDevice Unattached.\r\n"));
80 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
);
83 void EVENT_USB_HostError(uint8_t ErrorCode
)
87 puts_P(PSTR(ESC_BG_RED
"Host Mode Error\r\n"));
88 printf_P(PSTR(" -- Error Code %d\r\n"), ErrorCode
);
90 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
94 void EVENT_USB_DeviceEnumerationFailed(uint8_t ErrorCode
, uint8_t SubErrorCode
)
96 puts_P(PSTR(ESC_BG_RED
"Dev Enum Error\r\n"));
97 printf_P(PSTR(" -- Error Code %d\r\n"), ErrorCode
);
98 printf_P(PSTR(" -- In State %d\r\n"), USB_HostState
);
100 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
103 void EVENT_USB_DeviceEnumerationComplete(void)
105 LEDs_SetAllLEDs(LEDMASK_USB_READY
);
108 void USB_Printer_Host(void)
112 switch (USB_HostState
)
114 case HOST_STATE_Addressed
:
115 puts_P(PSTR("Getting Config Data.\r\n"));
117 /* Select the control pipe for the request transfer */
118 Pipe_SelectPipe(PIPE_CONTROLPIPE
);
120 /* Get and process the configuration descriptor data */
121 if ((ErrorCode
= ProcessConfigurationDescriptor()) != SuccessfulConfigRead
)
123 if (ErrorCode
== ControlError
)
124 puts_P(PSTR("Control Error (Get Configuration).\r\n"));
126 puts_P(PSTR("Invalid Device.\r\n"));
128 printf_P(PSTR(" -- Error Code: %d\r\n"), ErrorCode
);
130 /* Indicate error via status LEDs */
131 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
133 /* Wait until USB device disconnected */
134 USB_HostState
= HOST_STATE_WaitForDeviceRemoval
;
138 /* Set the device configuration to the first configuration (rarely do devices use multiple configurations) */
139 if ((ErrorCode
= USB_Host_SetDeviceConfiguration(1)) != HOST_SENDCONTROL_Successful
)
141 puts_P(PSTR("Control Error (Set Configuration).\r\n"));
142 printf_P(PSTR(" -- Error Code: %d\r\n"), ErrorCode
);
144 /* Indicate error via status LEDs */
145 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
147 /* Wait until USB device disconnected */
148 USB_HostState
= HOST_STATE_WaitForDeviceRemoval
;
152 /* Some printers use alternate settings to determine the communication protocol used - if so, send a SetInterface
153 * request to switch to the interface alternate setting with the Bidirectional protocol */
154 if (PrinterAltSetting
)
156 USB_ControlRequest
= (USB_Request_Header_t
)
158 bmRequestType
: (REQDIR_HOSTTODEVICE
| REQTYPE_STANDARD
| REQREC_INTERFACE
),
159 bRequest
: REQ_SetInterface
,
160 wValue
: PrinterAltSetting
,
161 wIndex
: PrinterInterfaceNumber
,
165 if ((ErrorCode
= USB_Host_SendControlRequest(NULL
)) != HOST_SENDCONTROL_Successful
)
167 puts_P(PSTR("Control Error (Set Interface).\r\n"));
168 printf_P(PSTR(" -- Error Code: %d\r\n"), ErrorCode
);
170 /* Indicate error via status LEDs */
171 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
173 /* Wait until USB device disconnected */
174 USB_HostState
= HOST_STATE_WaitForDeviceRemoval
;
179 USB_HostState
= HOST_STATE_Configured
;
181 case HOST_STATE_Configured
:
182 puts_P(PSTR("Retrieving Device ID...\r\n"));
184 char DeviceIDString
[128];
185 if ((ErrorCode
= Printer_GetDeviceID(DeviceIDString
, sizeof(DeviceIDString
))) != HOST_SENDCONTROL_Successful
)
187 puts_P(PSTR("Control Error (Get DeviceID).\r\n"));
188 printf_P(PSTR(" -- Error Code: %d\r\n"), ErrorCode
);
190 /* Indicate error via status LEDs */
191 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
193 /* Wait until USB device disconnected */
194 USB_HostState
= HOST_STATE_WaitForDeviceRemoval
;
198 printf_P(PSTR("Printer Device ID: %s\r\n"), DeviceIDString
);
200 puts_P(PSTR("Printer Enumerated.\r\n"));
202 USB_HostState
= HOST_STATE_Ready
;
204 case HOST_STATE_Ready
:
205 /* Indicate device busy via the status LEDs */
206 LEDs_SetAllLEDs(LEDMASK_USB_BUSY
);
208 char PCL_Test_Page
[] = "\033%-12345X\033E LUFA PCL Test Page \033E\033%-12345X";
209 // char ESCP2_Test_Page[] = "\033@\033i\001\033X\001\060\000\r\nLUFA ESCP/2 Test Page\r\n";
211 printf_P(PSTR("Sending Test Page (%d bytes)...\r\n"), strlen(PCL_Test_Page
));
213 if ((ErrorCode
= Printer_SendData(PCL_Test_Page
)) != PIPE_RWSTREAM_NoError
)
215 puts_P(PSTR("Error Sending Test Page.\r\n"));
216 printf_P(PSTR(" -- Error Code: %d\r\n"), ErrorCode
);
218 /* Indicate error via status LEDs */
219 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
221 /* Wait until USB device disconnected */
222 USB_HostState
= HOST_STATE_WaitForDeviceRemoval
;
226 puts_P(PSTR("Test Page Sent, Waiting for Pipe...\r\n"));
228 /* Indicate device no longer busy */
229 LEDs_SetAllLEDs(LEDMASK_USB_READY
);
231 USB_HostState
= HOST_STATE_WaitForDeviceRemoval
;