Statistics
| Branch: | Tag: | Revision:

root / LUFA / Drivers / USB / Core / XMEGA / Template / Template_Endpoint_Control_W.c @ 978b99e5

History | View | Annotate | Download (2.86 KB)

1
/*
2
             LUFA Library
3
     Copyright (C) Dean Camera, 2011.
4

5
  dean [at] fourwalledcubicle [dot] com
6
           www.lufa-lib.org
7
*/
8

    
9
/*
10
  Copyright 2011  Dean Camera (dean [at] fourwalledcubicle [dot] com)
11

12
  Permission to use, copy, modify, distribute, and sell this
13
  software and its documentation for any purpose is hereby granted
14
  without fee, provided that the above copyright notice appear in
15
  all copies and that both that the copyright notice and this
16
  permission notice and warranty disclaimer appear in supporting
17
  documentation, and that the name of the author not be used in
18
  advertising or publicity pertaining to distribution of the
19
  software without specific, written prior permission.
20

21
  The author disclaim all warranties with regard to this
22
  software, including all implied warranties of merchantability
23
  and fitness.  In no event shall the author be liable for any
24
  special, indirect or consequential damages or any damages
25
  whatsoever resulting from loss of use, data or profits, whether
26
  in an action of contract, negligence or other tortious action,
27
  arising out of or in connection with the use or performance of
28
  this software.
29
*/
30

    
31
#if defined(TEMPLATE_FUNC_NAME)
32

    
33
uint8_t TEMPLATE_FUNC_NAME (const void* const Buffer,
34
                            uint16_t Length)
35
{
36
        uint8_t* DataStream     = ((uint8_t*)Buffer + TEMPLATE_BUFFER_OFFSET(Length));
37
        bool     LastPacketFull = false;
38

    
39
        if (Length > USB_ControlRequest.wLength)
40
          Length = USB_ControlRequest.wLength;
41
        else if (!(Length))
42
          Endpoint_ClearIN();
43

    
44
        while (Length || LastPacketFull)
45
        {
46
                uint8_t USB_DeviceState_LCL = USB_DeviceState;
47

    
48
                if (USB_DeviceState_LCL == DEVICE_STATE_Unattached)
49
                  return ENDPOINT_RWCSTREAM_DeviceDisconnected;
50
                else if (USB_DeviceState_LCL == DEVICE_STATE_Suspended)
51
                  return ENDPOINT_RWCSTREAM_BusSuspended;
52
                else if (Endpoint_IsSETUPReceived())
53
                  return ENDPOINT_RWCSTREAM_HostAborted;
54
                else if (Endpoint_IsOUTReceived())
55
                  break;
56

    
57
                if (Endpoint_IsINReady())
58
                {
59
                        uint16_t BytesInEndpoint = Endpoint_BytesInEndpoint();
60

    
61
                        while (Length && (BytesInEndpoint < USB_Device_ControlEndpointSize))
62
                        {
63
                                TEMPLATE_TRANSFER_BYTE(DataStream);
64
                                TEMPLATE_BUFFER_MOVE(DataStream, 1);
65
                                Length--;
66
                                BytesInEndpoint++;
67
                        }
68

    
69
                        LastPacketFull = (BytesInEndpoint == USB_Device_ControlEndpointSize);
70
                        Endpoint_ClearIN();
71
                }
72
        }
73

    
74
        while (!(Endpoint_IsOUTReceived()))
75
        {
76
                uint8_t USB_DeviceState_LCL = USB_DeviceState;
77

    
78
                if (USB_DeviceState_LCL == DEVICE_STATE_Unattached)
79
                  return ENDPOINT_RWCSTREAM_DeviceDisconnected;
80
                else if (USB_DeviceState_LCL == DEVICE_STATE_Suspended)
81
                  return ENDPOINT_RWCSTREAM_BusSuspended;
82
        }
83

    
84
        return ENDPOINT_RWCSTREAM_NoError;
85
}
86

    
87
#undef TEMPLATE_BUFFER_OFFSET
88
#undef TEMPLATE_BUFFER_MOVE
89
#undef TEMPLATE_FUNC_NAME
90
#undef TEMPLATE_TRANSFER_BYTE
91

    
92
#endif