Add stream callback flags and class specific control request handler code to the...
[pub/lufa.git] / Demos / Device / Incomplete / TestAndMeasurement / TestAndMeasurement.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 #include "TestAndMeasurement.h"
32
33 /** Contains the (usually static) capabilities of the TMC device. This table is requested by the
34 * host upon enumeration to give it information on what features of the Test and Measurement USB
35 * Class the device supports.
36 */
37 TMC_Capabilities_t Capabilities =
38 {
39 .Status = TMC_REQUEST_STATUS_SUCCESS,
40 .TMCVersion = VERSION_BCD(1.00),
41
42 .Interface =
43 {
44 .ListenOnly = false,
45 .TalkOnly = false,
46 .PulseIndicateSupported = false,
47 },
48
49 .Device =
50 {
51 .SupportsAbortINOnMatch = false,
52 },
53 };
54
55 /** Current TMC control request that is being processed */
56 uint8_t RequestInProgess = 0;
57
58 /** Stream callback abort flag for bulk IN data */
59 bool IsTMCBulkINReset = false;
60
61 /** Stream callback abort flag for bulk OUT data */
62 bool IsTMCBulkOUTReset = false;
63
64
65 /** Main program entry point. This routine contains the overall program flow, including initial
66 * setup of all components and the main program loop.
67 */
68 int main(void)
69 {
70 SetupHardware();
71
72 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
73 sei();
74
75 for (;;)
76 {
77 TMC_Task();
78 USB_USBTask();
79 }
80 }
81
82 /** Configures the board hardware and chip peripherals for the demo's functionality. */
83 void SetupHardware(void)
84 {
85 /* Disable watchdog if enabled by bootloader/fuses */
86 MCUSR &= ~(1 << WDRF);
87 wdt_disable();
88
89 /* Disable clock division */
90 clock_prescale_set(clock_div_1);
91
92 /* Hardware Initialization */
93 LEDs_Init();
94 USB_Init();
95 }
96
97 void EVENT_USB_Device_Connect(void)
98 {
99 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
100 }
101
102 void EVENT_USB_Device_Disconnect(void)
103 {
104 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
105 }
106
107 void EVENT_USB_Device_ConfigurationChanged(void)
108 {
109 LEDs_SetAllLEDs(LEDMASK_USB_READY);
110
111 /* Setup TMC In and Out Endpoints */
112 if (!(Endpoint_ConfigureEndpoint(TMC_IN_EPNUM, EP_TYPE_BULK,
113 ENDPOINT_DIR_IN, TMC_IO_EPSIZE,
114 ENDPOINT_BANK_SINGLE)))
115 {
116 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
117 }
118
119 if (!(Endpoint_ConfigureEndpoint(TMC_OUT_EPNUM, EP_TYPE_BULK,
120 ENDPOINT_DIR_OUT, TMC_IO_EPSIZE,
121 ENDPOINT_BANK_SINGLE)))
122 {
123 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
124 }
125 }
126
127 void EVENT_USB_Device_UnhandledControlRequest(void)
128 {
129 switch (USB_ControlRequest.bRequest)
130 {
131 case Req_InitiateAbortBulkOut:
132 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_ENDPOINT))
133 {
134 Endpoint_ClearSETUP();
135
136 /* Check to see if a split request is already being processed before starting a new one */
137 if (RequestInProgess != 0)
138 {
139 Endpoint_Write_Byte(TMC_REQUEST_STATUS_SPLIT_IN_PROGRESS);
140 }
141 else
142 {
143 /* Indicate that all in-progress/pending data OUT requests should be aborted */
144 IsTMCBulkOUTReset = true;
145
146 /* Save the split request for later checking when a new request is received */
147 RequestInProgess = Req_InitiateAbortBulkOut;
148
149 Endpoint_Write_Byte(TMC_REQUEST_STATUS_SUCCESS);
150 }
151
152 Endpoint_ClearIN();
153 Endpoint_ClearStatusStage();
154 }
155
156 break;
157 case Req_CheckAbortBulkOutStatus:
158 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_ENDPOINT))
159 {
160 Endpoint_ClearSETUP();
161
162 /* Check to see the correct split request is in progress before the status can be retrieved */
163 if (RequestInProgess != Req_InitiateAbortBulkOut)
164 {
165 Endpoint_Write_Byte(TMC_REQUEST_STATUS_SPLIT_NOT_IN_PROGRESS);
166 }
167 else
168 {
169 // TODO: CLEAR BULK OUT
170
171 /* Clear the pending split request value so that a new request can be made */
172 RequestInProgess = 0;
173
174 Endpoint_Write_Byte(TMC_REQUEST_STATUS_SUCCESS);
175 }
176
177 Endpoint_ClearIN();
178 Endpoint_ClearStatusStage();
179 }
180
181 break;
182 case Req_InitiateAbortBulkIn:
183 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_ENDPOINT))
184 {
185 Endpoint_ClearSETUP();
186
187 /* Check to see if a split request is already being processed before starting a new one */
188 if (RequestInProgess != 0)
189 {
190 Endpoint_Write_Byte(TMC_REQUEST_STATUS_SPLIT_IN_PROGRESS);
191 }
192 else
193 {
194 /* Indicate that all in-progress/pending data IN requests should be aborted */
195 IsTMCBulkINReset = true;
196
197 /* Save the split request for later checking when a new request is received */
198 RequestInProgess = Req_InitiateAbortBulkIn;
199
200 Endpoint_Write_Byte(TMC_REQUEST_STATUS_SUCCESS);
201 }
202
203 Endpoint_ClearIN();
204 Endpoint_ClearStatusStage();
205 }
206
207 break;
208 case Req_CheckAbortBulkInStatus:
209 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_ENDPOINT))
210 {
211 Endpoint_ClearSETUP();
212
213 /* Check to see the correct split request is in progress before the status can be retrieved */
214 if (RequestInProgess != Req_InitiateAbortBulkIn)
215 {
216 Endpoint_Write_Byte(TMC_REQUEST_STATUS_SPLIT_NOT_IN_PROGRESS);
217 }
218 else
219 {
220 // TODO: CLEAR BULK IN
221
222 /* Clear the pending split request value so that a new request can be made */
223 RequestInProgess = 0;
224
225 Endpoint_Write_Byte(TMC_REQUEST_STATUS_SUCCESS);
226 }
227
228 Endpoint_ClearIN();
229 Endpoint_ClearStatusStage();
230 }
231
232 break;
233 case Req_InitiateClear:
234 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
235 {
236 Endpoint_ClearSETUP();
237
238 /* Check to see if a split request is already being processed before starting a new one */
239 if (RequestInProgess != 0)
240 {
241 Endpoint_Write_Byte(TMC_REQUEST_STATUS_SPLIT_IN_PROGRESS);
242 }
243 else
244 {
245 /* Indicate that all in-progress/pending data IN and OUT requests should be aborted */
246 IsTMCBulkINReset = true;
247 IsTMCBulkOUTReset = true;
248
249 /* Save the split request for later checking when a new request is received */
250 RequestInProgess = Req_InitiateClear;
251
252 Endpoint_Write_Byte(TMC_REQUEST_STATUS_SUCCESS);
253 }
254
255 Endpoint_ClearIN();
256 Endpoint_ClearStatusStage();
257 }
258
259 break;
260 case Req_CheckClearStatus:
261 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
262 {
263 Endpoint_ClearSETUP();
264
265 /* Check to see the correct split request is in progress before the status can be retrieved */
266 if (RequestInProgess != Req_InitiateClear)
267 {
268 Endpoint_Write_Byte(TMC_REQUEST_STATUS_SPLIT_NOT_IN_PROGRESS);
269 }
270 else
271 {
272 // TODO: CLEAR STATUS
273
274 /* Clear the pending split request value so that a new request can be made */
275 RequestInProgess = 0;
276
277 Endpoint_Write_Byte(TMC_REQUEST_STATUS_SUCCESS);
278 }
279
280 Endpoint_ClearIN();
281 Endpoint_ClearStatusStage();
282 }
283
284 break;
285 case Req_GetCapabilities:
286 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
287 {
288 /* Acknowledge the SETUP packet, ready for data transfer */
289 Endpoint_ClearSETUP();
290
291 /* Write the device capabilities to the control endpoint */
292 Endpoint_Write_Control_Stream_LE(&Capabilities, sizeof(TMC_Capabilities_t));
293
294 /* Finalize the stream transfer to send the last packet or clear the host abort */
295 Endpoint_ClearOUT();
296 }
297
298 break;
299 }
300 }
301
302 void TMC_Task(void)
303 {
304 /* Device must be connected and configured for the task to run */
305 if (USB_DeviceState != DEVICE_STATE_Configured)
306 return;
307
308 Endpoint_SelectEndpoint(TMC_OUT_EPNUM);
309
310 if (Endpoint_IsOUTReceived())
311 {
312 // TEMP - Indicate data received
313 LEDs_SetAllLEDs(LEDS_ALL_LEDS);
314 Endpoint_ClearOUT();
315 }
316 }
317
318 /** Stream callback function for the Endpoint stream write functions. This callback will abort the current stream transfer
319 * if a TMC Abort Bulk IN request has been issued to the control endpoint.
320 */
321 uint8_t StreamCallback_AbortINOnRequest(void)
322 {
323 /* Abort if a TMC Bulk Data IN abort was received */
324 if (IsTMCBulkINReset)
325 return STREAMCALLBACK_Abort;
326
327 /* Continue with the current stream operation */
328 return STREAMCALLBACK_Continue;
329 }
330
331 /** Stream callback function for the Endpoint stream read functions. This callback will abort the current stream transfer
332 * if a TMC Abort Bulk OUT request has been issued to the control endpoint.
333 */
334 uint8_t StreamCallback_AbortOUTOnRequest(void)
335 {
336 /* Abort if a TMC Bulk Data IN abort was received */
337 if (IsTMCBulkOUTReset)
338 return STREAMCALLBACK_Abort;
339
340 /* Continue with the current stream operation */
341 return STREAMCALLBACK_Continue;
342 }