Rename reserved members of all structs so that they are uniformly named across all...
[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_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 data transfers */
65 uint8_t CurrentTransferTag = 0;
66
67
68 /** Main program entry point. This routine contains the overall program flow, including initial
69 * setup of all components and the main program loop.
70 */
71 int main(void)
72 {
73 SetupHardware();
74
75 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
76 sei();
77
78 for (;;)
79 {
80 TMC_Task();
81 USB_USBTask();
82 }
83 }
84
85 /** Configures the board hardware and chip peripherals for the demo's functionality. */
86 void SetupHardware(void)
87 {
88 /* Disable watchdog if enabled by bootloader/fuses */
89 MCUSR &= ~(1 << WDRF);
90 wdt_disable();
91
92 /* Disable clock division */
93 clock_prescale_set(clock_div_1);
94
95 /* Hardware Initialization */
96 LEDs_Init();
97 USB_Init();
98 }
99
100 /** Event handler for the USB_Connect event. This indicates that the device is enumerating via the status LEDs and
101 * starts the library USB task to begin the enumeration and USB management process.
102 */
103 void EVENT_USB_Device_Connect(void)
104 {
105 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
106 }
107
108 /** Event handler for the USB_Disconnect event. This indicates that the device is no longer connected to a host via
109 * the status LEDs and stops the USB management and CDC management tasks.
110 */
111 void EVENT_USB_Device_Disconnect(void)
112 {
113 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
114 }
115
116 /** Event handler for the USB_ConfigurationChanged event. This is fired when the host set the current configuration
117 * of the USB device after enumeration - the device endpoints are configured and the CDC management task started.
118 */
119 void EVENT_USB_Device_ConfigurationChanged(void)
120 {
121 LEDs_SetAllLEDs(LEDMASK_USB_READY);
122
123 /* Setup TMC In and Out Endpoints */
124 if (!(Endpoint_ConfigureEndpoint(TMC_IN_EPNUM, EP_TYPE_BULK,
125 ENDPOINT_DIR_IN, TMC_IO_EPSIZE,
126 ENDPOINT_BANK_SINGLE)))
127 {
128 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
129 }
130
131 if (!(Endpoint_ConfigureEndpoint(TMC_OUT_EPNUM, EP_TYPE_BULK,
132 ENDPOINT_DIR_OUT, TMC_IO_EPSIZE,
133 ENDPOINT_BANK_SINGLE)))
134 {
135 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
136 }
137 }
138
139 /** Event handler for the USB_UnhandledControlRequest event. This is used to catch standard and class specific
140 * control requests that are not handled internally by the USB library (including the CDC control commands,
141 * which are all issued via the control endpoint), so that they can be handled appropriately for the application.
142 */
143 void EVENT_USB_Device_UnhandledControlRequest(void)
144 {
145 uint8_t TMCRequestStatus = TMC_STATUS_SUCCESS;
146
147 /* Process TMC specific control requests */
148 switch (USB_ControlRequest.bRequest)
149 {
150 case Req_InitiateAbortBulkOut:
151 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_ENDPOINT))
152 {
153 Endpoint_ClearSETUP();
154
155 /* Check that no split transaction is already in progress and the data transfer tag is valid */
156 if (RequestInProgess != 0)
157 {
158 TMCRequestStatus = TMC_STATUS_SPLIT_IN_PROGRESS;
159 }
160 else if (USB_ControlRequest.wValue != CurrentTransferTag)
161 {
162 TMCRequestStatus = TMC_STATUS_TRANSFER_NOT_IN_PROGRESS;
163 }
164 else
165 {
166 /* Indicate that all in-progress/pending data OUT requests should be aborted */
167 IsTMCBulkOUTReset = true;
168
169 /* Save the split request for later checking when a new request is received */
170 RequestInProgess = Req_InitiateAbortBulkOut;
171 }
172
173 /* Write the request response byte */
174 Endpoint_Write_Byte(TMCRequestStatus);
175
176 Endpoint_ClearIN();
177 Endpoint_ClearStatusStage();
178 }
179
180 break;
181 case Req_CheckAbortBulkOutStatus:
182 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_ENDPOINT))
183 {
184 Endpoint_ClearSETUP();
185
186 /* Check that an ABORT BULK OUT transaction has been requested and that the request has completed */
187 if (RequestInProgess != Req_InitiateAbortBulkOut)
188 TMCRequestStatus = TMC_STATUS_SPLIT_NOT_IN_PROGRESS;
189 else if (IsTMCBulkOUTReset)
190 TMCRequestStatus = TMC_STATUS_PENDING;
191 else
192 RequestInProgess = 0;
193
194 /* Write the request response bytes */
195 Endpoint_Write_Byte(TMCRequestStatus);
196 Endpoint_Write_Word_LE(0);
197 Endpoint_Write_DWord_LE(0); // TODO - Last transfer length
198
199 Endpoint_ClearIN();
200 Endpoint_ClearStatusStage();
201 }
202
203 break;
204 case Req_InitiateAbortBulkIn:
205 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_ENDPOINT))
206 {
207 Endpoint_ClearSETUP();
208
209 /* Check that no split transaction is already in progress and the data transfer tag is valid */
210 if (RequestInProgess != 0)
211 {
212 TMCRequestStatus = TMC_STATUS_SPLIT_IN_PROGRESS;
213 }
214 else if (USB_ControlRequest.wValue != CurrentTransferTag)
215 {
216 TMCRequestStatus = TMC_STATUS_TRANSFER_NOT_IN_PROGRESS;
217 }
218 else
219 {
220 /* Indicate that all in-progress/pending data IN requests should be aborted */
221 IsTMCBulkINReset = true;
222
223 /* Save the split request for later checking when a new request is received */
224 RequestInProgess = Req_InitiateAbortBulkIn;
225 }
226
227 /* Write the request response bytes */
228 Endpoint_Write_Byte(TMCRequestStatus);
229 Endpoint_Write_Byte(CurrentTransferTag);
230
231 Endpoint_ClearIN();
232 Endpoint_ClearStatusStage();
233 }
234
235 break;
236 case Req_CheckAbortBulkInStatus:
237 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_ENDPOINT))
238 {
239 Endpoint_ClearSETUP();
240
241 /* Check that an ABORT BULK IN transaction has been requested and that the request has completed */
242 if (RequestInProgess != Req_InitiateAbortBulkIn)
243 TMCRequestStatus = TMC_STATUS_SPLIT_NOT_IN_PROGRESS;
244 else if (IsTMCBulkINReset)
245 TMCRequestStatus = TMC_STATUS_PENDING;
246 else
247 RequestInProgess = 0;
248
249 /* Write the request response bytes */
250 Endpoint_Write_Byte(TMCRequestStatus);
251 Endpoint_Write_Word_LE(0);
252 Endpoint_Write_DWord_LE(0); // TODO - Last transfer length
253
254 Endpoint_ClearIN();
255 Endpoint_ClearStatusStage();
256 }
257
258 break;
259 case Req_InitiateClear:
260 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
261 {
262 Endpoint_ClearSETUP();
263
264 /* Check that no split transaction is already in progress */
265 if (RequestInProgess != 0)
266 {
267 Endpoint_Write_Byte(TMC_STATUS_SPLIT_IN_PROGRESS);
268 }
269 else
270 {
271 /* Indicate that all in-progress/pending data IN and OUT requests should be aborted */
272 IsTMCBulkINReset = true;
273 IsTMCBulkOUTReset = true;
274
275 /* Save the split request for later checking when a new request is received */
276 RequestInProgess = Req_InitiateClear;
277 }
278
279 /* Write the request response byte */
280 Endpoint_Write_Byte(TMCRequestStatus);
281
282 Endpoint_ClearIN();
283 Endpoint_ClearStatusStage();
284 }
285
286 break;
287 case Req_CheckClearStatus:
288 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
289 {
290 Endpoint_ClearSETUP();
291
292 /* Check that a CLEAR transaction has been requested and that the request has completed */
293 if (RequestInProgess != Req_InitiateClear)
294 TMCRequestStatus = TMC_STATUS_SPLIT_NOT_IN_PROGRESS;
295 else if (IsTMCBulkINReset || IsTMCBulkOUTReset)
296 TMCRequestStatus = TMC_STATUS_PENDING;
297 else
298 RequestInProgess = 0;
299
300 /* Write the request response bytes */
301 Endpoint_Write_Byte(TMCRequestStatus);
302 Endpoint_Write_Byte(0);
303
304 Endpoint_ClearIN();
305 Endpoint_ClearStatusStage();
306 }
307
308 break;
309 case Req_GetCapabilities:
310 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
311 {
312 /* Acknowledge the SETUP packet, ready for data transfer */
313 Endpoint_ClearSETUP();
314
315 /* Write the device capabilities to the control endpoint */
316 Endpoint_Write_Control_Stream_LE(&Capabilities, sizeof(TMC_Capabilities_t));
317
318 /* Finalize the stream transfer to send the last packet or clear the host abort */
319 Endpoint_ClearOUT();
320 }
321
322 break;
323 }
324 }
325
326 /** Function to manage TMC data transmission and reception to and from the host. */
327 void TMC_Task(void)
328 {
329 /* Device must be connected and configured for the task to run */
330 if (USB_DeviceState != DEVICE_STATE_Configured)
331 return;
332
333 Endpoint_SelectEndpoint(TMC_OUT_EPNUM);
334
335 if (Endpoint_IsOUTReceived())
336 {
337 TMC_MessageHeader_t MessageHeader;
338
339 Endpoint_Read_Stream_LE(&MessageHeader, sizeof(MessageHeader), StreamCallback_AbortOUTOnRequest);
340 CurrentTransferTag = MessageHeader.Tag;
341
342 switch (MessageHeader.MessageID)
343 {
344 case TMC_MESSAGEID_DEV_DEP_MSG_OUT:
345
346 break;
347 case TMC_MESSAGEID_DEV_DEP_MSG_IN:
348
349 break;
350 case TMC_MESSAGEID_DEV_VENDOR_OUT:
351
352 break;
353 case TMC_MESSAGEID_DEV_VENDOR_IN:
354 break;
355 }
356
357 Endpoint_ClearOUT();
358 }
359
360 /* All pending data has been processed - reset the data abort flags */
361 IsTMCBulkINReset = false;
362 IsTMCBulkOUTReset = false;
363 }
364
365 /** Stream callback function for the Endpoint stream write functions. This callback will abort the current stream transfer
366 * if a TMC Abort Bulk IN request has been issued to the control endpoint.
367 */
368 uint8_t StreamCallback_AbortINOnRequest(void)
369 {
370 /* Abort if a TMC Bulk Data IN abort was received */
371 if (IsTMCBulkINReset)
372 return STREAMCALLBACK_Abort;
373
374 /* Continue with the current stream operation */
375 return STREAMCALLBACK_Continue;
376 }
377
378 /** Stream callback function for the Endpoint stream read functions. This callback will abort the current stream transfer
379 * if a TMC Abort Bulk OUT request has been issued to the control endpoint.
380 */
381 uint8_t StreamCallback_AbortOUTOnRequest(void)
382 {
383 /* Abort if a TMC Bulk Data IN abort was received */
384 if (IsTMCBulkOUTReset)
385 return STREAMCALLBACK_Abort;
386
387 /* Continue with the current stream operation */
388 return STREAMCALLBACK_Continue;
389 }