Browse Source

clean up endian convert function

hathach 6 years ago
parent
commit
81fc7b7e2b
4 changed files with 15 additions and 25 deletions
  1. 6 6
      src/class/msc/msc_device.c
  2. 4 4
      src/class/msc/msc_host.c
  3. 0 11
      src/common/tusb_common.h
  4. 5 4
      src/common/tusb_compiler.h

+ 6 - 6
src/class/msc/msc_device.c

@@ -81,7 +81,7 @@ static inline uint32_t rdwr10_get_lba(uint8_t const command[])
   memcpy(&lba, &p_rdwr10->lba, 4);
 
   // lba is in Big Endian format
-  return __be2n(lba);
+  return tu_ntohl(lba);
 }
 
 static inline uint16_t rdwr10_get_blockcount(uint8_t const command[])
@@ -93,7 +93,7 @@ static inline uint16_t rdwr10_get_blockcount(uint8_t const command[])
   uint16_t block_count;
   memcpy(&block_count, &p_rdwr10->block_count, 2);
 
-  return __be2n_16(block_count);
+  return tu_ntohs(block_count);
 }
 
 //--------------------------------------------------------------------+
@@ -239,8 +239,8 @@ int32_t proc_builtin_scsi(uint8_t lun, uint8_t const scsi_cmd[16], uint8_t* buff
       {
         scsi_read_capacity10_resp_t read_capa10;
 
-        read_capa10.last_lba = ENDIAN_BE(block_count-1);
-        read_capa10.block_size = ENDIAN_BE(block_size);
+        read_capa10.last_lba = tu_htonl(block_count-1);
+        read_capa10.block_size = tu_htonl(block_size);
 
         resplen = sizeof(read_capa10);
         memcpy(buffer, &read_capa10, resplen);
@@ -273,8 +273,8 @@ int32_t proc_builtin_scsi(uint8_t lun, uint8_t const scsi_cmd[16], uint8_t* buff
         if ( _mscd_itf.sense_key == 0 ) tud_msc_set_sense(lun, SCSI_SENSE_NOT_READY, 0x04, 0x00);
       }else
       {
-        read_fmt_capa.block_num = ENDIAN_BE(block_count);
-        read_fmt_capa.block_size_u16 = ENDIAN_BE16(block_size);
+        read_fmt_capa.block_num = tu_htonl(block_count);
+        read_fmt_capa.block_size_u16 = tu_htons(block_size);
 
         resplen = sizeof(read_fmt_capa);
         memcpy(buffer, &read_fmt_capa, resplen);

+ 4 - 4
src/class/msc/msc_host.c

@@ -232,7 +232,7 @@ tusb_error_t  tuh_msc_read10(uint8_t dev_addr, uint8_t lun, void * p_buffer, uin
   scsi_read10_t cmd_read10 =
   {
       .cmd_code    = SCSI_CMD_READ_10,
-      .lba         = __n2be(lba),
+      .lba         = tu_htonl(lba),
       .block_count = tu_u16_le2be(block_count)
   };
 
@@ -258,7 +258,7 @@ tusb_error_t tuh_msc_write10(uint8_t dev_addr, uint8_t lun, void const * p_buffe
   scsi_write10_t cmd_write10 =
   {
       .cmd_code    = SCSI_CMD_WRITE_10,
-      .lba         = __n2be(lba),
+      .lba         = tu_htonl(lba),
       .block_count = tu_u16_le2be(block_count)
   };
 
@@ -372,8 +372,8 @@ bool msch_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *it
     TU_ASSERT(osal_semaphore_wait(msch_sem_hdl, SCSI_XFER_TIMEOUT));
   }
 
-  p_msc->last_lba   = __be2n( ((scsi_read_capacity10_resp_t*)msch_buffer)->last_lba );
-  p_msc->block_size = (uint16_t) __be2n( ((scsi_read_capacity10_resp_t*)msch_buffer)->block_size );
+  p_msc->last_lba   = tu_ntohl( ((scsi_read_capacity10_resp_t*)msch_buffer)->last_lba );
+  p_msc->block_size = (uint16_t) tu_ntohl( ((scsi_read_capacity10_resp_t*)msch_buffer)->block_size );
 
   p_msc->is_initialized = true;
 

+ 0 - 11
src/common/tusb_common.h

@@ -61,12 +61,6 @@
 #define TU_BIT_CLEAR(x, n) ( (x) & (~TU_BIT(n)) )               ///< clear n-th bit of x
 #define TU_BIT_TEST(x, n) ( ((x) & TU_BIT(n)) ? true : false )  ///< check if n-th bit of x is 1
 
-//------------- Endian Conversion -------------//
-#define ENDIAN_BE(u32) \
-    (uint32_t) ( (((u32) & 0xFF) << 24) | (((u32) & 0xFF00) << 8) | (((u32) >> 8) & 0xFF00) | (((u32) >> 24) & 0xFF) )
-
-#define ENDIAN_BE16(le16) ((uint16_t) ((U16_LOW_U8(le16) << 8) | U16_HIGH_U8(le16)) )
-
 // for declaration of reserved field, make use of _TU_COUNTER_
 #define TU_RESERVED   XSTRING_CONCAT_(reserved, _TU_COUNTER_)
 
@@ -92,11 +86,6 @@
 //--------------------------------------------------------------------+
 // INLINE FUNCTION
 //--------------------------------------------------------------------+
-#ifndef __n2be_16 // TODO clean up
-#define __n2be_16(u16)  ((uint16_t) ((U16_LOW_U8(u16) << 8) | U16_HIGH_U8(u16)) )
-#define __be2n_16(u16)  __n2be_16(u16)
-#endif
-
 #define tu_memclr(buffer, size)  memset((buffer), 0, (size))
 #define tu_varclr(_var)          tu_memclr(_var, sizeof(*(_var)))
 

+ 5 - 4
src/common/tusb_compiler.h

@@ -62,11 +62,12 @@
   #define ATTR_UNUSED                __attribute__ ((unused))           // Function/Variable is meant to be possibly unused
 
   #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
-  #define __n2be(x)       __builtin_bswap32(x) ///< built-in function to convert 32-bit from native to Big Endian
-  #define __be2n(x)       __n2be(x) ///< built-in function to convert 32-bit from Big Endian to native
+  // Endian conversion use well-known host to network (big endian) naming
+  #define tu_htonl(x)    __builtin_bswap32(x)
+  #define tu_ntohl(x)    tu_htonl(x)
 
-  #define __n2be_16(u16)  __builtin_bswap16(u16)
-  #define __be2n_16(u16)  __n2be_16(u16)
+  #define tu_htons(u16)  __builtin_bswap16(u16)
+  #define tu_ntohs(u16)  tu_htons(u16)
   #endif
 
 #else