Add TMC header read and write functions, so that TMC data can now be exchanged in...
authorDean Camera <dean@fourwalledcubicle.com>
Wed, 28 Jul 2010 09:17:22 +0000 (09:17 +0000)
committerDean Camera <dean@fourwalledcubicle.com>
Wed, 28 Jul 2010 09:17:22 +0000 (09:17 +0000)
Minor update to the LowLevel MassStorage device demo, so that the ReadInCommandBlock() performs the data OUT endpoint selection and packet arrival test.

Demos/Device/Incomplete/TestAndMeasurement/TestAndMeasurement.c
Demos/Device/Incomplete/TestAndMeasurement/TestAndMeasurement.h
Demos/Device/LowLevel/MassStorage/MassStorage.c

index ab98411..711ca3f 100644 (file)
@@ -329,36 +329,36 @@ void TMC_Task(void)
        /* Device must be connected and configured for the task to run */\r
        if (USB_DeviceState != DEVICE_STATE_Configured)\r
          return;\r
-         \r
-       Endpoint_SelectEndpoint(TMC_OUT_EPNUM);\r
        \r
-       if (Endpoint_IsOUTReceived())\r
+       TMC_MessageHeader_t MessageHeader;\r
+       \r
+       /* Check if a TMC packet has been received */\r
+       if (ReadTMCHeader(&MessageHeader))\r
        {\r
-               TMC_MessageHeader_t MessageHeader;\r
-               \r
-               Endpoint_Read_Stream_LE(&MessageHeader, sizeof(MessageHeader), StreamCallback_AbortOUTOnRequest);\r
-               CurrentTransferTag = MessageHeader.Tag;\r
+               /* Indicate busy */\r
+               LEDs_SetAllLEDs(LEDMASK_USB_BUSY);\r
                \r
                switch (MessageHeader.MessageID)\r
                {\r
                        case TMC_MESSAGEID_DEV_DEP_MSG_OUT:\r
-\r
+                               Endpoint_Discard_Stream(MessageHeader.TransferSize, StreamCallback_AbortOUTOnRequest);\r
+                               Endpoint_ClearOUT();\r
                                break;\r
                        case TMC_MESSAGEID_DEV_DEP_MSG_IN:\r
+                               Endpoint_ClearOUT();\r
 \r
-                               break;\r
-                       case TMC_MESSAGEID_DEV_VENDOR_OUT:\r
-\r
-                               break;\r
-                       case TMC_MESSAGEID_DEV_VENDOR_IN:\r
+                               MessageHeader.TransferSize = 3;\r
+                               WriteTMCHeader(&MessageHeader);\r
 \r
+                               Endpoint_Write_Stream_LE("TMC", 3, StreamCallback_AbortINOnRequest);\r
+                               Endpoint_ClearIN();\r
                                break;\r
                        default:\r
                                Endpoint_StallTransaction();\r
                                break;\r
                }\r
-               \r
-               Endpoint_ClearOUT();\r
+\r
+               LEDs_SetAllLEDs(LEDMASK_USB_READY);\r
        }\r
        \r
        /* All pending data has been processed - reset the data abort flags */\r
@@ -366,6 +366,45 @@ void TMC_Task(void)
        IsTMCBulkOUTReset = false;\r
 }\r
 \r
+bool ReadTMCHeader(TMC_MessageHeader_t* const MessageHeader)\r
+{\r
+       /* Select the Data Out endpoint */\r
+       Endpoint_SelectEndpoint(TMC_OUT_EPNUM);\r
+       \r
+       /* Abort if no command has been sent from the host */\r
+       if (!(Endpoint_IsOUTReceived()))\r
+         return false;\r
+       \r
+       /* Read in the header of the command from the host */\r
+       Endpoint_Read_Stream_LE(MessageHeader, sizeof(TMC_MessageHeader_t), StreamCallback_AbortOUTOnRequest);\r
+\r
+       /* Store the new command tag value for later use */\r
+       CurrentTransferTag = MessageHeader->Tag;\r
+       \r
+       /* Indicate if the command has been aborted or not */\r
+       return !IsTMCBulkOUTReset;\r
+}\r
+\r
+bool WriteTMCHeader(TMC_MessageHeader_t* const MessageHeader)\r
+{\r
+       /* Compute the next transfer tag value, must be between 1 and 254 */\r
+       if (++CurrentTransferTag == 0xFF)\r
+         CurrentTransferTag = 1;\r
+\r
+       /* Set the message tag of the command header */\r
+       MessageHeader->Tag        =  CurrentTransferTag;\r
+       MessageHeader->InverseTag = ~CurrentTransferTag;\r
+\r
+       /* Select the Data In endpoint */\r
+       Endpoint_SelectEndpoint(TMC_IN_EPNUM);\r
+\r
+       /* Send the command header to the host */\r
+       Endpoint_Write_Stream_LE(MessageHeader, sizeof(TMC_MessageHeader_t), StreamCallback_AbortINOnRequest);\r
+\r
+       /* Indicate if the command has been aborted or not */\r
+       return !IsTMCBulkINReset;\r
+}\r
+\r
 /** Stream callback function for the Endpoint stream write functions. This callback will abort the current stream transfer\r
  *  if a TMC Abort Bulk IN request has been issued to the control endpoint.\r
  */\r
index 191eb40..d0db01e 100644 (file)
 \r
        /* Macros: */\r
                /** LED mask for the library LED driver, to indicate that the USB interface is not ready. */\r
