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"
40 uint8_t PrinterProtocol
;
47 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
);
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
));
59 void SetupHardware(void)
61 /* Disable watchdog if enabled by bootloader/fuses */
62 MCUSR
&= ~(1 << WDRF
);
65 /* Disable clock division */
66 clock_prescale_set(clock_div_1
);
68 /* Hardware Initialization */
69 SerialStream_Init(9600, false);
74 void EVENT_USB_DeviceAttached(void)
76 puts_P(PSTR("Device Attached.\r\n"));
77 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING
);
80 void EVENT_USB_DeviceUnattached(void)
82 puts_P(PSTR("\r\nDevice Unattached.\r\n"));
83 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
);
86 void EVENT_USB_HostError(uint8_t ErrorCode
)
90 puts_P(PSTR(ESC_BG_RED
"Host Mode Error\r\n"));
91 printf_P(PSTR(" -- Error Code %d\r\n"), ErrorCode
);
93 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
97 void EVENT_USB_DeviceEnumerationFailed(uint8_t ErrorCode
, uint8_t SubErrorCode
)
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
);
103 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
106 void EVENT_USB_DeviceEnumerationComplete(void)
108 LEDs_SetAllLEDs(LEDMASK_USB_READY
);
111 void USB_Printer_Host(void)
115 switch (USB_HostState
)
117 case HOST_STATE_Addressed
:
118 /* Standard request to set the device configuration to configuration 1 */
119 USB_ControlRequest
= (USB_Request_Header_t
)
121 bmRequestType
: (REQDIR_HOSTTODEVICE
| REQTYPE_STANDARD
| REQREC_DEVICE
),
122 bRequest
: REQ_SetConfiguration
,
128 /* Send the request, display error and wait for device detatch if request fails */
129 if ((ErrorCode
= USB_Host_SendControlRequest(NULL
)) != HOST_SENDCONTROL_Successful
)
131 puts_P(PSTR("Control Error (Set Configuration).\r\n"));
132 printf_P(PSTR(" -- Error Code: %d\r\n"), ErrorCode
);
134 /* Indicate error via status LEDs */
135 LEDs_SetAllLEDs(LEDS_LED1
);
137 /* Wait until USB device disconnected */
138 while (USB_IsConnected
);
142 USB_HostState
= HOST_STATE_Configured
;
144 case HOST_STATE_Configured
:
145 puts_P(PSTR("Getting Config Data.\r\n"));
147 /* Get and process the configuration descriptor data */
148 if ((ErrorCode
= ProcessConfigurationDescriptor()) != SuccessfulConfigRead
)
150 if (ErrorCode
== ControlError
)
151 puts_P(PSTR("Control Error (Get Configuration).\r\n"));
153 puts_P(PSTR("Invalid Device.\r\n"));
155 printf_P(PSTR(" -- Error Code: %d\r\n"), ErrorCode
);
157 /* Indicate error via status LEDs */
158 LEDs_SetAllLEDs(LEDS_LED1
);
160 /* Wait until USB device disconnected */
161 while (USB_IsConnected
);
165 puts_P(PSTR("Printer Enumerated.\r\n"));
167 USB_HostState
= HOST_STATE_Ready
;
169 case HOST_STATE_Ready
:
170 /* Indicate device busy via the status LEDs */
171 LEDs_SetAllLEDs(LEDS_LED3
| LEDS_LED4
);
173 if (!(GetDeviceID()))
175 /* Indicate error via status LEDs */
176 LEDs_SetAllLEDs(LEDS_LED1
);
178 /* Wait until USB device disconnected */
179 while (USB_IsConnected
);
183 /* Indicate device no longer busy */
184 LEDs_SetAllLEDs(LEDS_LED4
);
186 /* Wait until USB device disconnected */
187 while (USB_IsConnected
);
193 bool GetDeviceID(void)
195 Device_ID_String_t DeviceIDString
;
197 /* Request to retrieve the device ID string */
198 USB_ControlRequest
= (USB_Request_Header_t
)
200 bmRequestType
: (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_INTERFACE
),
201 bRequest
: GET_DEVICE_ID
,
204 wLength
: sizeof(DeviceIDString
),
207 printf("Error Code: %d", USB_Host_SendControlRequest(&DeviceIDString
));
209 /* Send the request, display error and wait for device detatch if request fails */
210 if (USB_Host_SendControlRequest(&DeviceIDString
) != HOST_SENDCONTROL_Successful
)
213 /* Reverse the order of the string length as it is sent in big-endian format */
214 DeviceIDString
.Length
= SwapEndian_16(DeviceIDString
.Length
);
216 printf("%s", DeviceIDString
.String
);