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