3      Copyright (C) Dean Camera, 2011. 
   5   dean [at] fourwalledcubicle [dot] com 
  10   Copyright 2011  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 static uint8_t RequestInProgress 
= 0; 
  58 /** Stream callback abort flag for bulk IN data */ 
  59 static bool IsTMCBulkINReset 
= false; 
  61 /** Stream callback abort flag for bulk OUT data */ 
  62 static bool IsTMCBulkOUTReset 
= false; 
  64 /** Last used tag value for data transfers */ 
  65 static uint8_t CurrentTransferTag 
= 0; 
  67 /** Length of last data transfer, for reporting to the host in case an in-progress transfer is aborted */ 
  68 static uint32_t LastTransferLength 
= 0; 
  70 /** Main program entry point. This routine contains the overall program flow, including initial 
  71  *  setup of all components and the main program loop. 
  77         LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
); 
  87 /** Configures the board hardware and chip peripherals for the demo's functionality. */ 
  88 void SetupHardware(void) 
  90         /* Disable watchdog if enabled by bootloader/fuses */ 
  91         MCUSR 
&= ~(1 << WDRF
); 
  94         /* Disable clock division */ 
  95         clock_prescale_set(clock_div_1
); 
  97         /* Hardware Initialization */ 
 102 /** Event handler for the USB_Connect event. This indicates that the device is enumerating via the status LEDs and 
 103  *  starts the library USB task to begin the enumeration and USB management process. 
 105 void EVENT_USB_Device_Connect(void) 
 107         LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING
); 
 110 /** Event handler for the USB_Disconnect event. This indicates that the device is no longer connected to a host via 
 111  *  the status LEDs and stops the USB management and CDC management tasks. 
 113 void EVENT_USB_Device_Disconnect(void) 
 115         LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY
); 
 118 /** Event handler for the USB_ConfigurationChanged event. This is fired when the host set the current configuration 
 119  *  of the USB device after enumeration - the device endpoints are configured and the CDC management task started. 
 121 void EVENT_USB_Device_ConfigurationChanged(void) 
 123         bool ConfigSuccess 
= true; 
 125         /* Setup TMC In, Out and Notification Endpoints */ 
 126         ConfigSuccess 
&= Endpoint_ConfigureEndpoint(TMC_NOTIFICATION_EPNUM
, EP_TYPE_INTERRUPT
, ENDPOINT_DIR_IN
, 
 127                                                     TMC_IO_EPSIZE
, ENDPOINT_BANK_SINGLE
); 
 128         ConfigSuccess 
&= Endpoint_ConfigureEndpoint(TMC_IN_EPNUM
,  EP_TYPE_BULK
, ENDPOINT_DIR_IN
, 
 129                                                     TMC_IO_EPSIZE
, ENDPOINT_BANK_SINGLE
); 
 130         ConfigSuccess 
&= Endpoint_ConfigureEndpoint(TMC_OUT_EPNUM
, EP_TYPE_BULK
, ENDPOINT_DIR_OUT
, 
 131                                                     TMC_IO_EPSIZE
, ENDPOINT_BANK_SINGLE
); 
 133         /* Indicate endpoint configuration success or failure */ 
 134         LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY 
: LEDMASK_USB_ERROR
); 
 137 /** Event handler for the USB_ControlRequest event. This is used to catch and process control requests sent to 
 138  *  the device from the USB host before passing along unhandled control requests to the library for processing 
 141 void EVENT_USB_Device_ControlRequest(void) 
 143         uint8_t TMCRequestStatus 
= TMC_STATUS_SUCCESS
; 
 145         /* Process TMC specific control requests */ 
 146         switch (USB_ControlRequest
.bRequest
) 
 148                 case Req_InitiateAbortBulkOut
