Documentation improvements - put driver example code into its own section, fix incorr...
[pub/USBasp.git] / LUFA / Drivers / USB / HighLevel / EndpointStream.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2010.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
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_DEVICE)
35
36 #include "EndpointStream.h"
37
38 #if !defined(CONTROL_ONLY_DEVICE)
39 uint8_t Endpoint_Discard_Stream(uint16_t Length
40 __CALLBACK_PARAM)
41 {
42 uint8_t ErrorCode;
43
44 if ((ErrorCode = Endpoint_WaitUntilReady()))
45 return ErrorCode;
46
47 #if defined(FAST_STREAM_TRANSFERS)
48 uint8_t BytesRemToAlignment = (Endpoint_BytesInEndpoint() & 0x07);
49
50 if (Length >= 8)
51 {
52 Length -= BytesRemToAlignment;
53
54 switch (BytesRemToAlignment)
55 {
56 default:
57 do
58 {
59 if (!(Endpoint_IsReadWriteAllowed()))
60 {
61 Endpoint_ClearOUT();
62
63 #if !defined(NO_STREAM_CALLBACKS)
64 if ((Callback != NULL) && (Callback() == STREAMCALLBACK_Abort))
65 return ENDPOINT_RWSTREAM_CallbackAborted;
66 #endif
67
68 if ((ErrorCode = Endpoint_WaitUntilReady()))
69 return ErrorCode;
70 }
71
72 Length -= 8;
73
74 Endpoint_Discard_Byte();
75 case 7: Endpoint_Discard_Byte();
76 case 6: Endpoint_Discard_Byte();
77 case 5: Endpoint_Discard_Byte();
78 case 4: Endpoint_Discard_Byte();
79 case 3: Endpoint_Discard_Byte();
80 case 2: Endpoint_Discard_Byte();
81 case 1: Endpoint_Discard_Byte();
82 } while (Length >= 8);
83 }
84 }
85 #endif
86
87 while (Length)
88 {
89 if (!(Endpoint_IsReadWriteAllowed()))
90 {
91 Endpoint_ClearOUT();
92
93 #if !defined(NO_STREAM_CALLBACKS)
94 if ((Callback != NULL) && (Callback() == STREAMCALLBACK_Abort))
95 return ENDPOINT_RWSTREAM_CallbackAborted;
96 #endif
97
98 if ((ErrorCode = Endpoint_WaitUntilReady()))
99 return ErrorCode;
100 }
101 else
102 {
103 Endpoint_Discard_Byte();
104 Length--;
105 }
106 }
107
108 return ENDPOINT_RWSTREAM_NoError;
109 }
110
111 #define TEMPLATE_FUNC_NAME Endpoint_Write_Stream_LE
112 #define TEMPLATE_BUFFER_TYPE const void*
113 #define TEMPLATE_CLEAR_ENDPOINT() Endpoint_ClearIN()
114 #define TEMPLATE_BUFFER_OFFSET(Length) 0
115 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Endpoint_Write_Byte(*((uint8_t*)BufferPtr++))
116 #include "Template/Template_Endpoint_RW.c"
117
118 #define TEMPLATE_FUNC_NAME Endpoint_Write_PStream_LE
119 #define TEMPLATE_BUFFER_TYPE const void*
120 #define TEMPLATE_CLEAR_ENDPOINT() Endpoint_ClearIN()
121 #define TEMPLATE_BUFFER_OFFSET(Length) 0
122 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Endpoint_Write_Byte(pgm_read_byte((uint8_t*)BufferPtr++))
123 #include "Template/Template_Endpoint_RW.c"
124
125 #define TEMPLATE_FUNC_NAME Endpoint_Write_EStream_LE
126 #define TEMPLATE_BUFFER_TYPE const void*
127 #define TEMPLATE_CLEAR_ENDPOINT() Endpoint_ClearIN()
128 #define TEMPLATE_BUFFER_OFFSET(Length) 0
129 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Endpoint_Write_Byte(eeprom_read_byte((uint8_t*)BufferPtr++))
130 #include "Template/Template_Endpoint_RW.c"
131
132 #define TEMPLATE_FUNC_NAME Endpoint_Write_Stream_BE
133 #define TEMPLATE_BUFFER_TYPE const void*
134 #define TEMPLATE_CLEAR_ENDPOINT() Endpoint_ClearIN()
135 #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
136 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Endpoint_Write_Byte(*((uint8_t*)BufferPtr--))
137 #include "Template/Template_Endpoint_RW.c"
138
139 #define TEMPLATE_FUNC_NAME Endpoint_Write_EStream_BE
140 #define TEMPLATE_BUFFER_TYPE const void*
141 #define TEMPLATE_CLEAR_ENDPOINT() Endpoint_ClearIN()
142 #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
143 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Endpoint_Write_Byte(eeprom_read_byte((uint8_t*)BufferPtr--))
144 #include "Template/Template_Endpoint_RW.c"
145
146 #define TEMPLATE_FUNC_NAME Endpoint_Write_PStream_BE
147 #define TEMPLATE_BUFFER_TYPE const void*
148 #define TEMPLATE_CLEAR_ENDPOINT() Endpoint_ClearIN()
149 #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
150 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Endpoint_Write_Byte(pgm_read_byte((uint8_t*)BufferPtr--))
151 #include "Template/Template_Endpoint_RW.c"
152
153 #define TEMPLATE_FUNC_NAME Endpoint_Read_Stream_LE
154 #define TEMPLATE_BUFFER_TYPE void*
155 #define TEMPLATE_CLEAR_ENDPOINT() Endpoint_ClearOUT()
156 #define TEMPLATE_BUFFER_OFFSET(Length) 0
157 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) *((uint8_t*)BufferPtr++) = Endpoint_Read_Byte()
158 #include "Template/Template_Endpoint_RW.c"
159
160 #define TEMPLATE_FUNC_NAME Endpoint_Read_EStream_LE
161 #define TEMPLATE_BUFFER_TYPE void*
162 #define TEMPLATE_CLEAR_ENDPOINT() Endpoint_ClearOUT()
163 #define TEMPLATE_BUFFER_OFFSET(Length) 0
164 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) eeprom_update_byte((uint8_t*)BufferPtr++, Endpoint_Read_Byte())
165 #include "Template/Template_Endpoint_RW.c"
166
167 #define TEMPLATE_FUNC_NAME Endpoint_Read_Stream_BE
168 #define TEMPLATE_BUFFER_TYPE void*
169 #define TEMPLATE_CLEAR_ENDPOINT() Endpoint_ClearOUT()
170 #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
171 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) *((uint8_t*)BufferPtr--) = Endpoint_Read_Byte()
172 #include "Template/Template_Endpoint_RW.c"
173
174 #define TEMPLATE_FUNC_NAME Endpoint_Read_EStream_BE
175 #define TEMPLATE_BUFFER_TYPE void*
176 #define TEMPLATE_CLEAR_ENDPOINT() Endpoint_ClearOUT()
177 #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
178 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) eeprom_update_byte((uint8_t*)BufferPtr--, Endpoint_Read_Byte())
179 #include "Template/Template_Endpoint_RW.c"
180
181 #endif
182
183 #define TEMPLATE_FUNC_NAME Endpoint_Write_Control_Stream_LE
184 #define TEMPLATE_BUFFER_OFFSET(Length) 0
185 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Endpoint_Write_Byte(*((uint8_t*)BufferPtr++))
186 #include "Template/Template_Endpoint_Control_W.c"
187
188 #define TEMPLATE_FUNC_NAME Endpoint_Write_Control_PStream_LE
189 #define TEMPLATE_BUFFER_OFFSET(Length) 0
190 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Endpoint_Write_Byte(pgm_read_byte((uint8_t*)BufferPtr++))
191 #include "Template/Template_Endpoint_Control_W.c"
192
193 #define TEMPLATE_FUNC_NAME Endpoint_Write_Control_EStream_LE
194 #define TEMPLATE_BUFFER_OFFSET(Length) 0
195 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Endpoint_Write_Byte(eeprom_read_byte((uint8_t*)BufferPtr++))
196 #include "Template/Template_Endpoint_Control_W.c"
197
198 #define TEMPLATE_FUNC_NAME Endpoint_Write_Control_Stream_BE
199 #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
200 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Endpoint_Write_Byte(*((uint8_t*)BufferPtr--))
201 #include "Template/Template_Endpoint_Control_W.c"
202
203 #define TEMPLATE_FUNC_NAME Endpoint_Write_Control_PStream_BE
204 #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
205 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Endpoint_Write_Byte(pgm_read_byte((uint8_t*)BufferPtr--))
206 #include "Template/Template_Endpoint_Control_W.c"
207
208 #define TEMPLATE_FUNC_NAME Endpoint_Write_Control_EStream_BE
209 #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
210 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) Endpoint_Write_Byte(eeprom_read_byte((uint8_t*)BufferPtr--))
211 #include "Template/Template_Endpoint_Control_W.c"
212
213 #define TEMPLATE_FUNC_NAME Endpoint_Read_Control_Stream_LE
214 #define TEMPLATE_BUFFER_OFFSET(Length) 0
215 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) *((uint8_t*)BufferPtr++) = Endpoint_Read_Byte()
216 #include "Template/Template_Endpoint_Control_R.c"
217
218 #define TEMPLATE_FUNC_NAME Endpoint_Read_Control_EStream_LE
219 #define TEMPLATE_BUFFER_OFFSET(Length) 0
220 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) eeprom_update_byte((uint8_t*)BufferPtr++, Endpoint_Read_Byte())
221 #include "Template/Template_Endpoint_Control_R.c"
222
223 #define TEMPLATE_FUNC_NAME Endpoint_Read_Control_Stream_BE
224 #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
225 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) *((uint8_t*)BufferPtr--) = Endpoint_Read_Byte()
226 #include "Template/Template_Endpoint_Control_R.c"
227
228 #define TEMPLATE_FUNC_NAME Endpoint_Read_Control_EStream_BE
229 #define TEMPLATE_BUFFER_OFFSET(Length) (Length - 1)
230 #define TEMPLATE_TRANSFER_BYTE(BufferPtr) eeprom_update_byte((uint8_t*)BufferPtr--, Endpoint_Read_Byte())
231 #include "Template/Template_Endpoint_Control_R.c"
232
233 #endif