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 Mouse demo. This file contains the main tasks of the demo and
34 * is responsible for the initial application hardware configuration.
39 /* Global Variables */
40 /** Indicates what report mode the host has requested, true for normal HID reporting mode, false for special boot
41 * protocol reporting mode.
43 bool UsingReportProtocol
= true;
45 /** Current Idle period. This is set by the host via a Set Idle HID class request to silence the device's reports
46 * for either the entire idle duration, or until the report status changes (e.g. the user moves the mouse).
48 uint16_t IdleCount
= HID_IDLE_CHANGESONLY
;
50 /** Current Idle period remaining. When the IdleCount value is set, this tracks the remaining number of idle
51 * milliseconds. This is separate to the IdleCount timer and is incremented and compared as the host may request
52 * the current idle period via a Get Idle HID class request, thus its value must be preserved.
54 uint16_t IdleMSRemaining
= 0;
57 /** Main program entry point. This routine configures the hardware required by the application, then
58 * starts the scheduler to run the application tasks.
64 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
);
73 /** Configures the board hardware and chip peripherals for the demo's functionality. */
74 void SetupHardware(void)
76 /* Disable watchdog if enabled by bootloader/fuses */
77 MCUSR
&= ~(1 << WDRF
);
80 /* Disable clock division */
81 clock_prescale_set(clock_div_1
);
83 /* Hardware Initialization */
89 /* Millisecond timer initialization, with output compare interrupt enabled for the idle timing */
91 TCCR0A
= (1 << WGM01
);
92 TCCR0B
= ((1 << CS01
) | (1 << CS00
));
93 TIMSK0
= (1 << OCIE0A
);
96 /** Event handler for the USB_Connect event. This indicates that the device is enumerating via the status LEDs and
97 * starts the library USB task to begin the enumeration and USB management process.
99 void EVENT_USB_Connect(void)
101 /* Indicate USB enumerating */
102 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING
);
104 /* Default to report protocol on connect */
105 UsingReportProtocol
= true;
108 /** Event handler for the USB_Disconnect event. This indicates that the device is no longer connected to a host via
109 * the status LEDs and stops the USB management and Mouse reporting tasks.
111 void EVENT_USB_Disconnect(void)
113 /* Indicate USB not ready */
114 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
);
117 /** Event handler for the USB_ConfigurationChanged event. This is fired when the host sets the current configuration
118 * of the USB device after enumeration - the device endpoints are configured and the mouse reporting task started.
120 void EVENT_USB_ConfigurationChanged(void)
122 /* Indicate USB connected and ready */
123 LEDs_SetAllLEDs(LEDMASK_USB_READY
);
125 /* Setup Mouse Report Endpoint */
126 if (!(Endpoint_ConfigureEndpoint(MOUSE_EPNUM
, EP_TYPE_INTERRUPT
,
127 ENDPOINT_DIR_IN
, MOUSE_EPSIZE
,
128 ENDPOINT_BANK_SINGLE
)))
130 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
134 /** Event handler for the USB_UnhandledControlPacket event. This is used to catch standard and class specific
135 * control requests that are not handled internally by the USB library (including the HID commands, which are
136 * all issued via the control endpoint), so that they can be handled appropriately for the application.
138 void EVENT_USB_UnhandledControlPacket(void)
140 /* Handle HID Class specific requests */
141 switch (USB_ControlRequest
.bRequest
)
144 if (USB_ControlRequest
.bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_INTERFACE
))
146 USB_MouseReport_Data_t MouseReportData
;
148 Endpoint_ClearSETUP();
150 /* Create the next mouse report for transmission to the host */
151 CreateMouseReport(&MouseReportData
);
153 /* Write the report data to the control endpoint */
154 Endpoint_Write_Control_Stream_LE(&MouseReportData
, sizeof(MouseReportData
));
156 /* Clear the report data afterwards */
157 memset(&MouseReportData
, 0, sizeof(MouseReportData
));
159 /* Finalize the stream transfer to send the last packet or clear the host abort */
164 case REQ_GetProtocol
:
165 if (USB_ControlRequest
.bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_INTERFACE
))
167 Endpoint_ClearSETUP();
169 /* Write the current protocol flag to the host */
170 Endpoint_Write_Byte(UsingReportProtocol
);
172 /* Send the flag to the host */
175 /* Acknowledge status stage */
176 while (!(Endpoint_IsOUTReceived()));
181 case REQ_SetProtocol
:
182 if (USB_ControlRequest
.bmRequestType
== (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
))
184 Endpoint_ClearSETUP();
186 /* Set or clear the flag depending on what the host indicates that the current Protocol should be */
187 UsingReportProtocol
= (USB_ControlRequest
.wValue
!= 0);
189 /* Acknowledge status stage */
190 while (!(Endpoint_IsINReady()));
196 if (USB_ControlRequest
.bmRequestType
== (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
))
198 Endpoint_ClearSETUP();
200 /* Get idle period in MSB */
201 IdleCount
= (USB_ControlRequest
.wValue
>> 8);
203 /* Acknowledge status stage */
204 while (!(Endpoint_IsINReady()));
210 if (USB_ControlRequest
.bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_INTERFACE
))
212 Endpoint_ClearSETUP();
214 /* Write the current idle duration to the host */
215 Endpoint_Write_Byte(IdleCount
);
217 /* Send the flag to the host */
220 /* Acknowledge status stage */
221 while (!(Endpoint_IsOUTReceived()));
229 /** ISR for the timer 0 compare vector. This ISR fires once each millisecond, and increments the
230 * scheduler elapsed idle period counter when the host has set an idle period.
232 ISR(TIMER0_COMPA_vect
, ISR_BLOCK
)
234 /* One millisecond has elapsed, decrement the idle time remaining counter if it has not already elapsed */
239 /** Fills the given HID report data structure with the next HID report to send to the host.
241 * \param[out] ReportData Pointer to a HID report data structure to be filled
243 void CreateMouseReport(USB_MouseReport_Data_t
* ReportData
)
245 uint8_t JoyStatus_LCL
= Joystick_GetStatus();
246 uint8_t ButtonStatus_LCL
= Buttons_GetStatus();
248 /* Clear the report contents */
249 memset(ReportData
, 0, sizeof(USB_MouseReport_Data_t
));
251 if (JoyStatus_LCL
& JOY_UP
)
253 else if (JoyStatus_LCL
& JOY_DOWN
)
256 if (JoyStatus_LCL
& JOY_RIGHT
)
258 else if (JoyStatus_LCL
& JOY_LEFT
)
261 if (JoyStatus_LCL
& JOY_PRESS
)
262 ReportData
->Button
= (1 << 0);
264 if (ButtonStatus_LCL
& BUTTONS_BUTTON1
)
265 ReportData
->Button
|= (1 << 1);
268 /** Sends the next HID report to the host, via the keyboard data endpoint. */
269 void SendNextReport(void)
271 static USB_MouseReport_Data_t PrevMouseReportData
;
272 USB_MouseReport_Data_t MouseReportData
;
275 /* Create the next mouse report for transmission to the host */
276 CreateMouseReport(&MouseReportData
);
278 /* Check to see if the report data has changed - if so a report MUST be sent */
279 SendReport
= (memcmp(&PrevMouseReportData
, &MouseReportData
, sizeof(USB_MouseReport_Data_t
)) != 0);
281 /* Override the check if the Y or X values are non-zero - we want continuous movement while the joystick
282 * is being held down (via continuous reports), otherwise the cursor will only move once per joystick toggle */
283 if ((MouseReportData
.Y
!= 0) || (MouseReportData
.X
!= 0))
286 /* Save the current report data for later comparison to check for changes */
287 PrevMouseReportData
= MouseReportData
;
289 /* Check if the idle period is set and has elapsed */
290 if ((IdleCount
!= HID_IDLE_CHANGESONLY
) && (!(IdleMSRemaining
)))
292 /* Reset the idle time remaining counter, must multiply by 4 to get the duration in milliseconds */
293 IdleMSRemaining
= (IdleCount
<< 2);
295 /* Idle period is set and has elapsed, must send a report to the host */
299 /* Select the Mouse Report Endpoint */
300 Endpoint_SelectEndpoint(MOUSE_EPNUM
);
302 /* Check if Mouse Endpoint Ready for Read/Write and if we should send a new report */
303 if (Endpoint_IsReadWriteAllowed() && SendReport
)
305 /* Write Mouse Report Data */
306 Endpoint_Write_Stream_LE(&MouseReportData
, sizeof(MouseReportData
));
308 /* Finalize the stream transfer to send the last packet */
313 /** Task to manage HID report generation and transmission to the host, when in report mode. */
314 void Mouse_Task(void)
316 /* Check if the USB system is connected to a host */
319 /* Send the next mouse report to the host */