Template_Endpoint_Control_W.c 1.4 KB
Newer Older
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
5
6
7
	bool     LastPacketFull = false;
	
	if (Length > USB_ControlRequest.wLength)
	  Length = USB_ControlRequest.wLength;
8
9
10
	else if (!(Length))
	  Endpoint_ClearIN();

11
	while (Length || LastPacketFull)
12
	{
13
14
15
16
17
		if (Endpoint_IsSETUPReceived())
		  return ENDPOINT_RWCSTREAM_HostAborted;

		if (Endpoint_IsOUTReceived())
		  break;
18
		
19
20
		if (USB_DeviceState == DEVICE_STATE_Unattached)
		  return ENDPOINT_RWCSTREAM_DeviceDisconnected;
21
22
		else if (USB_DeviceState == DEVICE_STATE_Suspended)
		  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
31
			{
				TEMPLATE_TRANSFER_BYTE(DataStream);
				Length--;
32
				BytesInEndpoint++;
33
34
			}
			
35
			LastPacketFull = (BytesInEndpoint == USB_ControlEndpointSize);
36
			Endpoint_ClearIN();
37
		}
38
39
	}
	
40
41
42
	while (!(Endpoint_IsOUTReceived()))
	{
		if (USB_DeviceState == DEVICE_STATE_Unattached)
43
44
45
		  return ENDPOINT_RWCSTREAM_DeviceDisconnected;
		else if (USB_DeviceState == DEVICE_STATE_Suspended)
		  return ENDPOINT_RWCSTREAM_BusSuspended;
46
	}
47
48
49
50
51
52
53

	return ENDPOINT_RWCSTREAM_NoError;
}

#undef TEMPLATE_BUFFER_OFFSET
#undef TEMPLATE_FUNC_NAME
#undef TEMPLATE_TRANSFER_BYTE