Altered all endpoint/pipe stream transfers so that the new BytesProcessed parameter...
[pub/USBasp.git] / LUFA / Drivers / USB / HighLevel / PipeStream.c
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 #define __INCLUDE_FROM_USB_DRIVER
32 #include "USBMode.h"
33
34 #if defined(USB_CAN_BE_HOST)
35
36 #include "PipeStream.h"
37
38 uint8_t Pipe_Discard_Stream(uint16_t Length,
39 uint16_t* const BytesProcessed)
40 {
41 uint8_t ErrorCode;
42 uint16_t BytesInTransfer = 0;
43
44 Pipe_SetPipeToken(PIPE_TOKEN_IN);
45
46 if ((ErrorCode = Pipe_WaitUntilReady()))
47 return ErrorCode;
48
49 if (BytesProcessed != NULL)
50 Length -= *BytesProcessed;
51
52 while (Length)
53 {
54 if (!(Pipe_IsReadWriteAllowed()))
55 {
56 Pipe_ClearIN();
57
58 if (BytesProcessed != NULL)
59 {
60 *BytesProcessed += BytesInTransfer;
61 return PIPE_RWSTREAM_IncompleteTransfer;
62 }
63
64 if ((ErrorCode = Pipe_WaitUntilReady()))
65 return ErrorCode;
66 }
67 else
68 {
69 Pipe_Discard_Byte();
70
71 Length--;
72 BytesInTransfer++;
73 }
74 }
75
76 return PIPE_RWSTREAM_NoError;
77 }
78
79 uint8_t Pipe_Null_Stream(uint16_t Length,
80 uint16_t* const BytesProcessed)
81 {
82 uint8_t ErrorCode;
83 uint16_t BytesInTransfer = 0;
84
85 Pipe_SetPipeToken(PIPE_TOKEN_OUT);
86
87 if ((ErrorCode = Pipe_WaitUntilReady()))
88 return ErrorCode;
89
90 if (BytesProcessed != NULL)
91 Length -= *BytesProcessed;
92
93 while (Length)
94 {
95 if (!(Pipe_IsReadWriteAllowed()))
96 {
97 Pipe_ClearOUT();
98
99 if (BytesProcessed != NULL)
100 {
101 *BytesProcessed += BytesInTransfer;
102 return PIPE_RWSTREAM_IncompleteTransfer;
103 }
104
105 if ((ErrorCode = Pipe_WaitUntilReady()))
106 return ErrorCode;
107 }
108 else
109 {
110 Pipe_Write_Byte(0);
111
112 Length--;
113 BytesInTransfer++;
114 }
115 }
116
117 return PIPE_RWSTREAM_NoError;
118 }
119
120 /* The following abuses the C preprocessor in order to copy-past common code with slight alterations,
121 * so that the code needs to be written once. It is a crude form of templating to reduce code maintenance. */
122
123 #define TEMPLATE_FUNC_NAME Pipe_Write_Stream_LE
124 #define TEMPLATE_BUFFER_TYPE const void*
125 #define TEMPLATE_TOKEN PIPE_TOKEN_OUT
126 #define TEMPLATE_CLEAR_PIPE() Pipe_ClearOUT()
127 #define TEMPLATE_BUFFER_OFFSET(Length) 0
128 #define TEMPLATE_BUFFER_MOVE(BufferPtr, Amount) DataStream += Amount
129 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Pipe_Write_Byte(*BufferPtr)
130 #include "Template/Template_Pipe_RW.c"
131
132 #define TEMPLATE_FUNC_NAME Pipe_Write_PStream_LE
133 #define TEMPLATE_BUFFER_TYPE const void*
134 #define TEMPLATE_TOKEN PIPE_TOKEN_OUT
135 #define TEMPLATE_CLEAR_PIPE() Pipe_ClearOUT()
136 #define TEMPLATE_BUFFER_OFFSET(Length) 0
137 #define TEMPLATE_BUFFER_MOVE(BufferPtr, Amount) DataStream += Amount
138 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Pipe_Write_Byte(pgm_read_byte(BufferPtr))
139 #include "Template/Template_Pipe_RW.c"
140
141 #define TEMPLATE_FUNC_NAME Pipe_Write_EStream_LE
142 #define TEMPLATE_BUFFER_TYPE const void*
143 #define TEMPLATE_TOKEN PIPE_TOKEN_OUT
144 #define TEMPLATE_CLEAR_PIPE() Pipe_ClearOUT()
145 #define TEMPLATE_BUFFER_OFFSET(Length) 0
146 #define TEMPLATE_BUFFER_MOVE(BufferPtr, Amount) DataStream += Amount
147 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Pipe_Write_Byte(eeprom_read_byte(BufferPtr))
148 #include "Template/Template_Pipe_RW.c"
149
150 #define TEMPLATE_FUNC_NAME Pipe_Write_Stream_BE
151 #define TEMPLATE_BUFFER_TYPE const void*
152 #define TEMPLATE_TOKEN PIPE_TOKEN_OUT
153 #define TEMPLATE_CLEAR_PIPE() Pipe_ClearOUT()
154 #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
155 #define TEMPLATE_BUFFER_MOVE(BufferPtr, Amount) DataStream -= Amount
156 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Pipe_Write_Byte(*BufferPtr)
157 #include "Template/Template_Pipe_RW.c"
158
159 #define TEMPLATE_FUNC_NAME Pipe_Write_PStream_BE
160 #define TEMPLATE_BUFFER_TYPE const void*
161 #define TEMPLATE_TOKEN PIPE_TOKEN_OUT
162 #define TEMPLATE_CLEAR_PIPE() Pipe_ClearOUT()
163 #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
164 #define TEMPLATE_BUFFER_MOVE(BufferPtr, Amount) DataStream -= Amount
165 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Pipe_Write_Byte(pgm_read_byte(BufferPtr))
166 #include "Template/Template_Pipe_RW.c"
167
168 #define TEMPLATE_FUNC_NAME Pipe_Write_EStream_BE
169 #define TEMPLATE_BUFFER_TYPE const void*
170 #define TEMPLATE_TOKEN PIPE_TOKEN_OUT
171 #define TEMPLATE_CLEAR_PIPE() Pipe_ClearOUT()
172 #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
173 #define TEMPLATE_BUFFER_MOVE(BufferPtr, Amount) DataStream -= Amount
174 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Pipe_Write_Byte(eeprom_read_byte(BufferPtr))
175 #include "Template/Template_Pipe_RW.c"
176
177 #define TEMPLATE_FUNC_NAME Pipe_Read_Stream_LE
178 #define TEMPLATE_BUFFER_TYPE void*
179 #define TEMPLATE_TOKEN PIPE_TOKEN_IN
180 #define TEMPLATE_CLEAR_PIPE() Pipe_ClearIN()
181 #define TEMPLATE_BUFFER_OFFSET(Length) 0
182 #define TEMPLATE_BUFFER_MOVE(BufferPtr, Amount) DataStream += Amount
183 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) *BufferPtr = Pipe_Read_Byte()
184 #include "Template/Template_Pipe_RW.c"
185
186 #define TEMPLATE_FUNC_NAME Pipe_Read_EStream_LE
187 #define TEMPLATE_BUFFER_TYPE void*
188 #define TEMPLATE_TOKEN PIPE_TOKEN_IN
189 #define TEMPLATE_CLEAR_PIPE() Pipe_ClearIN()
190 #define TEMPLATE_BUFFER_OFFSET(Length) 0
191 #define TEMPLATE_BUFFER_MOVE(BufferPtr, Amount) DataStream += Amount
192 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) eeprom_update_byte(BufferPtr, Pipe_Read_Byte())
193 #include "Template/Template_Pipe_RW.c"
194
195 #define TEMPLATE_FUNC_NAME Pipe_Read_Stream_BE
196 #define TEMPLATE_BUFFER_TYPE void*
197 #define TEMPLATE_TOKEN PIPE_TOKEN_IN
198 #define TEMPLATE_CLEAR_PIPE() Pipe_ClearIN()
199 #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
200 #define TEMPLATE_BUFFER_MOVE(BufferPtr, Amount) DataStream -= Amount
201 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) *BufferPtr = Pipe_Read_Byte()
202 #include "Template/Template_Pipe_RW.c"
203
204 #define TEMPLATE_FUNC_NAME Pipe_Read_EStream_BE
205 #define TEMPLATE_BUFFER_TYPE void*
206 #define TEMPLATE_TOKEN PIPE_TOKEN_IN
207 #define TEMPLATE_CLEAR_PIPE() Pipe_ClearIN()
208 #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
209 #define TEMPLATE_BUFFER_MOVE(BufferPtr, Amount) DataStream -= Amount
210 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) eeprom_update_byte(BufferPtr, Pipe_Read_Byte())
211 #include "Template/Template_Pipe_RW.c"
212
213 #endif
214