Add new tag for the LUFA-120219-BETA release.
[pub/USBasp.git] / LUFA / Drivers / USB / Core / UC3 / EndpointStream_UC3.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2012.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2012 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 #define __INCLUDE_FROM_USB_DRIVER
32 #include "../USBMode.h"
33
34 #if defined(USB_CAN_BE_DEVICE)
35
36 #include "EndpointStream_UC3.h"
37
38 #if !defined(CONTROL_ONLY_DEVICE)
39 uint8_t Endpoint_Discard_Stream(uint16_t Length,
40 uint16_t* const BytesProcessed)
41 {
42 uint8_t ErrorCode;
43 uint16_t BytesInTransfer = 0;
44
45 if ((ErrorCode = Endpoint_WaitUntilReady()))
46 return ErrorCode;
47
48 if (BytesProcessed != NULL)
49 Length -= *BytesProcessed;
50
51 while (Length)
52 {
53 if (!(Endpoint_IsReadWriteAllowed()))
54 {
55 Endpoint_ClearOUT();
56
57 if (BytesProcessed != NULL)
58 {
59 *BytesProcessed += BytesInTransfer;
60 return ENDPOINT_RWSTREAM_IncompleteTransfer;
61 }
62
63 if ((ErrorCode = Endpoint_WaitUntilReady()))
64 return ErrorCode;
65 }
66 else
67 {
68 Endpoint_Discard_8();
69
70 Length--;
71 BytesInTransfer++;
72 }
73 }
74
75 return ENDPOINT_RWSTREAM_NoError;
76 }
77
78 uint8_t Endpoint_Null_Stream(uint16_t Length,
79 uint16_t* const BytesProcessed)
80 {
81 uint8_t ErrorCode;
82 uint16_t BytesInTransfer = 0;
83
84 if ((ErrorCode = Endpoint_WaitUntilReady()))
85 return ErrorCode;
86
87 if (BytesProcessed != NULL)
88 Length -= *BytesProcessed;
89
90 while (Length)
91 {
92 if (!(Endpoint_IsReadWriteAllowed()))
93 {
94 Endpoint_ClearIN();
95
96 if (BytesProcessed != NULL)
97 {
98 *BytesProcessed += BytesInTransfer;
99 return ENDPOINT_RWSTREAM_IncompleteTransfer;
100 }
101
102 if ((ErrorCode = Endpoint_WaitUntilReady()))
103 return ErrorCode;
104 }
105 else
106 {
107 Endpoint_Write_8(0);
108
109 Length--;
110 BytesInTransfer++;
111 }
112 }
113
114 return ENDPOINT_RWSTREAM_NoError;
115 }
116
117 /* The following abuses the C preprocessor in order to copy-paste common code with slight alterations,
118 * so that the code needs to be written once. It is a crude form of templating to reduce code maintenance. */
119
120 #define TEMPLATE_FUNC_NAME Endpoint_Write_Stream_LE
121 #define TEMPLATE_BUFFER_TYPE const void*
122 #define TEMPLATE_CLEAR_ENDPOINT() Endpoint_ClearIN()
123 #define TEMPLATE_BUFFER_OFFSET(Length) 0
124 #define TEMPLATE_BUFFER_MOVE(BufferPtr, Amount) BufferPtr += Amount
125 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Endpoint_Write_8(*BufferPtr)
126 #include "Template/Template_Endpoint_RW.c"
127
128 #define TEMPLATE_FUNC_NAME Endpoint_Write_Stream_BE
129 #define TEMPLATE_BUFFER_TYPE const void*
130 #define TEMPLATE_CLEAR_ENDPOINT() Endpoint_ClearIN()
131 #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
132 #define TEMPLATE_BUFFER_MOVE(BufferPtr, Amount) BufferPtr -= Amount
133 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Endpoint_Write_8(*BufferPtr)
134 #include "Template/Template_Endpoint_RW.c"
135
136 #define TEMPLATE_FUNC_NAME Endpoint_Read_Stream_LE
137 #define TEMPLATE_BUFFER_TYPE void*
138 #define TEMPLATE_CLEAR_ENDPOINT() Endpoint_ClearOUT()
139 #define TEMPLATE_BUFFER_OFFSET(Length) 0
140 #define TEMPLATE_BUFFER_MOVE(BufferPtr, Amount) BufferPtr += Amount
141 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) *BufferPtr = Endpoint_Read_8()
142 #include "Template/Template_Endpoint_RW.c"
143
144 #define TEMPLATE_FUNC_NAME Endpoint_Read_Stream_BE
145 #define TEMPLATE_BUFFER_TYPE void*
146 #define TEMPLATE_CLEAR_ENDPOINT() Endpoint_ClearOUT()
147 #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
148 #define TEMPLATE_BUFFER_MOVE(BufferPtr, Amount) BufferPtr -= Amount
149 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) *BufferPtr = Endpoint_Read_8()
150 #include "Template/Template_Endpoint_RW.c"
151
152 #if defined(ARCH_HAS_FLASH_ADDRESS_SPACE)
153 #define TEMPLATE_FUNC_NAME Endpoint_Write_PStream_LE
154 #define TEMPLATE_BUFFER_TYPE const void*
155 #define TEMPLATE_CLEAR_ENDPOINT() Endpoint_ClearIN()
156 #define TEMPLATE_BUFFER_OFFSET(Length) 0
157 #define TEMPLATE_BUFFER_MOVE(BufferPtr, Amount) BufferPtr += Amount
158 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Endpoint_Write_8(pgm_read_byte(BufferPtr))
159 #include "Template/Template_Endpoint_RW.c"
160
161 #define TEMPLATE_FUNC_NAME Endpoint_Write_PStream_BE
162 #define TEMPLATE_BUFFER_TYPE const void*
163 #define TEMPLATE_CLEAR_ENDPOINT() Endpoint_ClearIN()
164 #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
165 #define TEMPLATE_BUFFER_MOVE(BufferPtr, Amount) BufferPtr -= Amount
166 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Endpoint_Write_8(pgm_read_byte(BufferPtr))
167 #include "Template/Template_Endpoint_RW.c"
168 #endif
169
170 #if defined(ARCH_HAS_EEPROM_ADDRESS_SPACE)
171 #define TEMPLATE_FUNC_NAME Endpoint_Write_EStream_LE
172 #define TEMPLATE_BUFFER_TYPE const void*
173 #define TEMPLATE_CLEAR_ENDPOINT() Endpoint_ClearIN()
174 #define TEMPLATE_BUFFER_OFFSET(Length) 0
175 #define TEMPLATE_BUFFER_MOVE(BufferPtr, Amount) BufferPtr += Amount
176 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Endpoint_Write_8(eeprom_read_byte(BufferPtr))
177 #include "Template/Template_Endpoint_RW.c"
178
179 #define TEMPLATE_FUNC_NAME Endpoint_Write_EStream_BE
180 #define TEMPLATE_BUFFER_TYPE const void*
181 #define TEMPLATE_CLEAR_ENDPOINT() Endpoint_ClearIN()
182 #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
183 #define TEMPLATE_BUFFER_MOVE(BufferPtr, Amount) BufferPtr -= Amount
184 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Endpoint_Write_8(eeprom_read_byte(BufferPtr))
185 #include "Template/Template_Endpoint_RW.c"
186
187 #define TEMPLATE_FUNC_NAME Endpoint_Read_EStream_LE
188 #define TEMPLATE_BUFFER_TYPE void*
189 #define TEMPLATE_CLEAR_ENDPOINT() Endpoint_ClearOUT()
190 #define TEMPLATE_BUFFER_OFFSET(Length) 0
191 #define TEMPLATE_BUFFER_MOVE(BufferPtr, Amount) BufferPtr += Amount
192 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) eeprom_update_byte(BufferPtr, Endpoint_Read_8())
193 #include "Template/Template_Endpoint_RW.c"
194
195 #define TEMPLATE_FUNC_NAME Endpoint_Read_EStream_BE
196 #define TEMPLATE_BUFFER_TYPE void*
197 #define TEMPLATE_CLEAR_ENDPOINT() Endpoint_ClearOUT()
198 #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
199 #define TEMPLATE_BUFFER_MOVE(BufferPtr, Amount) BufferPtr -= Amount
200 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) eeprom_update_byte(BufferPtr, Endpoint_Read_8())
201 #include "Template/Template_Endpoint_RW.c"
202 #endif
203
204 #endif
205
206 #define TEMPLATE_FUNC_NAME Endpoint_Write_Control_Stream_LE
207 #define TEMPLATE_BUFFER_OFFSET(Length) 0
208 #define TEMPLATE_BUFFER_MOVE(BufferPtr, Amount) BufferPtr += Amount
209 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Endpoint_Write_8(*BufferPtr)
210 #include "Template/Template_Endpoint_Control_W.c"
211
212 #define TEMPLATE_FUNC_NAME Endpoint_Write_Control_Stream_BE
213 #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
214 #define TEMPLATE_BUFFER_MOVE(BufferPtr, Amount) BufferPtr -= Amount
215 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Endpoint_Write_8(*BufferPtr)
216 #include "Template/Template_Endpoint_Control_W.c"
217
218 #define TEMPLATE_FUNC_NAME Endpoint_Read_Control_Stream_LE
219 #define TEMPLATE_BUFFER_OFFSET(Length) 0
220 #define TEMPLATE_BUFFER_MOVE(BufferPtr, Amount) BufferPtr += Amount
221 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) *BufferPtr = Endpoint_Read_8()
222 #include "Template/Template_Endpoint_Control_R.c"
223
224 #define TEMPLATE_FUNC_NAME Endpoint_Read_Control_Stream_BE
225 #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
226 #define TEMPLATE_BUFFER_MOVE(BufferPtr, Amount) BufferPtr -= Amount
227 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) *BufferPtr = Endpoint_Read_8()
228 #include "Template/Template_Endpoint_Control_R.c"
229
230 #endif
231