Template_Endpoint_Control_W.c 1.56 KB
Newer Older
1
uint8_t TEMPLATE_FUNC_NAME (const void* const Buffer,
2
                            uint16_t Length)
3
4
5
{
	uint8_t* DataStream     = ((uint8_t*)Buffer + TEMPLATE_BUFFER_OFFSET(Length));
	bool     LastPacketFull = false;
6

7
8
9
10
11
12
13
	if (Length > USB_ControlRequest.wLength)
	  Length = USB_ControlRequest.wLength;
	else if (!(Length))
	  Endpoint_ClearIN();

	while (Length || LastPacketFull)
	{
14
		uint8_t USB_DeviceState_LCL = USB_DeviceState;
15

16
		if (USB_DeviceState_LCL == DEVICE_STATE_Unattached)
17
		  return ENDPOINT_RWCSTREAM_DeviceDisconnected;
18
		else if (USB_DeviceState_LCL == DEVICE_STATE_Suspended)
19
		  return ENDPOINT_RWCSTREAM_BusSuspended;
20
21
22
23
24
		else if (Endpoint_IsSETUPReceived())
		  return ENDPOINT_RWCSTREAM_HostAborted;
		else if (Endpoint_IsOUTReceived())
		  break;

25
26
		if (Endpoint_IsINReady())
		{
27
			uint16_t BytesInEndpoint = Endpoint_BytesInEndpoint();
28

29
30
31
			while (Length && (BytesInEndpoint < USB_ControlEndpointSize))
			{
				TEMPLATE_TRANSFER_BYTE(DataStream);
32
				TEMPLATE_BUFFER_MOVE(DataStream, 1);
33
34
35
				Length--;
				BytesInEndpoint++;
			}
36

37
38
39
40
			LastPacketFull = (BytesInEndpoint == USB_ControlEndpointSize);
			Endpoint_ClearIN();
		}
	}
41

42
43
	while (!(Endpoint_IsOUTReceived()))
	{
44
45
46
		uint8_t USB_DeviceState_LCL = USB_DeviceState;

		if (USB_DeviceState_LCL == DEVICE_STATE_Unattached)
47
		  return ENDPOINT_RWCSTREAM_DeviceDisconnected;
48
		else if (USB_DeviceState_LCL == DEVICE_STATE_Suspended)
49
50
51
52
53
54
55
		  return ENDPOINT_RWCSTREAM_BusSuspended;
	}

	return ENDPOINT_RWCSTREAM_NoError;
}

#undef TEMPLATE_BUFFER_OFFSET
56
#undef TEMPLATE_BUFFER_MOVE
57
#undef TEMPLATE_FUNC_NAME
58
#undef TEMPLATE_TRANSFER_BYTE