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 bool ConfigSuccess
= true;
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
);
131 /* Indicate endpoint configuration success or failure */
132 LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY
: LEDMASK_USB_ERROR
);
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.
139 void EVENT_USB_Device_UnhandledControlRequest(void)
141 uint8_t TMCRequestStatus
= TMC_STATUS_SUCCESS
;
143 /* Process TMC specific control requests */
144 switch (USB_ControlRequest
.bRequest
)
146 case Req_InitiateAbortBulkOut
:
147 if (USB_ControlRequest
.bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_ENDPOINT
))
149 Endpoint_ClearSETUP();
151 /* Check that no split transaction is already in progress and the data transfer tag is valid */
152 if (RequestInProgress
!= 0)
154 TMCRequestStatus
= TMC_STATUS_SPLIT_IN_PROGRESS
;
156 else if (USB_ControlRequest
.wValue
!= CurrentTransferTag
)
158 TMCRequestStatus
= TMC_STATUS_TRANSFER_NOT_IN_PROGRESS
;
162 /* Indicate that all in-progress/pending data OUT requests should be aborted */
163 IsTMCBulkOUTReset
= true;
165 /* Save the split request for later checking when a new request is received */
166 RequestInProgress
= Req_InitiateAbortBulkOut
;
169 /* Write the request response byte */
170 Endpoint_Write_Byte(TMCRequestStatus
);
173 Endpoint_ClearStatusStage();
177 case Req_CheckAbortBulkOutStatus
:
178 if (USB_ControlRequest
.bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_ENDPOINT
))
180 Endpoint_ClearSETUP();
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
;
188 RequestInProgress
= 0;
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
196 Endpoint_ClearStatusStage();
200 case Req_InitiateAbortBulkIn
:
201 if (USB_ControlRequest
.bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_ENDPOINT
))
203 Endpoint_ClearSETUP();
205 /* Check that no split transaction is already in progress and the data transfer tag is valid */
206 if (RequestInProgress
!= 0)
208 TMCRequestStatus
= TMC_STATUS_SPLIT_IN_PROGRESS
;
210 else if (USB_ControlRequest
.wValue
!= CurrentTransferTag
)
212 TMCRequestStatus
= TMC_STATUS_TRANSFER_NOT_IN_PROGRESS
;
216 /* Indicate that all in-progress/pending data IN requests should be aborted */
217 IsTMCBulkINReset
= true;
219 /* Save the split request for later checking when a new request is received */
220 RequestInProgress
= Req_InitiateAbortBulkIn
;
223 /* Write the request response bytes */
224 Endpoint_Write_Byte(TMCRequestStatus
);
225 Endpoint_Write_Byte(CurrentTransferTag
);
228 Endpoint_ClearStatusStage();
232 case Req_CheckAbortBulkInStatus
:
233 if (USB_ControlRequest
.bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_ENDPOINT
))
235 Endpoint_ClearSETUP();
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
;
243 RequestInProgress
= 0;
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
251 Endpoint_ClearStatusStage();
255 case Req_InitiateClear
:
256 if (USB_ControlRequest
.bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_INTERFACE
))
258 Endpoint_ClearSETUP();
260 /* Check that no split transaction is already in progress */
261 if (RequestInProgress
!= 0)
263 Endpoint_Write_Byte(TMC_STATUS_SPLIT_IN_PROGRESS
);
267 /* Indicate that all in-progress/pending data IN and OUT requests should be aborted */
268 IsTMCBulkINReset
= true;
269 IsTMCBulkOUTReset
= true;
271 /* Save the split request for later checking when a new request is received */
272 RequestInProgress
= Req_InitiateClear
;
275 /* Write the request response byte */
276 Endpoint_Write_Byte(TMCRequestStatus
);
279 Endpoint_ClearStatusStage();
283 case Req_CheckClearStatus
:
284 if (USB_ControlRequest
.bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_INTERFACE
))
286 Endpoint_ClearSETUP();
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
;
294 RequestInProgress
= 0;
296 /* Write the request response bytes */
297 Endpoint_Write_Byte(TMCRequestStatus
);
298 Endpoint_Write_Byte(0);
301 Endpoint_ClearStatusStage();
305 case Req_GetCapabilities
:
306 if (USB_ControlRequest
.bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_INTERFACE
))
308 /* Acknowledge the SETUP packet, ready for data transfer */
309 Endpoint_ClearSETUP();
311 /* Write the device capabilities to the control endpoint */
312 Endpoint_Write_Control_Stream_LE(&Capabilities
, sizeof(TMC_Capabilities_t
));
314 /* Finalize the stream transfer to send the last packet or clear the host abort */
322 /** Function to manage TMC data transmission and reception to and from the host. */
325 /* Device must be connected and configured for the task to run */
326 if (USB_DeviceState
!= DEVICE_STATE_Configured
)
329 TMC_MessageHeader_t MessageHeader
;
331 /* Try to read in a TMC message from the interface, process if one is available */
332 if (ReadTMCHeader(&MessageHeader
))
335 LEDs_SetAllLEDs(LEDMASK_USB_BUSY
);
337 switch (MessageHeader
.MessageID
)
339 case TMC_MESSAGEID_DEV_DEP_MSG_OUT
:
340 Endpoint_Discard_Stream(MessageHeader
.TransferSize
, StreamCallback_AbortOUTOnRequest
);
343 case TMC_MESSAGEID_DEV_DEP_MSG_IN
:
346 MessageHeader
.TransferSize
= 3;
347 WriteTMCHeader(&MessageHeader
);
349 Endpoint_Write_Stream_LE("TMC", 3, StreamCallback_AbortINOnRequest
);
353 Endpoint_StallTransaction();
357 LEDs_SetAllLEDs(LEDMASK_USB_READY
);
360 /* All pending data has been processed - reset the data abort flags */
361 IsTMCBulkINReset
= false;
362 IsTMCBulkOUTReset
= false;
365 /** Attempts to read in the TMC message header from the TMC interface.
367 * \param[out] MessageHeader Pointer to a location where the read header (if any) should be stored
369 * \return Boolean true if a header was read, false otherwise
371 bool ReadTMCHeader(TMC_MessageHeader_t
* const MessageHeader
)
373 /* Select the Data Out endpoint */
374 Endpoint_SelectEndpoint(TMC_OUT_EPNUM
);
376 /* Abort if no command has been sent from the host */
377 if (!(Endpoint_IsOUTReceived()))
380 /* Read in the header of the command from the host */
381 Endpoint_Read_Stream_LE(MessageHeader
, sizeof(TMC_MessageHeader_t
), StreamCallback_AbortOUTOnRequest
);
383 /* Store the new command tag value for later use */
384 CurrentTransferTag
= MessageHeader
->Tag
;
386 /* Indicate if the command has been aborted or not */
387 return !(IsTMCBulkOUTReset
);
390 bool WriteTMCHeader(TMC_MessageHeader_t
* const MessageHeader
)
392 /* Compute the next transfer tag value, must be between 1 and 254 */
393 if (++CurrentTransferTag
== 0xFF)
394 CurrentTransferTag
= 1;
396 /* Set the message tag of the command header */
397 MessageHeader
->Tag
= CurrentTransferTag
;
398 MessageHeader
->InverseTag
= ~CurrentTransferTag
;
400 /* Select the Data In endpoint */
401 Endpoint_SelectEndpoint(TMC_IN_EPNUM
);
403 /* Send the command header to the host */
404 Endpoint_Write_Stream_LE(MessageHeader
, sizeof(TMC_MessageHeader_t
), StreamCallback_AbortINOnRequest
);
406 /* Indicate if the command has been aborted or not */
407 return !(IsTMCBulkINReset
);
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.
413 uint8_t StreamCallback_AbortINOnRequest(void)
415 /* Abort if a TMC Bulk Data IN abort was received */
416 if (IsTMCBulkINReset
)
417 return STREAMCALLBACK_Abort
;
419 /* Continue with the current stream operation */
420 return STREAMCALLBACK_Continue
;
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.
426 uint8_t StreamCallback_AbortOUTOnRequest(void)
428 /* Abort if a TMC Bulk Data IN abort was received */
429 if (IsTMCBulkOUTReset
)
430 return STREAMCALLBACK_Abort
;
432 /* Continue with the current stream operation */
433 return STREAMCALLBACK_Continue
;