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, 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
)))
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
);
138 if (!(Endpoint_ConfigureEndpoint(TMC_NOTIFICATION_EPNUM
, EP_TYPE_INTERRUPT
,
139 ENDPOINT_DIR_IN
, TMC_NOTIFICATION_EPSIZE
,
140 ENDPOINT_BANK_SINGLE
)))
142 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
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.
150 void EVENT_USB_Device_UnhandledControlRequest(void)
152 uint8_t TMCRequestStatus
= TMC_STATUS_SUCCESS
;
154 /* Process TMC specific control requests */
155 switch (USB_ControlRequest
.bRequest
)
157 case Req_InitiateAbortBulkOut
:
158 if (USB_ControlRequest
.bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_ENDPOINT
))
160 Endpoint_ClearSETUP();
162 /* Check that no split transaction is already in progress and the data transfer tag is valid */
163 if (RequestInProgress
!= 0)
165 TMCRequestStatus
= TMC_STATUS_SPLIT_IN_PROGRESS
;
167 else if (USB_ControlRequest
.wValue
!= CurrentTransferTag
)
169 TMCRequestStatus
= TMC_STATUS_TRANSFER_NOT_IN_PROGRESS
;
173 /* Indicate that all in-progress/pending data OUT requests should be aborted */
174 IsTMCBulkOUTReset
= true;
176 /* Save the split request for later checking when a new request is received */
177 RequestInProgress
= Req_InitiateAbortBulkOut
;
180 /* Write the request response byte */
181 Endpoint_Write_Byte(TMCRequestStatus
);
184 Endpoint_ClearStatusStage();
188 case Req_CheckAbortBulkOutStatus
:
189 if (USB_ControlRequest
.bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_ENDPOINT
))
191 Endpoint_ClearSETUP();
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
;
199 RequestInProgress
= 0;
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
207 Endpoint_ClearStatusStage();
211 case Req_InitiateAbortBulkIn
:
212 if (USB_ControlRequest
.bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_ENDPOINT
))
214 Endpoint_ClearSETUP();
216 /* Check that no split transaction is already in progress and the data transfer tag is valid */
217 if (RequestInProgress
!= 0)
219 TMCRequestStatus
= TMC_STATUS_SPLIT_IN_PROGRESS
;
221 else if (USB_ControlRequest
.wValue
!= CurrentTransferTag
)
223 TMCRequestStatus
= TMC_STATUS_TRANSFER_NOT_IN_PROGRESS
;
227 /* Indicate that all in-progress/pending data IN requests should be aborted */
228 IsTMCBulkINReset
= true;
230 /* Save the split request for later checking when a new request is received */
231 RequestInProgress
= Req_InitiateAbortBulkIn
;
234 /* Write the request response bytes */
235 Endpoint_Write_Byte(TMCRequestStatus
);
236 Endpoint_Write_Byte(CurrentTransferTag
);
239 Endpoint_ClearStatusStage();
243 case Req_CheckAbortBulkInStatus
:
244 if (USB_ControlRequest
.bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_ENDPOINT
))
246 Endpoint_ClearSETUP();
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
;
254 RequestInProgress
= 0;
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
262 Endpoint_ClearStatusStage();
266 case Req_InitiateClear
:
267 if (USB_ControlRequest
.bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_INTERFACE
))
269 Endpoint_ClearSETUP();
271 /* Check that no split transaction is already in progress */
272 if (RequestInProgress
!= 0)
274 Endpoint_Write_Byte(TMC_STATUS_SPLIT_IN_PROGRESS
);
278 /* Indicate that all in-progress/pending data IN and OUT requests should be aborted */
279 IsTMCBulkINReset
= true;
280 IsTMCBulkOUTReset
= true;
282 /* Save the split request for later checking when a new request is received */
283 RequestInProgress
= Req_InitiateClear
;
286 /* Write the request response byte */
287 Endpoint_Write_Byte(TMCRequestStatus
);
290 Endpoint_ClearStatusStage();
294 case Req_CheckClearStatus
:
295 if (USB_ControlRequest
.bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_INTERFACE
))
297 Endpoint_ClearSETUP();
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
;
305 RequestInProgress
= 0;
307 /* Write the request response bytes */
308 Endpoint_Write_Byte(TMCRequestStatus
);
309 Endpoint_Write_Byte(0);
312 Endpoint_ClearStatusStage();
316 case Req_GetCapabilities
:
317 if (USB_ControlRequest
.bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_INTERFACE
))
319 /* Acknowledge the SETUP packet, ready for data transfer */
320 Endpoint_ClearSETUP();
322 /* Write the device capabilities to the control endpoint */
323 Endpoint_Write_Control_Stream_LE(&Capabilities
, sizeof(TMC_Capabilities_t
));
325 /* Finalize the stream transfer to send the last packet or clear the host abort */
333 /** Function to manage TMC data transmission and reception to and from the host. */
336 /* Device must be connected and configured for the task to run */
337 if (USB_DeviceState
!= DEVICE_STATE_Configured
)
340 TMC_MessageHeader_t MessageHeader
;
342 /* Try to read in a TMC message from the interface, process if one is available */
343 if (ReadTMCHeader(&MessageHeader
))
346 LEDs_SetAllLEDs(LEDMASK_USB_BUSY
);
348 switch (MessageHeader
.MessageID
)
350 case TMC_MESSAGEID_DEV_DEP_MSG_OUT
:
351 Endpoint_Discard_Stream(MessageHeader
.TransferSize
, StreamCallback_AbortOUTOnRequest
);
354 case TMC_MESSAGEID_DEV_DEP_MSG_IN
:
357 MessageHeader
.TransferSize
= 3;
358 WriteTMCHeader(&MessageHeader
);
360 Endpoint_Write_Stream_LE("TMC", 3, StreamCallback_AbortINOnRequest
);
364 Endpoint_StallTransaction();
368 LEDs_SetAllLEDs(LEDMASK_USB_READY
);
371 /* All pending data has been processed - reset the data abort flags */
372 IsTMCBulkINReset
= false;
373 IsTMCBulkOUTReset
= false;
376 /** Attempts to read in the TMC message header from the TMC interface.
378 * \param[out] MessageHeader Pointer to a location where the read header (if any) should be stored
380 * \return Boolean true if a header was read, false otherwise
382 bool ReadTMCHeader(TMC_MessageHeader_t
* const MessageHeader
)
384 /* Select the Data Out endpoint */
385 Endpoint_SelectEndpoint(TMC_OUT_EPNUM
);
387 /* Abort if no command has been sent from the host */
388 if (!(Endpoint_IsOUTReceived()))
391 /* Read in the header of the command from the host */
392 Endpoint_Read_Stream_LE(MessageHeader
, sizeof(TMC_MessageHeader_t
), StreamCallback_AbortOUTOnRequest
);
394 /* Store the new command tag value for later use */
395 CurrentTransferTag
= MessageHeader
->Tag
;
397 /* Indicate if the command has been aborted or not */
398 return !(IsTMCBulkOUTReset
);
401 bool WriteTMCHeader(TMC_MessageHeader_t
* const MessageHeader
)
403 /* Compute the next transfer tag value, must be between 1 and 254 */
404 if (++CurrentTransferTag
== 0xFF)
405 CurrentTransferTag
= 1;
407 /* Set the message tag of the command header */
408 MessageHeader
->Tag
= CurrentTransferTag
;
409 MessageHeader
->InverseTag
= ~CurrentTransferTag
;
411 /* Select the Data In endpoint */
412 Endpoint_SelectEndpoint(TMC_IN_EPNUM
);
414 /* Send the command header to the host */
415 Endpoint_Write_Stream_LE(MessageHeader
, sizeof(TMC_MessageHeader_t
), StreamCallback_AbortINOnRequest
);
417 /* Indicate if the command has been aborted or not */
418 return !(IsTMCBulkINReset
);
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.
424 uint8_t StreamCallback_AbortINOnRequest(void)
426 /* Abort if a TMC Bulk Data IN abort was received */
427 if (IsTMCBulkINReset
)
428 return STREAMCALLBACK_Abort
;
430 /* Continue with the current stream operation */
431 return STREAMCALLBACK_Continue
;
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.
437 uint8_t StreamCallback_AbortOUTOnRequest(void)
439 /* Abort if a TMC Bulk Data IN abort was received */
440 if (IsTMCBulkOUTReset
)
441 return STREAMCALLBACK_Abort
;
443 /* Continue with the current stream operation */
444 return STREAMCALLBACK_Continue
;