: 
 149                         if (USB_ControlRequest
.bmRequestType 
== (REQDIR_DEVICETOHOST 
| REQTYPE_CLASS 
| REQREC_ENDPOINT
)) 
 151                                 /* Check that no split transaction is already in progress and the data transfer tag is valid */ 
 152                                 if (RequestInProgress 
!= 0) 
 154                                         TMCRequestStatus 
= TMC_STATUS_SPLIT_IN_PROGRESS
; 
 156                                 else if (USB_ControlRequest
.wValue 
!= CurrentTransferTag
) 
 158                                         TMCRequestStatus 
= TMC_STATUS_TRANSFER_NOT_IN_PROGRESS
; 
 162                                         /* Indicate that all in-progress/pending data OUT requests should be aborted */ 
 163                                         IsTMCBulkOUTReset 
= true; 
 165                                         /* Save the split request for later checking when a new request is received */ 
 166                                         RequestInProgress 
= Req_InitiateAbortBulkOut
; 
 169                                 Endpoint_ClearSETUP(); 
 171                                 /* Write the request response byte */ 
 172                                 Endpoint_Write_8(TMCRequestStatus
); 
 175                                 Endpoint_ClearStatusStage(); 
 179                 case Req_CheckAbortBulkOutStatus
: 
 180                         if (USB_ControlRequest
.bmRequestType 
== (REQDIR_DEVICETOHOST 
| REQTYPE_CLASS 
| REQREC_ENDPOINT
)) 
 182                                 /* Check that an ABORT BULK OUT transaction has been requested and that the request has completed */ 
 183                                 if (RequestInProgress 
!= Req_InitiateAbortBulkOut
) 
 184                                   TMCRequestStatus 
= TMC_STATUS_SPLIT_NOT_IN_PROGRESS
;                           
 185                                 else if (IsTMCBulkOUTReset
) 
 186                                   TMCRequestStatus 
= TMC_STATUS_PENDING
; 
 188                                   RequestInProgress 
= 0;         
 190                                 Endpoint_ClearSETUP(); 
 192                                 /* Write the request response bytes */ 
 193                                 Endpoint_Write_8(TMCRequestStatus
); 
 194                                 Endpoint_Write_16_LE(0); 
 195                                 Endpoint_Write_32_LE(LastTransferLength
); 
 198                                 Endpoint_ClearStatusStage();                             
 202                 case Req_InitiateAbortBulkIn
: 
 203                         if (USB_ControlRequest
.bmRequestType 
== (REQDIR_DEVICETOHOST 
| REQTYPE_CLASS 
| REQREC_ENDPOINT
)) 
 205                                 /* Check that no split transaction is already in progress and the data transfer tag is valid */ 
 206                                 if (RequestInProgress 
!= 0) 
 208                                         TMCRequestStatus 
= TMC_STATUS_SPLIT_IN_PROGRESS
;                                 
 210                                 else if (USB_ControlRequest
.wValue 
!= CurrentTransferTag
) 
 212                                         TMCRequestStatus 
= TMC_STATUS_TRANSFER_NOT_IN_PROGRESS
; 
 216                                         /* Indicate that all in-progress/pending data IN requests should be aborted */ 
 217                                         IsTMCBulkINReset 
= true; 
 219                                         /* Save the split request for later checking when a new request is received */ 
 220                                         RequestInProgress 
= Req_InitiateAbortBulkIn
; 
 223                                 Endpoint_ClearSETUP(); 
 225                                 /* Write the request response bytes */ 
 226                                 Endpoint_Write_8(TMCRequestStatus
); 
 227                                 Endpoint_Write_8(CurrentTransferTag
); 
 230                                 Endpoint_ClearStatusStage(); 
 234                 case Req_CheckAbortBulkInStatus
: 
 235                         if (USB_ControlRequest
.bmRequestType 
== (REQDIR_DEVICETOHOST 
| REQTYPE_CLASS 
| REQREC_ENDPOINT
)) 
 237                                 /* Check that an ABORT BULK IN transaction has been requested and that the request has completed */ 
 238                                 if (RequestInProgress 
!= Req_InitiateAbortBulkIn
) 
 239                                   TMCRequestStatus 
