summaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
authorNathan Conrad <[email protected]>2019-09-27 00:37:21 -0400
committerNathan Conrad <[email protected]>2019-09-27 00:37:21 -0400
commit91fa24d99c333d89f29ad4b2cef16f26c5c80a3b (patch)
tree4d74f44887090e049db46e19d437adcaf22213d3 /src/common
parent705b8be956e64ac06f223b2fc3e08bd706602a18 (diff)
parent9a8d23e95ed6a48b3a3d43f7cf7d3f360ee61977 (diff)
Merge branch 'master' into ZLP_Request2
Diffstat (limited to 'src/common')
-rw-r--r--src/common/tusb_common.h2
-rw-r--r--src/common/tusb_compiler.h71
-rw-r--r--src/common/tusb_types.h3
-rw-r--r--src/common/tusb_verify.h30
4 files changed, 91 insertions, 15 deletions
diff --git a/src/common/tusb_common.h b/src/common/tusb_common.h
index 4f0105d13..57c6e2fcf 100644
--- a/src/common/tusb_common.h
+++ b/src/common/tusb_common.h
@@ -38,7 +38,7 @@
//--------------------------------------------------------------------+
// Macros Helper
//--------------------------------------------------------------------+
-#define TU_ARRAY_SZIE(_arr) ( sizeof(_arr) / sizeof(_arr[0]) )
+#define TU_ARRAY_SIZE(_arr) ( sizeof(_arr) / sizeof(_arr[0]) )
#define TU_MIN(_x, _y) ( (_x) < (_y) ) ? (_x) : (_y) )
#define TU_MAX(_x, _y) ( (_x) > (_y) ) ? (_x) : (_y) )
diff --git a/src/common/tusb_compiler.h b/src/common/tusb_compiler.h
index 09768ef88..58732b871 100644
--- a/src/common/tusb_compiler.h
+++ b/src/common/tusb_compiler.h
@@ -53,6 +53,9 @@
// for declaration of reserved field, make use of _TU_COUNTER_
#define TU_RESERVED TU_XSTRCAT(reserved, _TU_COUNTER_)
+#define TU_LITTLE_ENDIAN (0x12u)
+#define TU_BIG_ENDIAN (0x21u)
+
//--------------------------------------------------------------------+
// Compiler porting with Attribute and Endian
//--------------------------------------------------------------------+
@@ -67,20 +70,68 @@
// Endian conversion use well-known host to network (big endian) naming
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
- #define tu_htonl(u32) __builtin_bswap32(u32)
- #define tu_ntohl(u32) __builtin_bswap32(u32)
-
- #define tu_htons(u16) __builtin_bswap16(u16)
- #define tu_ntohs(u16) __builtin_bswap16(u16)
+ #define TU_BYTE_ORDER TU_LITTLE_ENDIAN
#else
- #define tu_htonl(u32) (u32)
- #define tu_ntohl(u32) (u32)
+ #define TU_BYTE_ORDER TU_BIG_ENDIAN
+ #endif
+
+#define TU_BSWAP16(u16) (__builtin_bswap16(u16))
+#define TU_BSWAP32(u32) (__builtin_bswap32(u32))
- #define tu_htons(u16) (u16)
- #define tu_ntohs(u16) (u16)
+#elif defined(__TI_COMPILER_VERSION__)
+ #define TU_ATTR_ALIGNED(Bytes) __attribute__ ((aligned(Bytes)))
+ #define TU_ATTR_SECTION(sec_name) __attribute__ ((section(#sec_name)))
+ #define TU_ATTR_PACKED __attribute__ ((packed))
+ #define TU_ATTR_PREPACKED
+ #define TU_ATTR_WEAK __attribute__ ((weak))
+ #define TU_ATTR_DEPRECATED(mess) __attribute__ ((deprecated(mess))) // warn if function with this attribute is used
+ #define TU_ATTR_UNUSED __attribute__ ((unused)) // Function/Variable is meant to be possibly unused
+
+ // __BYTE_ORDER is defined in the TI ARM compiler, but not MSP430 (which is little endian)
+ #if ((__BYTE_ORDER__) == (__ORDER_LITTLE_ENDIAN__)) || defined(__MSP430__)
+ #define TU_BYTE_ORDER TU_LITTLE_ENDIAN
+ #else
+ #define TU_BYTE_ORDER TU_BIG_ENDIAN
#endif
+
+ #define TU_BSWAP16(u16) (__builtin_bswap16(u16))
+ #define TU_BSWAP32(u32) (__builtin_bswap32(u32))
+
+#else
+ #error "Compiler attribute porting is required"
+#endif
+
+#if (TU_BYTE_ORDER == TU_LITTLE_ENDIAN)
+
+ #define tu_htons(u16) (TU_BSWAP16(u16))
+ #define tu_ntohs(u16) (TU_BSWAP16(u16))
+
+ #define tu_htonl(u32) (TU_BSWAP32(u32))
+ #define tu_ntohl(u32) (TU_BSWAP32(u32))
+
+ #define tu_htole16(u16) (u16)
+ #define tu_le16toh(u16) (u16)
+
+ #define tu_htole32(u32) (u32)
+ #define tu_le32toh(u32) (u32)
+
+#elif (TU_BYTE_ORDER == TU_BIG_ENDIAN)
+
+ #define tu_htons(u16) (u16)
+ #define tu_ntohs(u16) (u16)
+
+ #define tu_htonl(u32) (u32)
+ #define tu_ntohl(u32) (u32)
+
+
+ #define tu_htole16(u16) (tu_bswap16(u16))
+ #define tu_le16toh(u16) (tu_bswap16(u16))
+
+ #define tu_htole32(u32) (tu_bswap32(u32))
+ #define tu_le32toh(u32) (tu_bswap32(u32))
+
#else
- #error "Compiler attribute porting are required"
+ #error Byte order is undefined
#endif
#endif /* _TUSB_COMPILER_H_ */
diff --git a/src/common/tusb_types.h b/src/common/tusb_types.h
index a50e89934..ad42baad7 100644
--- a/src/common/tusb_types.h
+++ b/src/common/tusb_types.h
@@ -125,7 +125,8 @@ typedef enum
{
TUSB_REQ_TYPE_STANDARD = 0,
TUSB_REQ_TYPE_CLASS,
- TUSB_REQ_TYPE_VENDOR
+ TUSB_REQ_TYPE_VENDOR,
+ TUSB_REQ_TYPE_INVALID
} tusb_request_type_t;
typedef enum
diff --git a/src/common/tusb_verify.h b/src/common/tusb_verify.h
index 2727ce043..fae0c88ae 100644
--- a/src/common/tusb_verify.h
+++ b/src/common/tusb_verify.h
@@ -36,10 +36,34 @@
* as C++ for the sake of code simplicity. Beware of a headache macro
* manipulation that you are told to stay away.
*
- * e.g
*
- * - TU_VERIFY( cond ) will return false if cond is false
- * - TU_VERIFY( cond, err) will return err instead if cond is false
+ * This contains macros for both VERIFY and ASSERT:
+ *
+ * VERIFY: Used when there is an error condition which is not the
+ * fault of the MCU. For example, bounds checking on data
+ * sent to the micro over USB should use this function.
+ * Another example is checking for buffer overflows, where
+ * returning from the active function causes a NAK.
+ *
+ * ASSERT: Used for error conditions that are caused by MCU firmware
+ * bugs. This is used to discover bugs in the code more
+ * quickly. One example would be adding assertions in library
+ * function calls to confirm a function's (untainted)
+ * parameters are valid.
+ *
+ *
+ * The difference in behaviour is that ASSERT triggers a breakpoint while
+ * verify does not.
+ *
+ * #define TU_VERIFY(cond) if(cond) return false;
+ * #define TU_VERIFY(cond,ret) if(cond) return ret;
+ *
+ * #define TU_VERIFY_HDLR(cond,handler) if(cond) {handler; return false;}
+ * #define TU_VERIFY_HDLR(cond,ret,handler) if(cond) {handler; return ret;}
+ *
+ * #define TU_ASSERT(cond) if(cond) {_MESS_FAILED(); TU_BREAKPOINT(), return false;}
+ * #define TU_ASSERT(cond,ret) if(cond) {_MESS_FAILED(); TU_BREAKPOINT(), return ret;}
+ *
*------------------------------------------------------------------*/
#ifdef __cplusplus