-               #define LEDMASK_USB_NOTREADY                   LEDS_LED1\r
+               #define LEDMASK_USB_NOTREADY                  LEDS_LED1\r
 \r
                /** LED mask for the library LED driver, to indicate that the USB interface is enumerating. */\r
-               #define LEDMASK_USB_ENUMERATING               (LEDS_LED2 | LEDS_LED3)\r
+               #define LEDMASK_USB_ENUMERATING              (LEDS_LED2 | LEDS_LED3)\r
 \r
                /** LED mask for the library LED driver, to indicate that the USB interface is ready. */\r
-               #define LEDMASK_USB_READY                     (LEDS_LED2 | LEDS_LED4)\r
+               #define LEDMASK_USB_READY                    (LEDS_LED2 | LEDS_LED4)\r
 \r
                /** LED mask for the library LED driver, to indicate that an error has occurred in the USB interface. */\r
-               #define LEDMASK_USB_ERROR                     (LEDS_LED1 | LEDS_LED3)\r
+               #define LEDMASK_USB_ERROR                    (LEDS_LED1 | LEDS_LED3)\r
                \r
+               /** LED mask for the library LED driver, to indicate that the USB interface is busy. */\r
+               #define LEDMASK_USB_BUSY                      LEDS_LED2\r
+\r
                #define Req_InitiateAbortBulkOut              0x01\r
                #define Req_CheckAbortBulkOutStatus           0x02\r
                #define Req_InitiateAbortBulkIn               0x03\r
        /* Function Prototypes: */\r
                void SetupHardware(void);\r
                void TMC_Task(void);\r
+               bool ReadTMCHeader(TMC_MessageHeader_t* const MessageHeader);\r
+               bool WriteTMCHeader(TMC_MessageHeader_t* const MessageHeader);\r
 \r
                void EVENT_USB_Device_Connect(void);\r
                void EVENT_USB_Device_Disconnect(void);\r
index 9f13ff9..93b7c61 100644 (file)
@@ -173,47 +173,35 @@ void MassStorage_Task(void)
        /* Device must be connected and configured for the task to run */
        if (USB_DeviceState != DEVICE_STATE_Configured)
          return;
-         
-       /* Select the Data Out Endpoint */
-       Endpoint_SelectEndpoint(MASS_STORAGE_OUT_EPNUM);
-       
-       /* Check to see if a command from the host has been issued */
-       if (Endpoint_IsReadWriteAllowed())
+
+       /* Process sent command block from the host if one has been sent */
+       if (ReadInCommandBlock())
        {
                /* Indicate busy */
                LEDs_SetAllLEDs(LEDMASK_USB_BUSY);
 
-               /* Process sent command block from the host */
-               if (ReadInCommandBlock())
-               {
-                       /* Check direction of command, select Data IN endpoint if data is from the device */
-                       if (CommandBlock.Flags & COMMAND_DIRECTION_DATA_IN)
-                         Endpoint_SelectEndpoint(MASS_STORAGE_IN_EPNUM);
+               /* Check direction of command, select Data IN endpoint if data is from the device */
+               if (CommandBlock.Flags & COMMAND_DIRECTION_DATA_IN)
+                 Endpoint_SelectEndpoint(MASS_STORAGE_IN_EPNUM);
 
-                       /* Decode the received SCSI command, set returned status code */
-                       CommandStatus.Status = SCSI_DecodeSCSICommand() ? Command_Pass : Command_Fail;          
+               /* Decode the received SCSI command, set returned status code */
+               CommandStatus.Status = SCSI_DecodeSCSICommand() ? Command_Pass : Command_Fail;          
 
-                       /* Load in the CBW tag into the CSW to link them together */
-                       CommandStatus.Tag = CommandBlock.Tag;
+               /* Load in the CBW tag into the CSW to link them together */
+               CommandStatus.Tag = CommandBlock.Tag;
 
-                       /* Load in the data residue counter into the CSW */
-                       CommandStatus.DataTransferResidue = CommandBlock.DataTransferLength;
-                       
-                       /* Stall the selected data pipe if command failed (if data is still to be transferred) */
-                       if ((CommandStatus.Status == Command_Fail) && (CommandStatus.DataTransferResidue))
-                         Endpoint_StallTransaction();
-
-                       /* Return command status block to the host */
-                       ReturnCommandStatus();
-
-                       /* Indicate ready */
-                       LEDs_SetAllLEDs(LEDMASK_USB_READY);
-               }
-               else
-               {
-                       /* Indicate error reading in the command block from the host */
-                       LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
-               }
+               /* Load in the data residue counter into the CSW */
+               CommandStatus.DataTransferResidue = CommandBlock.DataTransferLength;
+               
+               /* Stall the selected data pipe if command failed (if data is still to be transferred) */
+               if ((CommandStatus.Status == Command_Fail) && (CommandStatus.DataTransferResidue))
+                 Endpoint_StallTransaction();
+
+               /* Return command status block to the host */
+               ReturnCommandStatus();
+
+               /* Indicate ready */
+               LEDs_SetAllLEDs(LEDMASK_USB_READY);
        }
 
        /* Check if a Mass Storage Reset occurred */
@@ -244,6 +232,10 @@ static bool ReadInCommandBlock(void)
 {
        /* Select the Data Out endpoint */
        Endpoint_SelectEndpoint(MASS_STORAGE_OUT_EPNUM);
+       
+       /* Abort if no command has been sent from the host */
+       if (!(Endpoint_IsOUTReceived()))
+         return false;
 
        /* Read in command block header */
        Endpoint_Read_Stream_LE(&CommandBlock, (sizeof(CommandBlock) - sizeof(CommandBlock.SCSICommandData)),