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 #if defined(USB_CAN_BE_DEVICE)
34 #define INCLUDE_FROM_DEVCHAPTER9_C
35 #include "DevChapter9.h"
37 uint8_t USB_ConfigurationNumber
;
38 bool USB_RemoteWakeupEnabled
;
39 bool USB_CurrentlySelfPowered
;
41 void USB_Device_ProcessControlPacket(void)
43 uint8_t bmRequestType
= Endpoint_Read_Byte();
44 uint8_t bRequest
= Endpoint_Read_Byte();
45 bool RequestHandled
= false;
50 if ((bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_STANDARD
| REQREC_DEVICE
)) ||
51 (bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_STANDARD
| REQREC_ENDPOINT
)))
53 USB_Device_GetStatus(bmRequestType
);
54 RequestHandled
= true;
58 #if !defined(NO_CLEARSET_FEATURE_REQUEST)
59 case REQ_ClearFeature
:
61 if (bmRequestType
== (REQDIR_HOSTTODEVICE
| REQTYPE_STANDARD
| REQREC_ENDPOINT
))
63 USB_Device_ClearSetFeature(bRequest
, bmRequestType
);
64 RequestHandled
= true;
70 if (bmRequestType
== (REQDIR_HOSTTODEVICE
| REQTYPE_STANDARD
| REQREC_DEVICE
))
72 USB_Device_SetAddress();
73 RequestHandled
= true;
77 case REQ_GetDescriptor
:
78 if ((bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_STANDARD
| REQREC_DEVICE
)) ||
79 (bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_STANDARD
| REQREC_INTERFACE
)))
81 USB_Device_GetDescriptor();
82 RequestHandled
= true;
86 case REQ_GetConfiguration
:
87 if (bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_STANDARD
| REQREC_DEVICE
))
89 USB_Device_GetConfiguration();
90 RequestHandled
= true;
94 case REQ_SetConfiguration
:
95 if (bmRequestType
== (REQDIR_HOSTTODEVICE
| REQTYPE_STANDARD
| REQREC_DEVICE
))
97 USB_Device_SetConfiguration();
98 RequestHandled
= true;
104 if (!(RequestHandled
))
105 RAISE_EVENT(USB_UnhandledControlPacket
, bRequest
, bmRequestType
);
107 if (Endpoint_IsSetupReceived())
109 Endpoint_StallTransaction();
110 Endpoint_ClearSetupReceived();
114 static void USB_Device_SetAddress(void)
116 uint8_t wValue_LSB
= Endpoint_Read_Byte();
118 Endpoint_ClearSetupReceived();
120 while (!(Endpoint_IsSetupINReady()));
122 Endpoint_ClearSetupIN();
124 while (!(Endpoint_IsSetupINReady()));
126 UDADDR
= ((1 << ADDEN
) | (wValue_LSB
& 0x7F));
131 static void USB_Device_SetConfiguration(void)
133 uint8_t wValue_LSB
= Endpoint_Read_Byte();
134 bool AlreadyConfigured
= (USB_ConfigurationNumber
!= 0);
136 #if defined(USE_SINGLE_DEVICE_CONFIGURATION)
139 USB_Descriptor_Device_t
* DevDescriptorPtr
;
141 if ((USB_GetDescriptor((DTYPE_Device
<< 8), 0, (void*)&DevDescriptorPtr
) == NO_DESCRIPTOR
) ||
142 #if defined(USE_RAM_DESCRIPTORS)
143 (wValue_LSB
> DevDescriptorPtr
->NumberOfConfigurations
))
144 #elif defined (USE_EEPROM_DESCRIPTORS)
145 (wValue_LSB
> eeprom_read_byte(&DevDescriptorPtr
->NumberOfConfigurations
)))
147 (wValue_LSB
> pgm_read_byte(&DevDescriptorPtr
->NumberOfConfigurations
)))
154 Endpoint_ClearSetupReceived();
156 USB_ConfigurationNumber
= wValue_LSB
;
158 Endpoint_ClearSetupIN();
160 if (!(AlreadyConfigured
) && USB_ConfigurationNumber
)
161 RAISE_EVENT(USB_DeviceEnumerationComplete
);
163 RAISE_EVENT(USB_ConfigurationChanged
);
166 void USB_Device_GetConfiguration(void)
168 Endpoint_ClearSetupReceived();
170 Endpoint_Write_Byte(USB_ConfigurationNumber
);
172 Endpoint_ClearSetupIN();
174 while (!(Endpoint_IsSetupOUTReceived()));
175 Endpoint_ClearSetupOUT();
178 static void USB_Device_GetDescriptor(void)
180 uint16_t wValue
= Endpoint_Read_Word_LE();
181 uint16_t wIndex
= Endpoint_Read_Word_LE();
182 uint16_t wLength
= Endpoint_Read_Word_LE();
184 void* DescriptorPointer
;
185 uint16_t DescriptorSize
;
189 if ((DescriptorSize
= USB_GetDescriptor(wValue
, wIndex
, &DescriptorPointer
)) == NO_DESCRIPTOR
)
192 Endpoint_ClearSetupReceived();
194 if (wLength
> DescriptorSize
)
195 wLength
= DescriptorSize
;
199 while (!(Endpoint_IsSetupINReady()))
201 if (Endpoint_IsSetupOUTReceived())
203 Endpoint_ClearSetupOUT();
208 while (wLength
&& (Endpoint_BytesInEndpoint() < USB_ControlEndpointSize
))
210 #if defined(USE_RAM_DESCRIPTORS)
211 Endpoint_Write_Byte(*((uint8_t*)DescriptorPointer
++));
212 #elif defined (USE_EEPROM_DESCRIPTORS)
213 Endpoint_Write_Byte(eeprom_read_byte(DescriptorPointer
++));
215 Endpoint_Write_Byte(pgm_read_byte(DescriptorPointer
++));
221 SendZLP
= (Endpoint_BytesInEndpoint() == USB_ControlEndpointSize
);
222 Endpoint_ClearSetupIN();
227 while (!(Endpoint_IsSetupINReady()));
228 Endpoint_ClearSetupIN();
231 while (!(Endpoint_IsSetupOUTReceived()));
232 Endpoint_ClearSetupOUT();
235 static void USB_Device_GetStatus(const uint8_t bmRequestType
)
237 uint8_t CurrentStatus
= 0;
239 Endpoint_Discard_Word();
241 uint8_t wIndex_LSB
= Endpoint_Read_Byte();
243 switch (bmRequestType
)
245 case (REQDIR_DEVICETOHOST
| REQTYPE_STANDARD
| REQREC_DEVICE
):
246 if (USB_CurrentlySelfPowered
)
247 CurrentStatus
|= FEATURE_SELFPOWERED_ENABLED
;
249 if (USB_RemoteWakeupEnabled
)
250 CurrentStatus
|= FEATURE_REMOTE_WAKEUP_ENABLED
;
253 case (REQDIR_DEVICETOHOST
| REQTYPE_STANDARD
| REQREC_ENDPOINT
):
254 Endpoint_SelectEndpoint(wIndex_LSB
);
256 CurrentStatus
= Endpoint_IsStalled();
261 Endpoint_SelectEndpoint(ENDPOINT_CONTROLEP
);
262 Endpoint_ClearSetupReceived();
263 Endpoint_Write_Word_LE(CurrentStatus
);
265 Endpoint_ClearSetupIN();
267 while (!(Endpoint_IsSetupOUTReceived()));
268 Endpoint_ClearSetupOUT();
271 #if !defined(NO_CLEARSET_FEATURE_REQUEST)
272 static void USB_Device_ClearSetFeature(const uint8_t bRequest
, const uint8_t bmRequestType
)
274 uint16_t wValue
= Endpoint_Read_Word_LE();
275 uint16_t wIndex
= Endpoint_Read_Word_LE();
277 switch (bmRequestType
& CONTROL_REQTYPE_RECIPIENT
)
279 case REQREC_ENDPOINT
:
280 if (wValue
== FEATURE_ENDPOINT_HALT
)
282 uint8_t EndpointIndex
= (wIndex
& ENDPOINT_EPNUM_MASK
);
284 if (EndpointIndex
!= ENDPOINT_CONTROLEP
)
286 Endpoint_SelectEndpoint(EndpointIndex
);
288 if (Endpoint_IsEnabled())
290 if (bRequest
== REQ_ClearFeature
)
292 Endpoint_ClearStall();
293 Endpoint_ResetFIFO(EndpointIndex
);
294 Endpoint_ResetDataToggle();
298 Endpoint_StallTransaction();
302 Endpoint_SelectEndpoint(ENDPOINT_CONTROLEP
);
303 Endpoint_ClearSetupReceived();
304 Endpoint_ClearSetupIN();