]> git.donarmstrong.com Git - qmk_firmware.git/blob - protocol/lufa/LUFA-120730/LUFA/Drivers/USB/Core/AVR8/PipeStream_AVR8.c
Change TOP_DIR to TMK_DIR in makefiles
[qmk_firmware.git] / protocol / lufa / LUFA-120730 / LUFA / Drivers / USB / Core / AVR8 / PipeStream_AVR8.c
1 /*\r
2              LUFA Library\r
3      Copyright (C) Dean Camera, 2012.\r
4               \r
5   dean [at] fourwalledcubicle [dot] com\r
6            www.lufa-lib.org\r
7 */\r
8 \r
9 /*\r
10   Copyright 2012  Dean Camera (dean [at] fourwalledcubicle [dot] com)\r
11 \r
12   Permission to use, copy, modify, distribute, and sell this \r
13   software and its documentation for any purpose is hereby granted\r
14   without fee, provided that the above copyright notice appear in \r
15   all copies and that both that the copyright notice and this\r
16   permission notice and warranty disclaimer appear in supporting \r
17   documentation, and that the name of the author not be used in \r
18   advertising or publicity pertaining to distribution of the \r
19   software without specific, written prior permission.\r
20 \r
21   The author disclaim all warranties with regard to this\r
22   software, including all implied warranties of merchantability\r
23   and fitness.  In no event shall the author be liable for any\r
24   special, indirect or consequential damages or any damages\r
25   whatsoever resulting from loss of use, data or profits, whether\r
26   in an action of contract, negligence or other tortious action,\r
27   arising out of or in connection with the use or performance of\r
28   this software.\r
29 */\r
30 \r
31 #include "../../../../Common/Common.h"\r
32 #if (ARCH == ARCH_AVR8)\r
33 \r
34 #define  __INCLUDE_FROM_USB_DRIVER\r
35 #include "../USBMode.h"\r
36 \r
37 #if defined(USB_CAN_BE_HOST)\r
38 \r
39 #include "PipeStream_AVR8.h"\r
40 \r
41 uint8_t Pipe_Discard_Stream(uint16_t Length,\r
42                             uint16_t* const BytesProcessed)\r
43 {\r
44         uint8_t  ErrorCode;\r
45         uint16_t BytesInTransfer = 0;\r
46         \r
47         Pipe_SetPipeToken(PIPE_TOKEN_IN);\r
48 \r
49         if ((ErrorCode = Pipe_WaitUntilReady()))\r
50           return ErrorCode;\r
51 \r
52         if (BytesProcessed != NULL)\r
53           Length -= *BytesProcessed;\r
54 \r
55         while (Length)\r
56         {\r
57                 if (!(Pipe_IsReadWriteAllowed()))\r
58                 {\r
59                         Pipe_ClearIN();\r
60                                 \r
61                         if (BytesProcessed != NULL)\r
62                         {\r
63                                 *BytesProcessed += BytesInTransfer;\r
64                                 return PIPE_RWSTREAM_IncompleteTransfer;\r
65                         }\r
66 \r
67                         if ((ErrorCode = Pipe_WaitUntilReady()))\r
68                           return ErrorCode;\r
69                 }\r
70                 else\r
71                 {\r
72                         Pipe_Discard_8();\r
73                         \r
74                         Length--;\r
75                         BytesInTransfer++;\r
76                 }\r
77         }\r
78 \r
79         return PIPE_RWSTREAM_NoError;\r
80 }\r
81 \r
82 uint8_t Pipe_Null_Stream(uint16_t Length,\r
83                          uint16_t* const BytesProcessed)\r
84 {\r
85         uint8_t  ErrorCode;\r
86         uint16_t BytesInTransfer = 0;\r
87         \r
88         Pipe_SetPipeToken(PIPE_TOKEN_OUT);\r
89 \r
90         if ((ErrorCode = Pipe_WaitUntilReady()))\r
91           return ErrorCode;\r
92 \r
93         if (BytesProcessed != NULL)\r
94           Length -= *BytesProcessed;\r
95 \r
96         while (Length)\r
97         {\r
98                 if (!(Pipe_IsReadWriteAllowed()))\r
99                 {\r
100                         Pipe_ClearOUT();\r
101                                 \r
102                         if (BytesProcessed != NULL)\r
103                         {\r
104                                 *BytesProcessed += BytesInTransfer;\r
105                                 return PIPE_RWSTREAM_IncompleteTransfer;\r
106                         }\r
107                         \r
108                         USB_USBTask();\r
109 \r
110                         if ((ErrorCode = Pipe_WaitUntilReady()))\r
111                           return ErrorCode;\r
112                 }\r
113                 else\r
114                 {\r
115                         Pipe_Write_8(0);\r
116                         \r
117                         Length--;\r
118                         BytesInTransfer++;\r
119                 }\r
120         }\r
121 \r
122         return PIPE_RWSTREAM_NoError;\r
123 }\r
124 \r
125 /* The following abuses the C preprocessor in order to copy-paste common code with slight alterations,\r
126  * so that the code needs to be written once. It is a crude form of templating to reduce code maintenance. */\r
127 \r
128 #define  TEMPLATE_FUNC_NAME                        Pipe_Write_Stream_LE\r
129 #define  TEMPLATE_BUFFER_TYPE                      const void*\r
130 #define  TEMPLATE_TOKEN                            PIPE_TOKEN_OUT\r
131 #define  TEMPLATE_CLEAR_PIPE()                     Pipe_ClearOUT()\r
132 #define  TEMPLATE_BUFFER_OFFSET(Length)            0\r
133 #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   DataStream += Amount\r
134 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Pipe_Write_8(*BufferPtr)\r
135 #include "Template/Template_Pipe_RW.c"\r
136 \r
137 #define  TEMPLATE_FUNC_NAME                        Pipe_Write_Stream_BE\r
138 #define  TEMPLATE_BUFFER_TYPE                      const void*\r
139 #define  TEMPLATE_TOKEN                            PIPE_TOKEN_OUT\r
140 #define  TEMPLATE_CLEAR_PIPE()                     Pipe_ClearOUT()\r
141 #define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1)\r
142 #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   DataStream -= Amount\r
143 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Pipe_Write_8(*BufferPtr)\r
144 #include "Template/Template_Pipe_RW.c"\r
145 \r
146 #define  TEMPLATE_FUNC_NAME                        Pipe_Read_Stream_LE\r
147 #define  TEMPLATE_BUFFER_TYPE                      void*\r
148 #define  TEMPLATE_TOKEN                            PIPE_TOKEN_IN\r
149 #define  TEMPLATE_CLEAR_PIPE()                     Pipe_ClearIN()\r
150 #define  TEMPLATE_BUFFER_OFFSET(Length)            0\r
151 #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   DataStream += Amount\r
152 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         *BufferPtr = Pipe_Read_8()\r
153 #include "Template/Template_Pipe_RW.c"\r
154 \r
155 #define  TEMPLATE_FUNC_NAME                        Pipe_Read_Stream_BE\r
156 #define  TEMPLATE_BUFFER_TYPE                      void*\r
157 #define  TEMPLATE_TOKEN                            PIPE_TOKEN_IN\r
158 #define  TEMPLATE_CLEAR_PIPE()                     Pipe_ClearIN()\r
159 #define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1)\r
160 #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   DataStream -= Amount\r
161 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         *BufferPtr = Pipe_Read_8()\r
162 #include "Template/Template_Pipe_RW.c"\r
163 \r
164 #define  TEMPLATE_FUNC_NAME                        Pipe_Write_PStream_LE\r
165 #define  TEMPLATE_BUFFER_TYPE                      const void*\r
166 #define  TEMPLATE_TOKEN                            PIPE_TOKEN_OUT\r
167 #define  TEMPLATE_CLEAR_PIPE()                     Pipe_ClearOUT()\r
168 #define  TEMPLATE_BUFFER_OFFSET(Length)            0\r
169 #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   DataStream += Amount\r
170 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Pipe_Write_8(pgm_read_byte(BufferPtr))\r
171 #include "Template/Template_Pipe_RW.c"\r
172 \r
173 #define  TEMPLATE_FUNC_NAME                        Pipe_Write_PStream_BE\r
174 #define  TEMPLATE_BUFFER_TYPE                      const void*\r
175 #define  TEMPLATE_TOKEN                            PIPE_TOKEN_OUT\r
176 #define  TEMPLATE_CLEAR_PIPE()                     Pipe_ClearOUT()\r
177 #define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1)\r
178 #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   DataStream -= Amount\r
179 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Pipe_Write_8(pgm_read_byte(BufferPtr))\r
180 #include "Template/Template_Pipe_RW.c"\r
181 \r
182 #define  TEMPLATE_FUNC_NAME                        Pipe_Write_EStream_LE\r
183 #define  TEMPLATE_BUFFER_TYPE                      const void*\r
184 #define  TEMPLATE_TOKEN                            PIPE_TOKEN_OUT\r
185 #define  TEMPLATE_CLEAR_PIPE()                     Pipe_ClearOUT()\r
186 #define  TEMPLATE_BUFFER_OFFSET(Length)            0\r
187 #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   DataStream += Amount\r
188 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Pipe_Write_8(eeprom_read_byte(BufferPtr))\r
189 #include "Template/Template_Pipe_RW.c"\r
190 \r
191 #define  TEMPLATE_FUNC_NAME                        Pipe_Write_EStream_BE\r
192 #define  TEMPLATE_BUFFER_TYPE                      const void*\r
193 #define  TEMPLATE_TOKEN                            PIPE_TOKEN_OUT\r
194 #define  TEMPLATE_CLEAR_PIPE()                     Pipe_ClearOUT()\r
195 #define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1)\r
196 #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   DataStream -= Amount\r
197 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Pipe_Write_8(eeprom_read_byte(BufferPtr))\r
198 #include "Template/Template_Pipe_RW.c"\r
199 \r
200 #define  TEMPLATE_FUNC_NAME                        Pipe_Read_EStream_LE\r
201 #define  TEMPLATE_BUFFER_TYPE                      void*\r
202 #define  TEMPLATE_TOKEN                            PIPE_TOKEN_IN\r
203 #define  TEMPLATE_CLEAR_PIPE()                     Pipe_ClearIN()\r
204 #define  TEMPLATE_BUFFER_OFFSET(Length)            0\r
205 #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   DataStream += Amount\r
206 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         eeprom_update_byte(BufferPtr, Pipe_Read_8())\r
207 #include "Template/Template_Pipe_RW.c"\r
208 \r
209 #define  TEMPLATE_FUNC_NAME                        Pipe_Read_EStream_BE\r
210 #define  TEMPLATE_BUFFER_TYPE                      void*\r
211 #define  TEMPLATE_TOKEN                            PIPE_TOKEN_IN\r
212 #define  TEMPLATE_CLEAR_PIPE()                     Pipe_ClearIN()\r
213 #define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1)\r
214 #define  TEMPLATE_BUFFER_MOVE(BufferPtr, Amount)   DataStream -= Amount\r
215 #define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         eeprom_update_byte(BufferPtr, Pipe_Read_8())\r
216 #include "Template/Template_Pipe_RW.c"\r
217 \r
218 #endif\r
219 \r
220 #endif\r
221 \r