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