Removed complicated logic for the Endpoint_ConfigureEndpoint() function to use inline...
[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_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_IN_EPNUM, EP_TYPE_BULK, ENDPOINT_DIR_IN,
125 TMC_IO_EPSIZE, ENDPOINT_BANK_SINGLE);
126 ConfigSuccess &= Endpoint_ConfigureEndpoint(TMC_OUT_EPNUM, EP_TYPE_BULK, ENDPOINT_DIR_OUT,
127 TMC_IO_EPSIZE, ENDPOINT_BANK_SINGLE);
128 ConfigSuccess &= Endpoint_ConfigureEndpoint(TMC_NOTIFICATION_EPNUM, EP_TYPE_INTERRUPT, ENDPOINT_DIR_IN,
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_UnhandledControlRequest event. This is used to catch standard and class specific
136 * control requests that are not handled internally by the USB library (including the CDC control commands,
137 * which are all issued via the control endpoint), so that they can be handled appropriately for the application.
138 */
139 void EVENT_USB_Device_UnhandledControlRequest(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 Endpoint_ClearSETUP();
150
151 /* Check that no split transaction is already in progress and the data transfer tag is valid */
152 if (RequestInProgress != 0)
153 {
154 TMCRequestStatus = TMC_STATUS_SPLIT_IN_PROGRESS;
155 }
156 else if (USB_ControlRequest.wValue != CurrentTransferTag)
157 {
158 TMCRequestStatus = TMC_STATUS_TRANSFER_NOT_IN_PROGRESS;
159 }
160 else
161 {
162 /* Indicate that all in-progress/pending data OUT requests should be aborted */
163 IsTMCBulkOUTReset = true;
164
165 /* Save the split request for later checking when a new request is received */
166 RequestInProgress = Req_InitiateAbortBulkOut;
167 }
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 Endpoint_ClearSETUP();
181
182 /* Check that an ABORT BULK OUT transaction has been requested and that the request has completed */
183 if (RequestInProgress != Req_InitiateAbortBulkOut)
184 TMCRequestStatus = TMC_STATUS_SPLIT_NOT_IN_PROGRESS;
185 else if (IsTMCBulkOUTReset)
186 TMCRequestStatus = TMC_STATUS_PENDING;
187 else
188 RequestInProgress = 0;
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 Endpoint_ClearSETUP();
204
205 /* Check that no split transaction is already in progress and the data transfer tag is valid */
206 if (RequestInProgress != 0)
207 {
208 TMCRequestStatus = TMC_STATUS_SPLIT_IN_PROGRESS;
209 }
210 else if (USB_ControlRequest.wValue != CurrentTransferTag)
211 {
212 TMCRequestStatus = TMC_STATUS_TRANSFER_NOT_IN_PROGRESS;
213 }
214 else
215 {
216 /* Indicate that all in-progress/pending data IN requests should be aborted */
217 IsTMCBulkINReset = true;
218
219 /* Save the split request for later checking when a new request is received */
220 RequestInProgress = Req_InitiateAbortBulkIn;
221 }
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 Endpoint_ClearSETUP();
236
237 /* Check that an ABORT BULK IN transaction has been requested and that the request has completed */
238 if (RequestInProgress != Req_InitiateAbortBulkIn)
239 TMCRequestStatus = TMC_STATUS_SPLIT_NOT_IN_PROGRESS;
240 else if (IsTMCBulkINReset)
241 TMCRequestStatus = TMC_STATUS_PENDING;
242 else
243 RequestInProgress = 0;
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 Endpoint_ClearSETUP();
259
260 /* Check that no split transaction is already in progress */
261 if (RequestInProgress != 0)
262 {
263 Endpoint_Write_Byte(TMC_STATUS_SPLIT_IN_PROGRESS);
264 }
265 else
266 {
267 /* Indicate that all in-progress/pending data IN and OUT requests should be aborted */
268 IsTMCBulkINReset = true;
269 IsTMCBulkOUTReset = true;
270
271 /* Save the split request for later checking when a new request is received */
272 RequestInProgress = Req_InitiateClear;
273 }
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 Endpoint_ClearSETUP();
287
288 /* Check that a CLEAR transaction has been requested and that the request has completed */
289 if (RequestInProgress != Req_InitiateClear)
290 TMCRequestStatus = TMC_STATUS_SPLIT_NOT_IN_PROGRESS;
291 else if (IsTMCBulkINReset || IsTMCBulkOUTReset)
292 TMCRequestStatus = TMC_STATUS_PENDING;
293 else
294 RequestInProgress = 0;
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 /* Acknowledge the SETUP packet, ready for data transfer */
309 Endpoint_ClearSETUP();
310
311 /* Write the device capabilities to the control endpoint */
312 Endpoint_Write_Control_Stream_LE(&Capabilities, sizeof(TMC_Capabilities_t));
313
314 /* Finalize the stream transfer to send the last packet or clear the host abort */
315 Endpoint_ClearOUT();
316 }
317
318 break;
319 }
320 }
321
322 /** Function to manage TMC data transmission and reception to and from the host. */
323 void TMC_Task(void)
324 {
325 /* Device must be connected and configured for the task to run */
326 if (USB_DeviceState != DEVICE_STATE_Configured)
327 return;
328
329 TMC_MessageHeader_t MessageHeader;
330
331 /* Try to read in a TMC message from the interface, process if one is available */
332 if (ReadTMCHeader(&MessageHeader))
333 {
334 /* Indicate busy */
335 LEDs_SetAllLEDs(LEDMASK_USB_BUSY);
336
337 switch (MessageHeader.MessageID)
338 {
339 case TMC_MESSAGEID_DEV_DEP_MSG_OUT:
340 Endpoint_Discard_Stream(MessageHeader.TransferSize, StreamCallback_AbortOUTOnRequest);
341 Endpoint_ClearOUT();
342 break;
343 case TMC_MESSAGEID_DEV_DEP_MSG_IN:
344 Endpoint_ClearOUT();
345
346 MessageHeader.TransferSize = 3;
347 WriteTMCHeader(&MessageHeader);
348
349 Endpoint_Write_Stream_LE("TMC", 3, StreamCallback_AbortINOnRequest);
350 Endpoint_ClearIN();
351 break;
352 default:
353 Endpoint_StallTransaction();
354 break;
355 }
356
357 LEDs_SetAllLEDs(LEDMASK_USB_READY);
358 }
359
360 /* All pending data has been processed - reset the data abort flags */
361 IsTMCBulkINReset = false;
362 IsTMCBulkOUTReset = false;
363 }
364
365 /** Attempts to read in the TMC message header from the TMC interface.
366 *
367 * \param[out] MessageHeader Pointer to a location where the read header (if any) should be stored
368 *
369 * \return Boolean true if a header was read, false otherwise
370 */
371 bool ReadTMCHeader(TMC_MessageHeader_t* const MessageHeader)
372 {
373 /* Select the Data Out endpoint */
374 Endpoint_SelectEndpoint(TMC_OUT_EPNUM);
375
376 /* Abort if no command has been sent from the host */
377 if (!(Endpoint_IsOUTReceived()))
378 return false;
379
380 /* Read in the header of the command from the host */
381 Endpoint_Read_Stream_LE(MessageHeader, sizeof(TMC_MessageHeader_t), StreamCallback_AbortOUTOnRequest);
382
383 /* Store the new command tag value for later use */
384 CurrentTransferTag = MessageHeader->Tag;
385
386 /* Indicate if the command has been aborted or not */
387 return !(IsTMCBulkOUTReset);
388 }
389
390 bool WriteTMCHeader(TMC_MessageHeader_t* const MessageHeader)
391 {
392 /* Compute the next transfer tag value, must be between 1 and 254 */
393 if (++CurrentTransferTag == 0xFF)
394 CurrentTransferTag = 1;
395
396 /* Set the message tag of the command header */
397 MessageHeader->Tag = CurrentTransferTag;
398 MessageHeader->InverseTag = ~CurrentTransferTag;
399
400 /* Select the Data In endpoint */
401 Endpoint_SelectEndpoint(TMC_IN_EPNUM);
402
403 /* Send the command header to the host */
404 Endpoint_Write_Stream_LE(MessageHeader, sizeof(TMC_MessageHeader_t), StreamCallback_AbortINOnRequest);
405
406 /* Indicate if the command has been aborted or not */
407 return !(IsTMCBulkINReset);
408 }
409
410 /** Stream callback function for the Endpoint stream write functions. This callback will abort the current stream transfer
411 * if a TMC Abort Bulk IN request has been issued to the control endpoint.
412 */
413 uint8_t StreamCallback_AbortINOnRequest(void)
414 {
415 /* Abort if a TMC Bulk Data IN abort was received */
416 if (IsTMCBulkINReset)
417 return STREAMCALLBACK_Abort;
418
419 /* Continue with the current stream operation */
420 return STREAMCALLBACK_Continue;
421 }
422
423 /** Stream callback function for the Endpoint stream read functions. This callback will abort the current stream transfer
424 * if a TMC Abort Bulk OUT request has been issued to the control endpoint.
425 */
426 uint8_t StreamCallback_AbortOUTOnRequest(void)
427 {
428 /* Abort if a TMC Bulk Data IN abort was received */
429 if (IsTMCBulkOUTReset)
430 return STREAMCALLBACK_Abort;
431
432 /* Continue with the current stream operation */
433 return STREAMCALLBACK_Continue;
434 }