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