Fixed Endpoint_Write_Control_Stream_* functions not sending a terminating IN when...
[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
22 if (Endpoint_IsINReady())
23 {
24 while (Length && (Endpoint_BytesInEndpoint() < USB_ControlEndpointSize))
25 {
26 TEMPLATE_TRANSFER_BYTE(DataStream);
27 Length--;
28 }
29
30 LastPacketFull = (Endpoint_BytesInEndpoint() == USB_ControlEndpointSize);
31 Endpoint_ClearIN();
32 }
33 }
34
35 while (!(Endpoint_IsOUTReceived()))
36 {
37 if (USB_DeviceState == DEVICE_STATE_Unattached)
38 return ENDPOINT_RWCSTREAM_DeviceDisconnected;
39 }
40
41 return ENDPOINT_RWCSTREAM_NoError;
42 }
43
44 #undef TEMPLATE_BUFFER_OFFSET
45 #undef TEMPLATE_FUNC_NAME
46 #undef TEMPLATE_TRANSFER_BYTE