Split out endpoint and pipe stream functions into new EndpointStream.c/.h and PipeStr...
[pub/USBasp.git] / LUFA / Drivers / USB / HighLevel / PipeStream.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2010.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2010 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 __CALLBACK_PARAM)
40 {
41 uint8_t ErrorCode;
42
43 Pipe_SetPipeToken(PIPE_TOKEN_IN);
44
45 if ((ErrorCode = Pipe_WaitUntilReady()))
46 return ErrorCode;
47
48 #if defined(FAST_STREAM_TRANSFERS)
49 uint8_t BytesRemToAlignment = (Pipe_BytesInPipe() & 0x07);
50
51 if (Length >= 8)
52 {
53 Length -= BytesRemToAlignment;
54
55 switch (BytesRemToAlignment)
56 {
57 default:
58 do
59 {
60 if (!(Pipe_IsReadWriteAllowed()))
61 {
62 Pipe_ClearIN();
63
64 #if !defined(NO_STREAM_CALLBACKS)
65 if ((Callback != NULL) && (Callback() == STREAMCALLBACK_Abort))
66 return PIPE_RWSTREAM_CallbackAborted;
67 #endif
68
69 if ((ErrorCode = Pipe_WaitUntilReady()))
70 return ErrorCode;
71 }
72
73 Length -= 8;
74
75 Pipe_Discard_Byte();
76 case 7: Pipe_Discard_Byte();
77 case 6: Pipe_Discard_Byte();
78 case 5: Pipe_Discard_Byte();
79 case 4: Pipe_Discard_Byte();
80 case 3: Pipe_Discard_Byte();
81 case 2: Pipe_Discard_Byte();
82 case 1: Pipe_Discard_Byte();
83 } while (Length >= 8);
84 }
85 }
86 #endif
87
88 while (Length)
89 {
90 if (!(Pipe_IsReadWriteAllowed()))
91 {
92 Pipe_ClearIN();
93
94 #if !defined(NO_STREAM_CALLBACKS)
95 if ((Callback != NULL) && (Callback() == STREAMCALLBACK_Abort))
96 return PIPE_RWSTREAM_CallbackAborted;
97 #endif
98
99 if ((ErrorCode = Pipe_WaitUntilReady()))
100 return ErrorCode;
101 }
102 else
103 {
104 Pipe_Discard_Byte();
105 Length--;
106 }
107 }
108
109 return PIPE_RWSTREAM_NoError;
110 }
111
112 /* The following abuses the C preprocessor in order to copy-past common code with slight alterations,
113 * so that the code needs to be written once. It is a crude form of templating to reduce code maintenance. */
114
115 #define TEMPLATE_FUNC_NAME Pipe_Write_Stream_LE
116 #define TEMPLATE_BUFFER_TYPE const void*
117 #define TEMPLATE_TOKEN PIPE_TOKEN_OUT
118 #define TEMPLATE_CLEAR_PIPE() Pipe_ClearOUT()
119 #define TEMPLATE_BUFFER_OFFSET(Length) 0
120 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Pipe_Write_Byte(*((uint8_t*)BufferPtr++))
121 #include "Template/Template_Pipe_RW.c"
122
123 #define TEMPLATE_FUNC_NAME Pipe_Write_PStream_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_TRANSFER_BYTE(BufferPtr) Pipe_Write_Byte(pgm_read_byte((uint8_t*)BufferPtr++))
129 #include "Template/Template_Pipe_RW.c"
130
131 #define TEMPLATE_FUNC_NAME Pipe_Write_EStream_LE
132 #define TEMPLATE_BUFFER_TYPE const void*
133 #define TEMPLATE_TOKEN PIPE_TOKEN_OUT
134 #define TEMPLATE_CLEAR_PIPE() Pipe_ClearOUT()
135 #define TEMPLATE_BUFFER_OFFSET(Length) 0
136 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Pipe_Write_Byte(eeprom_read_byte((uint8_t*)BufferPtr++))
137 #include "Template/Template_Pipe_RW.c"
138
139 #define TEMPLATE_FUNC_NAME Pipe_Write_Stream_BE
140 #define TEMPLATE_BUFFER_TYPE const void*
141 #define TEMPLATE_TOKEN PIPE_TOKEN_OUT
142 #define TEMPLATE_CLEAR_PIPE() Pipe_ClearOUT()
143 #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
144 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Pipe_Write_Byte(*((uint8_t*)BufferPtr--))
145 #include "Template/Template_Pipe_RW.c"
146
147 #define TEMPLATE_FUNC_NAME Pipe_Write_PStream_BE
148 #define TEMPLATE_BUFFER_TYPE const void*
149 #define TEMPLATE_TOKEN PIPE_TOKEN_OUT
150 #define TEMPLATE_CLEAR_PIPE() Pipe_ClearOUT()
151 #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
152 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Pipe_Write_Byte(pgm_read_byte((uint8_t*)BufferPtr--))
153 #include "Template/Template_Pipe_RW.c"
154
155 #define TEMPLATE_FUNC_NAME Pipe_Write_EStream_BE
156 #define TEMPLATE_BUFFER_TYPE const void*
157 #define TEMPLATE_TOKEN PIPE_TOKEN_OUT
158 #define TEMPLATE_CLEAR_PIPE() Pipe_ClearOUT()
159 #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
160 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Pipe_Write_Byte(eeprom_read_byte((uint8_t*)BufferPtr--))
161 #include "Template/Template_Pipe_RW.c"
162
163 #define TEMPLATE_FUNC_NAME Pipe_Read_Stream_LE
164 #define TEMPLATE_BUFFER_TYPE void*
165 #define TEMPLATE_TOKEN PIPE_TOKEN_IN
166 #define TEMPLATE_CLEAR_PIPE() Pipe_ClearIN()
167 #define TEMPLATE_BUFFER_OFFSET(Length) 0
168 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) *((uint8_t*)BufferPtr++) = Pipe_Read_Byte()
169 #include "Template/Template_Pipe_RW.c"
170
171 #define TEMPLATE_FUNC_NAME Pipe_Read_EStream_LE
172 #define TEMPLATE_BUFFER_TYPE void*
173 #define TEMPLATE_TOKEN PIPE_TOKEN_IN
174 #define TEMPLATE_CLEAR_PIPE() Pipe_ClearIN()
175 #define TEMPLATE_BUFFER_OFFSET(Length) 0
176 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) eeprom_update_byte((uint8_t*)BufferPtr++, Pipe_Read_Byte())
177 #include "Template/Template_Pipe_RW.c"
178
179 #define TEMPLATE_FUNC_NAME Pipe_Read_Stream_BE
180 #define TEMPLATE_BUFFER_TYPE void*
181 #define TEMPLATE_TOKEN PIPE_TOKEN_IN
182 #define TEMPLATE_CLEAR_PIPE() Pipe_ClearIN()
183 #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
184 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) *((uint8_t*)BufferPtr--) = Pipe_Read_Byte()
185 #include "Template/Template_Pipe_RW.c"
186
187 #define TEMPLATE_FUNC_NAME Pipe_Read_EStream_BE
188 #define TEMPLATE_BUFFER_TYPE void*
189 #define TEMPLATE_TOKEN PIPE_TOKEN_IN
190 #define TEMPLATE_CLEAR_PIPE() Pipe_ClearIN()
191 #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
192 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) eeprom_update_byte((uint8_t*)BufferPtr--, Pipe_Read_Byte())
193 #include "Template/Template_Pipe_RW.c"
194
195 #endif