Add missing INTERRUPT_CONTROL_ENDPOINT compile time option to the Mass Storage device...
[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 /** Last used tag value for bulk IN transfers */
65 uint8_t NextTransferINTag = 0;
66
67 /** Last used tag value for bulk IN transfers */
68 uint8_t NextTransferOUTTag = 0;
69
70
71 /** Main program entry point. This routine contains the overall program flow, including initial
72 * setup of all components and the main program loop.
73 */
74 int main(void)
75 {
76 SetupHardware();
77
78 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
79 sei();
80
81 for (;;)
82 {
83 TMC_Task();
84 USB_USBTask();
85 }
86 }
87
88 /** Configures the board hardware and chip peripherals for the demo's functionality. */
89 void SetupHardware(void)
90 {
91 /* Disable watchdog if enabled by bootloader/fuses */
92 MCUSR &= ~(1 << WDRF);
93 wdt_disable();
94
95 /* Disable clock division */
96 clock_prescale_set(clock_div_1);
97
98 /* Hardware Initialization */
99 LEDs_Init();
100 USB_Init();
101 }
102
103 void EVENT_USB_Device_Connect(void)
104 {
105 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
106 }
107
108 void EVENT_USB_Device_Disconnect(void)
109 {
110 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
111 }
112
113 void EVENT_USB_Device_ConfigurationChanged(void)
114 {
115 LEDs_SetAllLEDs(LEDMASK_USB_READY);
116
117 /* Setup TMC In and Out Endpoints */
118 if (!(Endpoint_ConfigureEndpoint(TMC_IN_EPNUM, EP_TYPE_BULK,
119 ENDPOINT_DIR_IN, TMC_IO_EPSIZE,
120 ENDPOINT_BANK_SINGLE)))
121 {
122 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
123 }
124
125 if (!(Endpoint_ConfigureEndpoint(TMC_OUT_EPNUM, EP_TYPE_BULK,
126 ENDPOINT_DIR_OUT, TMC_IO_EPSIZE,
127 ENDPOINT_BANK_SINGLE)))
128 {
129 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
130 }
131 }
132
133 void EVENT_USB_Device_UnhandledControlRequest(void)
134 {
135 uint8_t TMCRequestStatus = TMC_REQUEST_STATUS_SUCCESS;
136
137 switch (USB_ControlRequest.bRequest)
138 {
139 case Req_InitiateAbortBulkOut:
140 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_ENDPOINT))
141 {
142 Endpoint_ClearSETUP();
143
144 if (RequestInProgess != 0)
145 {
146 TMCRequestStatus = TMC_REQUEST_STATUS_SPLIT_IN_PROGRESS;
147 }
148 else if (USB_ControlRequest.wValue != NextTransferOUTTag)
149 {
150 TMCRequestStatus = TMC_REQUEST_STATUS_TRANSFER_NOT_IN_PROGRESS;
151 }
152 else
153 {
154 /* Indicate that all in-progress/pending data OUT requests should be aborted */
155 IsTMCBulkOUTReset = true;
156
157 /* Save the split request for later checking when a new request is received */
158 RequestInProgess = Req_InitiateAbortBulkOut;
159 }
160
161 /* Write the request response byte */
162 Endpoint_Write_Byte(TMCRequestStatus);
163
164 Endpoint_ClearIN();
165 Endpoint_ClearStatusStage();
166 }
167
168 break;
169 case Req_CheckAbortBulkOutStatus:
170 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_ENDPOINT))
171 {
172 Endpoint_ClearSETUP();
173
174 if (RequestInProgess != Req_InitiateAbortBulkOut)
175 TMCRequestStatus = TMC_REQUEST_STATUS_SPLIT_NOT_IN_PROGRESS;
176 else if (IsTMCBulkOUTReset)
177 TMCRequestStatus = TMC_REQUEST_STATUS_PENDING;
178 else
179 RequestInProgess = 0;
180
181 /* Write the request response bytes */
182 Endpoint_Write_Byte(TMCRequestStatus);
183 Endpoint_Write_Word_LE(0);
184 Endpoint_Write_DWord_LE(0); // TODO - Last transfer length
185
186 Endpoint_ClearIN();
187 Endpoint_ClearStatusStage();
188 }
189
190 break;
191 case Req_InitiateAbortBulkIn:
192 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_ENDPOINT))
193 {
194 Endpoint_ClearSETUP();
195
196 if (RequestInProgess != 0)
197 {
198 TMCRequestStatus = TMC_REQUEST_STATUS_SPLIT_IN_PROGRESS;
199 }
200 else if (USB_ControlRequest.wValue != NextTransferINTag)
201 {
202 TMCRequestStatus = TMC_REQUEST_STATUS_TRANSFER_NOT_IN_PROGRESS;
203 }
204 else
205 {
206 /* Indicate that all in-progress/pending data IN requests should be aborted */
207 IsTMCBulkINReset = true;
208
209 /* Save the split request for later checking when a new request is received */
210 RequestInProgess = Req_InitiateAbortBulkIn;
211 }
212
213 /* Write the request response bytes */
214 Endpoint_Write_Byte(TMCRequestStatus);
215 Endpoint_Write_Byte(NextTransferINTag);
216
217 Endpoint_ClearIN();
218 Endpoint_ClearStatusStage();
219 }
220
221 break;
222 case Req_CheckAbortBulkInStatus:
223 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_ENDPOINT))
224 {
225 Endpoint_ClearSETUP();
226
227 if (RequestInProgess != Req_InitiateAbortBulkIn)
228 TMCRequestStatus = TMC_REQUEST_STATUS_SPLIT_NOT_IN_PROGRESS;
229 else if (IsTMCBulkINReset)
230 TMCRequestStatus = TMC_REQUEST_STATUS_PENDING;
231 else
232 RequestInProgess = 0;
233
234 /* Write the request response bytes */
235 Endpoint_Write_Byte(TMCRequestStatus);
236 Endpoint_Write_Word_LE(0);
237 Endpoint_Write_DWord_LE(0); // TODO - Last transfer length
238
239 Endpoint_ClearIN();
240 Endpoint_ClearStatusStage();
241 }
242
243 break;
244 case Req_InitiateClear:
245 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
246 {
247 Endpoint_ClearSETUP();
248
249 if (RequestInProgess != 0)
250 {
251 Endpoint_Write_Byte(TMC_REQUEST_STATUS_SPLIT_IN_PROGRESS);
252 }
253 else
254 {
255 /* Indicate that all in-progress/pending data IN and OUT requests should be aborted */
256 IsTMCBulkINReset = true;
257 IsTMCBulkOUTReset = true;
258
259 /* Save the split request for later checking when a new request is received */
260 RequestInProgess = Req_InitiateClear;
261 }
262
263 /* Write the request response byte */
264 Endpoint_Write_Byte(TMCRequestStatus);
265
266 Endpoint_ClearIN();
267 Endpoint_ClearStatusStage();
268 }
269
270 break;
271 case Req_CheckClearStatus:
272 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
273 {
274 Endpoint_ClearSETUP();
275
276 if (RequestInProgess != Req_InitiateClear)
277 TMCRequestStatus = TMC_REQUEST_STATUS_SPLIT_NOT_IN_PROGRESS;
278 else if (IsTMCBulkINReset || IsTMCBulkOUTReset)
279 TMCRequestStatus = TMC_REQUEST_STATUS_PENDING;
280 else
281 RequestInProgess = 0;
282
283 /* Write the request response bytes */
284 Endpoint_Write_Byte(TMCRequestStatus);
285 Endpoint_Write_Byte(0);
286
287 Endpoint_ClearIN();
288 Endpoint_ClearStatusStage();
289 }
290
291 break;
292 case Req_GetCapabilities:
293 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
294 {
295 /* Acknowledge the SETUP packet, ready for data transfer */
296 Endpoint_ClearSETUP();
297
298 /* Write the device capabilities to the control endpoint */
299 Endpoint_Write_Control_Stream_LE(&Capabilities, sizeof(TMC_Capabilities_t));
300
301 /* Finalize the stream transfer to send the last packet or clear the host abort */
302 Endpoint_ClearOUT();
303 }
304
305 break;
306 }
307 }
308
309 void TMC_Task(void)
310 {
311 /* Device must be connected and configured for the task to run */
312 if (USB_DeviceState != DEVICE_STATE_Configured)
313 return;
314
315 Endpoint_SelectEndpoint(TMC_OUT_EPNUM);
316
317 if (Endpoint_IsOUTReceived())
318 {
319 // TEMP - Indicate data received
320 LEDs_SetAllLEDs(LEDS_ALL_LEDS);
321 Endpoint_ClearOUT();
322 }
323
324 /* All pending data has been processed - reset the data abort flags */
325 IsTMCBulkINReset = false;
326 IsTMCBulkOUTReset = false;
327 }
328
329 /** Stream callback function for the Endpoint stream write functions. This callback will abort the current stream transfer
330 * if a TMC Abort Bulk IN request has been issued to the control endpoint.
331 */
332 uint8_t StreamCallback_AbortINOnRequest(void)
333 {
334 /* Abort if a TMC Bulk Data IN abort was received */
335 if (IsTMCBulkINReset)
336 return STREAMCALLBACK_Abort;
337
338 /* Continue with the current stream operation */
339 return STREAMCALLBACK_Continue;
340 }
341
342 /** Stream callback function for the Endpoint stream read functions. This callback will abort the current stream transfer
343 * if a TMC Abort Bulk OUT request has been issued to the control endpoint.
344 */
345 uint8_t StreamCallback_AbortOUTOnRequest(void)
346 {
347 /* Abort if a TMC Bulk Data IN abort was received */
348 if (IsTMCBulkOUTReset)
349 return STREAMCALLBACK_Abort;
350
351 /* Continue with the current stream operation */
352 return STREAMCALLBACK_Continue;
353 }