3 Copyright (C) Dean Camera, 2011.
5 dean [at] fourwalledcubicle [dot] com
10 Copyright 2011 Dean Camera (dean [at] fourwalledcubicle [dot] com)
12 Permission to use, copy, modify, distribute, and sell this
13 software and its documentation for any purpose is hereby granted
14 without fee, provided that the above copyright notice appear in
15 all 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 GenericHID demo. This file contains the main tasks of the demo and
34 * is responsible for the initial application hardware configuration.
37 #include "GenericHID.h"
40 /** Main program entry point. This routine configures the hardware required by the application, then
41 * enters a loop to run the application tasks in sequence.
47 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
);
57 /** Configures the board hardware and chip peripherals for the demo's functionality. */
58 void SetupHardware(void)
60 /* Disable watchdog if enabled by bootloader/fuses */
61 MCUSR
&= ~(1 << WDRF
);
64 /* Disable clock division */
65 clock_prescale_set(clock_div_1
);
67 /* Hardware Initialization */
72 /** Event handler for the USB_Connect event. This indicates that the device is enumerating via the status LEDs and
73 * starts the library USB task to begin the enumeration and USB management process.
75 void EVENT_USB_Device_Connect(void)
77 /* Indicate USB enumerating */
78 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING
);
81 /** Event handler for the USB_Disconnect event. This indicates that the device is no longer connected to a host via
82 * the status LEDs and stops the USB management task.
84 void EVENT_USB_Device_Disconnect(void)
86 /* Indicate USB not ready */
87 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
);
90 /** Event handler for the USB_ConfigurationChanged event. This is fired when the host sets the current configuration
91 * of the USB device after enumeration, and configures the generic HID device endpoints.
93 void EVENT_USB_Device_ConfigurationChanged(void)
95 bool ConfigSuccess
= true;
97 /* Setup HID Report Endpoints */
98 ConfigSuccess
&= Endpoint_ConfigureEndpoint(GENERIC_IN_EPNUM
, EP_TYPE_INTERRUPT
, ENDPOINT_DIR_IN
,
99 GENERIC_EPSIZE
, ENDPOINT_BANK_SINGLE
);
100 ConfigSuccess
&= Endpoint_ConfigureEndpoint(GENERIC_OUT_EPNUM
, EP_TYPE_INTERRUPT
, ENDPOINT_DIR_OUT
,
101 GENERIC_EPSIZE
, ENDPOINT_BANK_SINGLE
);
103 /* Indicate endpoint configuration success or failure */
104 LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY
: LEDMASK_USB_ERROR
);
107 /** Event handler for the USB_ControlRequest event. This is used to catch and process control requests sent to
108 * the device from the USB host before passing along unhandled control requests to the library for processing
111 void EVENT_USB_Device_ControlRequest(void)
113 /* Handle HID Class specific requests */
114 switch (USB_ControlRequest
.bRequest
)
116 case HID_REQ_GetReport
:
117 if (USB_ControlRequest
.bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_INTERFACE
))
119 uint8_t GenericData
[GENERIC_REPORT_SIZE
];
120 CreateGenericHIDReport(GenericData
);
122 Endpoint_ClearSETUP();
124 /* Write the report data to the control endpoint */
125 Endpoint_Write_Control_Stream_LE(&GenericData
, sizeof(GenericData
));
130 case HID_REQ_SetReport
:
131 if (USB_ControlRequest
.bmRequestType
== (REQDIR_HOSTTODEVICE
| REQTYPE_CLASS
| REQREC_INTERFACE
))
133 uint8_t GenericData
[GENERIC_REPORT_SIZE
];
135 Endpoint_ClearSETUP();
137 /* Read the report data from the control endpoint */
138 Endpoint_Read_Control_Stream_LE(&GenericData
, sizeof(GenericData
));
141 ProcessGenericHIDReport(GenericData
);
148 /** Function to process the last received report from the host.
150 * \param[in] DataArray Pointer to a buffer where the last received report has been stored
152 void ProcessGenericHIDReport(uint8_t* DataArray
)
155 This is where you need to process reports sent from the host to the device. This
156 function is called each time the host has sent a new report. DataArray is an array
157 holding the report sent from the host.
160 uint8_t NewLEDMask
= LEDS_NO_LEDS
;
163 NewLEDMask
|= LEDS_LED1
;
166 NewLEDMask
|= LEDS_LED1
;
169 NewLEDMask
|= LEDS_LED1
;
172 NewLEDMask
|= LEDS_LED1
;
174 LEDs_SetAllLEDs(NewLEDMask
);
177 /** Function to create the next report to send back to the host at the next reporting interval.
179 * \param[out] DataArray Pointer to a buffer where the next report data should be stored
181 void CreateGenericHIDReport(uint8_t* DataArray
)
184 This is where you need to create reports to be sent to the host from the device. This
185 function is called each time the host is ready to accept a new report. DataArray is
186 an array to hold the report to the host.
189 uint8_t CurrLEDMask
= LEDs_GetLEDs();
191 DataArray
[0] = ((CurrLEDMask
& LEDS_LED1
) ?
1 : 0);
192 DataArray
[1] = ((CurrLEDMask
& LEDS_LED2
) ?
1 : 0);
193 DataArray
[2] = ((CurrLEDMask
& LEDS_LED3
) ?
1 : 0);
194 DataArray
[3] = ((CurrLEDMask
& LEDS_LED4
) ?
1 : 0);
199 /* Device must be connected and configured for the task to run */
200 if (USB_DeviceState
!= DEVICE_STATE_Configured
)
203 Endpoint_SelectEndpoint(GENERIC_OUT_EPNUM
);
205 /* Check to see if a packet has been sent from the host */
206 if (Endpoint_IsOUTReceived())
208 /* Check to see if the packet contains data */
209 if (Endpoint_IsReadWriteAllowed())
211 /* Create a temporary buffer to hold the read in report from the host */
212 uint8_t GenericData
[GENERIC_REPORT_SIZE
];
214 /* Read Generic Report Data */
215 Endpoint_Read_Stream_LE(&GenericData
, sizeof(GenericData
), NULL
);
217 /* Process Generic Report Data */
218 ProcessGenericHIDReport(GenericData
);
221 /* Finalize the stream transfer to send the last packet */
225 Endpoint_SelectEndpoint(GENERIC_IN_EPNUM
);
227 /* Check to see if the host is ready to accept another packet */
228 if (Endpoint_IsINReady())
230 /* Create a temporary buffer to hold the report to send to the host */
231 uint8_t GenericData
[GENERIC_REPORT_SIZE
];
233 /* Create Generic Report Data */
234 CreateGenericHIDReport(GenericData
);
236 /* Write Generic Report Data */
237 Endpoint_Write_Stream_LE(&GenericData
, sizeof(GenericData
), NULL
);
239 /* Finalize the stream transfer to send the last packet */