summaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
Diffstat (limited to 'src/common')
-rw-r--r--src/common/tusb_common.h25
-rw-r--r--src/common/tusb_compiler.h21
-rw-r--r--src/common/tusb_fifo.c111
3 files changed, 108 insertions, 49 deletions
diff --git a/src/common/tusb_common.h b/src/common/tusb_common.h
index 052caef21..d95c0ffc4 100644
--- a/src/common/tusb_common.h
+++ b/src/common/tusb_common.h
@@ -106,7 +106,7 @@ static inline uint16_t tu_max16 (uint16_t x, uint16_t y) { return (x > y) ? x :
static inline uint32_t tu_max32 (uint32_t x, uint32_t y) { return (x > y) ? x : y; }
// Align
-static inline uint32_t tu_align_n(uint32_t value, uint32_t alignment)
+static inline uint32_t tu_align(uint32_t value, uint32_t alignment)
{
return value & ((uint32_t) ~(alignment-1));
}
@@ -215,8 +215,11 @@ static inline bool tu_bit_test (uint32_t value, uint8_t pos) { return (value
void tu_print_mem(void const *buf, uint16_t count, uint8_t indent);
-#ifndef tu_printf
- #define tu_printf printf
+#ifdef CFG_TUSB_DEBUG_PRINTF
+ extern int CFG_TUSB_DEBUG_PRINTF(const char *format, ...);
+ #define tu_printf CFG_TUSB_DEBUG_PRINTF
+#else
+ #define tu_printf printf
#endif
static inline
@@ -232,31 +235,32 @@ void tu_print_var(uint8_t const* buf, uint32_t bufsize)
#define TU_LOG1_INT(_x) tu_printf(#_x " = %ld\n", (uint32_t) (_x) )
#define TU_LOG1_HEX(_x) tu_printf(#_x " = %lX\n", (uint32_t) (_x) )
#define TU_LOG1_LOCATION() tu_printf("%s: %d:\r\n", __PRETTY_FUNCTION__, __LINE__)
+#define TU_LOG1_FAILED() tu_printf("%s: %d: Failed\r\n", __PRETTY_FUNCTION__, __LINE__)
// Log with debug level 2
#if CFG_TUSB_DEBUG > 1
#define TU_LOG2 TU_LOG1
#define TU_LOG2_MEM TU_LOG1_MEM
#define TU_LOG2_VAR TU_LOG1_VAR
- #define TU_LOG2_LOCATION() TU_LOG1_LOCATION()
#define TU_LOG2_INT TU_LOG1_INT
#define TU_LOG2_HEX TU_LOG1_HEX
+ #define TU_LOG2_LOCATION() TU_LOG1_LOCATION()
#endif
typedef struct
{
uint32_t key;
- char const * data;
-}lookup_entry_t;
+ const char* data;
+} tu_lookup_entry_t;
typedef struct
{
uint16_t count;
- lookup_entry_t const* items;
-} lookup_table_t;
+ tu_lookup_entry_t const* items;
+} tu_lookup_table_t;
-static inline char const* lookup_find(lookup_table_t const* p_table, uint32_t key)
+static inline const char* tu_lookup_find(tu_lookup_table_t const* p_table, uint32_t key)
{
for(uint16_t i=0; i<p_table->count; i++)
{
@@ -274,6 +278,8 @@ static inline char const* lookup_find(lookup_table_t const* p_table, uint32_t ke
#define TU_LOG1_VAR(...)
#define TU_LOG1_INT(...)
#define TU_LOG1_HEX(...)
+ #define TU_LOG1_LOCATION()
+ #define TU_LOG1_FAILED()
#endif
#ifndef TU_LOG2
@@ -282,6 +288,7 @@ static inline char const* lookup_find(lookup_table_t const* p_table, uint32_t ke
#define TU_LOG2_VAR(...)
#define TU_LOG2_INT(...)
#define TU_LOG2_HEX(...)
+ #define TU_LOG2_LOCATION()
#endif
#ifdef __cplusplus
diff --git a/src/common/tusb_compiler.h b/src/common/tusb_compiler.h
index e403c749b..6f6aa3c39 100644
--- a/src/common/tusb_compiler.h
+++ b/src/common/tusb_compiler.h
@@ -99,7 +99,26 @@
#define TU_BSWAP16(u16) (__builtin_bswap16(u16))
#define TU_BSWAP32(u32) (__builtin_bswap32(u32))
-#else
+#elif defined(__ICCARM__)
+ #define TU_ATTR_ALIGNED(Bytes) __attribute__ ((aligned(Bytes)))
+ #define TU_ATTR_SECTION(sec_name) __attribute__ ((section(#sec_name)))
+ #define TU_ATTR_PACKED __attribute__ ((packed))
+ #define TU_ATTR_PREPACKED
+ #define TU_ATTR_WEAK __attribute__ ((weak))
+ #define TU_ATTR_DEPRECATED(mess) __attribute__ ((deprecated(mess))) // warn if function with this attribute is used
+ #define TU_ATTR_UNUSED __attribute__ ((unused)) // Function/Variable is meant to be possibly unused
+ #define TU_ATTR_USED __attribute__ ((used)) // Function/Variable is meant to be used
+
+ // Endian conversion use well-known host to network (big endian) naming
+ #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
+ #define TU_BYTE_ORDER TU_LITTLE_ENDIAN
+ #else
+ #define TU_BYTE_ORDER TU_BIG_ENDIAN
+ #endif
+
+ #define TU_BSWAP16(u16) (__iar_builtin_REV16(u16))
+ #define TU_BSWAP32(u32) (__iar_builtin_REV(u32))
+#else
#error "Compiler attribute porting is required"
#endif
diff --git a/src/common/tusb_fifo.c b/src/common/tusb_fifo.c
index 01e990311..6ab158cca 100644
--- a/src/common/tusb_fifo.c
+++ b/src/common/tusb_fifo.c
@@ -71,41 +71,46 @@ bool tu_fifo_config(tu_fifo_t *f, void* buffer, uint16_t depth, uint16_t item_si
return true;
}
+static inline uint16_t _ff_mod(uint16_t idx, uint16_t depth)
+{
+ return (idx < depth) ? idx : (idx-depth);
+}
+
// retrieve data from fifo
-static void _tu_ff_pull(tu_fifo_t* f, void * buffer)
+static inline void _ff_pull(tu_fifo_t* f, void * buffer, uint16_t n)
{
memcpy(buffer,
f->buffer + (f->rd_idx * f->item_size),
- f->item_size);
+ f->item_size*n);
- f->rd_idx = (f->rd_idx + 1) % f->depth;
- f->count--;
+ f->rd_idx = _ff_mod(f->rd_idx + n, f->depth);
+ f->count -= n;
}
// send data to fifo
-static void _tu_ff_push(tu_fifo_t* f, void const * data)
+static inline void _ff_push(tu_fifo_t* f, void const * data, uint16_t n)
{
- memcpy( f->buffer + (f->wr_idx * f->item_size),
- data,
- f->item_size);
+ memcpy(f->buffer + (f->wr_idx * f->item_size),
+ data,
+ f->item_size*n);
- f->wr_idx = (f->wr_idx + 1) % f->depth;
+ f->wr_idx = _ff_mod(f->wr_idx + n, f->depth);
if (tu_fifo_full(f))
{
- f->rd_idx = f->wr_idx; // keep the full state (rd == wr && len = size)
+ f->rd_idx = f->wr_idx; // keep the full state (rd == wr && count = depth)
}
else
{
- f->count++;
+ f->count += n;
}
}
/******************************************************************************/
/*!
- @brief Read one byte out of the RX buffer.
+ @brief Read one element out of the RX buffer.
- This function will return the byte located at the array index of the
+ This function will return the element 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.
@@ -123,7 +128,7 @@ bool tu_fifo_read(tu_fifo_t* f, void * buffer)
tu_fifo_lock(f);
- _tu_ff_pull(f, buffer);
+ _ff_pull(f, buffer, 1);
tu_fifo_unlock(f);
@@ -132,8 +137,8 @@ bool tu_fifo_read(tu_fifo_t* f, void * buffer)
/******************************************************************************/
/*!
- @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
+ @brief This function will read n elements from the array index specified by
+ the read pointer and increment the read index. If the read index
exceeds the max buffer size, then it will roll over to zero.
@param[in] f
@@ -148,32 +153,37 @@ bool tu_fifo_read(tu_fifo_t* f, void * buffer)
/******************************************************************************/
uint16_t tu_fifo_read_n (tu_fifo_t* f, void * buffer, uint16_t count)
{
- if( tu_fifo_empty(f) ) return 0;
+ if(tu_fifo_empty(f)) return 0;
tu_fifo_lock(f);
- /* Limit up to fifo's count */
- if ( count > f->count ) count = f->count;
-
- uint8_t* buf8 = (uint8_t*) buffer;
- uint16_t len = 0;
+ // Limit up to fifo's count
+ if(count > f->count) count = f->count;
- while (len < count)
+ if(count + f->rd_idx <= f->depth)
{
- _tu_ff_pull(f, buf8);
+ _ff_pull(f, buffer, count);
+ }
+ else
+ {
+ uint16_t const part1 = f->depth - f->rd_idx;
- len++;
- buf8 += f->item_size;
+ // Part 1: from rd_idx to end
+ _ff_pull(f, buffer, part1);
+ buffer = ((uint8_t*) buffer) + part1*f->item_size;
+
+ // Part 2: start to remaining
+ _ff_pull(f, buffer, count-part1);
}
tu_fifo_unlock(f);
- return len;
+ return count;
}
/******************************************************************************/
/*!
- @brief Reads one item without removing it from the FIFO
+ @brief Read one item without removing it from the FIFO
@param[in] f
Pointer to the FIFO buffer to manipulate
@@ -189,12 +199,16 @@ bool tu_fifo_peek_at(tu_fifo_t* f, uint16_t pos, void * p_buffer)
{
if ( pos >= f->count ) return false;
+ tu_fifo_lock(f);
+
// rd_idx is pos=0
- uint16_t index = (f->rd_idx + pos) % f->depth;
+ uint16_t index = _ff_mod(f->rd_idx + pos, f->depth);
memcpy(p_buffer,
f->buffer + (index * f->item_size),
f->item_size);
+ tu_fifo_unlock(f);
+
return true;
}
@@ -221,7 +235,7 @@ bool tu_fifo_write (tu_fifo_t* f, const void * data)
tu_fifo_lock(f);
- _tu_ff_push(f, data);
+ _ff_push(f, data, 1);
tu_fifo_unlock(f);
@@ -249,23 +263,42 @@ uint16_t tu_fifo_write_n (tu_fifo_t* f, const void * data, uint16_t count)
tu_fifo_lock(f);
- // Not overwritable limit up to full
- if (!f->overwritable) count = tu_min16(count, tu_fifo_remaining(f));
-
uint8_t const* buf8 = (uint8_t const*) data;
- uint16_t len = 0;
- while (len < count)
+ if (!f->overwritable)
{
- _tu_ff_push(f, buf8);
+ // Not overwritable limit up to full
+ count = tu_min16(count, tu_fifo_remaining(f));
+ }
+ else if (count > f->depth)
+ {
+ // Only copy last part
+ buf8 = buf8 + (count - f->depth) * f->item_size;
+ count = f->depth;
+ f->wr_idx = 0;
+ f->rd_idx = 0;
+ f->count = 0;
+ }
- len++;
- buf8 += f->item_size;
+ if (count + f->wr_idx <= f->depth )
+ {
+ _ff_push(f, buf8, count);
}
+ else
+ {
+ uint16_t const part1 = f->depth - f->wr_idx;
+ // Part 1: from wr_idx to end
+ _ff_push(f, buf8, part1);
+ buf8 += part1*f->item_size;
+
+ // Part 2: start to remaining
+ _ff_push(f, buf8, count-part1);
+ }
+
tu_fifo_unlock(f);
- return len;
+ return count;
}
/******************************************************************************/