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
33 * Main source file for the MouseHost demo. This file contains the main tasks of
34 * the demo and is responsible for the initial application hardware configuration.
37 #include "MouseHost.h"
39 /* Project Tags, for reading out using the ButtLoad project */
40 BUTTLOADTAG(ProjName
, "LUFA Mouse Host App");
41 BUTTLOADTAG(BuildTime
, __TIME__
);
42 BUTTLOADTAG(BuildDate
, __DATE__
);
43 BUTTLOADTAG(LUFAVersion
, "LUFA V" LUFA_VERSION_STRING
);
45 /* Scheduler Task List */
48 { Task
: USB_USBTask
, TaskStatus
: TASK_STOP
},
49 { Task
: USB_Mouse_Host
, TaskStatus
: TASK_STOP
},
53 /** Main program entry point. This routine configures the hardware required by the application, then
54 * starts the scheduler to run the application tasks.
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);
69 /* Indicate USB not ready */
70 UpdateStatus(Status_USBNotReady
);
72 /* Initialize Scheduler so that it can be used */
75 /* Initialize USB Subsystem */
79 puts_P(PSTR(ESC_RESET ESC_BG_WHITE ESC_INVERSE_ON ESC_ERASE_DISPLAY
80 "Mouse Host Demo running.\r\n" ESC_INVERSE_OFF
));
82 /* Scheduling - routine never returns, so put this last in the main function */
86 /** Event handler for the USB_DeviceAttached event. This indicates that a device has been attached to the host, and
87 * starts the library USB task to begin the enumeration and USB management process.
89 EVENT_HANDLER(USB_DeviceAttached
)
91 puts_P(PSTR("Device Attached.\r\n"));
92 UpdateStatus(Status_USBEnumerating
);
94 /* Start USB management task to enumerate the device */
95 Scheduler_SetTaskMode(USB_USBTask
, TASK_RUN
);
98 /** Event handler for the USB_DeviceUnattached event. This indicates that a device has been removed from the host, and
99 * stops the library USB task management process.
101 EVENT_HANDLER(USB_DeviceUnattached
)
103 /* Stop mouse and USB management task */
104 Scheduler_SetTaskMode(USB_USBTask
, TASK_STOP
);
105 Scheduler_SetTaskMode(USB_Mouse_Host
, TASK_STOP
);
107 puts_P(PSTR("Device Unattached.\r\n"));
108 UpdateStatus(Status_USBNotReady
);
111 /** Event handler for the USB_DeviceEnumerationComplete event. This indicates that a device has been successfully
112 * enumerated by the host and is now ready to be used by the application.
114 EVENT_HANDLER(USB_DeviceEnumerationComplete
)
116 /* Start Mouse Host task */
117 Scheduler_SetTaskMode(USB_Mouse_Host
, TASK_RUN
);
119 /* Indicate device enumeration complete */
120 UpdateStatus(Status_USBReady
);
123 /** Event handler for the USB_HostError event. This indicates that a hardware error occurred while in host mode. */
124 EVENT_HANDLER(USB_HostError
)
128 puts_P(PSTR(ESC_BG_RED
"Host Mode Error\r\n"));
129 printf_P(PSTR(" -- Error Code %d\r\n"), ErrorCode
);
131 UpdateStatus(Status_HardwareError
);
135 /** Event handler for the USB_DeviceEnumerationFailed event. This indicates that a problem occured while
136 * enumerating an attached USB device.
138 EVENT_HANDLER(USB_DeviceEnumerationFailed
)
140 puts_P(PSTR(ESC_BG_RED
"Dev Enum Error\r\n"));
141 printf_P(PSTR(" -- Error Code %d\r\n"), ErrorCode
);
142 printf_P(PSTR(" -- Sub Error Code %d\r\n"), SubErrorCode
);
143 printf_P(PSTR(" -- In State %d\r\n"), USB_HostState
);
145 UpdateStatus(Status_EnumerationError
);
148 /** Function to manage status updates to the user. This is done via LEDs on the given board, if available, but may be changed to
149 * log to a serial port, or anything else that is suitable for status updates.
151 * \param CurrentStatus Current status of the system, from the MouseHost_StatusCodes_t enum
153 void UpdateStatus(uint8_t CurrentStatus
)
155 uint8_t LEDMask
= LEDS_NO_LEDS
;
157 /* Set the LED mask to the appropriate LED mask based on the given status code */
158 switch (CurrentStatus
)
160 case Status_USBNotReady
:
161 LEDMask
= (LEDS_LED1
);
163 case Status_USBEnumerating
:
164 LEDMask
= (LEDS_LED1
| LEDS_LED2
);
166 case Status_USBReady
:
167 LEDMask
= (LEDS_LED2
);
169 case Status_EnumerationError
:
170 case Status_HardwareError
:
171 LEDMask
= (LEDS_LED1
| LEDS_LED3
);
175 /* Set the board LEDs to the new LED mask */
176 LEDs_SetAllLEDs(LEDMask
);
179 /** Reads in and processes the next report from the attached device, displaying the report
180 * contents on the board LEDs and via the serial port.
182 void ReadNextReport(void)
184 USB_MouseReport_Data_t MouseReport
;
185 uint8_t LEDMask
= LEDS_NO_LEDS
;
187 /* Select mouse data pipe */
188 Pipe_SelectPipe(MOUSE_DATAPIPE
);
190 #if !defined(INTERRUPT_DATA_PIPE)
191 /* Unfreeze mouse data pipe */
195 /* Ensure pipe contains data and is ready to be read before continuing */
196 if (!(Pipe_ReadWriteAllowed()))
198 #if !defined(INTERRUPT_DATA_PIPE)
199 /* Refreeze mouse data pipe */
206 /* Read in mouse report data */
207 Pipe_Read_Stream_LE(&MouseReport
, sizeof(MouseReport
));
209 /* Clear the IN endpoint, ready for next data packet */
210 Pipe_ClearCurrentBank();
212 /* Alter status LEDs according to mouse X movement */
213 if (MouseReport
.X
> 0)
214 LEDMask
|= LEDS_LED1
;
215 else if (MouseReport
.X
< 0)
216 LEDMask
|= LEDS_LED2
;
218 /* Alter status LEDs according to mouse Y movement */
219 if (MouseReport
.Y
> 0)
220 LEDMask
|= LEDS_LED3
;
221 else if (MouseReport
.Y
< 0)
222 LEDMask
|= LEDS_LED4
;
224 /* Alter status LEDs according to mouse button position */
225 if (MouseReport
.Button
)
226 LEDMask
= LEDS_ALL_LEDS
;
228 LEDs_SetAllLEDs(LEDMask
);
230 /* Print mouse report data through the serial port */
231 printf_P(PSTR("dX:%2d dY:%2d Button:%d\r\n"), MouseReport
.X
,
235 #if !defined(INTERRUPT_DATA_PIPE)
236 /* Refreeze mouse data pipe */
241 /** Task to set the configuration of the attached device after it has been enumerated, and to read and process
242 * HID reports from the device and display the results onto the board LEDs.
248 /* Switch to determine what user-application handled host state the host state machine is in */
249 switch (USB_HostState
)
251 case HOST_STATE_Addressed
:
252 /* Standard request to set the device configuration to configuration 1 */
253 USB_HostRequest
= (USB_Host_Request_Header_t
)
255 bmRequestType
: (REQDIR_HOSTTODEVICE
| REQTYPE_STANDARD
| REQREC_DEVICE
),
256 bRequest
: REQ_SetConfiguration
,
262 /* Send the request, display error and wait for device detatch if request fails */
263 if ((ErrorCode
= USB_Host_SendControlRequest(NULL
)) != HOST_SENDCONTROL_Successful
)
265 puts_P(PSTR("Control Error (Set Configuration).\r\n"));
266 printf_P(PSTR(" -- Error Code: %d\r\n"), ErrorCode
);
268 /* Indicate error status */
269 UpdateStatus(Status_EnumerationError
);
271 /* Wait until USB device disconnected */
272 while (USB_IsConnected
);
276 USB_HostState
= HOST_STATE_Configured
;
278 case HOST_STATE_Configured
:
279 puts_P(PSTR("Getting Config Data.\r\n"));
281 /* Get and process the configuration descriptor data */
282 if ((ErrorCode
= ProcessConfigurationDescriptor()) != SuccessfulConfigRead
)
284 if (ErrorCode
== ControlError
)
285 puts_P(PSTR("Control Error (Get Configuration).\r\n"));
287 puts_P(PSTR("Invalid Device.\r\n"));
289 printf_P(PSTR(" -- Error Code: %d\r\n"), ErrorCode
);
291 /* Indicate error status */
292 UpdateStatus(Status_EnumerationError
);
294 /* Wait until USB device disconnected */
295 while (USB_IsConnected
);
299 /* HID class request to set the mouse protocol to the Boot Protocol */
300 USB_HostRequest
= (USB_Host_Request_Header_t
)
302 bmRequestType
: (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
),
303 bRequest
: REQ_SetProtocol
,
309 /* Send the request, display error and wait for device detatch if request fails */
310 if ((ErrorCode
= USB_Host_SendControlRequest(NULL
)) != HOST_SENDCONTROL_Successful
)
312 puts_P(PSTR("Control Error (Set Protocol).\r\n"));
313 printf_P(PSTR(" -- Error Code: %d\r\n"), ErrorCode
);
315 /* Indicate error status */
316 UpdateStatus(Status_EnumerationError
);
318 /* Wait until USB device disconnected */
319 while (USB_IsConnected
);
323 #if defined(INTERRUPT_DATA_PIPE)
324 /* Select and unfreeze mouse data pipe */
325 Pipe_SelectPipe(MOUSE_DATAPIPE
);
329 puts_P(PSTR("Mouse Enumerated.\r\n"));
331 USB_HostState
= HOST_STATE_Ready
;
333 #if !defined(INTERRUPT_DATA_PIPE)
334 case HOST_STATE_Ready
:
335 /* If a report has been received, read and process it */
343 #if defined(INTERRUPT_DATA_PIPE)
344 /** Interrupt handler for the Endpoint/Pipe interrupt vector. This interrupt fires each time an enabled
345 * pipe interrupt occurs on a pipe which has had that interrupt enabled.
347 ISR(ENDPOINT_PIPE_vect
, ISR_BLOCK
)
349 /* Check to see if the mouse data pipe has caused the interrupt */
350 if (Pipe_HasPipeInterrupted(MOUSE_DATAPIPE
))
352 /* Clear the pipe interrupt, and select the mouse pipe */
353 Pipe_ClearPipeInterrupt(MOUSE_DATAPIPE
);
354 Pipe_SelectPipe(MOUSE_DATAPIPE
);
356 /* Check to see if the pipe IN interrupt has fired */
357 if (USB_INT_HasOccurred(PIPE_INT_IN
) && USB_INT_IsEnabled(PIPE_INT_IN
))
359 /* Clear interrupt flag */
360 USB_INT_Clear(PIPE_INT_IN
);
362 /* Read and process the next report from the device */