= TMC_STATUS_SPLIT_NOT_IN_PROGRESS
; 
 240                                 else if (IsTMCBulkINReset
) 
 241                                   TMCRequestStatus 
= TMC_STATUS_PENDING
; 
 243                                   RequestInProgress 
= 0; 
 245                                 Endpoint_ClearSETUP(); 
 247                                 /* Write the request response bytes */ 
 248                                 Endpoint_Write_8(TMCRequestStatus
); 
 249                                 Endpoint_Write_16_LE(0); 
 250                                 Endpoint_Write_32_LE(LastTransferLength
); 
 253                                 Endpoint_ClearStatusStage(); 
 257                 case Req_InitiateClear
: 
 258                         if (USB_ControlRequest
.bmRequestType 
== (REQDIR_DEVICETOHOST 
| REQTYPE_CLASS 
| REQREC_INTERFACE
)) 
 260                                 /* Check that no split transaction is already in progress */ 
 261                                 if (RequestInProgress 
!= 0) 
 263                                         Endpoint_Write_8(TMC_STATUS_SPLIT_IN_PROGRESS
);                          
 267                                         /* Indicate that all in-progress/pending data IN and OUT requests should be aborted */ 
 268                                         IsTMCBulkINReset  
= true; 
 269                                         IsTMCBulkOUTReset 
= true; 
 271                                         /* Save the split request for later checking when a new request is received */ 
 272                                         RequestInProgress 
= Req_InitiateClear
; 
 275                                 Endpoint_ClearSETUP(); 
 277                                 /* Write the request response byte */ 
 278                                 Endpoint_Write_8(TMCRequestStatus
); 
 281                                 Endpoint_ClearStatusStage(); 
 285                 case Req_CheckClearStatus
: 
 286                         if (USB_ControlRequest
.bmRequestType 
== (REQDIR_DEVICETOHOST 
| REQTYPE_CLASS 
| REQREC_INTERFACE
)) 
 288                                 /* Check that a CLEAR transaction has been requested and that the request has completed */ 
 289                                 if (RequestInProgress 
!= Req_InitiateClear
) 
 290                                   TMCRequestStatus 
= TMC_STATUS_SPLIT_NOT_IN_PROGRESS
;                           
 291                                 else if (IsTMCBulkINReset 
|| IsTMCBulkOUTReset
) 
 292                                   TMCRequestStatus 
= TMC_STATUS_PENDING
; 
 294                                   RequestInProgress 
= 0; 
 296                                 Endpoint_ClearSETUP(); 
 298                                 /* Write the request response bytes */ 
 299                                 Endpoint_Write_8(TMCRequestStatus
); 
 303                                 Endpoint_ClearStatusStage();                             
 307                 case Req_GetCapabilities
: 
 308                         if (USB_ControlRequest
.bmRequestType 
== (REQDIR_DEVICETOHOST 
| REQTYPE_CLASS 
| REQREC_INTERFACE
)) 
 310                                 Endpoint_ClearSETUP(); 
 312                                 /* Write the device capabilities to the control endpoint */ 
 313                                 Endpoint_Write_Control_Stream_LE(&Capabilities
, sizeof(TMC_Capabilities_t
));                             
 321 /** Function to manage TMC data transmission and reception to and from the host. */ 
 324         /* Device must be connected and configured for the task to run */ 
 325         if (USB_DeviceState 
!= DEVICE_STATE_Configured
) 
 328         TMC_MessageHeader_t MessageHeader
; 
 329         uint16_t            BytesTransferred
