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 RequestInProgess
= 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 (RequestInProgess
!= 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 RequestInProgess
= 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 (RequestInProgess
!= Req_InitiateAbortBulkOut
)
188 TMCRequestStatus
= TMC_STATUS_SPLIT_NOT_IN_PROGRESS
;
189 else if (IsTMCBulkOUTReset
)
190 TMCRequestStatus
= TMC_STATUS_PENDING
;
192 RequestInProgess
= 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 (RequestInProgess
!= 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 RequestInProgess
= 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 (RequestInProgess
!= Req_InitiateAbortBulkIn
)
243 TMCRequestStatus
= TMC_STATUS_SPLIT_NOT_IN_PROGRESS
;
244 else if (IsTMCBulkINReset
)
245 TMCRequestStatus
= TMC_STATUS_PENDING
;
247 RequestInProgess
= 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 (RequestInProgess
!= 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 RequestInProgess
= 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 (RequestInProgess
!= Req_InitiateClear
)
294 TMCRequestStatus
= TMC_STATUS_SPLIT_NOT_IN_PROGRESS
;
295 else if (IsTMCBulkINReset
|| IsTMCBulkOUTReset
)
296 TMCRequestStatus
= TMC_STATUS_PENDING
;
298 RequestInProgess
= 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 Endpoint_SelectEndpoint(TMC_OUT_EPNUM
);
335 if (Endpoint_IsOUTReceived())
337 TMC_MessageHeader_t MessageHeader
;
339 Endpoint_Read_Stream_LE(&MessageHeader
, sizeof(MessageHeader
), StreamCallback_AbortOUTOnRequest
);
340 CurrentTransferTag
= MessageHeader
.Tag
;
342 switch (MessageHeader
.MessageID
)
344 case TMC_MESSAGEID_DEV_DEP_MSG_OUT
:
347 case TMC_MESSAGEID_DEV_DEP_MSG_IN
:
350 case TMC_MESSAGEID_DEV_VENDOR_OUT
:
353 case TMC_MESSAGEID_DEV_VENDOR_IN
:
360 /* All pending data has been processed - reset the data abort flags */
361 IsTMCBulkINReset
= false;
362 IsTMCBulkOUTReset
= false;
365 /** Stream callback function for the Endpoint stream write functions. This callback will abort the current stream transfer
366 * if a TMC Abort Bulk IN request has been issued to the control endpoint.
368 uint8_t StreamCallback_AbortINOnRequest(void)
370 /* Abort if a TMC Bulk Data IN abort was received */
371 if (IsTMCBulkINReset
)
372 return STREAMCALLBACK_Abort
;
374 /* Continue with the current stream operation */
375 return STREAMCALLBACK_Continue
;
378 /** Stream callback function for the Endpoint stream read functions. This callback will abort the current stream transfer
379 * if a TMC Abort Bulk OUT request has been issued to the control endpoint.
381 uint8_t StreamCallback_AbortOUTOnRequest(void)
383 /* Abort if a TMC Bulk Data IN abort was received */
384 if (IsTMCBulkOUTReset
)
385 return STREAMCALLBACK_Abort
;
387 /* Continue with the current stream operation */
388 return STREAMCALLBACK_Continue
;