Rewrote the implementation of the SwapEndian_16() and SwapEndian_32() functions so...
authorDean Camera <dean@fourwalledcubicle.com>
Fri, 9 Jul 2010 07:24:34 +0000 (07:24 +0000)
committerDean Camera <dean@fourwalledcubicle.com>
Fri, 9 Jul 2010 07:24:34 +0000 (07:24 +0000)
Fixed SCSI.c implementations of all the demos/projects casting the block count to a 32-bit temporary before calling SwapEndian_16().

Demos/Device/ClassDriver/MassStorage/Lib/SCSI.c
Demos/Device/ClassDriver/MassStorageKeyboard/Lib/SCSI.c
Demos/Device/LowLevel/MassStorage/Lib/SCSI.c
LUFA/Common/Common.h
LUFA/ManPages/ChangeLog.txt
Projects/Incomplete/StandaloneProgrammer/Lib/SCSI.c
Projects/TemperatureDataLogger/Lib/SCSI.c
Projects/Webserver/Lib/SCSI.c

index 6c20576..08b055f 100644 (file)
@@ -160,7 +160,7 @@ static void SCSI_Command_Inquiry(USB_ClassInfo_MS_Device_t* MSInterfaceInfo)
        uint8_t PadBytes[AllocationLength - BytesTransferred];
        
        /* Pad out remaining bytes with 0x00 */
        uint8_t PadBytes[AllocationLength - BytesTransferred];
        
        /* Pad out remaining bytes with 0x00 */
-       Endpoint_Write_Stream_LE(&PadBytes, (AllocationLength - BytesTransferred), NO_STREAM_CALLBACK);
+       Endpoint_Write_Stream_LE(&PadBytes, sizeof(PadBytes), NO_STREAM_CALLBACK);
 
        /* Finalize the stream transfer to send the last packet */
        Endpoint_ClearIN();
 
        /* Finalize the stream transfer to send the last packet */
        Endpoint_ClearIN();
