3 Copyright (C) Dean Camera, 2010.
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
10 Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com)
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.
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
31 #include "TestAndMeasurement.h"
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.
37 TMC_Capabilities_t Capabilities
=
39 .Status
= TMC_STATUS_SUCCESS
,
40 .TMCVersion
= VERSION_BCD(1.00),
46 .PulseIndicateSupported
= false,
51 .SupportsAbortINOnMatch
= false,
55 /** Current TMC control request that is being processed */
56 uint8_t RequestInProgress
= 0;
58 /** Stream callback abort flag for bulk IN data */
59 bool IsTMCBulkINReset
= false;
61 /** Stream callback abort flag for bulk OUT data */
62 bool IsTMCBulkOUTReset
= false;
64 /** Last used tag value for data transfers */
65 uint8_t CurrentTransferTag
= 0;
68 /** Main program entry point. This routine contains the overall program flow, including initial
69 * setup of all components and the main program loop.
75 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
);
85 /** Configures the board hardware and chip peripherals for the demo's functionality. */
86 void SetupHardware(void)
88 /* Disable watchdog if enabled by bootloader/fuses */
89 MCUSR
&= ~(1 << WDRF
);
92 /* Disable clock division */
93 clock_prescale_set(clock_div_1
);
95 /* Hardware Initialization */
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.
103 void EVENT_USB_Device_Connect(void)
105 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING
);
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.
111 void EVENT_USB_Device_Disconnect(void)
113 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
);
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.
119 void EVENT_USB_Device_ConfigurationChanged(void)
121 LEDs_SetAllLEDs(LEDMASK_USB_READY
);
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
)))
128 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
131 if (!(Endpoint_ConfigureEndpoint(TMC_OUT_EPNUM
, EP_TYPE_BULK
,
132 ENDPOINT_DIR_OUT
, TMC_IO_EPSIZE
,
133 ENDPOINT_BANK_SINGLE
)))
135 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
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.
143 void EVENT_USB_Device_UnhandledControlRequest(void)
145 uint8_t TMCRequestStatus
= TMC_STATUS_SUCCESS
;
147 /* Process TMC specific control requests */
148 switch (USB_ControlRequest
.bRequest
)
150 case Req_InitiateAbortBulkOut
:
151 if (USB_ControlRequest
.bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_ENDPOINT
))
153 Endpoint_ClearSETUP();
155 /* Check that no split transaction is already in progress and the data transfer tag is valid */
156 if (RequestInProgress
!= 0)
158 TMCRequestStatus
= TMC_STATUS_SPLIT_IN_PROGRESS
;
160 else if (USB_ControlRequest
.wValue
!= CurrentTransferTag
)
162 TMCRequestStatus
= TMC_STATUS_TRANSFER_NOT_IN_PROGRESS
;
166 /* Indicate that all in-progress/pending data OUT requests should be aborted */
167 IsTMCBulkOUTReset
= true;
169 /* Save the split request for later checking when a new request is received */
170 RequestInProgress
= Req_InitiateAbortBulkOut
;
173 /* Write the request response byte */
174 Endpoint_Write_Byte(TMCRequestStatus
);
177 Endpoint_ClearStatusStage();
181 case Req_CheckAbortBulkOutStatus
:
182 if (USB_ControlRequest
.bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_ENDPOINT
))
184 Endpoint_ClearSETUP();
186 /* Check that an ABORT BULK OUT transaction has been requested and that the request has completed */
187 if (RequestInProgress
!= Req_InitiateAbortBulkOut
)
188 TMCRequestStatus
= TMC_STATUS_SPLIT_NOT_IN_PROGRESS
;
189 else if (IsTMCBulkOUTReset
)
190 TMCRequestStatus
= TMC_STATUS_PENDING
;
192 RequestInProgress
= 0;
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
200 Endpoint_ClearStatusStage();
204 case Req_InitiateAbortBulkIn
:
205 if (USB_ControlRequest
.bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_ENDPOINT
))
207 Endpoint_ClearSETUP();
209 /* Check that no split transaction is already in progress and the data transfer tag is valid */
210 if (RequestInProgress
!= 0)
212 TMCRequestStatus
= TMC_STATUS_SPLIT_IN_PROGRESS
;
214 else if (USB_ControlRequest
.wValue
!= CurrentTransferTag
)
216 TMCRequestStatus
= TMC_STATUS_TRANSFER_NOT_IN_PROGRESS
;
220 /* Indicate that all in-progress/pending data IN requests should be aborted */
221 IsTMCBulkINReset
= true;
223 /* Save the split request for later checking when a new request is received */
224 RequestInProgress
= Req_InitiateAbortBulkIn
;
227 /* Write the request response bytes */
228 Endpoint_Write_Byte(TMCRequestStatus
);
229 Endpoint_Write_Byte(CurrentTransferTag
);
232 Endpoint_ClearStatusStage();
236 case Req_CheckAbortBulkInStatus
:
237 if (USB_ControlRequest
.bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_ENDPOINT
))
239 Endpoint_ClearSETUP();
241 /* Check that an ABORT BULK IN transaction has been requested and that the request has completed */
242 if (RequestInProgress
!= Req_InitiateAbortBulkIn
)
243 TMCRequestStatus
= TMC_STATUS_SPLIT_NOT_IN_PROGRESS
;
244 else if (IsTMCBulkINReset
)
245 TMCRequestStatus
= TMC_STATUS_PENDING
;
247 RequestInProgress
= 0;
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
255 Endpoint_ClearStatusStage();
259 case Req_InitiateClear
:
260 if (USB_ControlRequest
.bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_INTERFACE
))
262 Endpoint_ClearSETUP();
264 /* Check that no split transaction is already in progress */
265 if (RequestInProgress
!= 0)
267 Endpoint_Write_Byte(TMC_STATUS_SPLIT_IN_PROGRESS
);
271 /* Indicate that all in-progress/pending data IN and OUT requests should be aborted */
272 IsTMCBulkINReset
= true;
273 IsTMCBulkOUTReset
= true;
275 /* Save the split request for later checking when a new request is received */
276 RequestInProgress
= Req_InitiateClear
;
279 /* Write the request response byte */
280 Endpoint_Write_Byte(TMCRequestStatus
);
283 Endpoint_ClearStatusStage();
287 case Req_CheckClearStatus
:
288 if (USB_ControlRequest
.bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_INTERFACE
))
290 Endpoint_ClearSETUP();
292 /* Check that a CLEAR transaction has been requested and that the request has completed */
293 if (RequestInProgress
!= Req_InitiateClear
)
294 TMCRequestStatus
= TMC_STATUS_SPLIT_NOT_IN_PROGRESS
;
295 else if (IsTMCBulkINReset
|| IsTMCBulkOUTReset
)
296 TMCRequestStatus
= TMC_STATUS_PENDING
;
298 RequestInProgress
= 0;
300 /* Write the request response bytes */
301 Endpoint_Write_Byte(TMCRequestStatus
);
302 Endpoint_Write_Byte(0);
305 Endpoint_ClearStatusStage();
309 case Req_GetCapabilities
:
310 if (USB_ControlRequest
.bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_INTERFACE
))
312 /* Acknowledge the SETUP packet, ready for data transfer */
313 Endpoint_ClearSETUP();
315 /* Write the device capabilities to the control endpoint */
316 Endpoint_Write_Control_Stream_LE(&Capabilities
, sizeof(TMC_Capabilities_t
));
318 /* Finalize the stream transfer to send the last packet or clear the host abort */
326 /** Function to manage TMC data transmission and reception to and from the host. */
329 /* Device must be connected and configured for the task to run */
330 if (USB_DeviceState
!= DEVICE_STATE_Configured
)
333 TMC_MessageHeader_t MessageHeader
;
335 /* Check if a TMC packet has been received */
336 if (ReadTMCHeader(&MessageHeader
))
339 LEDs_SetAllLEDs(LEDMASK_USB_BUSY
);
341 switch (MessageHeader
.MessageID
)
343 case TMC_MESSAGEID_DEV_DEP_MSG_OUT
:
344 Endpoint_Discard_Stream(MessageHeader
.TransferSize
, StreamCallback_AbortOUTOnRequest
);
347 case TMC_MESSAGEID_DEV_DEP_MSG_IN
:
350 MessageHeader
.TransferSize
= 3;
351 WriteTMCHeader(&MessageHeader
);
353 Endpoint_Write_Stream_LE("TMC", 3, StreamCallback_AbortINOnRequest
);
357 Endpoint_StallTransaction();
361 LEDs_SetAllLEDs(LEDMASK_USB_READY
);
364 /* All pending data has been processed - reset the data abort flags */
365 IsTMCBulkINReset
= false;
366 IsTMCBulkOUTReset
= false;
369 bool ReadTMCHeader(TMC_MessageHeader_t
* const MessageHeader
)
371 /* Select the Data Out endpoint */
372 Endpoint_SelectEndpoint(TMC_OUT_EPNUM
);
374 /* Abort if no command has been sent from the host */
375 if (!(Endpoint_IsOUTReceived()))
378 /* Read in the header of the command from the host */
379 Endpoint_Read_Stream_LE(MessageHeader
, sizeof(TMC_MessageHeader_t
), StreamCallback_AbortOUTOnRequest
);
381 /* Store the new command tag value for later use */
382 CurrentTransferTag
= MessageHeader
->Tag
;
384 /* Indicate if the command has been aborted or not */
385 return !IsTMCBulkOUTReset
;
388 bool WriteTMCHeader(TMC_MessageHeader_t
* const MessageHeader
)
390 /* Compute the next transfer tag value, must be between 1 and 254 */
391 if (++CurrentTransferTag
== 0xFF)
392 CurrentTransferTag
= 1;
394 /* Set the message tag of the command header */
395 MessageHeader
->Tag
= CurrentTransferTag
;
396 MessageHeader
->InverseTag
= ~CurrentTransferTag
;
398 /* Select the Data In endpoint */
399 Endpoint_SelectEndpoint(TMC_IN_EPNUM
);
401 /* Send the command header to the host */
402 Endpoint_Write_Stream_LE(MessageHeader
, sizeof(TMC_MessageHeader_t
), StreamCallback_AbortINOnRequest
);
404 /* Indicate if the command has been aborted or not */
405 return !IsTMCBulkINReset
;
408 /** Stream callback function for the Endpoint stream write functions. This callback will abort the current stream transfer
409 * if a TMC Abort Bulk IN request has been issued to the control endpoint.
411 uint8_t StreamCallback_AbortINOnRequest(void)
413 /* Abort if a TMC Bulk Data IN abort was received */
414 if (IsTMCBulkINReset
)
415 return STREAMCALLBACK_Abort
;
417 /* Continue with the current stream operation */
418 return STREAMCALLBACK_Continue
;
421 /** Stream callback function for the Endpoint stream read functions. This callback will abort the current stream transfer
422 * if a TMC Abort Bulk OUT request has been issued to the control endpoint.
424 uint8_t StreamCallback_AbortOUTOnRequest(void)
426 /* Abort if a TMC Bulk Data IN abort was received */
427 if (IsTMCBulkOUTReset
)
428 return STREAMCALLBACK_Abort
;
430 /* Continue with the current stream operation */
431 return STREAMCALLBACK_Continue
;