Renamed the EVENT_USB_Device_UnhandledControlRequest() event to EVENT_USB_Device_Cont...
[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.lufa-lib.org
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 RequestInProgress = 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 bool ConfigSuccess = true;
122
123 /* Setup TMC In, Out and Notification Endpoints */
124 ConfigSuccess &= Endpoint_ConfigureEndpoint(TMC_NOTIFICATION_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
125 TMC_IO_EPSIZE, ENDPOINT_BANK_SINGLE);
126 ConfigSuccess &= Endpoint_ConfigureEndpoint(TMC_IN_EPNUM, EP_TYPE_BULK, ENDPOINT_DIR_IN,
127 TMC_IO_EPSIZE, ENDPOINT_BANK_SINGLE);
128 ConfigSuccess &= Endpoint_ConfigureEndpoint(TMC_OUT_EPNUM, EP_TYPE_BULK, ENDPOINT_DIR_OUT,
129 TMC_IO_EPSIZE, ENDPOINT_BANK_SINGLE);
130
131 /* Indicate endpoint configuration success or failure */
132 LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR);
133 }
134
135 /** Event handler for the USB_ControlRequest event. This is used to catch and process control requests sent to
136 * the device from the USB host before passing along unhandled control requests to the library for processing
137 * internally.
138 */
139 void EVENT_USB_Device_ControlRequest(void)
140 {
141 uint8_t TMCRequestStatus = TMC_STATUS_SUCCESS;
142
143 /* Process TMC specific control requests */
144 switch (USB_ControlRequest.bRequest)
145 {
146 case Req_InitiateAbortBulkOut:
147 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_ENDPOINT))
148 {
149 /* Check that no split transaction is already in progress and the data transfer tag is valid */
150 if (RequestInProgress != 0)
151 {
152 TMCRequestStatus = TMC_STATUS_SPLIT_IN_PROGRESS;
153 }
154 else if (USB_ControlRequest.wValue != CurrentTransferTag)
155 {
156 TMCRequestStatus = TMC_STATUS_TRANSFER_NOT_IN_PROGRESS;
157 }
158 else
159 {
160 /* Indicate that all in-progress/pending data OUT requests should be aborted */
161 IsTMCBulkOUTReset = true;
162
163 /* Save the split request for later checking when a new request is received */
164 RequestInProgress = Req_InitiateAbortBulkOut;
165 }
166
167 Endpoint_ClearSETUP();
168
169 /* Write the request response byte */
170 Endpoint_Write_Byte(TMCRequestStatus);
171
172 Endpoint_ClearIN();
173 Endpoint_ClearStatusStage();
174 }
175
176 break;
177 case Req_CheckAbortBulkOutStatus:
178 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_ENDPOINT))
179 {
180 /* Check that an ABORT BULK OUT transaction has been requested and that the request has completed */
181 if (RequestInProgress != Req_InitiateAbortBulkOut)
182 TMCRequestStatus = TMC_STATUS_SPLIT_NOT_IN_PROGRESS;
183 else if (IsTMCBulkOUTReset)
184 TMCRequestStatus = TMC_STATUS_PENDING;
185 else
186 RequestInProgress = 0;
187
188 Endpoint_ClearSETUP();
189
190 /* Write the request response bytes */
191 Endpoint_Write_Byte(TMCRequestStatus);
192 Endpoint_Write_Word_LE(0);
193 Endpoint_Write_DWord_LE(0); // TODO - Last transfer length
194
195 Endpoint_ClearIN();
196 Endpoint_ClearStatusStage();
197 }
198
199 break;
200 case Req_InitiateAbortBulkIn:
201 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_ENDPOINT))
202 {
203 /* Check that no split transaction is already in progress and the data transfer tag is valid */
204 if (RequestInProgress != 0)
205 {
206 TMCRequestStatus = TMC_STATUS_SPLIT_IN_PROGRESS;
207 }
208 else if (USB_ControlRequest.wValue != CurrentTransferTag)
209 {
210 TMCRequestStatus = TMC_STATUS_TRANSFER_NOT_IN_PROGRESS;
211 }
212 else
213 {
214 /* Indicate that all in-progress/pending data IN requests should be aborted */
215 IsTMCBulkINReset = true;
216
217 /* Save the split request for later checking when a new request is received */
218 RequestInProgress = Req_InitiateAbortBulkIn;
219 }
220
221 Endpoint_ClearSETUP();
222
223 /* Write the request response bytes */
224 Endpoint_Write_Byte(TMCRequestStatus);
225 Endpoint_Write_Byte(CurrentTransferTag);
226
227 Endpoint_ClearIN();
228 Endpoint_ClearStatusStage();
229 }
230
231 break;
232 case Req_CheckAbortBulkInStatus:
233 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_ENDPOINT))
234 {
235 /* Check that an ABORT BULK IN transaction has been requested and that the request has completed */
236 if (RequestInProgress != Req_InitiateAbortBulkIn)
237 TMCRequestStatus = TMC_STATUS_SPLIT_NOT_IN_PROGRESS;
238 else if (IsTMCBulkINReset)
239 TMCRequestStatus = TMC_STATUS_PENDING;
240 else
241 RequestInProgress = 0;
242
243 Endpoint_ClearSETUP();
244
245 /* Write the request response bytes */
246 Endpoint_Write_Byte(TMCRequestStatus);
247 Endpoint_Write_Word_LE(0);
248 Endpoint_Write_DWord_LE(0); // TODO - Last transfer length
249
250 Endpoint_ClearIN();
251 Endpoint_ClearStatusStage();
252 }
253
254 break;
255 case Req_InitiateClear:
256 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
257 {
258 /* Check that no split transaction is already in progress */
259 if (RequestInProgress != 0)
260 {
261 Endpoint_Write_Byte(TMC_STATUS_SPLIT_IN_PROGRESS);
262 }
263 else
264 {
265 /* Indicate that all in-progress/pending data IN and OUT requests should be aborted */
266 IsTMCBulkINReset = true;
267 IsTMCBulkOUTReset = true;
268
269 /* Save the split request for later checking when a new request is received */
270 RequestInProgress = Req_InitiateClear;
271 }
272
273 Endpoint_ClearSETUP();
274
275 /* Write the request response byte */
276 Endpoint_Write_Byte(TMCRequestStatus);
277
278 Endpoint_ClearIN();
279 Endpoint_ClearStatusStage();
280 }
281
282 break;
283 case Req_CheckClearStatus:
284 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
285 {
286 /* Check that a CLEAR transaction has been requested and that the request has completed */
287 if (RequestInProgress != Req_InitiateClear)
288 TMCRequestStatus = TMC_STATUS_SPLIT_NOT_IN_PROGRESS;
289 else if (IsTMCBulkINReset || IsTMCBulkOUTReset)
290 TMCRequestStatus = TMC_STATUS_PENDING;
291 else
292 RequestInProgress = 0;
293
294 Endpoint_ClearSETUP();
295
296 /* Write the request response bytes */
297 Endpoint_Write_Byte(TMCRequestStatus);
298 Endpoint_Write_Byte(0);
299
300 Endpoint_ClearIN();
301 Endpoint_ClearStatusStage();
302 }
303
304 break;
305 case Req_GetCapabilities:
306 if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
307 {
308 Endpoint_ClearSETUP();
309
310 /* Write the device capabilities to the control endpoint */
311 Endpoint_Write_Control_Stream_LE(&Capabilities, sizeof(TMC_Capabilities_t));
312 Endpoint_ClearOUT();
313 }
314
315 break;
316 }
317 }
318
319 /** Function to manage TMC data transmission and reception to and from the host. */
320 void TMC_Task(void)
321 {
322 /* Device must be connected and configured for the task to run */
323 if (USB_DeviceState != DEVICE_STATE_Configured)
324 return;
325
326 TMC_MessageHeader_t MessageHeader;
327
328 /* Try to read in a TMC message from the interface, process if one is available */
329 if (ReadTMCHeader(&MessageHeader))
330 {
331 /* Indicate busy */
332 LEDs_SetAllLEDs(LEDMASK_USB_BUSY);
333
334 switch (MessageHeader.MessageID)
335 {
336 case TMC_MESSAGEID_DEV_DEP_MSG_OUT:
337 Endpoint_Discard_Stream(MessageHeader.TransferSize, StreamCallback_AbortOUTOnRequest);
338 Endpoint_ClearOUT();
339 break;
340 case TMC_MESSAGEID_DEV_DEP_MSG_IN:
341 Endpoint_ClearOUT();
342
343 MessageHeader.TransferSize = 3;
344 WriteTMCHeader(&MessageHeader);
345
346 Endpoint_Write_Stream_LE("TMC", 3, StreamCallback_AbortINOnRequest);
347 Endpoint_ClearIN();
348 break;
349 default:
350 Endpoint_StallTransaction();
351 break;
352 }
353
354 LEDs_SetAllLEDs(LEDMASK_USB_READY);
355 }
356
357 /* All pending data has been processed - reset the data abort flags */
358 IsTMCBulkINReset = false;
359 IsTMCBulkOUTReset = false;
360 }
361
362 /** Attempts to read in the TMC message header from the TMC interface.
363 *
364 * \param[out] MessageHeader Pointer to a location where the read header (if any) should be stored
365 *
366 * \return Boolean true if a header was read, false otherwise
367 */
368 bool ReadTMCHeader(TMC_MessageHeader_t* const MessageHeader)
369 {
370 /* Select the Data Out endpoint */
371 Endpoint_SelectEndpoint(TMC_OUT_EPNUM);
372
373 /* Abort if no command has been sent from the host */
374 if (!(Endpoint_IsOUTReceived()))
375 return false;
376
377 /* Read in the header of the command from the host */
378 Endpoint_Read_Stream_LE(MessageHeader, sizeof(TMC_MessageHeader_t), StreamCallback_AbortOUTOnRequest);
379
380 /* Store the new command tag value for later use */
381 CurrentTransferTag = MessageHeader->Tag;
382
383 /* Indicate if the command has been aborted or not */
384 return !(IsTMCBulkOUTReset);
385 }
386
387 bool WriteTMCHeader(TMC_MessageHeader_t* const MessageHeader)
388 {
389 /* Compute the next transfer tag value, must be between 1 and 254 */
390 if (++CurrentTransferTag == 0xFF)
391 CurrentTransferTag = 1;
392
393 /* Set the message tag of the command header */
394 MessageHeader->Tag = CurrentTransferTag;
395 MessageHeader->InverseTag = ~CurrentTransferTag;
396
397 /* Select the Data In endpoint */
398 Endpoint_SelectEndpoint(TMC_IN_EPNUM);
399
400 /* Send the command header to the host */
401 Endpoint_Write_Stream_LE(MessageHeader, sizeof(TMC_MessageHeader_t), StreamCallback_AbortINOnRequest);
402
403 /* Indicate if the command has been aborted or not */
404 return !(IsTMCBulkINReset);
405 }
406
407 /** Stream callback function for the Endpoint stream write functions. This callback will abort the current stream transfer
408 * if a TMC Abort Bulk IN request has been issued to the control endpoint.
409 */
410 uint8_t StreamCallback_AbortINOnRequest(void)
411 {
412 /* Abort if a TMC Bulk Data IN abort was received */
413 if (IsTMCBulkINReset)
414 return STREAMCALLBACK_Abort;
415
416 /* Continue with the current stream operation */
417 return STREAMCALLBACK_Continue;
418 }
419
420 /** Stream callback function for the Endpoint stream read functions. This callback will abort the current stream transfer
421 * if a TMC Abort Bulk OUT request has been issued to the control endpoint.
422 */
423 uint8_t StreamCallback_AbortOUTOnRequest(void)
424 {
425 /* Abort if a TMC Bulk Data IN abort was received */
426 if (IsTMCBulkOUTReset)
427 return STREAMCALLBACK_Abort;
428
429 /* Continue with the current stream operation */
430 return STREAMCALLBACK_Continue;
431 }
432