; 
 331         /* Try to read in a TMC message from the interface, process if one is available */ 
 332         if (ReadTMCHeader(&MessageHeader
)) 
 335                 LEDs_SetAllLEDs(LEDMASK_USB_BUSY
); 
 337                 switch (MessageHeader
.MessageID
) 
 339                         case TMC_MESSAGEID_DEV_DEP_MSG_OUT
: 
 340                                 BytesTransferred 
= 0; 
 341                                 while (Endpoint_Discard_Stream(MessageHeader
.TransferSize
, &BytesTransferred
) == 
 342                                        ENDPOINT_RWSTREAM_IncompleteTransfer
) 
 344                                         if (IsTMCBulkOUTReset
) 
 347                                 LastTransferLength 
= BytesTransferred
; 
 351                         case TMC_MESSAGEID_DEV_DEP_MSG_IN
: 
 354                                 MessageHeader
.TransferSize 
= 3; 
 355                                 MessageHeader
.MessageIDSpecific
.DeviceOUT
.LastMessageTransaction 
= true; 
 356                                 WriteTMCHeader(&MessageHeader
); 
 358                                 BytesTransferred 
= 0; 
 359                                 while (Endpoint_Write_Stream_LE("TMC", MessageHeader
.TransferSize
, &BytesTransferred
) == 
 360                                        ENDPOINT_RWSTREAM_IncompleteTransfer
) 
 362                                         if (IsTMCBulkINReset
) 
 365                                 LastTransferLength 
= BytesTransferred
; 
 370                                 Endpoint_StallTransaction(); 
 374                 LEDs_SetAllLEDs(LEDMASK_USB_READY
); 
 377         /* All pending data has been processed - reset the data abort flags */ 
 378         IsTMCBulkINReset  
= false; 
 379         IsTMCBulkOUTReset 
= false; 
 382 /** Attempts to read in the TMC message header from the TMC interface. 
 384  *  \param[out] MessageHeader  Pointer to a location where the read header (if any) should be stored 
 386  *  \return Boolean true if a header was read, false otherwise 
 388 bool ReadTMCHeader(TMC_MessageHeader_t
* const MessageHeader
) 
 390         uint16_t BytesTransferred
; 
 392         /* Select the Data Out endpoint */ 
 393         Endpoint_SelectEndpoint(TMC_OUT_EPNUM
); 
 395         /* Abort if no command has been sent from the host */ 
 396         if (!(Endpoint_IsOUTReceived())) 
 399         /* Read in the header of the command from the host */ 
 400         BytesTransferred 
= 0; 
 401         while (Endpoint_Read_Stream_LE(MessageHeader
, sizeof(TMC_MessageHeader_t
), &BytesTransferred
) == 
 402                ENDPOINT_RWSTREAM_IncompleteTransfer
) 
 404                 if (IsTMCBulkOUTReset
) 
 408         /* Store the new command tag value for later use */ 
 409         CurrentTransferTag 
= MessageHeader
->Tag
; 
 411         /* Indicate if the command has been aborted or not */ 
 412         return !(IsTMCBulkOUTReset
); 
 415 bool WriteTMCHeader(TMC_MessageHeader_t
* const MessageHeader
) 
 417         uint16_t BytesTransferred
; 
 419         /* Set the message tag of the command header */ 
 420         MessageHeader
->Tag        
=  CurrentTransferTag
; 
 421         MessageHeader
->InverseTag 
= ~CurrentTransferTag
; 
 423         /* Select the Data In endpoint */ 
 424         Endpoint_SelectEndpoint(TMC_IN_EPNUM
); 
 426         /* Send the command header to the host */ 
 427         BytesTransferred 
= 0; 
 428         while (Endpoint_Write_Stream_LE(MessageHeader
, sizeof(TMC_MessageHeader_t
), &BytesTransferred
) == 
 429                ENDPOINT_RWSTREAM_IncompleteTransfer
) 
 431                 if (IsTMCBulkINReset
) 
 435         /* Indicate if the command has been aborted or not */ 
 436         return !(IsTMCBulkINReset
);