@@ -257,7 +257,7 @@ static void SCSI_Command_ReadWrite_10(USB_ClassInfo_MS_Device_t* MSInterfaceInfo
        BlockAddress = SwapEndian_32(*(uint32_t*)&MSInterfaceInfo->State.CommandBlock.SCSICommandData[2]);
 
        /* Load in the 16-bit total blocks (SCSI uses big-endian, so have to reverse the byte order) */
        BlockAddress = SwapEndian_32(*(uint32_t*)&MSInterfaceInfo->State.CommandBlock.SCSICommandData[2]);
 
        /* Load in the 16-bit total blocks (SCSI uses big-endian, so have to reverse the byte order) */
-       TotalBlocks  = SwapEndian_16(*(uint32_t*)&MSInterfaceInfo->State.CommandBlock.SCSICommandData[7]);
+       TotalBlocks  = SwapEndian_16(*(uint16_t*)&MSInterfaceInfo->State.CommandBlock.SCSICommandData[7]);
        
        /* Check if the block address is outside the maximum allowable value for the LUN */
        if (BlockAddress >= LUN_MEDIA_BLOCKS)
        
        /* Check if the block address is outside the maximum allowable value for the LUN */
        if (BlockAddress >= LUN_MEDIA_BLOCKS)
index 2b4477a..897090c 100644 (file)
@@ -170,7 +170,7 @@ static bool SCSI_Command_Inquiry(USB_ClassInfo_MS_Device_t* MSInterfaceInfo)
        uint8_t PadBytes[AllocationLength - BytesTransferred];
        
        /* Pad out remaining bytes with 0x00 */
        uint8_t PadBytes[AllocationLength - BytesTransferred];
        
        /* Pad out remaining bytes with 0x00 */
-       Endpoint_Write_Stream_LE(&PadBytes, (AllocationLength - BytesTransferred), NO_STREAM_CALLBACK);
+       Endpoint_Write_Stream_LE(&PadBytes, sizeof(PadBytes), NO_STREAM_CALLBACK);
 
        /* Finalize the stream transfer to send the last packet */
        Endpoint_ClearIN();
 
        /* Finalize the stream transfer to send the last packet */
        Endpoint_ClearIN();
@@ -196,7 +196,7 @@ static bool SCSI_Command_Request_Sense(USB_ClassInfo_MS_Device_t* MSInterfaceInf
        uint8_t PadBytes[AllocationLength - BytesTransferred];
 
        Endpoint_Write_Stream_LE(&SenseData, BytesTransferred, NO_STREAM_CALLBACK);
        uint8_t PadBytes[AllocationLength - BytesTransferred];
 
        Endpoint_Write_Stream_LE(&SenseData, BytesTransferred, NO_STREAM_CALLBACK);
-       Endpoint_Write_Stream_LE(&PadBytes, (AllocationLength - BytesTransferred), NO_STREAM_CALLBACK);
+       Endpoint_Write_Stream_LE(&PadBytes, sizeof(PadBytes), NO_STREAM_CALLBACK);
        Endpoint_ClearIN();
 
        /* Succeed the command and update the bytes transferred counter */
        Endpoint_ClearIN();
 
        /* Succeed the command and update the bytes transferred counter */
@@ -306,15 +306,11 @@ static bool SCSI_Command_ReadWrite_10(USB_ClassInfo_MS_Device_t* MSInterfaceInfo
        uint32_t BlockAddress;
        uint16_t TotalBlocks;
        
        uint32_t BlockAddress;
        uint16_t TotalBlocks;
        
-       /* Load in the 32-bit block address (SCSI uses big-endian, so have to do it byte-by-byte) */
-       ((uint8_t*)&BlockAddress)[3] = MSInterfaceInfo->State.CommandBlock.SCSICommandData[2];
-       ((uint8_t*)&BlockAddress)[2] = MSInterfaceInfo->State.CommandBlock.SCSICommandData[3];
-       ((uint8_t*)&BlockAddress)[1] = MSInterfaceInfo->State.CommandBlock.SCSICommandData[4];
-       ((uint8_t*)&BlockAddress)[0] = MSInterfaceInfo->State.CommandBlock.SCSICommandData[5];
-
-       /* Load in the 16-bit total blocks (SCSI uses big-endian, so have to do it byte-by-byte) */
-       ((uint8_t*)&TotalBlocks)[1]  = MSInterfaceInfo->State.CommandBlock.SCSICommandData[7];
-       ((uint8_t*)&TotalBlocks)[0]  = MSInterfaceInfo->State.CommandBlock.SCSICommandData[8];
+       /* Load in the 32-bit block address (SCSI uses big-endian, so have to reverse the byte order) */
+       BlockAddress = SwapEndian_32(*(uint32_t*)&CommandBlock.SCSICommandData[2]);
+
+       /* Load in the 16-bit total blocks (SCSI uses big-endian, so have to reverse the byte order) */
+       TotalBlocks  = SwapEndian_16(*(uint16_t*)&CommandBlock.SCSICommandData[7]);
        
        /* Check if the block address is outside the maximum allowable value for the LUN */
        if (BlockAddress >= LUN_MEDIA_BLOCKS)
        
        /* Check if the block address is outside the maximum allowable value for the LUN */
        if (BlockAddress >= LUN_MEDIA_BLOCKS)
index 10e6bc4..111c3b7 100644 (file)
@@ -159,7 +159,7 @@ static void SCSI_Command_Inquiry(void)
        uint8_t PadBytes[AllocationLength - BytesTransferred];
        
        /* Pad out remaining bytes with 0x00 */
        uint8_t PadBytes[AllocationLength - BytesTransferred];
        
        /* Pad out remaining bytes with 0x00 */
-       Endpoint_Write_Stream_LE(&PadBytes, (AllocationLength - BytesTransferred), StreamCallback_AbortOnMassStoreReset);
+       Endpoint_Write_Stream_LE(&PadBytes, sizeof(PadBytes), StreamCallback_AbortOnMassStoreReset);
 
        /* Finalize the stream transfer to send the last packet */
        Endpoint_ClearIN();
 
        /* Finalize the stream transfer to send the last packet */
        Endpoint_ClearIN();
@@ -182,7 +182,7 @@ static void SCSI_Command_Request_Sense(void)
        uint8_t PadBytes[AllocationLength - BytesTransferred];
        
        /* Pad out remaining bytes with 0x00 */
        uint8_t PadBytes[AllocationLength - BytesTransferred];
        
        /* Pad out remaining bytes with 0x00 */
-       Endpoint_Write_Stream_LE(&PadBytes, (AllocationLength - BytesTransferred), StreamCallback_AbortOnMassStoreReset);
+       Endpoint_Write_Stream_LE(&PadBytes, sizeof(PadBytes), StreamCallback_AbortOnMassStoreReset);
 
        /* Finalize the stream transfer to send the last packet */
        Endpoint_ClearIN();
 
        /* Finalize the stream transfer to send the last packet */
        Endpoint_ClearIN();
@@ -256,16 +256,12 @@ static void SCSI_Command_ReadWrite_10(const bool IsDataRead)
        uint32_t BlockAddress;
        uint16_t TotalBlocks;
        
        uint32_t BlockAddress;
        uint16_t TotalBlocks;
        
-       /* Load in the 32-bit block address (SCSI uses big-endian, so have to do it byte-by-byte) */
-       ((uint8_t*)&BlockAddress)[3] = CommandBlock.SCSICommandData[2];
-       ((uint8_t*)&BlockAddress)[2] = CommandBlock.SCSICommandData[3];
-       ((uint8_t*)&BlockAddress)[1] = CommandBlock.SCSICommandData[4];
-       ((uint8_t*)&BlockAddress)[0] = CommandBlock.SCSICommandData[5];
-
-       /* Load in the 16-bit total blocks (SCSI uses big-endian, so have to do it byte-by-byte) */
-       ((uint8_t*)&TotalBlocks)[1]  = CommandBlock.SCSICommandData[7];
-       ((uint8_t*)&TotalBlocks)[0]  = CommandBlock.SCSICommandData[8];
-       
+       /* Load in the 32-bit block address (SCSI uses big-endian, so have to reverse the byte order) */
+       BlockAddress = SwapEndian_32(*(uint32_t*)&CommandBlock.SCSICommandData[2]);
+
+       /* Load in the 16-bit total blocks (SCSI uses big-endian, so have to reverse the byte order) */
+       TotalBlocks  = SwapEndian_16(*(uint16_t*)&CommandBlock.SCSICommandData[7]);
+
        /* Check if the block address is outside the maximum allowable value for the LUN */
        if (BlockAddress >= LUN_MEDIA_BLOCKS)
        {
        /* Check if the block address is outside the maximum allowable value for the LUN */
        if (BlockAddress >= LUN_MEDIA_BLOCKS)
        {
index 7fd1555..76144f9 100644 (file)
                        static inline uint16_t SwapEndian_16(uint16_t Word) ATTR_WARN_UNUSED_RESULT ATTR_CONST;
                        static inline uint16_t SwapEndian_16(uint16_t Word)
                        {
                        static inline uint16_t SwapEndian_16(uint16_t Word) ATTR_WARN_UNUSED_RESULT ATTR_CONST;
                        static inline uint16_t SwapEndian_16(uint16_t Word)
                        {
-                               return ((Word >> 8) | (Word << 8));                             
+                               uint8_t Temp;
+
+                               union
+                               {
+                                       uint16_t Word;
+                                       uint8_t  Bytes[2];
+                               } Data;
+                               
+                               Data.Word = Word;
+                               
+                               Temp = Data.Bytes[0];
+                               Data.Bytes[0] = Data.Bytes[1];
+                               Data.Bytes[1] = Temp;
+                               
+                               return Data.Word;
                        }
 
                        /** Function to reverse the byte ordering of the individual bytes in a 32 bit number.
                        }
 
                        /** Function to reverse the byte ordering of the individual bytes in a 32 bit number.
                        static inline uint32_t SwapEndian_32(uint32_t DWord) ATTR_WARN_UNUSED_RESULT ATTR_CONST;
                        static inline uint32_t SwapEndian_32(uint32_t DWord)
                        {
                        static inline uint32_t SwapEndian_32(uint32_t DWord) ATTR_WARN_UNUSED_RESULT ATTR_CONST;
                        static inline uint32_t SwapEndian_32(uint32_t DWord)
                        {
-                               return (((DWord & 0xFF000000) >> 24) |
-                                       ((DWord & 0x00FF0000) >> 8)  |
-                                               ((DWord & 0x0000FF00) << 8)  |
-                                               ((DWord & 0x000000FF) << 24));
+                               uint8_t Temp;
+
+                               union
+                               {
+                                       uint32_t DWord;
+                                       uint8_t  Bytes[4];
+                               } Data;
+                               
+                               Data.DWord = DWord;
+                               
+                               Temp = Data.Bytes[0];
+                               Data.Bytes[0] = Data.Bytes[3];
+                               Data.Bytes[3] = Temp;
+                               
+                               Temp = Data.Bytes[1];
+                               Data.Bytes[1] = Data.Bytes[2];
+                               Data.Bytes[2] = Temp;
+                               
+                               return Data.DWord;
                        }
 
                        /** Function to reverse the byte ordering of the individual bytes in a n byte number.
                        }
 
                        /** Function to reverse the byte ordering of the individual bytes in a n byte number.
index 593751a..0a2f43a 100644 (file)
@@ -25,6 +25,8 @@
   *  - Removed unused line encoding data and control requests from the CDC Bootloader code, to save space
   *  - Renamed SERIAL_STREAM_ASSERT() macro to STDOUT_ASSERT()
   *  - The USB_Device_IsRemoteWakeupSent() and USB_Device_IsUSBSuspended() macros have been deleted, as they are now obsolete
   *  - Removed unused line encoding data and control requests from the CDC Bootloader code, to save space
   *  - Renamed SERIAL_STREAM_ASSERT() macro to STDOUT_ASSERT()
   *  - The USB_Device_IsRemoteWakeupSent() and USB_Device_IsUSBSuspended() macros have been deleted, as they are now obsolete
+  *  - Rewrote the implementation of the SwapEndian_16() and SwapEndian_32() functions so that they compile down in most instances to
+  *    minimal loads and stores rather than complicated shifts
   *
   *  <b>Fixed:</b>
   *  - Fixed AVRISP project sending a LOAD EXTENDED ADDRESS command to 128KB AVRs after programming or reading from
   *
   *  <b>Fixed:</b>
   *  - Fixed AVRISP project sending a LOAD EXTENDED ADDRESS command to 128KB AVRs after programming or reading from
index 4021604..21dfbfa 100644 (file)
@@ -161,7 +161,7 @@ static void SCSI_Command_Inquiry(USB_ClassInfo_MS_Device_t* const MSInterfaceInf
        uint8_t PadBytes[AllocationLength - BytesTransferred];
        
        /* Pad out remaining bytes with 0x00 */
        uint8_t PadBytes[AllocationLength - BytesTransferred];
        
        /* Pad out remaining bytes with 0x00 */
-       Endpoint_Write_Stream_LE(&PadBytes, (AllocationLength - BytesTransferred), NO_STREAM_CALLBACK);
+       Endpoint_Write_Stream_LE(&PadBytes, sizeof(PadBytes), NO_STREAM_CALLBACK);
 
        /* Finalize the stream transfer to send the last packet */
        Endpoint_ClearIN();
 
        /* Finalize the stream transfer to send the last packet */
        Endpoint_ClearIN();
@@ -183,7 +183,7 @@ static void SCSI_Command_Request_Sense(USB_ClassInfo_MS_Device_t* const MSInterf
        uint8_t PadBytes[AllocationLength - BytesTransferred];
 
        Endpoint_Write_Stream_LE(&SenseData, BytesTransferred, NO_STREAM_CALLBACK);
        uint8_t PadBytes[AllocationLength - BytesTransferred];
 
        Endpoint_Write_Stream_LE(&SenseData, BytesTransferred, NO_STREAM_CALLBACK);
-       Endpoint_Write_Stream_LE(&PadBytes, (AllocationLength - BytesTransferred), NO_STREAM_CALLBACK);
+       Endpoint_Write_Stream_LE(&PadBytes, sizeof(PadBytes), NO_STREAM_CALLBACK);
        Endpoint_ClearIN();
 
        /* Succeed the command and update the bytes transferred counter */
        Endpoint_ClearIN();
 
        /* Succeed the command and update the bytes transferred counter */
@@ -258,7 +258,7 @@ static void SCSI_Command_ReadWrite_10(USB_ClassInfo_MS_Device_t* const MSInterfa
        BlockAddress = SwapEndian_32(*(uint32_t*)&MSInterfaceInfo->State.CommandBlock.SCSICommandData[2]);
 
        /* Load in the 16-bit total blocks (SCSI uses big-endian, so have to reverse the byte order) */
        BlockAddress = SwapEndian_32(*(uint32_t*)&MSInterfaceInfo->State.CommandBlock.SCSICommandData[2]);
 
        /* Load in the 16-bit total blocks (SCSI uses big-endian, so have to reverse the byte order) */
-       TotalBlocks  = SwapEndian_16(*(uint32_t*)&MSInterfaceInfo->State.CommandBlock.SCSICommandData[7]);
+       TotalBlocks  = SwapEndian_16(*(uint16_t*)&MSInterfaceInfo->State.CommandBlock.SCSICommandData[7]);
        
        /* Check if the block address is outside the maximum allowable value for the LUN */
        if (BlockAddress >= VIRTUAL_MEMORY_BLOCKS)
        
        /* Check if the block address is outside the maximum allowable value for the LUN */
        if (BlockAddress >= VIRTUAL_MEMORY_BLOCKS)
index 55312e8..d499d9a 100644 (file)
@@ -160,7 +160,7 @@ static void SCSI_Command_Inquiry(USB_ClassInfo_MS_Device_t* const MSInterfaceInf
        uint8_t PadBytes[AllocationLength - BytesTransferred];
        
        /* Pad out remaining bytes with 0x00 */
        uint8_t PadBytes[AllocationLength - BytesTransferred];
        
        /* Pad out remaining bytes with 0x00 */
-       Endpoint_Write_Stream_LE(&PadBytes, (AllocationLength - BytesTransferred), NO_STREAM_CALLBACK);
+       Endpoint_Write_Stream_LE(&PadBytes, sizeof(PadBytes), NO_STREAM_CALLBACK);
 
        /* Finalize the stream transfer to send the last packet */
        Endpoint_ClearIN();
 
        /* Finalize the stream transfer to send the last packet */
        Endpoint_ClearIN();
@@ -182,7 +182,7 @@ static void SCSI_Command_Request_Sense(USB_ClassInfo_MS_Device_t* const MSInterf
        uint8_t PadBytes[AllocationLength - BytesTransferred];
 
        Endpoint_Write_Stream_LE(&SenseData, BytesTransferred, NO_STREAM_CALLBACK);
        uint8_t PadBytes[AllocationLength - BytesTransferred];
 
        Endpoint_Write_Stream_LE(&SenseData, BytesTransferred, NO_STREAM_CALLBACK);
-       Endpoint_Write_Stream_LE(&PadBytes, (AllocationLength - BytesTransferred), NO_STREAM_CALLBACK);
+       Endpoint_Write_Stream_LE(&PadBytes, sizeof(PadBytes), NO_STREAM_CALLBACK);
        Endpoint_ClearIN();
 
        /* Succeed the command and update the bytes transferred counter */
        Endpoint_ClearIN();
 
        /* Succeed the command and update the bytes transferred counter */
@@ -257,7 +257,7 @@ static void SCSI_Command_ReadWrite_10(USB_ClassInfo_MS_Device_t* const MSInterfa
        BlockAddress = SwapEndian_32(*(uint32_t*)&MSInterfaceInfo->State.CommandBlock.SCSICommandData[2]);
 
        /* Load in the 16-bit total blocks (SCSI uses big-endian, so have to reverse the byte order) */
        BlockAddress = SwapEndian_32(*(uint32_t*)&MSInterfaceInfo->State.CommandBlock.SCSICommandData[2]);
 
        /* Load in the 16-bit total blocks (SCSI uses big-endian, so have to reverse the byte order) */
-       TotalBlocks  = SwapEndian_16(*(uint32_t*)&MSInterfaceInfo->State.CommandBlock.SCSICommandData[7]);
+       TotalBlocks  = SwapEndian_16(*(uint16_t*)&MSInterfaceInfo->State.CommandBlock.SCSICommandData[7]);
        
        /* Check if the block address is outside the maximum allowable value for the LUN */
        if (BlockAddress >= VIRTUAL_MEMORY_BLOCKS)
        
        /* Check if the block address is outside the maximum allowable value for the LUN */
        if (BlockAddress >= VIRTUAL_MEMORY_BLOCKS)
index 55312e8..d499d9a 100644 (file)
@@ -160,7 +160,7 @@ static void SCSI_Command_Inquiry(USB_ClassInfo_MS_Device_t* const MSInterfaceInf
        uint8_t PadBytes[AllocationLength - BytesTransferred];
        
        /* Pad out remaining bytes with 0x00 */
        uint8_t PadBytes[AllocationLength - BytesTransferred];
        
        /* Pad out remaining bytes with 0x00 */
-       Endpoint_Write_Stream_LE(&PadBytes, (AllocationLength - BytesTransferred), NO_STREAM_CALLBACK);
+       Endpoint_Write_Stream_LE(&PadBytes, sizeof(PadBytes), NO_STREAM_CALLBACK);
 
        /* Finalize the stream transfer to send the last packet */
        Endpoint_ClearIN();
 
        /* Finalize the stream transfer to send the last packet */
        Endpoint_ClearIN();
@@ -182,7 +182,7 @@ static void SCSI_Command_Request_Sense(USB_ClassInfo_MS_Device_t* const MSInterf
        uint8_t PadBytes[AllocationLength - BytesTransferred];
 
        Endpoint_Write_Stream_LE(&SenseData, BytesTransferred, NO_STREAM_CALLBACK);
        uint8_t PadBytes[AllocationLength - BytesTransferred];
 
        Endpoint_Write_Stream_LE(&SenseData, BytesTransferred, NO_STREAM_CALLBACK);
-       Endpoint_Write_Stream_LE(&PadBytes, (AllocationLength - BytesTransferred), NO_STREAM_CALLBACK);
+       Endpoint_Write_Stream_LE(&PadBytes, sizeof(PadBytes), NO_STREAM_CALLBACK);
        Endpoint_ClearIN();
 
        /* Succeed the command and update the bytes transferred counter */
        Endpoint_ClearIN();
 
        /* Succeed the command and update the bytes transferred counter */
@@ -257,7 +257,7 @@ static void SCSI_Command_ReadWrite_10(USB_ClassInfo_MS_Device_t* const MSInterfa
        BlockAddress = SwapEndian_32(*(uint32_t*)&MSInterfaceInfo->State.CommandBlock.SCSICommandData[2]);
 
        /* Load in the 16-bit total blocks (SCSI uses big-endian, so have to reverse the byte order) */
        BlockAddress = SwapEndian_32(*(uint32_t*)&MSInterfaceInfo->State.CommandBlock.SCSICommandData[2]);
 
        /* Load in the 16-bit total blocks (SCSI uses big-endian, so have to reverse the byte order) */
-       TotalBlocks  = SwapEndian_16(*(uint32_t*)&MSInterfaceInfo->State.CommandBlock.SCSICommandData[7]);
+       TotalBlocks  = SwapEndian_16(*(uint16_t*)&MSInterfaceInfo->State.CommandBlock.SCSICommandData[7]);
        
        /* Check if the block address is outside the maximum allowable value for the LUN */
        if (BlockAddress >= VIRTUAL_MEMORY_BLOCKS)
        
        /* Check if the block address is outside the maximum allowable value for the LUN */
        if (BlockAddress >= VIRTUAL_MEMORY_BLOCKS)