Add comments to the currently completed portions of the incomplete Test and Measureme...
[pub/USBasp.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 /** Event handler for the USB_Connect event. This indicates that the device is enumerating via the status LEDs and
104 * starts the library USB task to begin the enumeration and USB management process.
105 */
106 void EVENT_USB_Device_Connect(void)
107 {
108 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
109 }
110
111 /** Event handler for the USB_Disconnect event. This indicates that the device is no longer connected to a host via
112 * the status LEDs and stops the USB management and CDC management tasks.
113 */
114 void EVENT_USB_Device_Disconnect(void)
115 {
116 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
117 }
118
119 /** Event handler for the USB_ConfigurationChanged event. This is fired when the host set the current configuration
120 * of the USB device after enumeration - the device endpoints are configured and the CDC management task started.
121 */
122 void EVENT_USB_Device_ConfigurationChanged(void)
123 {
124 LEDs_SetAllLEDs(LEDMASK_USB_READY);
125
126 /* Setup TMC In and Out Endpoints */
127 if (!(Endpoint_ConfigureEndpoint(TMC_IN_EPNUM, EP_TYPE_BULK,
128 ENDPOINT_DIR_IN, TMC_IO_EPSIZE,
129 ENDPOINT_BANK_SINGLE)))
130 {
131 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
132 }
133
134 if (!(Endpoint_ConfigureEndpoint(TMC_OUT_EPNUM, EP_TYPE_BULK,
135 ENDPOINT_DIR_OUT, TMC_IO_EPSIZE,
136 ENDPOINT_BANK_SINGLE)))
137 {
138 LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
139 }
140 }
141
142 /** Event handler for the USB_UnhandledControlRequest event. This is used to catch standard and class specific
143 * control requests that are not handled internally by the USB library (including the CDC control commands,
144 * which are all issued via the control endpoint), so that they can be handled appropriately for the application.
145 */
146 void EVENT_USB_Device_UnhandledControlRequest(void)
147 {
148 uint8_t TMCRequestStatus = TMC_REQUEST_STATUS_SUCCESS;
149
150 /* Process TMC specific control requests */
151 switch (USB_ControlRequest.bRequest)
152 {
153 case Req_InitiateAbortBulkOut:
154 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_ENDPOINT))
155 {
156 Endpoint_ClearSETUP();
157
158 /* Check that no split transaction is already in progress and the data OUT transfer tag is valid */
159 if (RequestInProgess != 0)
160 {
161 TMCRequestStatus = TMC_REQUEST_STATUS_SPLIT_IN_PROGRESS;
162 }
163 else if (USB_ControlRequest.wValue != NextTransferOUTTag)
164 {
165 TMCRequestStatus = TMC_REQUEST_STATUS_TRANSFER_NOT_IN_PROGRESS;
166 }
167 else
168 {
169 /* Indicate that all in-progress/pending data OUT requests should be aborted */
170 IsTMCBulkOUTReset = true;
171
172 /* Save the split request for later checking when a new request is received */
173 RequestInProgess = Req_InitiateAbortBulkOut;
174 }
175
176 /* Write the request response byte */
177 Endpoint_Write_Byte(TMCRequestStatus);
178
179 Endpoint_ClearIN();
180 Endpoint_ClearStatusStage();
181 }
182
183 break;
184 case Req_CheckAbortBulkOutStatus:
185 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_ENDPOINT))
186 {
187 Endpoint_ClearSETUP();
188
189 /* Check that an ABORT BULK OUT transaction has been requested and that the request has completed */
190 if (RequestInProgess != Req_InitiateAbortBulkOut)
191 TMCRequestStatus = TMC_REQUEST_STATUS_SPLIT_NOT_IN_PROGRESS;
192 else if (IsTMCBulkOUTReset)
193 TMCRequestStatus = TMC_REQUEST_STATUS_PENDING;
194 else
195 RequestInProgess = 0;
196
197 /* Write the request response bytes */
198 Endpoint_Write_Byte(TMCRequestStatus);
199 Endpoint_Write_Word_LE(0);
200 Endpoint_Write_DWord_LE(0); // TODO - Last transfer length
201
202 Endpoint_ClearIN();
203 Endpoint_ClearStatusStage();
204 }
205
206 break;
207 case Req_InitiateAbortBulkIn:
208 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_ENDPOINT))
209 {
210 Endpoint_ClearSETUP();
211
212 /* Check that no split transaction is already in progress and the data IN transfer tag is valid */
213 if (RequestInProgess != 0)
214 {
215 TMCRequestStatus = TMC_REQUEST_STATUS_SPLIT_IN_PROGRESS;
216 }
217 else if (USB_ControlRequest.wValue != NextTransferINTag)
218 {
219 TMCRequestStatus = TMC_REQUEST_STATUS_TRANSFER_NOT_IN_PROGRESS;
220 }
221 else
222 {
223 /* Indicate that all in-progress/pending data IN requests should be aborted */
224 IsTMCBulkINReset = true;
225
226 /* Save the split request for later checking when a new request is received */
227 RequestInProgess = Req_InitiateAbortBulkIn;
228 }
229
230 /* Write the request response bytes */
231 Endpoint_Write_Byte(TMCRequestStatus);
232 Endpoint_Write_Byte(NextTransferINTag);
233
234 Endpoint_ClearIN();
235 Endpoint_ClearStatusStage();
236 }
237
238 break;
239 case Req_CheckAbortBulkInStatus:
240 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_ENDPOINT))
241 {
242 Endpoint_ClearSETUP();
243
244 /* Check that an ABORT BULK IN transaction has been requested and that the request has completed */
245 if (RequestInProgess != Req_InitiateAbortBulkIn)
246 TMCRequestStatus = TMC_REQUEST_STATUS_SPLIT_NOT_IN_PROGRESS;
247 else if (IsTMCBulkINReset)
248 TMCRequestStatus = TMC_REQUEST_STATUS_PENDING;
249 else
250 RequestInProgess = 0;
251
252 /* Write the request response bytes */
253 Endpoint_Write_Byte(TMCRequestStatus);
254 Endpoint_Write_Word_LE(0);
255 Endpoint_Write_DWord_LE(0); // TODO - Last transfer length
256
257 Endpoint_ClearIN();
258 Endpoint_ClearStatusStage();
259 }
260
261 break;
262 case Req_InitiateClear:
263 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
264 {
265 Endpoint_ClearSETUP();
266
267 /* Check that no split transaction is already in progress */
268 if (RequestInProgess != 0)
269 {
270 Endpoint_Write_Byte(TMC_REQUEST_STATUS_SPLIT_IN_PROGRESS);
271 }
272 else
273 {
274 /* Indicate that all in-progress/pending data IN and OUT requests should be aborted */
275 IsTMCBulkINReset = true;
276 IsTMCBulkOUTReset = true;
277
278 /* Save the split request for later checking when a new request is received */
279 RequestInProgess = Req_InitiateClear;
280 }
281
282 /* Write the request response byte */
283 Endpoint_Write_Byte(TMCRequestStatus);
284
285 Endpoint_ClearIN();
286 Endpoint_ClearStatusStage();
287 }
288
289 break;
290 case Req_CheckClearStatus:
291 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
292 {
293 Endpoint_ClearSETUP();
294
295 /* Check that a CLEAR transaction has been requested and that the request has completed */
296 if (RequestInProgess != Req_InitiateClear)
297 TMCRequestStatus = TMC_REQUEST_STATUS_SPLIT_NOT_IN_PROGRESS;
298 else if (IsTMCBulkINReset || IsTMCBulkOUTReset)
299 TMCRequestStatus = TMC_REQUEST_STATUS_PENDING;
300 else
301 RequestInProgess = 0;
302
303 /* Write the request response bytes */
304 Endpoint_Write_Byte(TMCRequestStatus);
305 Endpoint_Write_Byte(0);
306
307 Endpoint_ClearIN();
308 Endpoint_ClearStatusStage();
309 }
310
311 break;
312 case Req_GetCapabilities:
313 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
314 {
315 /* Acknowledge the SETUP packet, ready for data transfer */
316 Endpoint_ClearSETUP();
317
318 /* Write the device capabilities to the control endpoint */
319 Endpoint_Write_Control_Stream_LE(&Capabilities, sizeof(TMC_Capabilities_t));
320
321 /* Finalize the stream transfer to send the last packet or clear the host abort */
322 Endpoint_ClearOUT();
323 }
324
325 break;
326 }
327 }
328
329 /** Function to manage TMC data transmission and reception to and from the host. */
330 void TMC_Task(void)
331 {
332 /* Device must be connected and configured for the task to run */
333 if (USB_DeviceState != DEVICE_STATE_Configured)
334 return;
335
336 Endpoint_SelectEndpoint(TMC_OUT_EPNUM);
337
338 if (Endpoint_IsOUTReceived())
339 {
340 // TEMP - Indicate data received
341 LEDs_SetAllLEDs(LEDS_ALL_LEDS);
342 Endpoint_ClearOUT();
343 }
344
345 /* All pending data has been processed - reset the data abort flags */
346 IsTMCBulkINReset = false;
347 IsTMCBulkOUTReset = false;
348 }
349
350 /** Stream callback function for the Endpoint stream write functions. This callback will abort the current stream transfer
351 * if a TMC Abort Bulk IN request has been issued to the control endpoint.
352 */
353 uint8_t StreamCallback_AbortINOnRequest(void)
354 {
355 /* Abort if a TMC Bulk Data IN abort was received */
356 if (IsTMCBulkINReset)
357 return STREAMCALLBACK_Abort;
358
359 /* Continue with the current stream operation */
360 return STREAMCALLBACK_Continue;
361 }
362
363 /** Stream callback function for the Endpoint stream read functions. This callback will abort the current stream transfer
364 * if a TMC Abort Bulk OUT request has been issued to the control endpoint.
365 */
366 uint8_t StreamCallback_AbortOUTOnRequest(void)
367 {
368 /* Abort if a TMC Bulk Data IN abort was received */
369 if (IsTMCBulkOUTReset)
370 return STREAMCALLBACK_Abort;
371
372 /* Continue with the current stream operation */
373 return STREAMCALLBACK_Continue;
374 }