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_REQUEST_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 bulk IN transfers */
65 uint8_t NextTransferINTag
= 0;
67 /** Last used tag value for bulk IN transfers */
68 uint8_t NextTransferOUTTag
= 0;
71 /** Main program entry point. This routine contains the overall program flow, including initial
72 * setup of all components and the main program loop.
78 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
);
88 /** Configures the board hardware and chip peripherals for the demo's functionality. */
89 void SetupHardware(void)
91 /* Disable watchdog if enabled by bootloader/fuses */
92 MCUSR
&= ~(1 << WDRF
);
95 /* Disable clock division */
96 clock_prescale_set(clock_div_1
);
98 /* Hardware Initialization */
103 /** Event handler for the USB_Connect event. This indicates that the device is enumerating via the status LEDs and
104 * starts the library USB task to begin the enumeration and USB management process.
106 void EVENT_USB_Device_Connect(void)
108 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING
);
111 /** Event handler for the USB_Disconnect event. This indicates that the device is no longer connected to a host via
112 * the status LEDs and stops the USB management and CDC management tasks.
114 void EVENT_USB_Device_Disconnect(void)
116 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
);
119 /** Event handler for the USB_ConfigurationChanged event. This is fired when the host set the current configuration
120 * of the USB device after enumeration - the device endpoints are configured and the CDC management task started.
122 void EVENT_USB_Device_ConfigurationChanged(void)
124 LEDs_SetAllLEDs(LEDMASK_USB_READY
);
126 /* Setup TMC In and Out Endpoints */
127 if (!(Endpoint_ConfigureEndpoint(TMC_IN_EPNUM
, EP_TYPE_BULK
,
128 ENDPOINT_DIR_IN
, TMC_IO_EPSIZE
,
129 ENDPOINT_BANK_SINGLE
)))
131 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
134 if (!(Endpoint_ConfigureEndpoint(TMC_OUT_EPNUM
, EP_TYPE_BULK
,
135 ENDPOINT_DIR_OUT
, TMC_IO_EPSIZE
,
136 ENDPOINT_BANK_SINGLE
)))
138 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
142 /** Event handler for the USB_UnhandledControlRequest event. This is used to catch standard and class specific
143 * control requests that are not handled internally by the USB library (including the CDC control commands,
144 * which are all issued via the control endpoint), so that they can be handled appropriately for the application.
146 void EVENT_USB_Device_UnhandledControlRequest(void)
148 uint8_t TMCRequestStatus
= TMC_REQUEST_STATUS_SUCCESS
;
150 /* Process TMC specific control requests */
151 switch (USB_ControlRequest
.bRequest
)
153 case Req_InitiateAbortBulkOut
:
154 if (USB_ControlRequest
.bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_ENDPOINT
))
156 Endpoint_ClearSETUP();
158 /* Check that no split transaction is already in progress and the data OUT transfer tag is valid */
159 if (RequestInProgess
!= 0)
161 TMCRequestStatus
= TMC_REQUEST_STATUS_SPLIT_IN_PROGRESS
;
163 else if (USB_ControlRequest
.wValue
!= NextTransferOUTTag
)
165 TMCRequestStatus
= TMC_REQUEST_STATUS_TRANSFER_NOT_IN_PROGRESS
;
169 /* Indicate that all in-progress/pending data OUT requests should be aborted */
170 IsTMCBulkOUTReset
= true;
172 /* Save the split request for later checking when a new request is received */
173 RequestInProgess
= Req_InitiateAbortBulkOut
;
176 /* Write the request response byte */
177 Endpoint_Write_Byte(TMCRequestStatus
);
180 Endpoint_ClearStatusStage();
184 case Req_CheckAbortBulkOutStatus
:
185 if (USB_ControlRequest
.bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_ENDPOINT
))
187 Endpoint_ClearSETUP();
189 /* Check that an ABORT BULK OUT transaction has been requested and that the request has completed */
190 if (RequestInProgess
!= Req_InitiateAbortBulkOut
)
191 TMCRequestStatus
= TMC_REQUEST_STATUS_SPLIT_NOT_IN_PROGRESS
;
192 else if (IsTMCBulkOUTReset
)
193 TMCRequestStatus
= TMC_REQUEST_STATUS_PENDING
;
195 RequestInProgess
= 0;
197 /* Write the request response bytes */
198 Endpoint_Write_Byte(TMCRequestStatus
);
199 Endpoint_Write_Word_LE(0);
200 Endpoint_Write_DWord_LE(0); // TODO - Last transfer length
203 Endpoint_ClearStatusStage();
207 case Req_InitiateAbortBulkIn
:
208 if (USB_ControlRequest
.bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_ENDPOINT
))
210 Endpoint_ClearSETUP();
212 /* Check that no split transaction is already in progress and the data IN transfer tag is valid */
213 if (RequestInProgess
!= 0)
215 TMCRequestStatus
= TMC_REQUEST_STATUS_SPLIT_IN_PROGRESS
;
217 else if (USB_ControlRequest
.wValue
!= NextTransferINTag
)
219 TMCRequestStatus
= TMC_REQUEST_STATUS_TRANSFER_NOT_IN_PROGRESS
;
223 /* Indicate that all in-progress/pending data IN requests should be aborted */
224 IsTMCBulkINReset
= true;
226 /* Save the split request for later checking when a new request is received */
227 RequestInProgess
= Req_InitiateAbortBulkIn
;
230 /* Write the request response bytes */
231 Endpoint_Write_Byte(TMCRequestStatus
);
232 Endpoint_Write_Byte(NextTransferINTag
);
235 Endpoint_ClearStatusStage();
239 case Req_CheckAbortBulkInStatus
:
240 if (USB_ControlRequest
.bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_ENDPOINT
))
242 Endpoint_ClearSETUP();
244 /* Check that an ABORT BULK IN transaction has been requested and that the request has completed */
245 if (RequestInProgess
!= Req_InitiateAbortBulkIn
)
246 TMCRequestStatus
= TMC_REQUEST_STATUS_SPLIT_NOT_IN_PROGRESS
;
247 else if (IsTMCBulkINReset
)
248 TMCRequestStatus
= TMC_REQUEST_STATUS_PENDING
;
250 RequestInProgess
= 0;
252 /* Write the request response bytes */
253 Endpoint_Write_Byte(TMCRequestStatus
);
254 Endpoint_Write_Word_LE(0);
255 Endpoint_Write_DWord_LE(0); // TODO - Last transfer length
258 Endpoint_ClearStatusStage();
262 case Req_InitiateClear
:
263 if (USB_ControlRequest
.bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_INTERFACE
))
265 Endpoint_ClearSETUP();
267 /* Check that no split transaction is already in progress */
268 if (RequestInProgess
!= 0)
270 Endpoint_Write_Byte(TMC_REQUEST_STATUS_SPLIT_IN_PROGRESS
);
274 /* Indicate that all in-progress/pending data IN and OUT requests should be aborted */
275 IsTMCBulkINReset
= true;
276 IsTMCBulkOUTReset
= true;
278 /* Save the split request for later checking when a new request is received */
279 RequestInProgess
= Req_InitiateClear
;
282 /* Write the request response byte */
283 Endpoint_Write_Byte(TMCRequestStatus
);
286 Endpoint_ClearStatusStage();
290 case Req_CheckClearStatus
:
291 if (USB_ControlRequest
.bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_INTERFACE
))
293 Endpoint_ClearSETUP();
295 /* Check that a CLEAR transaction has been requested and that the request has completed */
296 if (RequestInProgess
!= Req_InitiateClear
)
297 TMCRequestStatus
= TMC_REQUEST_STATUS_SPLIT_NOT_IN_PROGRESS
;
298 else if (IsTMCBulkINReset
|| IsTMCBulkOUTReset
)
299 TMCRequestStatus
= TMC_REQUEST_STATUS_PENDING
;
301 RequestInProgess
= 0;
303 /* Write the request response bytes */
304 Endpoint_Write_Byte(TMCRequestStatus
);
305 Endpoint_Write_Byte(0);
308 Endpoint_ClearStatusStage();
312 case Req_GetCapabilities
:
313 if (USB_ControlRequest
.bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_INTERFACE
))
315 /* Acknowledge the SETUP packet, ready for data transfer */
316 Endpoint_ClearSETUP();
318 /* Write the device capabilities to the control endpoint */
319 Endpoint_Write_Control_Stream_LE(&Capabilities
, sizeof(TMC_Capabilities_t
));
321 /* Finalize the stream transfer to send the last packet or clear the host abort */
329 /** Function to manage TMC data transmission and reception to and from the host. */
332 /* Device must be connected and configured for the task to run */
333 if (USB_DeviceState
!= DEVICE_STATE_Configured
)
336 Endpoint_SelectEndpoint(TMC_OUT_EPNUM
);
338 if (Endpoint_IsOUTReceived())
340 // TEMP - Indicate data received
341 LEDs_SetAllLEDs(LEDS_ALL_LEDS
);
345 /* All pending data has been processed - reset the data abort flags */
346 IsTMCBulkINReset
= false;
347 IsTMCBulkOUTReset
= false;
350 /** Stream callback function for the Endpoint stream write functions. This callback will abort the current stream transfer
351 * if a TMC Abort Bulk IN request has been issued to the control endpoint.
353 uint8_t StreamCallback_AbortINOnRequest(void)
355 /* Abort if a TMC Bulk Data IN abort was received */
356 if (IsTMCBulkINReset
)
357 return STREAMCALLBACK_Abort
;
359 /* Continue with the current stream operation */
360 return STREAMCALLBACK_Continue
;
363 /** Stream callback function for the Endpoint stream read functions. This callback will abort the current stream transfer
364 * if a TMC Abort Bulk OUT request has been issued to the control endpoint.
366 uint8_t StreamCallback_AbortOUTOnRequest(void)
368 /* Abort if a TMC Bulk Data IN abort was received */
369 if (IsTMCBulkOUTReset
)
370 return STREAMCALLBACK_Abort
;
372 /* Continue with the current stream operation */
373 return STREAMCALLBACK_Continue
;