Open FFBoard
Open source force feedback firmware
tusb_common.h
Go to the documentation of this file.
1/*
2 * The MIT License (MIT)
3 *
4 * Copyright (c) 2019 Ha Thach (tinyusb.org)
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 *
24 * This file is part of the TinyUSB stack.
25 */
26
27#ifndef _TUSB_COMMON_H_
28#define _TUSB_COMMON_H_
29
30#ifdef __cplusplus
31 extern "C" {
32#endif
33
34//--------------------------------------------------------------------+
35// Macros Helper
36//--------------------------------------------------------------------+
37#define TU_ARRAY_SIZE(_arr) ( sizeof(_arr) / sizeof(_arr[0]) )
38#define TU_MIN(_x, _y) ( ( (_x) < (_y) ) ? (_x) : (_y) )
39#define TU_MAX(_x, _y) ( ( (_x) > (_y) ) ? (_x) : (_y) )
40#define TU_DIV_CEIL(n, d) (((n) + (d) - 1) / (d))
41
42#define TU_U16(_high, _low) ((uint16_t) (((_high) << 8) | (_low)))
43#define TU_U16_HIGH(_u16) ((uint8_t) (((_u16) >> 8) & 0x00ff))
44#define TU_U16_LOW(_u16) ((uint8_t) ((_u16) & 0x00ff))
45#define U16_TO_U8S_BE(_u16) TU_U16_HIGH(_u16), TU_U16_LOW(_u16)
46#define U16_TO_U8S_LE(_u16) TU_U16_LOW(_u16), TU_U16_HIGH(_u16)
47
48#define TU_U32_BYTE3(_u32) ((uint8_t) ((((uint32_t) _u32) >> 24) & 0x000000ff)) // MSB
49#define TU_U32_BYTE2(_u32) ((uint8_t) ((((uint32_t) _u32) >> 16) & 0x000000ff))
50#define TU_U32_BYTE1(_u32) ((uint8_t) ((((uint32_t) _u32) >> 8) & 0x000000ff))
51#define TU_U32_BYTE0(_u32) ((uint8_t) (((uint32_t) _u32) & 0x000000ff)) // LSB
52
53#define U32_TO_U8S_BE(_u32) TU_U32_BYTE3(_u32), TU_U32_BYTE2(_u32), TU_U32_BYTE1(_u32), TU_U32_BYTE0(_u32)
54#define U32_TO_U8S_LE(_u32) TU_U32_BYTE0(_u32), TU_U32_BYTE1(_u32), TU_U32_BYTE2(_u32), TU_U32_BYTE3(_u32)
55
56#define TU_BIT(n) (1UL << (n))
57
58// Generate a mask with bit from high (31) to low (0) set, e.g TU_GENMASK(3, 0) = 0b1111
59#define TU_GENMASK(h, l) ( (UINT32_MAX << (l)) & (UINT32_MAX >> (31 - (h))) )
60
61//--------------------------------------------------------------------+
62// Includes
63//--------------------------------------------------------------------+
64
65// Standard Headers
66#include <stdbool.h>
67#include <stdint.h>
68#include <inttypes.h>
69#include <stddef.h>
70#include <string.h>
71#include <stdio.h>
72
73// Tinyusb Common Headers
74#include "tusb_option.h"
75#include "tusb_compiler.h"
76#include "tusb_verify.h"
77#include "tusb_types.h"
78#include "tusb_debug.h"
79
80//--------------------------------------------------------------------+
81// Optional API implemented by application if needed
82// TODO move to a more obvious place/file
83//--------------------------------------------------------------------+
84
85// flush data cache
86TU_ATTR_WEAK extern void tusb_app_dcache_flush(uintptr_t addr, uint32_t data_size);
87
88// invalidate data cache
89TU_ATTR_WEAK extern void tusb_app_dcache_invalidate(uintptr_t addr, uint32_t data_size);
90
91// Optional physical <-> virtual address translation
92TU_ATTR_WEAK extern void* tusb_app_virt_to_phys(void *virt_addr);
93TU_ATTR_WEAK extern void* tusb_app_phys_to_virt(void *phys_addr);
94
95//--------------------------------------------------------------------+
96// Internal Inline Functions
97//--------------------------------------------------------------------+
98
99//------------- Mem -------------//
100#define tu_memclr(buffer, size) memset((buffer), 0, (size))
101#define tu_varclr(_var) tu_memclr(_var, sizeof(*(_var)))
102
103// This is a backport of memset_s from c11
104TU_ATTR_ALWAYS_INLINE static inline int tu_memset_s(void *dest, size_t destsz, int ch, size_t count) {
105 // TODO may check if desst and src is not NULL
106 if ( count > destsz ) {
107 return -1;
108 }
109 memset(dest, ch, count);
110 return 0;
111}
112
113// This is a backport of memcpy_s from c11
114TU_ATTR_ALWAYS_INLINE static inline int tu_memcpy_s(void *dest, size_t destsz, const void *src, size_t count) {
115 // TODO may check if desst and src is not NULL
116 if ( count > destsz ) {
117 return -1;
118 }
119 memcpy(dest, src, count);
120 return 0;
121}
122
123
124//------------- Bytes -------------//
125TU_ATTR_ALWAYS_INLINE static inline uint32_t tu_u32(uint8_t b3, uint8_t b2, uint8_t b1, uint8_t b0) {
126 return (((uint32_t)b3) << 24) | (((uint32_t)b2) << 16) | (((uint32_t)b1) << 8) | b0;
127}
128
129TU_ATTR_ALWAYS_INLINE static inline uint32_t tu_u32_from_u16(uint16_t high, uint16_t low) {
130 return (((uint32_t)high) << 16) | low;
131}
132
133TU_ATTR_ALWAYS_INLINE static inline uint16_t tu_u16(uint8_t high, uint8_t low) {
134 return (uint16_t)((((uint16_t)high) << 8) | low);
135}
136
137TU_ATTR_ALWAYS_INLINE static inline uint8_t tu_u32_byte3(uint32_t ui32) { return TU_U32_BYTE3(ui32); }
138TU_ATTR_ALWAYS_INLINE static inline uint8_t tu_u32_byte2(uint32_t ui32) { return TU_U32_BYTE2(ui32); }
139TU_ATTR_ALWAYS_INLINE static inline uint8_t tu_u32_byte1(uint32_t ui32) { return TU_U32_BYTE1(ui32); }
140TU_ATTR_ALWAYS_INLINE static inline uint8_t tu_u32_byte0(uint32_t ui32) { return TU_U32_BYTE0(ui32); }
141
142TU_ATTR_ALWAYS_INLINE static inline uint16_t tu_u32_high16(uint32_t ui32) { return (uint16_t) (ui32 >> 16); }
143TU_ATTR_ALWAYS_INLINE static inline uint16_t tu_u32_low16 (uint32_t ui32) { return (uint16_t) (ui32 & 0x0000ffffu); }
144
145TU_ATTR_ALWAYS_INLINE static inline uint8_t tu_u16_high(uint16_t ui16) { return TU_U16_HIGH(ui16); }
146TU_ATTR_ALWAYS_INLINE static inline uint8_t tu_u16_low (uint16_t ui16) { return TU_U16_LOW(ui16); }
147
148//------------- Bits -------------//
149TU_ATTR_ALWAYS_INLINE static inline uint32_t tu_bit_set (uint32_t value, uint8_t pos) { return value | TU_BIT(pos); }
150TU_ATTR_ALWAYS_INLINE static inline uint32_t tu_bit_clear(uint32_t value, uint8_t pos) { return value & (~TU_BIT(pos)); }
151TU_ATTR_ALWAYS_INLINE static inline bool tu_bit_test (uint32_t value, uint8_t pos) { return (value & TU_BIT(pos)) ? true : false; }
152
153//------------- Min -------------//
154TU_ATTR_ALWAYS_INLINE static inline uint8_t tu_min8 (uint8_t x, uint8_t y ) { return (x < y) ? x : y; }
155TU_ATTR_ALWAYS_INLINE static inline uint16_t tu_min16 (uint16_t x, uint16_t y) { return (x < y) ? x : y; }
156TU_ATTR_ALWAYS_INLINE static inline uint32_t tu_min32 (uint32_t x, uint32_t y) { return (x < y) ? x : y; }
157
158//------------- Max -------------//
159TU_ATTR_ALWAYS_INLINE static inline uint8_t tu_max8 (uint8_t x, uint8_t y ) { return (x > y) ? x : y; }
160TU_ATTR_ALWAYS_INLINE static inline uint16_t tu_max16 (uint16_t x, uint16_t y) { return (x > y) ? x : y; }
161TU_ATTR_ALWAYS_INLINE static inline uint32_t tu_max32 (uint32_t x, uint32_t y) { return (x > y) ? x : y; }
162
163//------------- Align -------------//
164TU_ATTR_ALWAYS_INLINE static inline uint32_t tu_align(uint32_t value, uint32_t alignment) {
165 return value & ((uint32_t) ~(alignment-1));
166}
167
168TU_ATTR_ALWAYS_INLINE static inline uint32_t tu_align4 (uint32_t value) { return (value & 0xFFFFFFFCUL); }
169TU_ATTR_ALWAYS_INLINE static inline uint32_t tu_align8 (uint32_t value) { return (value & 0xFFFFFFF8UL); }
170TU_ATTR_ALWAYS_INLINE static inline uint32_t tu_align16 (uint32_t value) { return (value & 0xFFFFFFF0UL); }
171TU_ATTR_ALWAYS_INLINE static inline uint32_t tu_align32 (uint32_t value) { return (value & 0xFFFFFFE0UL); }
172TU_ATTR_ALWAYS_INLINE static inline uint32_t tu_align4k (uint32_t value) { return (value & 0xFFFFF000UL); }
173TU_ATTR_ALWAYS_INLINE static inline uint32_t tu_offset4k(uint32_t value) { return (value & 0xFFFUL); }
174
175TU_ATTR_ALWAYS_INLINE static inline bool tu_is_aligned32(uint32_t value) { return (value & 0x1FUL) == 0; }
176TU_ATTR_ALWAYS_INLINE static inline bool tu_is_aligned64(uint64_t value) { return (value & 0x3FUL) == 0; }
177
178//------------- Mathematics -------------//
179TU_ATTR_ALWAYS_INLINE static inline uint32_t tu_div_ceil(uint32_t v, uint32_t d) { return (v + d -1)/d; }
180
181// log2 of a value is its MSB's position
182// TODO use clz TODO remove
183static inline uint8_t tu_log2(uint32_t value)
184{
185 uint8_t result = 0;
186 while (value >>= 1) { result++; }
187 return result;
188}
189
190//static inline uint8_t tu_log2(uint32_t value)
191//{
192// return sizeof(uint32_t) * CHAR_BIT - __builtin_clz(x) - 1;
193//}
194
195static inline bool tu_is_power_of_two(uint32_t value)
196{
197 return (value != 0) && ((value & (value - 1)) == 0);
198}
199
200//------------- Unaligned Access -------------//
201#if TUP_ARCH_STRICT_ALIGN
202
203// Rely on compiler to generate correct code for unaligned access
204typedef struct { uint16_t val; } TU_ATTR_PACKED tu_unaligned_uint16_t;
205typedef struct { uint32_t val; } TU_ATTR_PACKED tu_unaligned_uint32_t;
206
207TU_ATTR_ALWAYS_INLINE static inline uint32_t tu_unaligned_read32(const void* mem)
208{
209 tu_unaligned_uint32_t const* ua32 = (tu_unaligned_uint32_t const*) mem;
210 return ua32->val;
211}
212
213TU_ATTR_ALWAYS_INLINE static inline void tu_unaligned_write32(void* mem, uint32_t value)
214{
215 tu_unaligned_uint32_t* ua32 = (tu_unaligned_uint32_t*) mem;
216 ua32->val = value;
217}
218
219TU_ATTR_ALWAYS_INLINE static inline uint16_t tu_unaligned_read16(const void* mem)
220{
221 tu_unaligned_uint16_t const* ua16 = (tu_unaligned_uint16_t const*) mem;
222 return ua16->val;
223}
224
225TU_ATTR_ALWAYS_INLINE static inline void tu_unaligned_write16(void* mem, uint16_t value)
226{
227 tu_unaligned_uint16_t* ua16 = (tu_unaligned_uint16_t*) mem;
228 ua16->val = value;
229}
230
231#elif TUP_MCU_STRICT_ALIGN
232
233// MCU such as LPC_IP3511 Highspeed cannot access unaligned memory on USB_RAM although it is ARM M4.
234// We have to manually pick up bytes since tu_unaligned_uint32_t will still generate unaligned code
235// NOTE: volatile cast to memory to prevent compiler to optimize and generate unaligned code
236// TODO Big Endian may need minor changes
237TU_ATTR_ALWAYS_INLINE static inline uint32_t tu_unaligned_read32(const void* mem)
238{
239 volatile uint8_t const* buf8 = (uint8_t const*) mem;
240 return tu_u32(buf8[3], buf8[2], buf8[1], buf8[0]);
241}
242
243TU_ATTR_ALWAYS_INLINE static inline void tu_unaligned_write32(void* mem, uint32_t value)
244{
245 volatile uint8_t* buf8 = (uint8_t*) mem;
246 buf8[0] = tu_u32_byte0(value);
247 buf8[1] = tu_u32_byte1(value);
248 buf8[2] = tu_u32_byte2(value);
249 buf8[3] = tu_u32_byte3(value);
250}
251
252TU_ATTR_ALWAYS_INLINE static inline uint16_t tu_unaligned_read16(const void* mem)
253{
254 volatile uint8_t const* buf8 = (uint8_t const*) mem;
255 return tu_u16(buf8[1], buf8[0]);
256}
257
258TU_ATTR_ALWAYS_INLINE static inline void tu_unaligned_write16(void* mem, uint16_t value)
259{
260 volatile uint8_t* buf8 = (uint8_t*) mem;
261 buf8[0] = tu_u16_low(value);
262 buf8[1] = tu_u16_high(value);
263}
264
265
266#else
267
268// MCU that could access unaligned memory natively
269TU_ATTR_ALWAYS_INLINE static inline uint32_t tu_unaligned_read32(const void *mem) {
270 return *((uint32_t const *) mem);
271}
272
273TU_ATTR_ALWAYS_INLINE static inline uint16_t tu_unaligned_read16(const void *mem) {
274 return *((uint16_t const *) mem);
275}
276
277TU_ATTR_ALWAYS_INLINE static inline void tu_unaligned_write32(void *mem, uint32_t value) {
278 *((uint32_t *) mem) = value;
279}
280
281TU_ATTR_ALWAYS_INLINE static inline void tu_unaligned_write16(void *mem, uint16_t value) {
282 *((uint16_t *) mem) = value;
283}
284
285#endif
286
287// To be removed
288//------------- Binary constant -------------//
289#if defined(__GNUC__) && !defined(__CC_ARM)
290
291#define TU_BIN8(x) ((uint8_t) (0b##x))
292#define TU_BIN16(b1, b2) ((uint16_t) (0b##b1##b2))
293#define TU_BIN32(b1, b2, b3, b4) ((uint32_t) (0b##b1##b2##b3##b4))
294
295#else
296
297// internal macro of B8, B16, B32
298#define _B8__(x) (((x&0x0000000FUL)?1:0) \
299 +((x&0x000000F0UL)?2:0) \
300 +((x&0x00000F00UL)?4:0) \
301 +((x&0x0000F000UL)?8:0) \
302 +((x&0x000F0000UL)?16:0) \
303 +((x&0x00F00000UL)?32:0) \
304 +((x&0x0F000000UL)?64:0) \
305 +((x&0xF0000000UL)?128:0))
306
307#define TU_BIN8(d) ((uint8_t) _B8__(0x##d##UL))
308#define TU_BIN16(dmsb,dlsb) (((uint16_t)TU_BIN8(dmsb)<<8) + TU_BIN8(dlsb))
309#define TU_BIN32(dmsb,db2,db3,dlsb) \
310 (((uint32_t)TU_BIN8(dmsb)<<24) \
311 + ((uint32_t)TU_BIN8(db2)<<16) \
312 + ((uint32_t)TU_BIN8(db3)<<8) \
313 + TU_BIN8(dlsb))
314#endif
315
316#ifdef __cplusplus
317 }
318#endif
319
320#endif /* _TUSB_COMMON_H_ */
static void * memcpy(void *dst, const void *src, size_t n)
Definition: ringbuffer.c:8
AUDIO Channel Cluster Descriptor (4.1)
Definition: audio.h:647
uint16_t val
Definition: tusb_common.h:204
uint32_t val
Definition: tusb_common.h:205
static TU_ATTR_ALWAYS_INLINE uint32_t tu_div_ceil(uint32_t v, uint32_t d)
Definition: tusb_common.h:179
static TU_ATTR_ALWAYS_INLINE uint32_t tu_bit_set(uint32_t value, uint8_t pos)
Definition: tusb_common.h:149
static TU_ATTR_ALWAYS_INLINE bool tu_is_aligned32(uint32_t value)
Definition: tusb_common.h:175
static TU_ATTR_ALWAYS_INLINE uint32_t tu_align32(uint32_t value)
Definition: tusb_common.h:171
static TU_ATTR_ALWAYS_INLINE uint32_t tu_bit_clear(uint32_t value, uint8_t pos)
Definition: tusb_common.h:150
static TU_ATTR_ALWAYS_INLINE uint8_t tu_u16_low(uint16_t ui16)
Definition: tusb_common.h:146
static TU_ATTR_ALWAYS_INLINE uint8_t tu_max8(uint8_t x, uint8_t y)
Definition: tusb_common.h:159
TU_ATTR_WEAK void * tusb_app_phys_to_virt(void *phys_addr)
static TU_ATTR_ALWAYS_INLINE uint32_t tu_align8(uint32_t value)
Definition: tusb_common.h:169
static TU_ATTR_ALWAYS_INLINE uint16_t tu_min16(uint16_t x, uint16_t y)
Definition: tusb_common.h:155
static TU_ATTR_ALWAYS_INLINE uint8_t tu_min8(uint8_t x, uint8_t y)
Definition: tusb_common.h:154
static TU_ATTR_ALWAYS_INLINE uint8_t tu_u32_byte1(uint32_t ui32)
Definition: tusb_common.h:139
static TU_ATTR_ALWAYS_INLINE uint16_t tu_u32_low16(uint32_t ui32)
Definition: tusb_common.h:143
static bool tu_is_power_of_two(uint32_t value)
Definition: tusb_common.h:195
static TU_ATTR_ALWAYS_INLINE uint8_t tu_u32_byte0(uint32_t ui32)
Definition: tusb_common.h:140
static TU_ATTR_ALWAYS_INLINE uint16_t tu_u32_high16(uint32_t ui32)
Definition: tusb_common.h:142
static TU_ATTR_ALWAYS_INLINE uint32_t tu_align4k(uint32_t value)
Definition: tusb_common.h:172
static TU_ATTR_ALWAYS_INLINE void tu_unaligned_write32(void *mem, uint32_t value)
Definition: tusb_common.h:213
static TU_ATTR_ALWAYS_INLINE uint32_t tu_min32(uint32_t x, uint32_t y)
Definition: tusb_common.h:156
static TU_ATTR_ALWAYS_INLINE int tu_memset_s(void *dest, size_t destsz, int ch, size_t count)
Definition: tusb_common.h:104
static TU_ATTR_ALWAYS_INLINE uint32_t tu_unaligned_read32(const void *mem)
Definition: tusb_common.h:207
static TU_ATTR_ALWAYS_INLINE uint8_t tu_u16_high(uint16_t ui16)
Definition: tusb_common.h:145
static TU_ATTR_ALWAYS_INLINE uint32_t tu_align4(uint32_t value)
Definition: tusb_common.h:168
TU_ATTR_WEAK void * tusb_app_virt_to_phys(void *virt_addr)
static TU_ATTR_ALWAYS_INLINE uint16_t tu_unaligned_read16(const void *mem)
Definition: tusb_common.h:219
static TU_ATTR_ALWAYS_INLINE bool tu_is_aligned64(uint64_t value)
Definition: tusb_common.h:176
static TU_ATTR_ALWAYS_INLINE uint32_t tu_max32(uint32_t x, uint32_t y)
Definition: tusb_common.h:161
TU_ATTR_WEAK void tusb_app_dcache_invalidate(uintptr_t addr, uint32_t data_size)
static TU_ATTR_ALWAYS_INLINE int tu_memcpy_s(void *dest, size_t destsz, const void *src, size_t count)
Definition: tusb_common.h:114
static uint8_t tu_log2(uint32_t value)
Definition: tusb_common.h:183
static TU_ATTR_ALWAYS_INLINE uint8_t tu_u32_byte2(uint32_t ui32)
Definition: tusb_common.h:138
static TU_ATTR_ALWAYS_INLINE uint32_t tu_u32(uint8_t b3, uint8_t b2, uint8_t b1, uint8_t b0)
Definition: tusb_common.h:125
static TU_ATTR_ALWAYS_INLINE void tu_unaligned_write16(void *mem, uint16_t value)
Definition: tusb_common.h:225
static TU_ATTR_ALWAYS_INLINE bool tu_bit_test(uint32_t value, uint8_t pos)
Definition: tusb_common.h:151
static TU_ATTR_ALWAYS_INLINE uint32_t tu_align16(uint32_t value)
Definition: tusb_common.h:170
static TU_ATTR_ALWAYS_INLINE uint32_t tu_align(uint32_t value, uint32_t alignment)
Definition: tusb_common.h:164
static TU_ATTR_ALWAYS_INLINE uint16_t tu_max16(uint16_t x, uint16_t y)
Definition: tusb_common.h:160
static TU_ATTR_ALWAYS_INLINE uint32_t tu_offset4k(uint32_t value)
Definition: tusb_common.h:173
static TU_ATTR_ALWAYS_INLINE uint32_t tu_u32_from_u16(uint16_t high, uint16_t low)
Definition: tusb_common.h:129
static TU_ATTR_ALWAYS_INLINE uint8_t tu_u32_byte3(uint32_t ui32)
Definition: tusb_common.h:137
static TU_ATTR_ALWAYS_INLINE uint16_t tu_u16(uint8_t high, uint8_t low)
Definition: tusb_common.h:133