Added new ENDPOINT_*_BusSuspended error code to the Endpoint function, so that the...
[pub/USBasp.git] / LUFA / Drivers / USB / LowLevel / Template / Template_Endpoint_Control_W.c
1 uint8_t TEMPLATE_FUNC_NAME (const void* Buffer, uint16_t Length)
2 {
3 uint8_t* DataStream = ((uint8_t*)Buffer + TEMPLATE_BUFFER_OFFSET(Length));
4 bool LastPacketFull = false;
5
6 if (Length > USB_ControlRequest.wLength)
7 Length = USB_ControlRequest.wLength;
8 else if (!(Length))
9 Endpoint_ClearIN();
10
11 while (Length || LastPacketFull)
12 {
13 if (Endpoint_IsSETUPReceived())
14 return ENDPOINT_RWCSTREAM_HostAborted;
15
16 if (Endpoint_IsOUTReceived())
17 break;
18
19 if (USB_DeviceState == DEVICE_STATE_Unattached)
20 return ENDPOINT_RWCSTREAM_DeviceDisconnected;
21 else if (USB_DeviceState == DEVICE_STATE_Suspended)
22 return ENDPOINT_RWCSTREAM_BusSuspended;
23
24 if (Endpoint_IsINReady())
25 {
26 uint8_t BytesInEndpoint = Endpoint_BytesInEndpoint();
27
28 while (Length && (BytesInEndpoint < USB_ControlEndpointSize))
29 {
30 TEMPLATE_TRANSFER_BYTE(DataStream);
31 Length--;
32 BytesInEndpoint++;
33 }
34
35 LastPacketFull = (BytesInEndpoint == USB_ControlEndpointSize);
36 Endpoint_ClearIN();
37 }
38 }
39
40 while (!(Endpoint_IsOUTReceived()))
41 {
42 if (USB_DeviceState == DEVICE_STATE_Unattached)
43 return ENDPOINT_RWCSTREAM_DeviceDisconnected;
44 else if (USB_DeviceState == DEVICE_STATE_Suspended)
45 return ENDPOINT_RWCSTREAM_BusSuspended;
46 }
47
48 return ENDPOINT_RWCSTREAM_NoError;
49 }
50
51 #undef TEMPLATE_BUFFER_OFFSET
52 #undef TEMPLATE_FUNC_NAME
53 #undef TEMPLATE_TRANSFER_BYTE