summaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
authorWilliam D. Jones <[email protected]>2019-01-10 09:58:06 -0500
committerWilliam D. Jones <[email protected]>2019-01-10 09:58:06 -0500
commitb367baeaf1d64930654f136a2409f1322fc73ab2 (patch)
tree710698145d52c72b807971d05512330fde2cc9b1 /src/common
parente4d6f336f0d5004fd9ee3074305b7980ba777dfd (diff)
parent5804e56e3c2ab4480bf72d94d997f769a645af47 (diff)
Merge branch 'master' of https://github.com/hathach/tinyusb into stm32f4
Diffstat (limited to 'src/common')
-rw-r--r--src/common/binary.h132
-rw-r--r--src/common/compiler/tusb_compiler_gcc.h68
-rw-r--r--src/common/compiler/tusb_compiler_iar.h31
-rw-r--r--src/common/tusb_common.h217
-rw-r--r--src/common/tusb_error.h33
-rw-r--r--src/common/tusb_fifo.c4
-rw-r--r--src/common/tusb_fifo.h3
-rw-r--r--src/common/tusb_timeout.h10
-rw-r--r--src/common/tusb_types.h34
9 files changed, 150 insertions, 382 deletions
diff --git a/src/common/binary.h b/src/common/binary.h
deleted file mode 100644
index 3ab87e3f8..000000000
--- a/src/common/binary.h
+++ /dev/null
@@ -1,132 +0,0 @@
-/**************************************************************************/
-/*!
- @file binary.h
- @author hathach (tinyusb.org)
-
- @section LICENSE
-
- Software License Agreement (BSD License)
-
- Copyright (c) 2013, hathach (tinyusb.org)
- All rights reserved.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions are met:
- 1. Redistributions of source code must retain the above copyright
- notice, this list of conditions and the following disclaimer.
- 2. Redistributions in binary form must reproduce the above copyright
- notice, this list of conditions and the following disclaimer in the
- documentation and/or other materials provided with the distribution.
- 3. Neither the name of the copyright holders nor the
- names of its contributors may be used to endorse or promote products
- derived from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
- EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
- DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION HOWEVER CAUSED AND
- ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- INCLUDING NEGLIGENCE OR OTHERWISE ARISING IN ANY WAY OUT OF THE USE OF THIS
- SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
- This file is part of the tinyusb stack.
-*/
-/**************************************************************************/
-
-/** \ingroup Group_Common
- * \defgroup Group_Binary Binary
- * @{ */
-
-#ifndef _TUSB_BINARY_H_
-#define _TUSB_BINARY_H_
-
-#ifdef __cplusplus
- extern "C" {
-#endif
-
-#include <stdbool.h>
-#include <stdint.h>
-#include "tusb_compiler.h"
-
-//------------- Bit manipulation -------------//
-#define BIT_(n) (1U << (n)) ///< n-th Bit
-#define BIT_SET_(x, n) ( (x) | BIT_(n) ) ///< set n-th bit of x to 1
-#define BIT_CLR_(x, n) ( (x) & (~BIT_(n)) ) ///< clear n-th bit of x
-#define BIT_TEST_(x, n) ( ((x) & BIT_(n)) ? true : false ) ///< check if n-th bit of x is 1
-
-static inline uint32_t bit_set(uint32_t value, uint8_t n) ATTR_CONST ATTR_ALWAYS_INLINE;
-static inline uint32_t bit_set(uint32_t value, uint8_t n)
-{
- return value | BIT_(n);
-}
-
-static inline uint32_t bit_clear(uint32_t value, uint8_t n) ATTR_CONST ATTR_ALWAYS_INLINE;
-static inline uint32_t bit_clear(uint32_t value, uint8_t n)
-{
- return value & (~BIT_(n));
-}
-
-static inline bool bit_test(uint32_t value, uint8_t n) ATTR_CONST ATTR_ALWAYS_INLINE;
-static inline bool bit_test(uint32_t value, uint8_t n)
-{
- return (value & BIT_(n)) ? true : false;
-}
-
-///< create a mask with n-bit lsb set to 1
-static inline uint32_t bit_mask(uint8_t n) ATTR_CONST ATTR_ALWAYS_INLINE;
-static inline uint32_t bit_mask(uint8_t n)
-{
- return (n < 32) ? ( BIT_(n) - 1 ) : UINT32_MAX;
-}
-
-static inline uint32_t bit_mask_range(uint8_t start, uint32_t end) ATTR_CONST ATTR_ALWAYS_INLINE;
-static inline uint32_t bit_mask_range(uint8_t start, uint32_t end)
-{
- return bit_mask(end+1) & ~ bit_mask(start);
-}
-
-static inline uint32_t bit_set_range(uint32_t value, uint8_t start, uint8_t end, uint32_t pattern) ATTR_CONST ATTR_ALWAYS_INLINE;
-static inline uint32_t bit_set_range(uint32_t value, uint8_t start, uint8_t end, uint32_t pattern)
-{
- return ( value & ~bit_mask_range(start, end) ) | (pattern << start);
-}
-
-
-//------------- Binary Constant -------------//
-#if defined(__GNUC__) && !defined(__CC_ARM)
-
-#define BIN8(x) ((uint8_t) (0b##x))
-#define BIN16(b1, b2) ((uint16_t) (0b##b1##b2))
-#define BIN32(b1, b2, b3, b4) ((uint32_t) (0b##b1##b2##b3##b4))
-
-#else
-
-// internal macro of B8, B16, B32
-#define _B8__(x) (((x&0x0000000FUL)?1:0) \
- +((x&0x000000F0UL)?2:0) \
- +((x&0x00000F00UL)?4:0) \
- +((x&0x0000F000UL)?8:0) \
- +((x&0x000F0000UL)?16:0) \
- +((x&0x00F00000UL)?32:0) \
- +((x&0x0F000000UL)?64:0) \
- +((x&0xF0000000UL)?128:0))
-
-#define BIN8(d) ((uint8_t) _B8__(0x##d##UL))
-#define BIN16(dmsb,dlsb) (((uint16_t)BIN8(dmsb)<<8) + BIN8(dlsb))
-#define BIN32(dmsb,db2,db3,dlsb) \
- (((uint32_t)BIN8(dmsb)<<24) \
- + ((uint32_t)BIN8(db2)<<16) \
- + ((uint32_t)BIN8(db3)<<8) \
- + BIN8(dlsb))
-#endif
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif /* _TUSB_BINARY_H_ */
-
-/** @} */
diff --git a/src/common/compiler/tusb_compiler_gcc.h b/src/common/compiler/tusb_compiler_gcc.h
index e40ad9c41..6ea89f24f 100644
--- a/src/common/compiler/tusb_compiler_gcc.h
+++ b/src/common/compiler/tusb_compiler_gcc.h
@@ -1,6 +1,6 @@
/**************************************************************************/
/*!
- @file compiler_gcc.h
+ @file tusb_compiler_gcc.h
@author hathach (tinyusb.org)
@section LICENSE
@@ -49,72 +49,34 @@
#define ALIGN_OF(x) __alignof__(x)
-/// Normally, the compiler places the objects it generates in sections like data or bss & function in text. Sometimes, however, you need additional sections, or you need certain particular variables to appear in special sections, for example to map to special hardware. The section attribute specifies that a variable (or function) lives in a particular section
-#define ATTR_SECTION(sec_name) __attribute__ (( section(#sec_name) ))
-
-/// If this attribute is used on a function declaration and a call to such a function is not eliminated through dead code elimination or other optimizations, an error that includes message is diagnosed. This is useful for compile-time checking
-#define ATTR_ERROR(Message) __attribute__ ((error(Message)))
-
-/// If this attribute is used on a function declaration and a call to such a function is not eliminated through dead code elimination or other optimizations, a warning that includes message is diagnosed. This is useful for compile-time checking
-#define ATTR_WARNING(Message) __attribute__ ((warning(Message)))
-
-/** \defgroup Group_VariableAttr Variable Attributes
- * @{ */
-
/// This attribute specifies a minimum alignment for the variable or structure field, measured in bytes
#define ATTR_ALIGNED(Bytes) __attribute__ ((aligned(Bytes)))
-/// The packed attribute specifies that a variable or structure field should have the smallest possible alignment—one byte for a variable, and one bit for a field, unless you specify a larger value with the aligned attribute
+/// Place variable in a specific section
+#define ATTR_SECTION(sec_name) __attribute__ (( section(#sec_name) ))
+
+/// The packed attribute specifies that a variable or structure field should have
+/// the smallest possible alignment—one byte for a variable, and one bit for a field.
#define ATTR_PACKED __attribute__ ((packed))
#define ATTR_PREPACKED
-/** @} */
-
-/** \defgroup Group_FuncAttr Function Attributes
- * @{ */
-
-/// Generally, functions are not inlined unless optimization is specified. For functions declared inline, this attribute inlines the function even if no optimization level is specified
+/// This attribute inlines the function even if no optimization level is specified
#define ATTR_ALWAYS_INLINE __attribute__ ((always_inline))
-/// The nonnull attribute specifies that some function parameters should be non-null pointers. f the compiler determines that a null pointer is passed in an argument slot marked as non-null, and the -Wnonnull option is enabled, a warning is issued. All pointer arguments are marked as non-null
-#define ATTR_NON_NULL __attribute__ ((nonull))
+/// The deprecated attribute results in a warning if the function is used anywhere in the source file.
+/// This is useful when identifying functions that are expected to be removed in a future version of a program.
+#define ATTR_DEPRECATED(mess) __attribute__ ((deprecated(mess)))
-/// Many functions have no effects except the return value and their return value depends only on the parameters and/or global variables. Such a function can be subject to common subexpression elimination and loop optimization just as an arithmetic operator would be. These functions should be declared with the attribute pure
-#define ATTR_PURE __attribute__ ((pure))
-
-/// \brief Many functions do not examine any values except their arguments, and have no effects except the return value. Basically this is just slightly more strict class than the pure attribute below, since function is not allowed to read global memory.
-/// Note that a function that has pointer arguments and examines the data pointed to must not be declared const. Likewise, a function that calls a non-const function usually must not be const. It does not make sense for a const function to return void
-#define ATTR_CONST __attribute__ ((const))
-
-/// The deprecated attribute results in a warning if the function is used anywhere in the source file. This is useful when identifying functions that are expected to be removed in a future version of a program. The warning also includes the location of the declaration of the deprecated function, to enable users to easily find further information about why the function is deprecated, or what they should do instead. Note that the warnings only occurs for uses
-#define ATTR_DEPRECATED __attribute__ ((deprecated))
-
-/// Same as the deprecated attribute with optional message in the warning
-#define ATTR_DEPRECATED_MESS(mess) __attribute__ ((deprecated(mess)))
-
-/// The weak attribute causes the declaration to be emitted as a weak symbol rather than a global. This is primarily useful in defining library functions that can be overridden in user code
+/// The weak attribute causes the declaration to be emitted as a weak symbol rather than a global.
+/// This is primarily useful in defining library functions that can be overridden in user code
#define ATTR_WEAK __attribute__ ((weak))
-/// The alias attribute causes the declaration to be emitted as an alias for another symbol, which must be specified
-#define ATTR_ALIAS(func) __attribute__ ((alias(#func)))
-
-/// The weakref attribute marks a declaration as a weak reference. It is equivalent with weak + alias attribute, but require function is static
-#define ATTR_WEAKREF(func) __attribute__ ((weakref(#func)))
-
-/// The warn_unused_result attribute causes a warning to be emitted if a caller of the function with this attribute does not use its return value. This is useful for functions where not checking the result is either a security problem or always a bug
+/// Warn if a caller of the function with this attribute does not use its return value.
#define ATTR_WARN_UNUSED_RESULT __attribute__ ((warn_unused_result))
-/// This attribute, attached to a function, means that code must be emitted for the function even if it appears that the function is not referenced. This is useful, for example, when the function is referenced only in inline assembly.
-#define ATTR_USED __attribute__ ((used))
-
-/// This attribute, attached to a function, means that the function is meant to be possibly unused. GCC does not produce a warning for this function.
+/// Function is meant to be possibly unused. GCC does not produce a warning for this function.
#define ATTR_UNUSED __attribute__ ((unused))
-/** @} */
-
-/** \defgroup Group_BuiltinFunc Built-in Functions
-* @{ */
-
// TODO mcu specific
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
#define __n2be(x) __builtin_bswap32(x) ///< built-in function to convert 32-bit from native to Big Endian
@@ -124,8 +86,6 @@
#define __be2n_16(u16) __n2be_16(u16)
#endif
-/** @} */
-
#ifdef __cplusplus
}
#endif
diff --git a/src/common/compiler/tusb_compiler_iar.h b/src/common/compiler/tusb_compiler_iar.h
index 1f8936859..d62a65e27 100644
--- a/src/common/compiler/tusb_compiler_iar.h
+++ b/src/common/compiler/tusb_compiler_iar.h
@@ -1,6 +1,6 @@
/**************************************************************************/
/*!
- @file compiler_iar.h
+ @file tusb_compiler_iar.h
@author hathach (tinyusb.org)
@section LICENSE
@@ -36,15 +36,6 @@
*/
/**************************************************************************/
-/** \file
- * \brief IAR Compiler
- */
-
-/** \ingroup Group_Compiler
- * \defgroup Group_IAR IAR ARM
- * @{
- */
-
#ifndef _TUSB_COMPILER_IAR_H_
#define _TUSB_COMPILER_IAR_H_
@@ -52,24 +43,17 @@
extern "C" {
#endif
-#define ALIGN_OF(x) __ALIGNOF__(x)
-#define ATTR_PREPACKED __packed
-#define ATTR_PACKED
+#define ALIGN_OF(x) __ALIGNOF__(x)
+#define ATTR_ALIGNED(bytes) _Pragma(XSTRING_(data_alignment=##bytes))
//#define ATTR_SECTION(section) _Pragma((#section))
+#define ATTR_PREPACKED __packed
+#define ATTR_PACKED
-#define ATTR_ALIGNED(bytes) _Pragma(XSTRING_(data_alignment=##bytes))
-
-#ifndef ATTR_ALWAYS_INLINE
-/// Generally, functions are not inlined unless optimization is specified. For functions declared inline, this attribute inlines the function even if no optimization level is specified
-#define ATTR_ALWAYS_INLINE error
-#endif
-
-#define ATTR_PURE // TODO IAR pure function attribute
-#define ATTR_CONST // TODO IAR const function attribute
+#define ATTR_ALWAYS_INLINE
+#define ATTR_DEPRECATED(mess)
#define ATTR_WEAK __weak
#define ATTR_WARN_UNUSED_RESULT
-#define ATTR_USED
#define ATTR_UNUSED
// built-in function to convert 32-bit Big-Endian to Little-Endian
@@ -86,4 +70,3 @@
#endif /* _TUSB_COMPILER_IAR_H_ */
-/** @} */
diff --git a/src/common/tusb_common.h b/src/common/tusb_common.h
index 3c5402e59..22eddd913 100644
--- a/src/common/tusb_common.h
+++ b/src/common/tusb_common.h
@@ -47,32 +47,12 @@
extern "C" {
#endif
-//--------------------------------------------------------------------+
-// INCLUDES
-//--------------------------------------------------------------------+
-
-//------------- Standard Header -------------//
-#include <stdbool.h>
-#include <stdint.h>
-#include <stddef.h>
-#include <string.h>
-#include <stdio.h>
-
-//------------- TUSB Option Header -------------//
-#include "tusb_option.h"
-
-//------------- Common Header -------------//
-#include "tusb_compiler.h"
-#include "tusb_verify.h"
-#include "binary.h"
-#include "tusb_error.h"
-#include "tusb_timeout.h"
-#include "tusb_types.h"
-
-//--------------------------------------------------------------------+
-// MACROS
+ //--------------------------------------------------------------------+
+// Macros Helper
//--------------------------------------------------------------------+
#define TU_ARRAY_SZIE(_arr) ( sizeof(_arr) / sizeof(_arr[0]) )
+#define TU_MIN(_x, _y) ( (_x) < (_y) ) ? (_x) : (_y) )
+#define TU_MAX(_x, _y) ( (_x) > (_y) ) ? (_x) : (_y) )
#define U16_HIGH_U8(u16) ((uint8_t) (((u16) >> 8) & 0x00ff))
#define U16_LOW_U8(u16) ((uint8_t) ((u16) & 0x00ff))
@@ -87,54 +67,77 @@
#define U32_TO_U8S_BE(u32) U32_B1_U8(u32), U32_B2_U8(u32), U32_B3_U8(u32), U32_B4_U8(u32)
#define U32_TO_U8S_LE(u32) U32_B4_U8(u32), U32_B3_U8(u32), U32_B2_U8(u32), U32_B1_U8(u32)
+//------------- Bit -------------//
+#define TU_BIT(n) (1U << (n)) ///< n-th Bit
+#define TU_BIT_SET(x, n) ( (x) | TU_BIT(n) ) ///< set n-th bit of x to 1
+#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)) )
-#ifndef __n2be_16
-#define __n2be_16(u16) ((uint16_t) ((U16_LOW_U8(u16) << 8) | U16_HIGH_U8(u16)) )
-#define __be2n_16(u16) __n2be_16(u16)
-#endif
+//------------- Binary constant -------------//
+#if defined(__GNUC__) && !defined(__CC_ARM)
+
+#define TU_BIN8(x) ((uint8_t) (0b##x))
+#define TU_BIN16(b1, b2) ((uint16_t) (0b##b1##b2))
+#define TU_BIN32(b1, b2, b3, b4) ((uint32_t) (0b##b1##b2##b3##b4))
+
+#else
+// internal macro of B8, B16, B32
+#define _B8__(x) (((x&0x0000000FUL)?1:0) \
+ +((x&0x000000F0UL)?2:0) \
+ +((x&0x00000F00UL)?4:0) \
+ +((x&0x0000F000UL)?8:0) \
+ +((x&0x000F0000UL)?16:0) \
+ +((x&0x00F00000UL)?32:0) \
+ +((x&0x0F000000UL)?64:0) \
+ +((x&0xF0000000UL)?128:0))
+
+#define TU_BIN8(d) ((uint8_t) _B8__(0x##d##UL))
+#define TU_BIN16(dmsb,dlsb) (((uint16_t)TU_BIN8(dmsb)<<8) + TU_BIN8(dlsb))
+#define TU_BIN32(dmsb,db2,db3,dlsb) \
+ (((uint32_t)TU_BIN8(dmsb)<<24) \
+ + ((uint32_t)TU_BIN8(db2)<<16) \
+ + ((uint32_t)TU_BIN8(db3)<<8) \
+ + TU_BIN8(dlsb))
+#endif
// for declaration of reserved field, make use of _TU_COUNTER_
#define TU_RESERVED XSTRING_CONCAT_(reserved, _TU_COUNTER_)
-/*------------------------------------------------------------------*/
-/* Count number of arguments of __VA_ARGS__
- * - reference https://groups.google.com/forum/#!topic/comp.std.c/d-6Mj5Lko_s
- * - _GET_NTH_ARG() takes args >= N (64) but only expand to Nth one (64th)
- * - _RSEQ_N() is reverse sequential to N to add padding to have
- * Nth position is the same as the number of arguments
- * - ##__VA_ARGS__ is used to deal with 0 paramerter (swallows comma)
- *------------------------------------------------------------------*/
-#ifndef VA_ARGS_NUM_
+//--------------------------------------------------------------------+
+// INCLUDES
+//--------------------------------------------------------------------+
-#define VA_ARGS_NUM_(...) NARG_(_0, ##__VA_ARGS__,_RSEQ_N())
-#define NARG_(...) _GET_NTH_ARG(__VA_ARGS__)
-#define _GET_NTH_ARG( \
- _1, _2, _3, _4, _5, _6, _7, _8, _9,_10, \
- _11,_12,_13,_14,_15,_16,_17,_18,_19,_20, \
- _21,_22,_23,_24,_25,_26,_27,_28,_29,_30, \
- _31,_32,_33,_34,_35,_36,_37,_38,_39,_40, \
- _41,_42,_43,_44,_45,_46,_47,_48,_49,_50, \
- _51,_52,_53,_54,_55,_56,_57,_58,_59,_60, \
- _61,_62,_63,N,...) N
-#define _RSEQ_N() \
- 62,61,60, \
- 59,58,57,56,55,54,53,52,51,50, \
- 49,48,47,46,45,44,43,42,41,40, \
- 39,38,37,36,35,34,33,32,31,30, \
- 29,28,27,26,25,24,23,22,21,20, \
- 19,18,17,16,15,14,13,12,11,10, \
- 9,8,7,6,5,4,3,2,1,0
-#endif
+// Standard Headers
+#include <stdbool.h>
+#include <stdint.h>
+#include <stddef.h>
+#include <string.h>
+#include <stdio.h>
+
+// Tinyusb Common Headers
+#include "tusb_option.h"
+#include "tusb_compiler.h"
+#include "tusb_verify.h"
+//#include "binary.h"
+#include "tusb_error.h"
+#include "tusb_timeout.h"
+#include "tusb_types.h"
//--------------------------------------------------------------------+
// 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)))
@@ -167,60 +170,26 @@ static inline uint16_t tu_u16_le2be(uint16_t u16)
return ((uint16_t)(tu_u16_low(u16) << 8)) | tu_u16_high(u16);
}
-//------------- Min -------------//
-static inline uint8_t tu_min8(uint8_t x, uint8_t y)
-{
- return (x < y) ? x : y;
-}
+// Min
+static inline uint8_t tu_min8(uint8_t x, uint8_t y) { return (x < y) ? x : y; }
+static inline uint16_t tu_min16(uint16_t x, uint16_t y) { return (x < y) ? x : y; }
+static inline uint32_t tu_min32(uint32_t x, uint32_t y) { return (x < y) ? x : y; }
-static inline uint16_t tu_min16(uint16_t x, uint16_t y)
-{
- return (x < y) ? x : y;
-}
+// Max
+static inline uint8_t tu_max8(uint8_t x, uint8_t y) { return (x > y) ? x : y; }
+static inline uint16_t tu_max16(uint16_t x, uint16_t y) { return (x > y) ? x : y; }
+static inline uint32_t tu_max32(uint32_t x, uint32_t y) { return (x > y) ? x : y; }
-static inline uint32_t tu_min32(uint32_t x, uint32_t y)
-{
- return (x < y) ? x : y;
-}
+// Align
+static inline uint32_t tu_align32 (uint32_t value) { return (value & 0xFFFFFFE0UL); }
+static inline uint32_t tu_align16 (uint32_t value) { return (value & 0xFFFFFFF0UL); }
+static inline uint32_t tu_align_n (uint32_t alignment, uint32_t value) { return value & ((uint32_t) ~(alignment-1)); }
+static inline uint32_t tu_align4k (uint32_t value) { return (value & 0xFFFFF000UL); }
-//------------- Max -------------//
-static inline uint32_t tu_max32(uint32_t x, uint32_t y)
-{
- return (x > y) ? x : y;
-}
-
-//------------- Align -------------//
-static inline uint32_t tu_align32 (uint32_t value)
-{
- return (value & 0xFFFFFFE0UL);
-}
-
-static inline uint32_t tu_align16 (uint32_t value)
-{
- return (value & 0xFFFFFFF0UL);
-}
-
-static inline uint32_t tu_align_n (uint32_t alignment, uint32_t value)
-{
- return value & ((uint32_t) ~(alignment-1));
-}
-
-static inline uint32_t tu_align4k (uint32_t value)
-{
- return (value & 0xFFFFF000UL);
-}
-
-static inline uint32_t tu_offset4k(uint32_t value)
-{
- return (value & 0xFFFUL);
-}
+static inline uint32_t tu_offset4k(uint32_t value) { return (value & 0xFFFUL); }
//------------- Mathematics -------------//
-static inline uint32_t tu_abs(int32_t value)
-{
- return (value < 0) ? (-value) : value;
-}
-
+static inline uint32_t tu_abs(int32_t value) { return (value < 0) ? (-value) : value; }
/// inclusive range checking
static inline bool tu_within(uint32_t lower, uint32_t value, uint32_t upper)
@@ -228,10 +197,11 @@ static inline bool tu_within(uint32_t lower, uint32_t value, uint32_t upper)
return (lower <= value) && (value <= upper);
}
+// log2 of a value is its MSB's position
// TODO use clz
static inline uint8_t tu_log2(uint32_t value)
{
- uint8_t result = 0; // log2 of a value is its MSB's position
+ uint8_t result = 0;
while (value >>= 1)
{
@@ -240,6 +210,41 @@ static inline uint8_t tu_log2(uint32_t value)
return result;
}
+// Bit
+static inline uint32_t tu_bit_set(uint32_t value, uint8_t n) { return value | TU_BIT(n); }
+static inline uint32_t tu_bit_clear(uint32_t value, uint8_t n) { return value & (~TU_BIT(n)); }
+static inline bool tu_bit_test(uint32_t value, uint8_t n) { return (value & TU_BIT(n)) ? true : false; }
+
+/*------------------------------------------------------------------*/
+/* Count number of arguments of __VA_ARGS__
+ * - reference https://groups.google.com/forum/#!topic/comp.std.c/d-6Mj5Lko_s
+ * - _GET_NTH_ARG() takes args >= N (64) but only expand to Nth one (64th)
+ * - _RSEQ_N() is reverse sequential to N to add padding to have
+ * Nth position is the same as the number of arguments
+ * - ##__VA_ARGS__ is used to deal with 0 paramerter (swallows comma)
+ *------------------------------------------------------------------*/
+#ifndef VA_ARGS_NUM_
+
+#define VA_ARGS_NUM_(...) NARG_(_0, ##__VA_ARGS__,_RSEQ_N())
+#define NARG_(...) _GET_NTH_ARG(__VA_ARGS__)
+#define _GET_NTH_ARG( \
+ _1, _2, _3, _4, _5, _6, _7, _8, _9,_10, \
+ _11,_12,_13,_14,_15,_16,_17,_18,_19,_20, \
+ _21,_22,_23,_24,_25,_26,_27,_28,_29,_30, \
+ _31,_32,_33,_34,_35,_36,_37,_38,_39,_40, \
+ _41,_42,_43,_44,_45,_46,_47,_48,_49,_50, \
+ _51,_52,_53,_54,_55,_56,_57,_58,_59,_60, \
+ _61,_62,_63,N,...) N
+#define _RSEQ_N() \
+ 62,61,60, \
+ 59,58,57,56,55,54,53,52,51,50, \
+ 49,48,47,46,45,44,43,42,41,40, \
+ 39,38,37,36,35,34,33,32,31,30, \
+ 29,28,27,26,25,24,23,22,21,20, \
+ 19,18,17,16,15,14,13,12,11,10, \
+ 9,8,7,6,5,4,3,2,1,0
+#endif
+
#ifdef __cplusplus
}
#endif
diff --git a/src/common/tusb_error.h b/src/common/tusb_error.h
index 677fc44e4..7e67dd0a0 100644
--- a/src/common/tusb_error.h
+++ b/src/common/tusb_error.h
@@ -57,44 +57,13 @@
ENTRY(TUSB_ERROR_INVALID_PARA )\
ENTRY(TUSB_ERROR_DEVICE_NOT_READY )\
ENTRY(TUSB_ERROR_INTERFACE_IS_BUSY )\
- ENTRY(TUSB_ERROR_HCD_FAILED )\
ENTRY(TUSB_ERROR_HCD_OPEN_PIPE_FAILED )\
- ENTRY(TUSB_ERROR_USBH_MOUNT_DEVICE_NOT_RESPOND )\
- ENTRY(TUSB_ERROR_USBH_MOUNT_CONFIG_DESC_TOO_LONG )\
- ENTRY(TUSB_ERROR_USBH_DESCRIPTOR_CORRUPTED )\
- ENTRY(TUSB_ERROR_USBH_XFER_STALLED )\
- ENTRY(TUSB_ERROR_USBH_XFER_FAILED )\
ENTRY(TUSB_ERROR_OSAL_TIMEOUT )\
- ENTRY(TUSB_ERROR_OSAL_WAITING ) /* only used by OSAL_NONE in the subtask */ \
- ENTRY(TUSB_ERROR_OSAL_TASK_FAILED )\
- ENTRY(TUSB_ERROR_OSAL_QUEUE_FAILED )\
- ENTRY(TUSB_ERROR_OSAL_SEMAPHORE_FAILED )\
- ENTRY(TUSB_ERROR_OSAL_MUTEX_FAILED )\
- ENTRY(TUSB_ERROR_EHCI_NOT_ENOUGH_QTD )\
- ENTRY(TUSB_ERROR_HIDD_DESCRIPTOR_INTERFACE )\
- ENTRY(TUSB_ERROR_HIDH_NOT_SUPPORTED_PROTOCOL )\
- ENTRY(TUSB_ERROR_HIDH_NOT_SUPPORTED_SUBCLASS )\
- ENTRY(TUSB_ERROR_CDC_UNSUPPORTED_SUBCLASS )\
- ENTRY(TUSB_ERROR_CDC_UNSUPPORTED_PROTOCOL )\
ENTRY(TUSB_ERROR_CDCH_DEVICE_NOT_MOUNTED )\
- ENTRY(TUSB_ERROR_MSC_UNSUPPORTED_PROTOCOL )\
- ENTRY(TUSB_ERROR_MSCH_UNKNOWN_SCSI_COMMAND )\
ENTRY(TUSB_ERROR_MSCH_DEVICE_NOT_MOUNTED )\
- ENTRY(TUSB_ERROR_HUB_FEATURE_NOT_SUPPORTED )\
- ENTRY(TUSB_ERROR_DESCRIPTOR_CORRUPTED )\
- ENTRY(TUSB_ERROR_DCD_FAILED )\
- ENTRY(TUSB_ERROR_DCD_CONTROL_REQUEST_NOT_SUPPORT )\
- ENTRY(TUSB_ERROR_DCD_NOT_ENOUGH_QTD )\
- ENTRY(TUSB_ERROR_DCD_OPEN_PIPE_FAILED )\
- ENTRY(TUSB_ERROR_DCD_EDPT_XFER )\
- ENTRY(TUSB_ERROR_NOT_SUPPORTED_YET )\
- ENTRY(TUSB_ERROR_USBD_DEVICE_NOT_CONFIGURED )\
+ ENTRY(TUSB_ERROR_NOT_SUPPORTED )\
ENTRY(TUSB_ERROR_NOT_ENOUGH_MEMORY )\
ENTRY(TUSB_ERROR_FAILED )\
- \
- ENTRY(ERR_TUD_INVALID_DESCRIPTOR) \
- ENTRY(ERR_TUD_EDPT_OPEN_FAILED) \
-
/// \brief Error Code returned
typedef enum
diff --git a/src/common/tusb_fifo.c b/src/common/tusb_fifo.c
index 2acd4b696..8902f5a4e 100644
--- a/src/common/tusb_fifo.c
+++ b/src/common/tusb_fifo.c
@@ -48,9 +48,7 @@ static void tu_fifo_lock(tu_fifo_t *f)
{
if (f->mutex)
{
- uint32_t err;
- (void) err;
- osal_mutex_lock(f->mutex, OSAL_TIMEOUT_WAIT_FOREVER, &err);
+ osal_mutex_lock(f->mutex, OSAL_TIMEOUT_WAIT_FOREVER);
}
}
diff --git a/src/common/tusb_fifo.h b/src/common/tusb_fifo.h
index 61d92b602..e88c2ac6d 100644
--- a/src/common/tusb_fifo.h
+++ b/src/common/tusb_fifo.h
@@ -67,13 +67,12 @@ typedef struct
uint8_t* buffer ; ///< buffer pointer
uint16_t depth ; ///< max items
uint16_t item_size ; ///< size of each item
+ bool overwritable ;
volatile uint16_t count ; ///< number of items in queue
volatile uint16_t wr_idx ; ///< write pointer
volatile uint16_t rd_idx ; ///< read pointer
- bool overwritable ;
-
#if CFG_FIFO_MUTEX
tu_fifo_mutex_t mutex;
#endif
diff --git a/src/common/tusb_timeout.h b/src/common/tusb_timeout.h
index 9d80f5fe7..06d5ff5bf 100644
--- a/src/common/tusb_timeout.h
+++ b/src/common/tusb_timeout.h
@@ -79,16 +79,6 @@ static inline void tu_timeout_restart(tu_timeout_t* tt)
tt->start = tusb_hal_millis();
}
-
-static inline void tu_timeout_wait(uint32_t msec)
-{
- tu_timeout_t tt;
- tu_timeout_set(&tt, msec);
-
- // blocking delay
- while ( !tu_timeout_expired(&tt) ) { }
-}
-
#ifdef __cplusplus
}
#endif
diff --git a/src/common/tusb_types.h b/src/common/tusb_types.h
index b11481b18..9a6d5a520 100644
--- a/src/common/tusb_types.h
+++ b/src/common/tusb_types.h
@@ -170,9 +170,9 @@ typedef enum
}misc_protocol_type_t;
enum {
- TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP = BIT_(5),
- TUSB_DESC_CONFIG_ATT_SELF_POWER = BIT_(6),
- TUSB_DESC_CONFIG_ATT_BUS_POWER = BIT_(7)
+ TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP = TU_BIT(5),
+ TUSB_DESC_CONFIG_ATT_SELF_POWER = TU_BIT(6),
+ TUSB_DESC_CONFIG_ATT_BUS_POWER = TU_BIT(7)
};
#define TUSB_DESC_CONFIG_POWER_MA(x) ((x)/2)
@@ -181,14 +181,8 @@ enum {
typedef enum
{
TUSB_DEVICE_STATE_UNPLUG = 0 ,
- TUSB_DEVICE_STATE_ADDRESSED ,
TUSB_DEVICE_STATE_CONFIGURED ,
TUSB_DEVICE_STATE_SUSPENDED ,
-
- TUSB_DEVICE_STATE_REMOVING ,
- TUSB_DEVICE_STATE_SAFE_REMOVE ,
-
- TUSB_DEVICE_STATE_INVALID_PARAMETER
}tusb_device_state_t;
typedef enum
@@ -358,6 +352,7 @@ typedef struct ATTR_PACKED{
uint8_t type : 2; ///< Request type tusb_request_type_t.
uint8_t direction : 1; ///< Direction type. tusb_dir_t
} bmRequestType_bit;
+
uint8_t bmRequestType;
};
@@ -380,38 +375,39 @@ static inline uint8_t bm_request_type(uint8_t direction, uint8_t type, uint8_t r
//--------------------------------------------------------------------+
// Get direction from Endpoint address
-static inline tusb_dir_t edpt_dir(uint8_t addr)
+static inline tusb_dir_t tu_edpt_dir(uint8_t addr)
{
return (addr & TUSB_DIR_IN_MASK) ? TUSB_DIR_IN : TUSB_DIR_OUT;
}
// Get Endpoint number from address
-static inline uint8_t edpt_number(uint8_t addr)
+static inline uint8_t tu_edpt_number(uint8_t addr)
{
return addr & (~TUSB_DIR_IN_MASK);
}
-static inline uint8_t edpt_addr(uint8_t num, tusb_dir_t dir)
+static inline uint8_t tu_edpt_addr(uint8_t num, uint8_t dir)
{
- return num | (dir == TUSB_DIR_IN ? TUSB_DIR_IN_MASK : 0);
+ return num | (dir ? TUSB_DIR_IN_MASK : 0);
}
//--------------------------------------------------------------------+
// Descriptor helper
//--------------------------------------------------------------------+
-static inline uint8_t const * descriptor_next(uint8_t const p_desc[])
+static inline uint8_t const * tu_desc_next(void const* desc)
{
- return p_desc + p_desc[DESC_OFFSET_LEN];
+ uint8_t const* desc8 = (uint8_t const*) desc;
+ return desc8 + desc8[DESC_OFFSET_LEN];
}
-static inline uint8_t descriptor_type(uint8_t const p_desc[])
+static inline uint8_t tu_desc_type(void const* desc)
{
- return p_desc[DESC_OFFSET_TYPE];
+ return ((uint8_t const*) desc)[DESC_OFFSET_TYPE];
}
-static inline uint8_t descriptor_len(uint8_t const p_desc[])
+static inline uint8_t tu_desc_len(void const* desc)
{
- return p_desc[DESC_OFFSET_LEN];
+ return ((uint8_t const*) desc)[DESC_OFFSET_LEN];
}
// Length of the string descriptors in bytes with slen characters