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 void EVENT_USB_Device_Connect(void)
105 LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING
);
108 void EVENT_USB_Device_Disconnect(void)
110 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
);
113 void EVENT_USB_Device_ConfigurationChanged(void)
115 LEDs_SetAllLEDs(LEDMASK_USB_READY
);
117 /* Setup TMC In and Out Endpoints */
118 if (!(Endpoint_ConfigureEndpoint(TMC_IN_EPNUM
, EP_TYPE_BULK
,
119 ENDPOINT_DIR_IN
, TMC_IO_EPSIZE
,
120 ENDPOINT_BANK_SINGLE
)))
122 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
125 if (!(Endpoint_ConfigureEndpoint(TMC_OUT_EPNUM
, EP_TYPE_BULK
,
126 ENDPOINT_DIR_OUT
, TMC_IO_EPSIZE
,
127 ENDPOINT_BANK_SINGLE
)))
129 LEDs_SetAllLEDs(LEDMASK_USB_ERROR
);
133 void EVENT_USB_Device_UnhandledControlRequest(void)
135 uint8_t TMCRequestStatus
= TMC_REQUEST_STATUS_SUCCESS
;
137 switch (USB_ControlRequest
.bRequest
)
139 case Req_InitiateAbortBulkOut
:
140 if (USB_ControlRequest
.bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_ENDPOINT
))
142 Endpoint_ClearSETUP();
144 if (RequestInProgess
!= 0)
146 TMCRequestStatus
= TMC_REQUEST_STATUS_SPLIT_IN_PROGRESS
;
148 else if (USB_ControlRequest
.wValue
!= NextTransferOUTTag
)
150 TMCRequestStatus
= TMC_REQUEST_STATUS_TRANSFER_NOT_IN_PROGRESS
;
154 /* Indicate that all in-progress/pending data OUT requests should be aborted */
155 IsTMCBulkOUTReset
= true;
157 /* Save the split request for later checking when a new request is received */
158 RequestInProgess
= Req_InitiateAbortBulkOut
;
161 /* Write the request response byte */
162 Endpoint_Write_Byte(TMCRequestStatus
);
165 Endpoint_ClearStatusStage();
169 case Req_CheckAbortBulkOutStatus
:
170 if (USB_ControlRequest
.bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_ENDPOINT
))
172 Endpoint_ClearSETUP();
174 if (RequestInProgess
!= Req_InitiateAbortBulkOut
)
175 TMCRequestStatus
= TMC_REQUEST_STATUS_SPLIT_NOT_IN_PROGRESS
;
176 else if (IsTMCBulkOUTReset
)
177 TMCRequestStatus
= TMC_REQUEST_STATUS_PENDING
;
179 RequestInProgess
= 0;
181 /* Write the request response bytes */
182 Endpoint_Write_Byte(TMCRequestStatus
);
183 Endpoint_Write_Word_LE(0);
184 Endpoint_Write_DWord_LE(0); // TODO - Last transfer length
187 Endpoint_ClearStatusStage();
191 case Req_InitiateAbortBulkIn
:
192 if (USB_ControlRequest
.bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_ENDPOINT
))
194 Endpoint_ClearSETUP();
196 if (RequestInProgess
!= 0)
198 TMCRequestStatus
= TMC_REQUEST_STATUS_SPLIT_IN_PROGRESS
;
200 else if (USB_ControlRequest
.wValue
!= NextTransferINTag
)
202 TMCRequestStatus
= TMC_REQUEST_STATUS_TRANSFER_NOT_IN_PROGRESS
;
206 /* Indicate that all in-progress/pending data IN requests should be aborted */
207 IsTMCBulkINReset
= true;
209 /* Save the split request for later checking when a new request is received */
210 RequestInProgess
= Req_InitiateAbortBulkIn
;
213 /* Write the request response bytes */
214 Endpoint_Write_Byte(TMCRequestStatus
);
215 Endpoint_Write_Byte(NextTransferINTag
);
218 Endpoint_ClearStatusStage();
222 case Req_CheckAbortBulkInStatus
:
223 if (USB_ControlRequest
.bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_ENDPOINT
))
225 Endpoint_ClearSETUP();
227 if (RequestInProgess
!= Req_InitiateAbortBulkIn
)
228 TMCRequestStatus
= TMC_REQUEST_STATUS_SPLIT_NOT_IN_PROGRESS
;
229 else if (IsTMCBulkINReset
)
230 TMCRequestStatus
= TMC_REQUEST_STATUS_PENDING
;
232 RequestInProgess
= 0;
234 /* Write the request response bytes */
235 Endpoint_Write_Byte(TMCRequestStatus
);
236 Endpoint_Write_Word_LE(0);
237 Endpoint_Write_DWord_LE(0); // TODO - Last transfer length
240 Endpoint_ClearStatusStage();
244 case Req_InitiateClear
:
245 if (USB_ControlRequest
.bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_INTERFACE
))
247 Endpoint_ClearSETUP();
249 if (RequestInProgess
!= 0)
251 Endpoint_Write_Byte(TMC_REQUEST_STATUS_SPLIT_IN_PROGRESS
);
255 /* Indicate that all in-progress/pending data IN and OUT requests should be aborted */
256 IsTMCBulkINReset
= true;
257 IsTMCBulkOUTReset
= true;
259 /* Save the split request for later checking when a new request is received */
260 RequestInProgess
= Req_InitiateClear
;
263 /* Write the request response byte */
264 Endpoint_Write_Byte(TMCRequestStatus
);
267 Endpoint_ClearStatusStage();
271 case Req_CheckClearStatus
:
272 if (USB_ControlRequest
.bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_INTERFACE
))
274 Endpoint_ClearSETUP();
276 if (RequestInProgess
!= Req_InitiateClear
)
277 TMCRequestStatus
= TMC_REQUEST_STATUS_SPLIT_NOT_IN_PROGRESS
;
278 else if (IsTMCBulkINReset
|| IsTMCBulkOUTReset
)
279 TMCRequestStatus
= TMC_REQUEST_STATUS_PENDING
;
281 RequestInProgess
= 0;
283 /* Write the request response bytes */
284 Endpoint_Write_Byte(TMCRequestStatus
);
285 Endpoint_Write_Byte(0);
288 Endpoint_ClearStatusStage();
292 case Req_GetCapabilities
:
293 if (USB_ControlRequest
.bmRequestType
== (REQDIR_DEVICETOHOST
| REQTYPE_CLASS
| REQREC_INTERFACE
))
295 /* Acknowledge the SETUP packet, ready for data transfer */
296 Endpoint_ClearSETUP();
298 /* Write the device capabilities to the control endpoint */
299 Endpoint_Write_Control_Stream_LE(&Capabilities
, sizeof(TMC_Capabilities_t
));
301 /* Finalize the stream transfer to send the last packet or clear the host abort */
311 /* Device must be connected and configured for the task to run */
312 if (USB_DeviceState
!= DEVICE_STATE_Configured
)
315 Endpoint_SelectEndpoint(TMC_OUT_EPNUM
);
317 if (Endpoint_IsOUTReceived())
319 // TEMP - Indicate data received
320 LEDs_SetAllLEDs(LEDS_ALL_LEDS
);
324 /* All pending data has been processed - reset the data abort flags */
325 IsTMCBulkINReset
= false;
326 IsTMCBulkOUTReset
= false;
329 /** Stream callback function for the Endpoint stream write functions. This callback will abort the current stream transfer
330 * if a TMC Abort Bulk IN request has been issued to the control endpoint.
332 uint8_t StreamCallback_AbortINOnRequest(void)
334 /* Abort if a TMC Bulk Data IN abort was received */
335 if (IsTMCBulkINReset
)
336 return STREAMCALLBACK_Abort
;
338 /* Continue with the current stream operation */
339 return STREAMCALLBACK_Continue
;
342 /** Stream callback function for the Endpoint stream read functions. This callback will abort the current stream transfer
343 * if a TMC Abort Bulk OUT request has been issued to the control endpoint.
345 uint8_t StreamCallback_AbortOUTOnRequest(void)
347 /* Abort if a TMC Bulk Data IN abort was received */
348 if (IsTMCBulkOUTReset
)
349 return STREAMCALLBACK_Abort
;
351 /* Continue with the current stream operation */
352 return STREAMCALLBACK_Continue
;