Fixed PrinterHost demo returning invalid Device ID data when the attached device...
[pub/lufa.git] / Demos / OTG / TestApp / TestEvents.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 /** \file
32 *
33 * This file contains dummy handlers for all the possible USB events passed to the
34 * application by the library (see library documentation for more details on USB events).
35 *
36 * Each event is caught and printed to the USART so that they may be monitored.
37 */
38
39 #define INCLUDE_FROM_TESTEVENTS_C
40 #include "TestEvents.h"
41
42 /** Simple routine which aborts the program execution when a fatal error occurs, and is passed to the
43 * application via an event. When run, this function shuts down the USB interface, indicates an error
44 * via the board LEDs, prints an error message to the USART and then enters an infinite loop, preventing
45 * any more application code (other than interrupts) from executing.
46 */
47 static void Abort_Program(void)
48 {
49 USB_ShutDown();
50
51 LEDs_SetAllLEDs(LEDS_LED1 | LEDS_LED3);
52
53 puts_P(PSTR(ESC_FG_RED ESC_INVERSE_ON "\r\n**PROGRAM ABORT**" ESC_FG_WHITE));
54 for (;;);
55 }
56
57 /** Event handler for the USB_UIDChange event. When fired, the event is logged to the USART. */
58 void EVENT_USB_UIDChange(void)
59 {
60 char* ModeStrPtr;
61
62 puts_P(PSTR(ESC_FG_RED EVENT_PREFIX "UID Change\r\n"));
63
64 if (USB_CurrentMode == USB_MODE_DEVICE)
65 ModeStrPtr = PSTR("DEVICE");
66 else if (USB_CurrentMode == USB_MODE_HOST)
67 ModeStrPtr = PSTR("HOST");
68 else
69 ModeStrPtr = PSTR("N/A");
70
71 printf_P(PSTR(" -- New Mode %S\r\n" ESC_FG_WHITE), ModeStrPtr);
72 }
73
74 /**
75 * Event handler for the USB_InitFailure event. When fired, the event is logged to the USART and the program
76 * execution aborted.
77 */
78 void EVENT_USB_InitFailure(const uint8_t ErrorCode)
79 {
80 char* ModeStrPtr;
81
82 puts_P(PSTR(ESC_FG_RED EVENT_PREFIX "Power On Fail\r\n"));
83
84 if (USB_CurrentMode == USB_MODE_DEVICE)
85 ModeStrPtr = PSTR("DEVICE");
86 else if (USB_CurrentMode == USB_MODE_HOST)
87 ModeStrPtr = PSTR("HOST");
88 else
89 ModeStrPtr = PSTR("N/A");
90
91 printf_P(PSTR(" -- Mode %S\r\n"
92 " -- Error Code %d\r\n" ESC_FG_WHITE), ModeStrPtr, ErrorCode);
93
94 Abort_Program();
95 }
96
97 /** Event handler for the USB_Device_Connect event. When fired, the event is logged to the USART. */
98 void EVENT_USB_Device_Connect(void)
99 {
100 puts_P(PSTR(ESC_FG_GREEN EVENT_PREFIX "USB Connect\r\n" ESC_FG_WHITE));
101 }
102
103 /** Event handler for the USB_Device_Disconnect event. When fired, the event is logged to the USART. */
104 void EVENT_USB_Device_Disconnect(void)
105 {
106 puts_P(PSTR(ESC_FG_GREEN EVENT_PREFIX "USB Disconnect\r\n" ESC_FG_WHITE));
107 }
108
109 /** Event handler for the USB_Device_Suspend event. When fired, the event is logged to the USART. */
110 void EVENT_USB_Device_Suspend(void)
111 {
112 puts_P(PSTR(ESC_FG_YELLOW EVENT_PREFIX "USB Sleep\r\n" ESC_FG_WHITE));
113 }
114
115 /** Event handler for the USB_Device_WakeUp event. When fired, the event is logged to the USART. */
116 void EVENT_USB_Device_WakeUp(void)
117 {
118 puts_P(PSTR(ESC_FG_YELLOW EVENT_PREFIX "USB Wakeup\r\n" ESC_FG_WHITE));
119 }
120
121 /** Event handler for the USB_Device_Reset event. When fired, the event is logged to the USART. */
122 void EVENT_USB_Device_Reset(void)
123 {
124 puts_P(PSTR(ESC_FG_YELLOW EVENT_PREFIX "USB Reset\r\n" ESC_FG_WHITE));
125 }
126
127 /** Event handler for the USB_Device_UnhandledControlRequest event. When fired, the event is logged to the USART. */
128 void EVENT_USB_Device_UnhandledControlRequest(void)
129 {
130 printf_P(PSTR(ESC_FG_YELLOW EVENT_PREFIX "Ctrl Request\r\n"
131 " -- Req Data %d\r\n"
132 " -- Req Type %d\r\n"
133 " -- Req Length %d\r\n" ESC_FG_WHITE), USB_ControlRequest.bRequest,
134 USB_ControlRequest.bmRequestType,
135 USB_ControlRequest.wLength);
136 }
137
138 /** Event handler for the USB_Device_ConfigurationChanged event. When fired, the event is logged to the USART. */
139 void EVENT_USB_Device_ConfigurationChanged(void)
140 {
141 puts_P(PSTR(ESC_FG_YELLOW EVENT_PREFIX "Configuration Number Changed\r\n" ESC_FG_WHITE));
142 }
143
144 /**
145 * Event handler for the USB_Host_HostError event. When fired, the event is logged to the USART and the program
146 * execution aborted.
147 */
148 void EVENT_USB_Host_HostError(const uint8_t ErrorCode)
149 {
150 printf_P(PSTR(ESC_FG_RED EVENT_PREFIX "Host Mode Error\r\n"
151 " -- Error Code %d\r\n" ESC_FG_WHITE), ErrorCode);
152
153 Abort_Program();
154 }
155
156 /** Event handler for the USB_Host_DeviceEnumerationFailed event. When fired, the event is logged to the USART. */
157 void EVENT_USB_Host_DeviceEnumerationFailed(const uint8_t ErrorCode, const uint8_t SubErrorCode)
158 {
159 printf_P(PSTR(ESC_FG_RED EVENT_PREFIX "Dev Enum Error\r\n"
160 " -- Error Code %d\r\n"
161 " -- Sub Error Code %d\r\n"
162 " -- In State %d\r\n" ESC_FG_WHITE), ErrorCode, SubErrorCode, USB_HostState);
163 }
164
165 /** Event handler for the USB_Host_DeviceEnumerationComplete event. When fired, the event is logged to the USART. */
166 void EVENT_USB_Host_DeviceEnumerationComplete(void)
167 {
168 puts_P(PSTR(ESC_FG_YELLOW EVENT_PREFIX "Device Enumeration Complete\r\n" ESC_FG_WHITE));
169 }
170
171 /** Event handler for the USB_Host_DeviceAttached event. When fired, the event is logged to the USART. */
172 void EVENT_USB_Host_DeviceAttached(void)
173 {
174 puts_P(PSTR(ESC_FG_GREEN EVENT_PREFIX "Device Attached\r\n" ESC_FG_WHITE));
175 }
176
177 /** Event handler for the USB_Host_DeviceUnattached event. When fired, the event is logged to the USART. */
178 void EVENT_USB_Host_DeviceUnattached(void)
179 {
180 puts_P(PSTR(ESC_FG_GREEN EVENT_PREFIX "Device Unattached\r\n" ESC_FG_WHITE));
181 }