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;
65 /** Main program entry point. This routine contains the overall program flow, including initial
66 * setup of all components and the main program loop.
72 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
);
82 /** Configures the board hardware and chip peripherals for the demo's functionality. */
83 void SetupHardware(void)
85 /* Disable watchdog if enabled by bootloader/fuses */
86 MCUSR
&= ~(1 << WDRF
);
89 /* Disable clock division */
90 clock_prescale_set(clock_div_1
);
92 /* Hardware Initialization */
97 void EVENT_USB_Device_Connect(void)
99 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING
);
102 void EVENT_USB_Device_Disconnect(void)
104 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
);
107 void EVENT_USB_Device_ConfigurationChanged(void)
109 LEDs_SetAllLEDs(LEDMASK_USB_READY
);
111 /* Setup TMC In and Out Endpoints */
112 if (!(Endpoint_ConfigureEndpoint(TMC_IN_EPNUM
, EP_TYPE_BULK
,
113 ENDPOINT_DIR_IN
, TMC_IO_EPSIZE
,
114 ENDPOINT_BANK_SINGLE
)))
116 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
119 if (!(Endpoint_ConfigureEndpoint(TMC_OUT_EPNUM
, EP_TYPE_BULK
,
120 ENDPOINT_DIR_OUT
, TMC_IO_EPSIZE
,
121 ENDPOINT_BANK_SINGLE
)))
123 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
127 void EVENT_USB_Device_UnhandledControlRequest(void)
129 switch (USB_ControlRequest
.bRequest
)
131 case Req_InitiateAbortBulkOut
:
132 if (USB_ControlRequest
.bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_ENDPOINT
))
134 Endpoint_ClearSETUP();
136 /* Check to see if a split request is already being processed before starting a new one */
137 if (RequestInProgess
!= 0)
139 Endpoint_Write_Byte(TMC_REQUEST_STATUS_SPLIT_IN_PROGRESS
);
143 /* Indicate that all in-progress/pending data OUT requests should be aborted */
144 IsTMCBulkOUTReset
= true;
146 /* Save the split request for later checking when a new request is received */
147 RequestInProgess
= Req_InitiateAbortBulkOut
;
149 Endpoint_Write_Byte(TMC_REQUEST_STATUS_SUCCESS
);
153 Endpoint_ClearStatusStage();
157 case Req_CheckAbortBulkOutStatus
:
158 if (USB_ControlRequest
.bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_ENDPOINT
))
160 Endpoint_ClearSETUP();
162 /* Check to see the correct split request is in progress before the status can be retrieved */
163 if (RequestInProgess
!= Req_InitiateAbortBulkOut
)
165 Endpoint_Write_Byte(TMC_REQUEST_STATUS_SPLIT_NOT_IN_PROGRESS
);
169 // TODO: CLEAR BULK OUT
171 /* Clear the pending split request value so that a new request can be made */
172 RequestInProgess
= 0;
174 Endpoint_Write_Byte(TMC_REQUEST_STATUS_SUCCESS
);
178 Endpoint_ClearStatusStage();
182 case Req_InitiateAbortBulkIn
:
183 if (USB_ControlRequest
.bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_ENDPOINT
))
185 Endpoint_ClearSETUP();
187 /* Check to see if a split request is already being processed before starting a new one */
188 if (RequestInProgess
!= 0)
190 Endpoint_Write_Byte(TMC_REQUEST_STATUS_SPLIT_IN_PROGRESS
);
194 /* Indicate that all in-progress/pending data IN requests should be aborted */
195 IsTMCBulkINReset
= true;
197 /* Save the split request for later checking when a new request is received */
198 RequestInProgess
= Req_InitiateAbortBulkIn
;
200 Endpoint_Write_Byte(TMC_REQUEST_STATUS_SUCCESS
);
204 Endpoint_ClearStatusStage();
208 case Req_CheckAbortBulkInStatus
:
209 if (USB_ControlRequest
.bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_ENDPOINT
))
211 Endpoint_ClearSETUP();
213 /* Check to see the correct split request is in progress before the status can be retrieved */
214 if (RequestInProgess
!= Req_InitiateAbortBulkIn
)
216 Endpoint_Write_Byte(TMC_REQUEST_STATUS_SPLIT_NOT_IN_PROGRESS
);
220 // TODO: CLEAR BULK IN
222 /* Clear the pending split request value so that a new request can be made */
223 RequestInProgess
= 0;
225 Endpoint_Write_Byte(TMC_REQUEST_STATUS_SUCCESS
);
229 Endpoint_ClearStatusStage();
233 case Req_InitiateClear
:
234 if (USB_ControlRequest
.bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_INTERFACE
))
236 Endpoint_ClearSETUP();
238 /* Check to see if a split request is already being processed before starting a new one */
239 if (RequestInProgess
!= 0)
241 Endpoint_Write_Byte(TMC_REQUEST_STATUS_SPLIT_IN_PROGRESS
);
245 /* Indicate that all in-progress/pending data IN and OUT requests should be aborted */
246 IsTMCBulkINReset
= true;
247 IsTMCBulkOUTReset
= true;
249 /* Save the split request for later checking when a new request is received */
250 RequestInProgess
= Req_InitiateClear
;
252 Endpoint_Write_Byte(TMC_REQUEST_STATUS_SUCCESS
);
256 Endpoint_ClearStatusStage();
260 case Req_CheckClearStatus
:
261 if (USB_ControlRequest
.bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_INTERFACE
))
263 Endpoint_ClearSETUP();
265 /* Check to see the correct split request is in progress before the status can be retrieved */
266 if (RequestInProgess
!= Req_InitiateClear
)
268 Endpoint_Write_Byte(TMC_REQUEST_STATUS_SPLIT_NOT_IN_PROGRESS
);
272 // TODO: CLEAR STATUS
274 /* Clear the pending split request value so that a new request can be made */
275 RequestInProgess
= 0;
277 Endpoint_Write_Byte(TMC_REQUEST_STATUS_SUCCESS
);
281 Endpoint_ClearStatusStage();
285 case Req_GetCapabilities
:
286 if (USB_ControlRequest
.bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_INTERFACE
))
288 /* Acknowledge the SETUP packet, ready for data transfer */
289 Endpoint_ClearSETUP();
291 /* Write the device capabilities to the control endpoint */
292 Endpoint_Write_Control_Stream_LE(&Capabilities
, sizeof(TMC_Capabilities_t
));
294 /* Finalize the stream transfer to send the last packet or clear the host abort */
304 /* Device must be connected and configured for the task to run */
305 if (USB_DeviceState
!= DEVICE_STATE_Configured
)
308 Endpoint_SelectEndpoint(TMC_OUT_EPNUM
);
310 if (Endpoint_IsOUTReceived())
312 // TEMP - Indicate data received
313 LEDs_SetAllLEDs(LEDS_ALL_LEDS
);
318 /** Stream callback function for the Endpoint stream write functions. This callback will abort the current stream transfer
319 * if a TMC Abort Bulk IN request has been issued to the control endpoint.
321 uint8_t StreamCallback_AbortINOnRequest(void)
323 /* Abort if a TMC Bulk Data IN abort was received */
324 if (IsTMCBulkINReset
)
325 return STREAMCALLBACK_Abort
;
327 /* Continue with the current stream operation */
328 return STREAMCALLBACK_Continue
;
331 /** Stream callback function for the Endpoint stream read functions. This callback will abort the current stream transfer
332 * if a TMC Abort Bulk OUT request has been issued to the control endpoint.
334 uint8_t StreamCallback_AbortOUTOnRequest(void)
336 /* Abort if a TMC Bulk Data IN abort was received */
337 if (IsTMCBulkOUTReset
)
338 return STREAMCALLBACK_Abort
;
340 /* Continue with the current stream operation */
341 return STREAMCALLBACK_Continue
;