rename subfolder source to src
This commit is contained in:
132
src/common/binary.h
Normal file
132
src/common/binary.h
Normal file
@@ -0,0 +1,132 @@
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@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_ */
|
||||
|
||||
/** @} */
|
||||
137
src/common/compiler/tusb_compiler_gcc.h
Normal file
137
src/common/compiler/tusb_compiler_gcc.h
Normal file
@@ -0,0 +1,137 @@
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@file compiler_gcc.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_Compiler
|
||||
* \defgroup Group_GCC GNU GCC
|
||||
* @{ */
|
||||
|
||||
#ifndef _TUSB_COMPILER_GCC_H_
|
||||
#define _TUSB_COMPILER_GCC_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#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
|
||||
#define ATTR_PACKED __attribute__ ((packed))
|
||||
|
||||
#define ATTR_PREPACKED
|
||||
|
||||
#define ATTR_PACKED_STRUCT(x) x __attribute__ ((packed))
|
||||
/** @} */
|
||||
|
||||
/** \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
|
||||
#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))
|
||||
|
||||
/// 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
|
||||
#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
|
||||
#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.
|
||||
#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
|
||||
#define __be2n(x) __n2be(x) ///< built-in function to convert 32-bit from Big Endian to native
|
||||
|
||||
#define __n2be_16(u16) __builtin_bswap16(u16)
|
||||
#define __be2n_16(u16) __n2be_16(u16)
|
||||
#endif
|
||||
|
||||
/** @} */
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* _TUSB_COMPILER_GCC_H_ */
|
||||
|
||||
/// @}
|
||||
91
src/common/compiler/tusb_compiler_iar.h
Normal file
91
src/common/compiler/tusb_compiler_iar.h
Normal file
@@ -0,0 +1,91 @@
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@file compiler_iar.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.
|
||||
*/
|
||||
/**************************************************************************/
|
||||
|
||||
/** \file
|
||||
* \brief IAR Compiler
|
||||
*/
|
||||
|
||||
/** \ingroup Group_Compiler
|
||||
* \defgroup Group_IAR IAR ARM
|
||||
* @{
|
||||
*/
|
||||
|
||||
#ifndef _TUSB_COMPILER_IAR_H_
|
||||
#define _TUSB_COMPILER_IAR_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define ALIGN_OF(x) __ALIGNOF__(x)
|
||||
|
||||
#define ATTR_PACKED_STRUCT(x) __packed x
|
||||
#define ATTR_PREPACKED __packed
|
||||
#define ATTR_PACKED
|
||||
//#define ATTR_SECTION(section) _Pragma((#section))
|
||||
|
||||
#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_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
|
||||
//#if __LITTLE_ENDIAN__
|
||||
#define __be2n __REV
|
||||
#define __n2be __be2n
|
||||
|
||||
#define __n2be_16(u16) ((uint16_t) __REV16(u16))
|
||||
#define __be2n_16(u16) __n2be_16(u16)
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* _TUSB_COMPILER_IAR_H_ */
|
||||
|
||||
/** @} */
|
||||
76
src/common/timeout_timer.h
Normal file
76
src/common/timeout_timer.h
Normal file
@@ -0,0 +1,76 @@
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@file timeout_timer.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 Common Files
|
||||
* \defgroup Group_TimeoutTimer timeout timer
|
||||
* @{ */
|
||||
|
||||
|
||||
#ifndef _TUSB_TIMEOUT_TTIMER_H_
|
||||
#define _TUSB_TIMEOUT_TTIMER_H_
|
||||
|
||||
#include "tusb_compiler.h"
|
||||
#include "tusb_hal.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
uint32_t start;
|
||||
uint32_t interval;
|
||||
}timeout_timer_t;
|
||||
|
||||
static inline void timeout_set(timeout_timer_t* tt, uint32_t msec)
|
||||
{
|
||||
tt->interval = msec;
|
||||
tt->start = tusb_hal_millis();
|
||||
}
|
||||
|
||||
static inline bool timeout_expired(timeout_timer_t* tt)
|
||||
{
|
||||
return ( tusb_hal_millis() - tt->start ) >= tt->interval;
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* _TUSB_TIMEOUT_TTIMER_H_ */
|
||||
|
||||
/** @} */
|
||||
273
src/common/tusb_common.h
Normal file
273
src/common/tusb_common.h
Normal file
@@ -0,0 +1,273 @@
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@file tusb_common.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_CommonH common.h
|
||||
* @{ */
|
||||
|
||||
#ifndef _TUSB_COMMON_H_
|
||||
#define _TUSB_COMMON_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
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"
|
||||
|
||||
//------------- General Header -------------//
|
||||
#include "tusb_compiler.h"
|
||||
#include "tusb_verify.h"
|
||||
#include "binary.h"
|
||||
#include "tusb_error.h"
|
||||
#include "tusb_hal.h"
|
||||
#include "tusb_fifo.h"
|
||||
|
||||
//------------- TUSB Header -------------//
|
||||
#include "tusb_types.h"
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// MACROS
|
||||
//--------------------------------------------------------------------+
|
||||
#define MAX_OF(a, b) ( (a) > (b) ? (a) : (b) )
|
||||
#define MIN_OF(a, b) ( (a) < (b) ? (a) : (b) )
|
||||
|
||||
#define U16_HIGH_U8(u16) ((uint8_t) (((u16) >> 8) & 0x00ff))
|
||||
#define U16_LOW_U8(u16) ((uint8_t) ((u16) & 0x00ff))
|
||||
#define U16_TO_U8S_BE(u16) U16_HIGH_U8(u16), U16_LOW_U8(u16)
|
||||
#define U16_TO_U8S_LE(u16) U16_LOW_U8(u16), U16_HIGH_U8(u16)
|
||||
|
||||
#define U32_B1_U8(u32) ((uint8_t) (((u32) >> 24) & 0x000000ff)) // MSB
|
||||
#define U32_B2_U8(u32) ((uint8_t) (((u32) >> 16) & 0x000000ff))
|
||||
#define U32_B3_U8(u32) ((uint8_t) (((u32) >> 8) & 0x000000ff))
|
||||
#define U32_B4_U8(u32) ((uint8_t) ((u32) & 0x000000ff)) // LSB
|
||||
|
||||
#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)
|
||||
|
||||
//------------- 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
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// INLINE FUNCTION
|
||||
//--------------------------------------------------------------------+
|
||||
#define memclr_(buffer, size) memset((buffer), 0, (size))
|
||||
#define varclr_(_var) memclr_(_var, sizeof(*(_var)))
|
||||
#define arrclr_(_arr) memclr_(_arr, sizeof(_arr))
|
||||
|
||||
#define arrcount_(_arr) ( sizeof(_arr) / sizeof(_arr[0]) )
|
||||
|
||||
static inline uint8_t const * descriptor_next(uint8_t const p_desc[])
|
||||
{
|
||||
return p_desc + p_desc[DESCRIPTOR_OFFSET_LENGTH];
|
||||
}
|
||||
|
||||
static inline uint8_t descriptor_typeof(uint8_t const p_desc[])
|
||||
{
|
||||
return p_desc[DESCRIPTOR_OFFSET_TYPE];
|
||||
}
|
||||
|
||||
//------------- Conversion -------------//
|
||||
/// form an uint32_t from 4 x uint8_t
|
||||
static inline uint32_t u32_from_u8(uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4) ATTR_ALWAYS_INLINE ATTR_CONST;
|
||||
static inline uint32_t u32_from_u8(uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4)
|
||||
{
|
||||
return ( ((uint32_t) b1) << 24) + ( ((uint32_t) b2) << 16) + ( ((uint32_t) b3) << 8) + b4;
|
||||
}
|
||||
|
||||
static inline uint8_t u16_high_u8(uint16_t u16) ATTR_CONST ATTR_ALWAYS_INLINE;
|
||||
static inline uint8_t u16_high_u8(uint16_t u16)
|
||||
{
|
||||
return (uint8_t) ( ((uint16_t) (u16 >> 8)) & 0x00ff);
|
||||
}
|
||||
|
||||
static inline uint8_t u16_low_u8(uint16_t u16) ATTR_CONST ATTR_ALWAYS_INLINE;
|
||||
static inline uint8_t u16_low_u8(uint16_t u16)
|
||||
{
|
||||
return (uint8_t) (u16 & 0x00ff);
|
||||
}
|
||||
|
||||
static inline uint16_t u16_le2be(uint16_t u16) ATTR_CONST ATTR_ALWAYS_INLINE;
|
||||
static inline uint16_t u16_le2be(uint16_t u16)
|
||||
{
|
||||
return ((uint16_t)(u16_low_u8(u16) << 8)) | u16_high_u8(u16);
|
||||
}
|
||||
|
||||
//------------- Min -------------//
|
||||
static inline uint8_t min8_of(uint8_t x, uint8_t y) ATTR_ALWAYS_INLINE ATTR_CONST;
|
||||
static inline uint8_t min8_of(uint8_t x, uint8_t y)
|
||||
{
|
||||
return (x < y) ? x : y;
|
||||
}
|
||||
|
||||
static inline uint16_t min16_of(uint16_t x, uint16_t y) ATTR_ALWAYS_INLINE ATTR_CONST;
|
||||
static inline uint16_t min16_of(uint16_t x, uint16_t y)
|
||||
{
|
||||
return (x < y) ? x : y;
|
||||
}
|
||||
|
||||
static inline uint32_t min32_of(uint32_t x, uint32_t y) ATTR_ALWAYS_INLINE ATTR_CONST;
|
||||
static inline uint32_t min32_of(uint32_t x, uint32_t y)
|
||||
{
|
||||
return (x < y) ? x : y;
|
||||
}
|
||||
|
||||
//------------- Max -------------//
|
||||
static inline uint32_t max32_of(uint32_t x, uint32_t y) ATTR_ALWAYS_INLINE ATTR_CONST;
|
||||
static inline uint32_t max32_of(uint32_t x, uint32_t y)
|
||||
{
|
||||
return (x > y) ? x : y;
|
||||
}
|
||||
|
||||
static inline uint16_t max16_of(uint16_t x, uint16_t y) ATTR_ALWAYS_INLINE ATTR_CONST;
|
||||
static inline uint16_t max16_of(uint16_t x, uint16_t y)
|
||||
{
|
||||
return (x > y) ? x : y;
|
||||
}
|
||||
|
||||
//------------- Align -------------//
|
||||
static inline uint32_t align32 (uint32_t value) ATTR_ALWAYS_INLINE ATTR_CONST;
|
||||
static inline uint32_t align32 (uint32_t value)
|
||||
{
|
||||
return (value & 0xFFFFFFE0UL);
|
||||
}
|
||||
|
||||
static inline uint32_t align16 (uint32_t value) ATTR_ALWAYS_INLINE ATTR_CONST;
|
||||
static inline uint32_t align16 (uint32_t value)
|
||||
{
|
||||
return (value & 0xFFFFFFF0UL);
|
||||
}
|
||||
|
||||
static inline uint32_t align_n (uint32_t alignment, uint32_t value) ATTR_ALWAYS_INLINE ATTR_CONST;
|
||||
static inline uint32_t align_n (uint32_t alignment, uint32_t value)
|
||||
{
|
||||
return value & ((uint32_t) ~(alignment-1));
|
||||
}
|
||||
|
||||
static inline uint32_t align4k (uint32_t value) ATTR_ALWAYS_INLINE ATTR_CONST;
|
||||
static inline uint32_t align4k (uint32_t value)
|
||||
{
|
||||
return (value & 0xFFFFF000UL);
|
||||
}
|
||||
|
||||
static inline uint32_t offset4k(uint32_t value) ATTR_ALWAYS_INLINE ATTR_CONST;
|
||||
static inline uint32_t offset4k(uint32_t value)
|
||||
{
|
||||
return (value & 0xFFFUL);
|
||||
}
|
||||
|
||||
//------------- Mathematics -------------//
|
||||
static inline uint32_t abs_of(int32_t value) ATTR_ALWAYS_INLINE ATTR_CONST;
|
||||
static inline uint32_t abs_of(int32_t value)
|
||||
{
|
||||
return (value < 0) ? (-value) : value;
|
||||
}
|
||||
|
||||
|
||||
/// inclusive range checking
|
||||
static inline bool is_in_range(uint32_t lower, uint32_t value, uint32_t upper) ATTR_ALWAYS_INLINE ATTR_CONST;
|
||||
static inline bool is_in_range(uint32_t lower, uint32_t value, uint32_t upper)
|
||||
{
|
||||
return (lower <= value) && (value <= upper);
|
||||
}
|
||||
|
||||
/// exclusive range checking
|
||||
static inline bool is_in_range_exclusive(uint32_t lower, uint32_t value, uint32_t upper) ATTR_ALWAYS_INLINE ATTR_CONST;
|
||||
static inline bool is_in_range_exclusive(uint32_t lower, uint32_t value, uint32_t upper)
|
||||
{
|
||||
return (lower < value) && (value < upper);
|
||||
}
|
||||
|
||||
// TODO use clz
|
||||
static inline uint8_t log2_of(uint32_t value) ATTR_ALWAYS_INLINE ATTR_CONST;
|
||||
static inline uint8_t log2_of(uint32_t value)
|
||||
{
|
||||
uint8_t result = 0; // log2 of a value is its MSB's position
|
||||
|
||||
while (value >>= 1)
|
||||
{
|
||||
result++;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// return the number of set bits in value
|
||||
static inline uint8_t cardinality_of(uint32_t value) ATTR_ALWAYS_INLINE ATTR_CONST;
|
||||
static inline uint8_t cardinality_of(uint32_t value)
|
||||
{
|
||||
// Brian Kernighan's method goes through as many iterations as there are set bits. So if we have a 32-bit word with only
|
||||
// the high bit set, then it will only go once through the loop
|
||||
// Published in 1988, the C Programming Language 2nd Ed. (by Brian W. Kernighan and Dennis M. Ritchie)
|
||||
// mentions this in exercise 2-9. On April 19, 2006 Don Knuth pointed out to me that this method
|
||||
// "was first published by Peter Wegner in CACM 3 (1960), 322. (Also discovered independently by Derrick Lehmer and
|
||||
// published in 1964 in a book edited by Beckenbach.)"
|
||||
uint8_t count;
|
||||
for (count = 0; value; count++)
|
||||
{
|
||||
value &= value - 1; // clear the least significant bit set
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* _TUSB_COMMON_H_ */
|
||||
|
||||
/** @} */
|
||||
83
src/common/tusb_compiler.h
Normal file
83
src/common/tusb_compiler.h
Normal file
@@ -0,0 +1,83 @@
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@file compiler.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_Compiler Compiler
|
||||
* \brief Group_Compiler brief
|
||||
* @{ */
|
||||
|
||||
#ifndef _TUSB_COMPILER_H_
|
||||
#define _TUSB_COMPILER_H_
|
||||
|
||||
#define STRING_(x) #x ///< stringify without expand
|
||||
#define XSTRING_(x) STRING_(x) ///< expand then stringify
|
||||
#define STRING_CONCAT_(a, b) a##b ///< concat without expand
|
||||
#define XSTRING_CONCAT_(a, b) STRING_CONCAT_(a, b) ///< expand then concat
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Compile-time Assert (use VERIFY_STATIC to avoid name conflict)
|
||||
//--------------------------------------------------------------------+
|
||||
#if defined(__ICCARM__) || (__STDC_VERSION__ >= 201112L )
|
||||
#define VERIFY_STATIC static_assert
|
||||
#else
|
||||
#if defined __COUNTER__ && __COUNTER__ != __COUNTER__
|
||||
#define _VERIFY_COUNTER __COUNTER__
|
||||
#else
|
||||
#define _VERIFY_COUNTER __LINE__
|
||||
#endif
|
||||
|
||||
#define VERIFY_STATIC(const_expr, _mess) enum { XSTRING_CONCAT_(_verify_static_, _VERIFY_COUNTER) = 1/(!!(const_expr)) }
|
||||
#endif
|
||||
|
||||
// allow debugger to watch any module-wide variables anywhere
|
||||
#if CFG_TUSB_DEBUG
|
||||
#define STATIC_VAR
|
||||
#else
|
||||
#define STATIC_VAR static
|
||||
#endif
|
||||
|
||||
|
||||
#if defined(__GNUC__)
|
||||
#include "compiler/tusb_compiler_gcc.h"
|
||||
#elif defined __ICCARM__
|
||||
#include "compiler/tusb_compiler_iar.h"
|
||||
#endif
|
||||
|
||||
#endif /* _TUSB_COMPILER_H_ */
|
||||
|
||||
/// @}
|
||||
114
src/common/tusb_error.h
Normal file
114
src/common/tusb_error.h
Normal file
@@ -0,0 +1,114 @@
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@file tusb_error.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_Error Error Codes
|
||||
* @{ */
|
||||
|
||||
#ifndef _TUSB_ERRORS_H_
|
||||
#define _TUSB_ERRORS_H_
|
||||
|
||||
#include "tusb_option.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define ERROR_ENUM(x) x,
|
||||
#define ERROR_STRING(x) #x,
|
||||
|
||||
#define ERROR_TABLE(ENTRY) \
|
||||
ENTRY(TUSB_ERROR_NONE )\
|
||||
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_ENOUGH_MEMORY )\
|
||||
ENTRY(TUSB_ERROR_FAILED )\
|
||||
|
||||
|
||||
/// \brief Error Code returned
|
||||
typedef enum
|
||||
{
|
||||
ERROR_TABLE(ERROR_ENUM)
|
||||
TUSB_ERROR_COUNT
|
||||
}tusb_error_t;
|
||||
|
||||
#if CFG_TUSB_DEBUG
|
||||
/// Enum to String for debugging purposes. Only available if \ref CFG_TUSB_DEBUG > 0
|
||||
extern char const* const tusb_strerr[TUSB_ERROR_COUNT];
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* _TUSB_ERRORS_H_ */
|
||||
|
||||
/** @} */
|
||||
280
src/common/tusb_fifo.c
Normal file
280
src/common/tusb_fifo.c
Normal file
@@ -0,0 +1,280 @@
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@file fifo.c
|
||||
@author hathach (tinyusb.org)
|
||||
|
||||
@section LICENSE
|
||||
|
||||
Software License Agreement (BSD License)
|
||||
|
||||
Copyright (c) 2018, 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.
|
||||
*/
|
||||
/**************************************************************************/
|
||||
|
||||
#include "tusb_fifo.h"
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/*
|
||||
*------------------------------------------------------------------*/
|
||||
#if CFG_FIFO_MUTEX
|
||||
|
||||
#define mutex_lock_if_needed(_ff) if (_ff->mutex) fifo_mutex_lock(_ff->mutex)
|
||||
#define mutex_unlock_if_needed(_ff) if (_ff->mutex) fifo_mutex_unlock(_ff->mutex)
|
||||
|
||||
#else
|
||||
|
||||
#define mutex_lock_if_needed(_ff)
|
||||
#define mutex_unlock_if_needed(_ff)
|
||||
|
||||
#endif
|
||||
|
||||
static inline uint16_t min16_of(uint16_t x, uint16_t y)
|
||||
{
|
||||
return (x < y) ? x : y;
|
||||
}
|
||||
|
||||
static inline bool fifo_initalized(fifo_t* f)
|
||||
{
|
||||
return (f->buffer != NULL) && (f->depth > 0) && (f->item_size > 0);
|
||||
}
|
||||
|
||||
|
||||
void fifo_config(fifo_t *f, void* buffer, uint16_t depth, uint16_t item_size, bool overwritable)
|
||||
{
|
||||
mutex_lock_if_needed(f);
|
||||
|
||||
f->buffer = (uint8_t*) buffer;
|
||||
f->depth = depth;
|
||||
f->item_size = item_size;
|
||||
f->overwritable = overwritable;
|
||||
|
||||
f->rd_idx = f->wr_idx = f->count = 0;
|
||||
|
||||
mutex_unlock_if_needed(f);
|
||||
}
|
||||
|
||||
|
||||
/******************************************************************************/
|
||||
/*!
|
||||
@brief Read one byte out of the RX buffer.
|
||||
|
||||
This function will return the byte located at the array index of the
|
||||
read pointer, and then increment the read pointer index. If the read
|
||||
pointer exceeds the maximum buffer size, it will roll over to zero.
|
||||
|
||||
@param[in] f
|
||||
Pointer to the FIFO buffer to manipulate
|
||||
@param[in] p_buffer
|
||||
Pointer to the place holder for data read from the buffer
|
||||
|
||||
@returns TRUE if the queue is not empty
|
||||
*/
|
||||
/******************************************************************************/
|
||||
bool fifo_read(fifo_t* f, void * p_buffer)
|
||||
{
|
||||
if( !fifo_initalized(f) ) return false;
|
||||
if( fifo_empty(f) ) return false;
|
||||
|
||||
mutex_lock_if_needed(f);
|
||||
|
||||
memcpy(p_buffer,
|
||||
f->buffer + (f->rd_idx * f->item_size),
|
||||
f->item_size);
|
||||
f->rd_idx = (f->rd_idx + 1) % f->depth;
|
||||
f->count--;
|
||||
|
||||
mutex_unlock_if_needed(f);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*!
|
||||
@brief This function will read n elements into the array index specified by
|
||||
the write pointer and increment the write index. If the write index
|
||||
exceeds the max buffer size, then it will roll over to zero.
|
||||
|
||||
@param[in] f
|
||||
Pointer to the FIFO buffer to manipulate
|
||||
@param[in] p_data
|
||||
The pointer to data location
|
||||
@param[in] count
|
||||
Number of element that buffer can afford
|
||||
|
||||
@returns number of items read from the FIFO
|
||||
*/
|
||||
/******************************************************************************/
|
||||
uint16_t fifo_read_n (fifo_t* f, void * p_buffer, uint16_t count)
|
||||
{
|
||||
if( !fifo_initalized(f) ) return 0;
|
||||
if( fifo_empty(f) ) return 0;
|
||||
|
||||
/* Limit up to fifo's count */
|
||||
count = min16_of(count, f->count);
|
||||
if( count == 0 ) return 0;
|
||||
|
||||
mutex_lock_if_needed(f);
|
||||
|
||||
/* Could copy up to 2 portions marked as 'x' if queue is wrapped around
|
||||
* case 1: ....RxxxxW.......
|
||||
* case 2: xxxxxW....Rxxxxxx
|
||||
*/
|
||||
// uint16_t index2upper = min16_of(count, f->count-f->rd_idx);
|
||||
|
||||
uint8_t* p_buf = (uint8_t*) p_buffer;
|
||||
uint16_t len = 0;
|
||||
while( (len < count) && fifo_read(f, p_buf) )
|
||||
{
|
||||
len++;
|
||||
p_buf += f->item_size;
|
||||
}
|
||||
|
||||
mutex_unlock_if_needed(f);
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*!
|
||||
@brief Reads one item without removing it from the FIFO
|
||||
|
||||
@param[in] f
|
||||
Pointer to the FIFO buffer to manipulate
|
||||
@param[in] position
|
||||
Position to read from in the FIFO buffer
|
||||
@param[in] p_buffer
|
||||
Pointer to the place holder for data read from the buffer
|
||||
|
||||
@returns TRUE if the queue is not empty
|
||||
*/
|
||||
/******************************************************************************/
|
||||
bool fifo_peek_at(fifo_t* f, uint16_t position, void * p_buffer)
|
||||
{
|
||||
if ( !fifo_initalized(f) ) return false;
|
||||
if ( position >= f->count ) return false;
|
||||
|
||||
// rd_idx is position=0
|
||||
uint16_t index = (f->rd_idx + position) % f->depth;
|
||||
memcpy(p_buffer,
|
||||
f->buffer + (index * f->item_size),
|
||||
f->item_size);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*!
|
||||
@brief Write one element into the RX buffer.
|
||||
|
||||
This function will write one element into the array index specified by
|
||||
the write pointer and increment the write index. If the write index
|
||||
exceeds the max buffer size, then it will roll over to zero.
|
||||
|
||||
@param[in] f
|
||||
Pointer to the FIFO buffer to manipulate
|
||||
@param[in] p_data
|
||||
The byte to add to the FIFO
|
||||
|
||||
@returns TRUE if the data was written to the FIFO (overwrittable
|
||||
FIFO will always return TRUE)
|
||||
*/
|
||||
/******************************************************************************/
|
||||
bool fifo_write(fifo_t* f, void const * p_data)
|
||||
{
|
||||
if ( !fifo_initalized(f) ) return false;
|
||||
if ( fifo_full(f) && !f->overwritable ) return false;
|
||||
|
||||
mutex_lock_if_needed(f);
|
||||
|
||||
memcpy( f->buffer + (f->wr_idx * f->item_size),
|
||||
p_data,
|
||||
f->item_size);
|
||||
|
||||
f->wr_idx = (f->wr_idx + 1) % f->depth;
|
||||
|
||||
if (fifo_full(f))
|
||||
{
|
||||
f->rd_idx = f->wr_idx; // keep the full state (rd == wr && len = size)
|
||||
}
|
||||
else
|
||||
{
|
||||
f->count++;
|
||||
}
|
||||
|
||||
mutex_unlock_if_needed(f);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*!
|
||||
@brief This function will write n elements into the array index specified by
|
||||
the write pointer and increment the write index. If the write index
|
||||
exceeds the max buffer size, then it will roll over to zero.
|
||||
|
||||
@param[in] f
|
||||
Pointer to the FIFO buffer to manipulate
|
||||
@param[in] p_data
|
||||
The pointer to data to add to the FIFO
|
||||
@param[in] count
|
||||
Number of element
|
||||
@return Number of written elements
|
||||
*/
|
||||
/******************************************************************************/
|
||||
uint16_t fifo_write_n(fifo_t* f, void const * p_data, uint16_t count)
|
||||
{
|
||||
if ( count == 0 ) return 0;
|
||||
|
||||
uint8_t* p_buf = (uint8_t*) p_data;
|
||||
|
||||
uint16_t len = 0;
|
||||
while( (len < count) && fifo_write(f, p_buf) )
|
||||
{
|
||||
len++;
|
||||
p_buf += f->item_size;
|
||||
}
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
/*!
|
||||
@brief Clear the fifo read and write pointers and set length to zero
|
||||
|
||||
@param[in] f
|
||||
Pointer to the FIFO buffer to manipulate
|
||||
*/
|
||||
/******************************************************************************/
|
||||
void fifo_clear(fifo_t *f)
|
||||
{
|
||||
mutex_lock_if_needed(f);
|
||||
|
||||
f->rd_idx = f->wr_idx = f->count = 0;
|
||||
|
||||
mutex_unlock_if_needed(f);
|
||||
}
|
||||
162
src/common/tusb_fifo.h
Normal file
162
src/common/tusb_fifo.h
Normal file
@@ -0,0 +1,162 @@
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@file fifo.h
|
||||
@author hathach (tinyusb.org)
|
||||
|
||||
@section LICENSE
|
||||
|
||||
Software License Agreement (BSD License)
|
||||
|
||||
Copyright (c) 2018, 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_fifo fifo
|
||||
* @{ */
|
||||
|
||||
#ifndef _TUSB_FIFO_H_
|
||||
#define _TUSB_FIFO_H_
|
||||
|
||||
#define CFG_FIFO_MUTEX 0
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#if CFG_FIFO_MUTEX
|
||||
|
||||
#include "osal/osal.h"
|
||||
|
||||
#if CFG_TUSB_OS == OPT_OS_NONE
|
||||
// Since all fifo read/write is done in thread mode, there should be
|
||||
// no conflict except for osal queue which will be address seperatedly.
|
||||
// Therefore there may be no need for mutex with internal use of fifo
|
||||
|
||||
#define _ff_mutex_def(mutex)
|
||||
|
||||
#else
|
||||
#define fifo_mutex_t struct os_mutex
|
||||
|
||||
#define fifo_mutex_lock(m) os_mutex_pend(m, OS_TIMEOUT_NEVER)
|
||||
#define fifo_mutex_unlock(m) os_mutex_release(m)
|
||||
|
||||
/* Internal use only */
|
||||
#define _mutex_declare(m) .mutex = m
|
||||
|
||||
#endif
|
||||
|
||||
#else
|
||||
|
||||
#define _mutex_declare(m)
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
/** \struct fifo_t
|
||||
* \brief Simple Circular FIFO
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
uint8_t* buffer ; ///< buffer pointer
|
||||
uint16_t depth ; ///< max items
|
||||
uint16_t item_size ; ///< size of each item
|
||||
|
||||
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
|
||||
fifo_mutex_t * const mutex;
|
||||
#endif
|
||||
|
||||
} fifo_t;
|
||||
|
||||
#define FIFO_DEF(name, ff_depth, type, is_overwritable) /*, irq_mutex)*/ \
|
||||
uint8_t name##_buffer[ff_depth*sizeof(type)];\
|
||||
fifo_t name = {\
|
||||
.buffer = name##_buffer,\
|
||||
.depth = ff_depth,\
|
||||
.item_size = sizeof(type),\
|
||||
.overwritable = is_overwritable,\
|
||||
/*.irq = irq_mutex*/\
|
||||
_mutex_declare(_mutex)\
|
||||
}
|
||||
|
||||
void fifo_clear(fifo_t *f);
|
||||
void fifo_config(fifo_t *f, void* buffer, uint16_t depth, uint16_t item_size, bool overwritable);
|
||||
|
||||
bool fifo_write (fifo_t* f, void const * p_data);
|
||||
uint16_t fifo_write_n (fifo_t* f, void const * p_data, uint16_t count);
|
||||
|
||||
bool fifo_read (fifo_t* f, void * p_buffer);
|
||||
uint16_t fifo_read_n (fifo_t* f, void * p_buffer, uint16_t count);
|
||||
|
||||
bool fifo_peek_at (fifo_t* f, uint16_t position, void * p_buffer);
|
||||
|
||||
static inline bool fifo_peek(fifo_t* f, void * p_buffer)
|
||||
{
|
||||
return fifo_peek_at(f, 0, p_buffer);
|
||||
}
|
||||
|
||||
static inline bool fifo_empty(fifo_t* f)
|
||||
{
|
||||
return (f->count == 0);
|
||||
}
|
||||
|
||||
static inline bool fifo_full(fifo_t* f)
|
||||
{
|
||||
return (f->count == f->depth);
|
||||
}
|
||||
|
||||
static inline uint16_t fifo_count(fifo_t* f)
|
||||
{
|
||||
return f->count;
|
||||
}
|
||||
|
||||
static inline uint16_t fifo_remaining(fifo_t* f)
|
||||
{
|
||||
return f->depth - f->count;
|
||||
}
|
||||
|
||||
static inline uint16_t fifo_depth(fifo_t* f)
|
||||
{
|
||||
return f->depth;
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* _TUSB_FIFO_H_ */
|
||||
414
src/common/tusb_types.h
Normal file
414
src/common/tusb_types.h
Normal file
@@ -0,0 +1,414 @@
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@file tusb_types.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_usb_definitions
|
||||
* \defgroup USBDef_Type USB Types
|
||||
* @{ */
|
||||
|
||||
#ifndef _TUSB_TYPES_H_
|
||||
#define _TUSB_TYPES_H_
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include "tusb_compiler.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* CONSTANTS
|
||||
*------------------------------------------------------------------*/
|
||||
|
||||
/// defined base on EHCI specs value for Endpoint Speed
|
||||
typedef enum
|
||||
{
|
||||
TUSB_SPEED_FULL = 0,
|
||||
TUSB_SPEED_LOW ,
|
||||
TUSB_SPEED_HIGH
|
||||
}tusb_speed_t;
|
||||
|
||||
/// defined base on USB Specs Endpoint's bmAttributes
|
||||
typedef enum
|
||||
{
|
||||
TUSB_XFER_CONTROL = 0 ,
|
||||
TUSB_XFER_ISOCHRONOUS ,
|
||||
TUSB_XFER_BULK ,
|
||||
TUSB_XFER_INTERRUPT
|
||||
}tusb_xfer_type_t;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
TUSB_DIR_OUT = 0,
|
||||
TUSB_DIR_IN = 1,
|
||||
|
||||
TUSB_DIR_IN_MASK = 0x80
|
||||
}tusb_dir_t;
|
||||
|
||||
|
||||
/// USB Descriptor Types (section 9.4 table 9-5)
|
||||
typedef enum
|
||||
{
|
||||
TUSB_DESC_DEVICE = 0x01 ,
|
||||
TUSB_DESC_CONFIGURATION = 0x02 ,
|
||||
TUSB_DESC_STRING = 0x03 ,
|
||||
TUSB_DESC_INTERFACE = 0x04 ,
|
||||
TUSB_DESC_ENDPOINT = 0x05 ,
|
||||
TUSB_DESC_DEVICE_QUALIFIER = 0x06 ,
|
||||
TUSB_DESC_OTHER_SPEED_CONFIG = 0x07 ,
|
||||
TUSB_DESC_INTERFACE_POWER = 0x08 ,
|
||||
TUSB_DESC_OTG = 0x09 ,
|
||||
TUSB_DESC_DEBUG = 0x0A ,
|
||||
TUSB_DESC_INTERFACE_ASSOCIATION = 0x0B ,
|
||||
TUSB_DESC_CLASS_SPECIFIC = 0x24
|
||||
}tusb_desc_type_t;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
TUSB_REQ_GET_STATUS =0 , ///< 0
|
||||
TUSB_REQ_CLEAR_FEATURE , ///< 1
|
||||
TUSB_REQ_RESERVED , ///< 2
|
||||
TUSB_REQ_SET_FEATURE , ///< 3
|
||||
TUSB_REQ_RESERVED2 , ///< 4
|
||||
TUSB_REQ_SET_ADDRESS , ///< 5
|
||||
TUSB_REQ_GET_DESCRIPTOR , ///< 6
|
||||
TUSB_REQ_SET_DESCRIPTOR , ///< 7
|
||||
TUSB_REQ_GET_CONFIGURATION , ///< 8
|
||||
TUSB_REQ_SET_CONFIGURATION , ///< 9
|
||||
TUSB_REQ_GET_INTERFACE , ///< 10
|
||||
TUSB_REQ_SET_INTERFACE , ///< 11
|
||||
TUSB_REQ_SYNCH_FRAME ///< 12
|
||||
}tusb_request_code_t;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
TUSB_REQ_TYPE_STANDARD = 0,
|
||||
TUSB_REQ_TYPE_CLASS,
|
||||
TUSB_REQ_TYPE_VENDOR
|
||||
} tusb_request_type_t;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
TUSB_REQ_RCPT_DEVICE =0,
|
||||
TUSB_REQ_RCPT_INTERFACE,
|
||||
TUSB_REQ_RCPT_ENDPOINT,
|
||||
TUSB_REQ_RCPT_OTHER
|
||||
} tusb_request_recipient_t;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
TUSB_CLASS_UNSPECIFIED = 0 , ///< 0
|
||||
TUSB_CLASS_AUDIO = 1 , ///< 1
|
||||
TUSB_CLASS_CDC = 2 , ///< 2
|
||||
TUSB_CLASS_HID = 3 , ///< 3
|
||||
TUSB_CLASS_RESERVED_4 = 4 , ///< 4
|
||||
TUSB_CLASS_PHYSICAL = 5 , ///< 5
|
||||
TUSB_CLASS_IMAGE = 6 , ///< 6
|
||||
TUSB_CLASS_PRINTER = 7 , ///< 7
|
||||
TUSB_CLASS_MSC = 8 , ///< 8
|
||||
TUSB_CLASS_HUB = 9 , ///< 9
|
||||
TUSB_CLASS_CDC_DATA = 10 , ///< 10
|
||||
TUSB_CLASS_SMART_CARD = 11 , ///< 11
|
||||
TUSB_CLASS_RESERVED_12 = 12 , ///< 12
|
||||
TUSB_CLASS_CONTENT_SECURITY = 13 , ///< 13
|
||||
TUSB_CLASS_VIDEO = 14 , ///< 14
|
||||
TUSB_CLASS_PERSONAL_HEALTHCARE = 15 , ///< 15
|
||||
TUSB_CLASS_AUDIO_VIDEO = 16 , ///< 16
|
||||
|
||||
TUSB_CLASS_MAPPED_INDEX_START = 17 , // TODO Map DIAGNOSTIC, WIRELESS_CONTROLLER, MISC, VENDOR_SPECIFIC to this to minimize the array
|
||||
|
||||
TUSB_CLASS_DIAGNOSTIC = 0xDC ,
|
||||
TUSB_CLASS_WIRELESS_CONTROLLER = 0xE0 ,
|
||||
TUSB_CLASS_MISC = 0xEF ,
|
||||
TUSB_CLASS_APPLICATION_SPECIFIC = 0xFE ,
|
||||
TUSB_CLASS_VENDOR_SPECIFIC = 0xFF
|
||||
}tusb_class_code_t;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
MISC_SUBCLASS_COMMON = 2
|
||||
}misc_subclass_type_t;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
MISC_PROTOCOL_IAD = 1
|
||||
}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)
|
||||
};
|
||||
|
||||
#define TUSB_DESC_CONFIG_POWER_MA(x) ((x)/2)
|
||||
|
||||
/// Device State
|
||||
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
|
||||
{
|
||||
TUSB_EVENT_NONE = 0,
|
||||
TUSB_EVENT_XFER_COMPLETE,
|
||||
TUSB_EVENT_XFER_ERROR,
|
||||
TUSB_EVENT_XFER_STALLED,
|
||||
|
||||
TUSB_EVENT_BUS_RESET, // TODO refractor
|
||||
TUSB_EVENT_SETUP_RECEIVED,
|
||||
}tusb_event_t;
|
||||
|
||||
enum {
|
||||
DESCRIPTOR_OFFSET_LENGTH = 0,
|
||||
DESCRIPTOR_OFFSET_TYPE = 1
|
||||
};
|
||||
|
||||
enum {
|
||||
INTERFACE_INVALID_NUMBER = 0xff
|
||||
};
|
||||
|
||||
static inline uint8_t std_class_code_to_index(uint8_t std_class_code)
|
||||
{
|
||||
return (std_class_code <= TUSB_CLASS_AUDIO_VIDEO ) ? std_class_code :
|
||||
(std_class_code == TUSB_CLASS_DIAGNOSTIC ) ? TUSB_CLASS_MAPPED_INDEX_START :
|
||||
(std_class_code == TUSB_CLASS_WIRELESS_CONTROLLER ) ? TUSB_CLASS_MAPPED_INDEX_START + 1 :
|
||||
(std_class_code == TUSB_CLASS_MISC ) ? TUSB_CLASS_MAPPED_INDEX_START + 2 :
|
||||
(std_class_code == TUSB_CLASS_APPLICATION_SPECIFIC ) ? TUSB_CLASS_MAPPED_INDEX_START + 3 :
|
||||
(std_class_code == TUSB_CLASS_VENDOR_SPECIFIC ) ? TUSB_CLASS_MAPPED_INDEX_START + 4 : 0;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// STANDARD DESCRIPTORS
|
||||
//--------------------------------------------------------------------+
|
||||
|
||||
/// USB Standard Device Descriptor (section 9.6.1, table 9-8)
|
||||
typedef struct ATTR_PACKED
|
||||
{
|
||||
uint8_t bLength ; ///< Size of this descriptor in bytes.
|
||||
uint8_t bDescriptorType ; ///< DEVICE Descriptor Type.
|
||||
uint16_t bcdUSB ; ///< BUSB Specification Release Number in Binary-Coded Decimal (i.e., 2.10 is 210H). This field identifies the release of the USB Specification with which the device and its descriptors are compliant.
|
||||
|
||||
uint8_t bDeviceClass ; ///< Class code (assigned by the USB-IF). \li If this field is reset to zero, each interface within a configuration specifies its own class information and the various interfaces operate independently. \li If this field is set to a value between 1 and FEH, the device supports different class specifications on different interfaces and the interfaces may not operate independently. This value identifies the class definition used for the aggregate interfaces. \li If this field is set to FFH, the device class is vendor-specific.
|
||||
uint8_t bDeviceSubClass ; ///< Subclass code (assigned by the USB-IF). These codes are qualified by the value of the bDeviceClass field. \li If the bDeviceClass field is reset to zero, this field must also be reset to zero. \li If the bDeviceClass field is not set to FFH, all values are reserved for assignment by the USB-IF.
|
||||
uint8_t bDeviceProtocol ; ///< Protocol code (assigned by the USB-IF). These codes are qualified by the value of the bDeviceClass and the bDeviceSubClass fields. If a device supports class-specific protocols on a device basis as opposed to an interface basis, this code identifies the protocols that the device uses as defined by the specification of the device class. \li If this field is reset to zero, the device does not use class-specific protocols on a device basis. However, it may use classspecific protocols on an interface basis. \li If this field is set to FFH, the device uses a vendor-specific protocol on a device basis.
|
||||
uint8_t bMaxPacketSize0 ; ///< Maximum packet size for endpoint zero (only 8, 16, 32, or 64 are valid). For HS devices is fixed to 64.
|
||||
|
||||
uint16_t idVendor ; ///< Vendor ID (assigned by the USB-IF).
|
||||
uint16_t idProduct ; ///< Product ID (assigned by the manufacturer).
|
||||
uint16_t bcdDevice ; ///< Device release number in binary-coded decimal.
|
||||
uint8_t iManufacturer ; ///< Index of string descriptor describing manufacturer.
|
||||
uint8_t iProduct ; ///< Index of string descriptor describing product.
|
||||
uint8_t iSerialNumber ; ///< Index of string descriptor describing the device's serial number.
|
||||
|
||||
uint8_t bNumConfigurations ; ///< Number of possible configurations.
|
||||
} tusb_desc_device_t;
|
||||
|
||||
/// USB Standard Configuration Descriptor (section 9.6.1 table 9-10) */
|
||||
typedef struct ATTR_PACKED
|
||||
{
|
||||
uint8_t bLength ; ///< Size of this descriptor in bytes
|
||||
uint8_t bDescriptorType ; ///< CONFIGURATION Descriptor Type
|
||||
uint16_t wTotalLength ; ///< Total length of data returned for this configuration. Includes the combined length of all descriptors (configuration, interface, endpoint, and class- or vendor-specific) returned for this configuration.
|
||||
|
||||
uint8_t bNumInterfaces ; ///< Number of interfaces supported by this configuration
|
||||
uint8_t bConfigurationValue ; ///< Value to use as an argument to the SetConfiguration() request to select this configuration.
|
||||
uint8_t iConfiguration ; ///< Index of string descriptor describing this configuration
|
||||
uint8_t bmAttributes ; ///< Configuration characteristics \n D7: Reserved (set to one)\n D6: Self-powered \n D5: Remote Wakeup \n D4...0: Reserved (reset to zero) \n D7 is reserved and must be set to one for historical reasons. \n A device configuration that uses power from the bus and a local source reports a non-zero value in bMaxPower to indicate the amount of bus power required and sets D6. The actual power source at runtime may be determined using the GetStatus(DEVICE) request (see USB 2.0 spec Section 9.4.5). \n If a device configuration supports remote wakeup, D5 is set to one.
|
||||
uint8_t bMaxPower ; ///< Maximum power consumption of the USB device from the bus in this specific configuration when the device is fully operational. Expressed in 2 mA units (i.e., 50 = 100 mA).
|
||||
} tusb_desc_configuration_t;
|
||||
|
||||
/// USB Standard Interface Descriptor (section 9.6.1 table 9-12)
|
||||
typedef struct ATTR_PACKED
|
||||
{
|
||||
uint8_t bLength ; ///< Size of this descriptor in bytes
|
||||
uint8_t bDescriptorType ; ///< INTERFACE Descriptor Type
|
||||
|
||||
uint8_t bInterfaceNumber ; ///< Number of this interface. Zero-based value identifying the index in the array of concurrent interfaces supported by this configuration.
|
||||
uint8_t bAlternateSetting ; ///< Value used to select this alternate setting for the interface identified in the prior field
|
||||
uint8_t bNumEndpoints ; ///< Number of endpoints used by this interface (excluding endpoint zero). If this value is zero, this interface only uses the Default Control Pipe.
|
||||
uint8_t bInterfaceClass ; ///< Class code (assigned by the USB-IF). \li A value of zero is reserved for future standardization. \li If this field is set to FFH, the interface class is vendor-specific. \li All other values are reserved for assignment by the USB-IF.
|
||||
uint8_t bInterfaceSubClass ; ///< Subclass code (assigned by the USB-IF). \n These codes are qualified by the value of the bInterfaceClass field. \li If the bInterfaceClass field is reset to zero, this field must also be reset to zero. \li If the bInterfaceClass field is not set to FFH, all values are reserved for assignment by the USB-IF.
|
||||
uint8_t bInterfaceProtocol ; ///< Protocol code (assigned by the USB). \n These codes are qualified by the value of the bInterfaceClass and the bInterfaceSubClass fields. If an interface supports class-specific requests, this code identifies the protocols that the device uses as defined by the specification of the device class. \li If this field is reset to zero, the device does not use a class-specific protocol on this interface. \li If this field is set to FFH, the device uses a vendor-specific protocol for this interface.
|
||||
uint8_t iInterface ; ///< Index of string descriptor describing this interface
|
||||
} tusb_desc_interface_t;
|
||||
|
||||
/// USB Standard Endpoint Descriptor (section 9.6.1 table 9-13)
|
||||
typedef struct ATTR_PACKED
|
||||
{
|
||||
uint8_t bLength ; ///< Size of this descriptor in bytes
|
||||
uint8_t bDescriptorType ; ///< ENDPOINT Descriptor Type
|
||||
|
||||
uint8_t bEndpointAddress ; ///< The address of the endpoint on the USB device described by this descriptor. The address is encoded as follows: \n Bit 3...0: The endpoint number \n Bit 6...4: Reserved, reset to zero \n Bit 7: Direction, ignored for control endpoints 0 = OUT endpoint 1 = IN endpoint.
|
||||
|
||||
struct ATTR_PACKED {
|
||||
uint8_t xfer : 2;
|
||||
uint8_t sync : 2;
|
||||
uint8_t usage : 2;
|
||||
uint8_t : 2;
|
||||
} bmAttributes ; ///< This field describes the endpoint's attributes when it is configured using the bConfigurationValue. \n Bits 1..0: Transfer Type \n- 00 = Control \n- 01 = Isochronous \n- 10 = Bulk \n- 11 = Interrupt \n If not an isochronous endpoint, bits 5..2 are reserved and must be set to zero. If isochronous, they are defined as follows: \n Bits 3..2: Synchronization Type \n- 00 = No Synchronization \n- 01 = Asynchronous \n- 10 = Adaptive \n- 11 = Synchronous \n Bits 5..4: Usage Type \n- 00 = Data endpoint \n- 01 = Feedback endpoint \n- 10 = Implicit feedback Data endpoint \n- 11 = Reserved \n Refer to Chapter 5 of USB 2.0 specification for more information. \n All other bits are reserved and must be reset to zero. Reserved bits must be ignored by the host.
|
||||
|
||||
struct ATTR_PACKED {
|
||||
uint16_t size : 11; ///< Maximum packet size this endpoint is capable of sending or receiving when this configuration is selected. \n For isochronous endpoints, this value is used to reserve the bus time in the schedule, required for the per-(micro)frame data payloads. The pipe may, on an ongoing basis, actually use less bandwidth than that reserved. The device reports, if necessary, the actual bandwidth used via its normal, non-USB defined mechanisms. \n For all endpoints, bits 10..0 specify the maximum packet size (in bytes). \n For high-speed isochronous and interrupt endpoints: \n Bits 12..11 specify the number of additional transaction opportunities per microframe: \n- 00 = None (1 transaction per microframe) \n- 01 = 1 additional (2 per microframe) \n- 10 = 2 additional (3 per microframe) \n- 11 = Reserved \n Bits 15..13 are reserved and must be set to zero.
|
||||
uint16_t hs_period_mult : 2;
|
||||
uint16_t : 0;
|
||||
}wMaxPacketSize;
|
||||
|
||||
uint8_t bInterval ; ///< Interval for polling endpoint for data transfers. Expressed in frames or microframes depending on the device operating speed (i.e., either 1 millisecond or 125 us units). \n- For full-/high-speed isochronous endpoints, this value must be in the range from 1 to 16. The bInterval value is used as the exponent for a \f$ 2^(bInterval-1) \f$ value; e.g., a bInterval of 4 means a period of 8 (\f$ 2^(4-1) \f$). \n- For full-/low-speed interrupt endpoints, the value of this field may be from 1 to 255. \n- For high-speed interrupt endpoints, the bInterval value is used as the exponent for a \f$ 2^(bInterval-1) \f$ value; e.g., a bInterval of 4 means a period of 8 (\f$ 2^(4-1) \f$) . This value must be from 1 to 16. \n- For high-speed bulk/control OUT endpoints, the bInterval must specify the maximum NAK rate of the endpoint. A value of 0 indicates the endpoint never NAKs. Other values indicate at most 1 NAK each bInterval number of microframes. This value must be in the range from 0 to 255. \n Refer to Chapter 5 of USB 2.0 specification for more information.
|
||||
} tusb_desc_endpoint_t;
|
||||
|
||||
/// USB Other Speed Configuration Descriptor (section 9.6.1 table 9-11)
|
||||
typedef struct ATTR_PACKED
|
||||
{
|
||||
uint8_t bLength ; ///< Size of descriptor
|
||||
uint8_t bDescriptorType ; ///< Other_speed_Configuration Type
|
||||
uint16_t wTotalLength ; ///< Total length of data returned
|
||||
|
||||
uint8_t bNumInterfaces ; ///< Number of interfaces supported by this speed configuration
|
||||
uint8_t bConfigurationValue ; ///< Value to use to select configuration
|
||||
uint8_t IConfiguration ; ///< Index of string descriptor
|
||||
uint8_t bmAttributes ; ///< Same as Configuration descriptor
|
||||
uint8_t bMaxPower ; ///< Same as Configuration descriptor
|
||||
} tusb_desc_other_speed_t;
|
||||
|
||||
/// USB Device Qualifier Descriptor (section 9.6.1 table 9-9)
|
||||
typedef struct ATTR_PACKED
|
||||
{
|
||||
uint8_t bLength ; ///< Size of descriptor
|
||||
uint8_t bDescriptorType ; ///< Device Qualifier Type
|
||||
uint16_t bcdUSB ; ///< USB specification version number (e.g., 0200H for V2.00)
|
||||
|
||||
uint8_t bDeviceClass ; ///< Class Code
|
||||
uint8_t bDeviceSubClass ; ///< SubClass Code
|
||||
uint8_t bDeviceProtocol ; ///< Protocol Code
|
||||
uint8_t bMaxPacketSize0 ; ///< Maximum packet size for other speed
|
||||
uint8_t bNumConfigurations ; ///< Number of Other-speed Configurations
|
||||
uint8_t bReserved ; ///< Reserved for future use, must be zero
|
||||
} tusb_desc_device_qualifier_t;
|
||||
|
||||
/// USB Interface Association Descriptor (IAD ECN)
|
||||
typedef struct ATTR_PACKED
|
||||
{
|
||||
uint8_t bLength ; ///< Size of descriptor
|
||||
uint8_t bDescriptorType ; ///< Other_speed_Configuration Type
|
||||
|
||||
uint8_t bFirstInterface ; ///< Index of the first associated interface.
|
||||
uint8_t bInterfaceCount ; ///< Total number of associated interfaces.
|
||||
|
||||
uint8_t bFunctionClass ; ///< Interface class ID.
|
||||
uint8_t bFunctionSubClass ; ///< Interface subclass ID.
|
||||
uint8_t bFunctionProtocol ; ///< Interface protocol ID.
|
||||
|
||||
uint8_t iFunction ; ///< Index of the string descriptor describing the interface association.
|
||||
} tusb_desc_interface_assoc_t;
|
||||
|
||||
/// USB Header Descriptor
|
||||
typedef struct ATTR_PACKED
|
||||
{
|
||||
uint8_t bLength ; ///< Size of this descriptor in bytes
|
||||
uint8_t bDescriptorType ; ///< Descriptor Type
|
||||
} tusb_desc_header_t;
|
||||
|
||||
typedef struct ATTR_PACKED
|
||||
{
|
||||
uint8_t bLength ; ///< Size of this descriptor in bytes
|
||||
uint8_t bDescriptorType ; ///< Descriptor Type
|
||||
uint16_t unicode_string[];
|
||||
} tusb_desc_string_t;
|
||||
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* Types
|
||||
*------------------------------------------------------------------*/
|
||||
|
||||
typedef struct ATTR_PACKED{
|
||||
union {
|
||||
struct ATTR_PACKED {
|
||||
uint8_t recipient : 5; ///< Recipient type tusb_request_recipient_t.
|
||||
uint8_t type : 2; ///< Request type tusb_request_type_t.
|
||||
uint8_t direction : 1; ///< Direction type. tusb_dir_t
|
||||
} bmRequestType_bit;
|
||||
uint8_t bmRequestType;
|
||||
};
|
||||
|
||||
uint8_t bRequest;
|
||||
uint16_t wValue;
|
||||
uint16_t wIndex;
|
||||
uint16_t wLength;
|
||||
} tusb_control_request_t;
|
||||
|
||||
VERIFY_STATIC( sizeof(tusb_control_request_t) == 8, "mostly compiler option issue");
|
||||
|
||||
// TODO move to somewhere suitable
|
||||
static inline uint8_t bm_request_type(uint8_t direction, uint8_t type, uint8_t recipient)
|
||||
{
|
||||
return ((uint8_t) (direction << 7)) | ((uint8_t) (type << 5)) | (recipient);
|
||||
}
|
||||
|
||||
// Get direction from Endpoint address
|
||||
static inline tusb_dir_t 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)
|
||||
{
|
||||
return addr & (~TUSB_DIR_IN_MASK);
|
||||
}
|
||||
|
||||
static inline uint8_t edpt_addr(uint8_t num, tusb_dir_t dir)
|
||||
{
|
||||
return num | (dir == TUSB_DIR_IN ? TUSB_DIR_IN_MASK : 0);
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* _TUSB_TYPES_H_ */
|
||||
|
||||
/** @} */
|
||||
183
src/common/tusb_verify.h
Normal file
183
src/common/tusb_verify.h
Normal file
@@ -0,0 +1,183 @@
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@file verify.h
|
||||
@author hathach
|
||||
|
||||
@section LICENSE
|
||||
|
||||
Software License Agreement (BSD License)
|
||||
|
||||
Copyright (c) 2018, 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.
|
||||
*/
|
||||
/**************************************************************************/
|
||||
#ifndef TUSB_VERIFY_H_
|
||||
#define TUSB_VERIFY_H_
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include "tusb_option.h"
|
||||
#include "tusb_compiler.h"
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* This file use an advanced macro technique to mimic the default parameter
|
||||
* as C++ for the sake of code simplicity. Beware of a headache macro
|
||||
* manipulation that you are told to stay away.
|
||||
*
|
||||
* e.g
|
||||
*
|
||||
* - VERIFY( cond ) will return false if cond is false
|
||||
* - VERIFY( cond, err) will return err instead if cond is false
|
||||
*------------------------------------------------------------------*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// VERIFY Helper
|
||||
//--------------------------------------------------------------------+
|
||||
#if CFG_TUSB_DEBUG >= 1
|
||||
#define _MESS_ERR(_err) printf("%s: %d: failed, error = %s\n", __func__, __LINE__, tusb_strerr[_err])
|
||||
#define _MESS_FAILED() printf("%s: %d: failed\n", __func__, __LINE__)
|
||||
#else
|
||||
#define _MESS_ERR(_err)
|
||||
#define _MESS_FAILED()
|
||||
#endif
|
||||
|
||||
// Halt CPU (breakpoint) when hitting error, only apply for Cortex M3, M4, M7
|
||||
#if defined(__ARM_ARCH_7M__) || defined (__ARM_ARCH_7EM__)
|
||||
|
||||
#define verify_breakpoint() \
|
||||
do {\
|
||||
volatile uint32_t* ARM_CM_DHCSR = ((volatile uint32_t*) 0xE000EDF0UL); /* Cortex M CoreDebug->DHCSR */ \
|
||||
if ( (*ARM_CM_DHCSR) & 1UL ) __asm("BKPT #0\n"); /* Only halt mcu if debugger is attached */\
|
||||
} while(0)
|
||||
|
||||
#else
|
||||
#define verify_breakpoint()
|
||||
#endif
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* Macro Generator
|
||||
*------------------------------------------------------------------*/
|
||||
|
||||
// Helper to implement optional parameter for VERIFY Macro family
|
||||
#define GET_3RD_ARG(arg1, arg2, arg3, ...) arg3
|
||||
#define GET_4TH_ARG(arg1, arg2, arg3, arg4, ...) arg4
|
||||
|
||||
/*------------- Generator for VERIFY and VERIFY_HDLR -------------*/
|
||||
#define VERIFY_DEFINE(_cond, _handler, _ret) do { if ( !(_cond) ) { _handler; return _ret; } } while(0)
|
||||
|
||||
/*------------- Generator for VERIFY_ERR and VERIFY_ERR_HDLR -------------*/
|
||||
#define VERIFY_ERR_DEF2(_error, _handler) \
|
||||
do { \
|
||||
uint32_t _err = (uint32_t)(_error); \
|
||||
if ( 0 != _err ) { _MESS_ERR(_err); _handler; return _err; }\
|
||||
} while(0)
|
||||
|
||||
#define VERIFY_ERR_DEF3(_error, _handler, _ret) \
|
||||
do { \
|
||||
uint32_t _err = (uint32_t)(_error); \
|
||||
if ( 0 != _err ) { _MESS_ERR(_err); _handler; return _ret; }\
|
||||
} while(0)
|
||||
|
||||
|
||||
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* VERIFY
|
||||
* - VERIFY_1ARGS : return false if failed
|
||||
* - VERIFY_2ARGS : return provided value if failed
|
||||
*------------------------------------------------------------------*/
|
||||
#define VERIFY_1ARGS(_cond) VERIFY_DEFINE(_cond, , false)
|
||||
#define VERIFY_2ARGS(_cond, _ret) VERIFY_DEFINE(_cond, , _ret)
|
||||
|
||||
#define VERIFY(...) GET_3RD_ARG(__VA_ARGS__, VERIFY_2ARGS, VERIFY_1ARGS)(__VA_ARGS__)
|
||||
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* VERIFY WITH HANDLER
|
||||
* - VERIFY_HDLR_2ARGS : execute handler, return false if failed
|
||||
* - VERIFY_HDLR_3ARGS : execute handler, return provided error if failed
|
||||
*------------------------------------------------------------------*/
|
||||
#define VERIFY_HDLR_2ARGS(_cond, _handler) VERIFY_DEFINE(_cond, _handler, false)
|
||||
#define VERIFY_HDLR_3ARGS(_cond, _handler, _ret) VERIFY_DEFINE(_cond, _handler, _ret)
|
||||
|
||||
#define VERIFY_HDLR(...) GET_4TH_ARG(__VA_ARGS__, VERIFY_HDLR_3ARGS, VERIFY_HDLR_2ARGS)(__VA_ARGS__)
|
||||
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* VERIFY STATUS
|
||||
* - VERIFY_ERR_1ARGS : return status of condition if failed
|
||||
* - VERIFY_ERR_2ARGS : return provided status code if failed
|
||||
*------------------------------------------------------------------*/
|
||||
#define VERIFY_ERR_1ARGS(_error) VERIFY_ERR_DEF2(_error, )
|
||||
#define VERIFY_ERR_2ARGS(_error, _ret) VERIFY_ERR_DEF3(_error, ,_ret)
|
||||
|
||||
#define VERIFY_ERR(...) GET_3RD_ARG(__VA_ARGS__, VERIFY_ERR_2ARGS, VERIFY_ERR_1ARGS)(__VA_ARGS__)
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* VERIFY STATUS WITH HANDLER
|
||||
* - VERIFY_ERR_HDLR_2ARGS : execute handler, return status if failed
|
||||
* - VERIFY_ERR_HDLR_3ARGS : execute handler, return provided error if failed
|
||||
*------------------------------------------------------------------*/
|
||||
#define VERIFY_ERR_HDLR_2ARGS(_error, _handler) VERIFY_ERR_DEF2(_error, _handler)
|
||||
#define VERIFY_ERR_HDLR_3ARGS(_error, _handler, _ret) VERIFY_ERR_DEF3(_error, _handler, _ret)
|
||||
|
||||
#define VERIFY_ERR_HDLR(...) GET_4TH_ARG(__VA_ARGS__, VERIFY_ERR_HDLR_3ARGS, VERIFY_ERR_HDLR_2ARGS)(__VA_ARGS__)
|
||||
|
||||
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* ASSERT
|
||||
* basically VERIFY with verify_breakpoint() as handler
|
||||
* - 1 arg : return false if failed
|
||||
* - 2 arg : return error if failed
|
||||
*------------------------------------------------------------------*/
|
||||
#define ASSERT_1ARGS(_cond) VERIFY_DEFINE(_cond, _MESS_FAILED(); verify_breakpoint(), false)
|
||||
#define ASSERT_2ARGS(_cond, _ret) VERIFY_DEFINE(_cond, _MESS_FAILED(); verify_breakpoint(), _ret)
|
||||
|
||||
#define TU_ASSERT(...) GET_3RD_ARG(__VA_ARGS__, ASSERT_2ARGS, ASSERT_1ARGS)(__VA_ARGS__)
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* ASSERT Error
|
||||
* basically VERIFY Error with verify_breakpoint() as handler
|
||||
*------------------------------------------------------------------*/
|
||||
#define ASERT_ERR_1ARGS(_error) VERIFY_ERR_DEF2(_error, verify_breakpoint())
|
||||
#define ASERT_ERR_2ARGS(_error, _ret) VERIFY_ERR_DEF3(_error, verify_breakpoint(), _ret)
|
||||
|
||||
#define TU_ASSERT_ERR(...) GET_3RD_ARG(__VA_ARGS__, ASERT_ERR_2ARGS, ASERT_ERR_1ARGS)(__VA_ARGS__)
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* ASSERT HDLR
|
||||
*------------------------------------------------------------------*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* TUSB_VERIFY_H_ */
|
||||
Reference in New Issue
Block a user