blob: 9b9e2b007711100867ceca0fad44ffb8ea6d3184 [file] [log] [blame]
Austin Schuh41baf202022-01-01 14:33:40 -08001/*
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
41#define TU_U16_HIGH(_u16) ((uint8_t) (((_u16) >> 8) & 0x00ff))
42#define TU_U16_LOW(_u16) ((uint8_t) ((_u16) & 0x00ff))
43#define U16_TO_U8S_BE(_u16) TU_U16_HIGH(_u16), TU_U16_LOW(_u16)
44#define U16_TO_U8S_LE(_u16) TU_U16_LOW(_u16), TU_U16_HIGH(_u16)
45
46#define TU_U32_BYTE3(_u32) ((uint8_t) ((((uint32_t) _u32) >> 24) & 0x000000ff)) // MSB
47#define TU_U32_BYTE2(_u32) ((uint8_t) ((((uint32_t) _u32) >> 16) & 0x000000ff))
48#define TU_U32_BYTE1(_u32) ((uint8_t) ((((uint32_t) _u32) >> 8) & 0x000000ff))
49#define TU_U32_BYTE0(_u32) ((uint8_t) (((uint32_t) _u32) & 0x000000ff)) // LSB
50
51#define U32_TO_U8S_BE(_u32) TU_U32_BYTE3(_u32), TU_U32_BYTE2(_u32), TU_U32_BYTE1(_u32), TU_U32_BYTE0(_u32)
52#define U32_TO_U8S_LE(_u32) TU_U32_BYTE0(_u32), TU_U32_BYTE1(_u32), TU_U32_BYTE2(_u32), TU_U32_BYTE3(_u32)
53
54#define TU_BIT(n) (1UL << (n))
55#define TU_GENMASK(h, l) ( (UINT32_MAX << (l)) & (UINT32_MAX >> (31 - (h))) )
56
57//--------------------------------------------------------------------+
58// Includes
59//--------------------------------------------------------------------+
60
61// Standard Headers
62#include <stdbool.h>
63#include <stdint.h>
64#include <stddef.h>
65#include <string.h>
66#include <stdio.h>
67
68// Tinyusb Common Headers
69#include "tusb_option.h"
70#include "tusb_compiler.h"
71#include "tusb_verify.h"
72#include "tusb_types.h"
73
74#include "tusb_error.h" // TODO remove
75#include "tusb_timeout.h" // TODO remove
76
77//--------------------------------------------------------------------+
78// Internal Helper used by Host and Device Stack
79//--------------------------------------------------------------------+
80
81// Check if endpoint descriptor is valid per USB specs
82bool tu_edpt_validate(tusb_desc_endpoint_t const * desc_ep, tusb_speed_t speed);
83
84// Bind all endpoint of a interface descriptor to class driver
85void tu_edpt_bind_driver(uint8_t ep2drv[][2], tusb_desc_interface_t const* p_desc, uint16_t desc_len, uint8_t driver_id);
86
87// Calculate total length of n interfaces (depending on IAD)
88uint16_t tu_desc_get_interface_total_len(tusb_desc_interface_t const* desc_itf, uint8_t itf_count, uint16_t max_len);
89
90//--------------------------------------------------------------------+
91// Internal Inline Functions
92//--------------------------------------------------------------------+
93
94//------------- Mem -------------//
95#define tu_memclr(buffer, size) memset((buffer), 0, (size))
96#define tu_varclr(_var) tu_memclr(_var, sizeof(*(_var)))
97
98//------------- Bytes -------------//
99TU_ATTR_ALWAYS_INLINE static inline uint32_t tu_u32(uint8_t b3, uint8_t b2, uint8_t b1, uint8_t b0)
100{
101 return ( ((uint32_t) b3) << 24) | ( ((uint32_t) b2) << 16) | ( ((uint32_t) b1) << 8) | b0;
102}
103
104TU_ATTR_ALWAYS_INLINE static inline uint16_t tu_u16(uint8_t high, uint8_t low)
105{
106 return (uint16_t) ((((uint16_t) high) << 8) | low);
107}
108
109TU_ATTR_ALWAYS_INLINE static inline uint8_t tu_u32_byte3(uint32_t ui32) { return TU_U32_BYTE3(ui32); }
110TU_ATTR_ALWAYS_INLINE static inline uint8_t tu_u32_byte2(uint32_t ui32) { return TU_U32_BYTE2(ui32); }
111TU_ATTR_ALWAYS_INLINE static inline uint8_t tu_u32_byte1(uint32_t ui32) { return TU_U32_BYTE1(ui32); }
112TU_ATTR_ALWAYS_INLINE static inline uint8_t tu_u32_byte0(uint32_t ui32) { return TU_U32_BYTE0(ui32); }
113
114TU_ATTR_ALWAYS_INLINE static inline uint16_t tu_u32_high16(uint32_t ui32) { return (uint16_t) (ui32 >> 16); }
115TU_ATTR_ALWAYS_INLINE static inline uint16_t tu_u32_low16 (uint32_t ui32) { return (uint16_t) (ui32 & 0x0000ffffu); }
116
117TU_ATTR_ALWAYS_INLINE static inline uint8_t tu_u16_high(uint16_t ui16) { return TU_U16_HIGH(ui16); }
118TU_ATTR_ALWAYS_INLINE static inline uint8_t tu_u16_low (uint16_t ui16) { return TU_U16_LOW(ui16); }
119
120//------------- Bits -------------//
121TU_ATTR_ALWAYS_INLINE static inline uint32_t tu_bit_set (uint32_t value, uint8_t pos) { return value | TU_BIT(pos); }
122TU_ATTR_ALWAYS_INLINE static inline uint32_t tu_bit_clear(uint32_t value, uint8_t pos) { return value & (~TU_BIT(pos)); }
123TU_ATTR_ALWAYS_INLINE static inline bool tu_bit_test (uint32_t value, uint8_t pos) { return (value & TU_BIT(pos)) ? true : false; }
124
125//------------- Min -------------//
126TU_ATTR_ALWAYS_INLINE static inline uint8_t tu_min8 (uint8_t x, uint8_t y ) { return (x < y) ? x : y; }
127TU_ATTR_ALWAYS_INLINE static inline uint16_t tu_min16 (uint16_t x, uint16_t y) { return (x < y) ? x : y; }
128TU_ATTR_ALWAYS_INLINE static inline uint32_t tu_min32 (uint32_t x, uint32_t y) { return (x < y) ? x : y; }
129
130//------------- Max -------------//
131TU_ATTR_ALWAYS_INLINE static inline uint8_t tu_max8 (uint8_t x, uint8_t y ) { return (x > y) ? x : y; }
132TU_ATTR_ALWAYS_INLINE static inline uint16_t tu_max16 (uint16_t x, uint16_t y) { return (x > y) ? x : y; }
133TU_ATTR_ALWAYS_INLINE static inline uint32_t tu_max32 (uint32_t x, uint32_t y) { return (x > y) ? x : y; }
134
135//------------- Align -------------//
136TU_ATTR_ALWAYS_INLINE static inline uint32_t tu_align(uint32_t value, uint32_t alignment)
137{
138 return value & ((uint32_t) ~(alignment-1));
139}
140
141TU_ATTR_ALWAYS_INLINE static inline uint32_t tu_align16 (uint32_t value) { return (value & 0xFFFFFFF0UL); }
142TU_ATTR_ALWAYS_INLINE static inline uint32_t tu_align32 (uint32_t value) { return (value & 0xFFFFFFE0UL); }
143TU_ATTR_ALWAYS_INLINE static inline uint32_t tu_align4k (uint32_t value) { return (value & 0xFFFFF000UL); }
144TU_ATTR_ALWAYS_INLINE static inline uint32_t tu_offset4k(uint32_t value) { return (value & 0xFFFUL); }
145
146//------------- Mathematics -------------//
147TU_ATTR_ALWAYS_INLINE static inline uint32_t tu_div_ceil(uint32_t v, uint32_t d) { return (v + d -1)/d; }
148
149/// inclusive range checking TODO remove
150TU_ATTR_ALWAYS_INLINE static inline bool tu_within(uint32_t lower, uint32_t value, uint32_t upper)
151{
152 return (lower <= value) && (value <= upper);
153}
154
155// log2 of a value is its MSB's position
156// TODO use clz TODO remove
157static inline uint8_t tu_log2(uint32_t value)
158{
159 uint8_t result = 0;
160 while (value >>= 1) { result++; }
161 return result;
162}
163
164//------------- Unaligned Access -------------//
165#if TUP_ARCH_STRICT_ALIGN
166
167// Rely on compiler to generate correct code for unaligned access
168typedef struct { uint16_t val; } TU_ATTR_PACKED tu_unaligned_uint16_t;
169typedef struct { uint32_t val; } TU_ATTR_PACKED tu_unaligned_uint32_t;
170
171TU_ATTR_ALWAYS_INLINE static inline uint32_t tu_unaligned_read32(const void* mem)
172{
173 tu_unaligned_uint32_t const* ua32 = (tu_unaligned_uint32_t const*) mem;
174 return ua32->val;
175}
176
177TU_ATTR_ALWAYS_INLINE static inline void tu_unaligned_write32(void* mem, uint32_t value)
178{
179 tu_unaligned_uint32_t* ua32 = (tu_unaligned_uint32_t*) mem;
180 ua32->val = value;
181}
182
183TU_ATTR_ALWAYS_INLINE static inline uint16_t tu_unaligned_read16(const void* mem)
184{
185 tu_unaligned_uint16_t const* ua16 = (tu_unaligned_uint16_t const*) mem;
186 return ua16->val;
187}
188
189TU_ATTR_ALWAYS_INLINE static inline void tu_unaligned_write16(void* mem, uint16_t value)
190{
191 tu_unaligned_uint16_t* ua16 = (tu_unaligned_uint16_t*) mem;
192 ua16->val = value;
193}
194
195#elif TUP_MCU_STRICT_ALIGN
196
197// MCU such as LPC_IP3511 Highspeed cannot access unaligned memory on USB_RAM although it is ARM M4.
198// We have to manually pick up bytes since tu_unaligned_uint32_t will still generate unaligned code
199// NOTE: volatile cast to memory to prevent compiler to optimize and generate unaligned code
200// TODO Big Endian may need minor changes
201TU_ATTR_ALWAYS_INLINE static inline uint32_t tu_unaligned_read32(const void* mem)
202{
203 volatile uint8_t const* buf8 = (uint8_t const*) mem;
204 return tu_u32(buf8[3], buf8[2], buf8[1], buf8[0]);
205}
206
207TU_ATTR_ALWAYS_INLINE static inline void tu_unaligned_write32(void* mem, uint32_t value)
208{
209 volatile uint8_t* buf8 = (uint8_t*) mem;
210 buf8[0] = tu_u32_byte0(value);
211 buf8[1] = tu_u32_byte1(value);
212 buf8[2] = tu_u32_byte2(value);
213 buf8[3] = tu_u32_byte3(value);
214}
215
216TU_ATTR_ALWAYS_INLINE static inline uint16_t tu_unaligned_read16(const void* mem)
217{
218 volatile uint8_t const* buf8 = (uint8_t const*) mem;
219 return tu_u16(buf8[1], buf8[0]);
220}
221
222TU_ATTR_ALWAYS_INLINE static inline void tu_unaligned_write16(void* mem, uint16_t value)
223{
224 volatile uint8_t* buf8 = (uint8_t*) mem;
225 buf8[0] = tu_u16_low(value);
226 buf8[1] = tu_u16_high(value);
227}
228
229
230#else
231
232// MCU that could access unaligned memory natively
233TU_ATTR_ALWAYS_INLINE static inline uint32_t tu_unaligned_read32 (const void* mem) { return *((uint32_t const *) mem); }
234TU_ATTR_ALWAYS_INLINE static inline uint16_t tu_unaligned_read16 (const void* mem) { return *((uint16_t const *) mem); }
235
236TU_ATTR_ALWAYS_INLINE static inline void tu_unaligned_write32 (void* mem, uint32_t value ) { *((uint32_t*) mem) = value; }
237TU_ATTR_ALWAYS_INLINE static inline void tu_unaligned_write16 (void* mem, uint16_t value ) { *((uint16_t*) mem) = value; }
238
239#endif
240
241// To be removed
242//------------- Binary constant -------------//
243#if defined(__GNUC__) && !defined(__CC_ARM)
244
245#define TU_BIN8(x) ((uint8_t) (0b##x))
246#define TU_BIN16(b1, b2) ((uint16_t) (0b##b1##b2))
247#define TU_BIN32(b1, b2, b3, b4) ((uint32_t) (0b##b1##b2##b3##b4))
248
249#else
250
251// internal macro of B8, B16, B32
252#define _B8__(x) (((x&0x0000000FUL)?1:0) \
253 +((x&0x000000F0UL)?2:0) \
254 +((x&0x00000F00UL)?4:0) \
255 +((x&0x0000F000UL)?8:0) \
256 +((x&0x000F0000UL)?16:0) \
257 +((x&0x00F00000UL)?32:0) \
258 +((x&0x0F000000UL)?64:0) \
259 +((x&0xF0000000UL)?128:0))
260
261#define TU_BIN8(d) ((uint8_t) _B8__(0x##d##UL))
262#define TU_BIN16(dmsb,dlsb) (((uint16_t)TU_BIN8(dmsb)<<8) + TU_BIN8(dlsb))
263#define TU_BIN32(dmsb,db2,db3,dlsb) \
264 (((uint32_t)TU_BIN8(dmsb)<<24) \
265 + ((uint32_t)TU_BIN8(db2)<<16) \
266 + ((uint32_t)TU_BIN8(db3)<<8) \
267 + TU_BIN8(dlsb))
268#endif
269
270//--------------------------------------------------------------------+
271// Debug Function
272//--------------------------------------------------------------------+
273
274// CFG_TUSB_DEBUG for debugging
275// 0 : no debug
276// 1 : print error
277// 2 : print warning
278// 3 : print info
279#if CFG_TUSB_DEBUG
280
281void tu_print_mem(void const *buf, uint32_t count, uint8_t indent);
282
283#ifdef CFG_TUSB_DEBUG_PRINTF
284 extern int CFG_TUSB_DEBUG_PRINTF(const char *format, ...);
285 #define tu_printf CFG_TUSB_DEBUG_PRINTF
286#else
287 #define tu_printf printf
288#endif
289
290static inline
291void tu_print_var(uint8_t const* buf, uint32_t bufsize)
292{
293 for(uint32_t i=0; i<bufsize; i++) tu_printf("%02X ", buf[i]);
294}
295
296// Log with Level
297#define TU_LOG(n, ...) TU_XSTRCAT(TU_LOG, n)(__VA_ARGS__)
298#define TU_LOG_MEM(n, ...) TU_XSTRCAT3(TU_LOG, n, _MEM)(__VA_ARGS__)
299#define TU_LOG_VAR(n, ...) TU_XSTRCAT3(TU_LOG, n, _VAR)(__VA_ARGS__)
300#define TU_LOG_INT(n, ...) TU_XSTRCAT3(TU_LOG, n, _INT)(__VA_ARGS__)
301#define TU_LOG_HEX(n, ...) TU_XSTRCAT3(TU_LOG, n, _HEX)(__VA_ARGS__)
302#define TU_LOG_LOCATION() tu_printf("%s: %d:\r\n", __PRETTY_FUNCTION__, __LINE__)
303#define TU_LOG_FAILED() tu_printf("%s: %d: Failed\r\n", __PRETTY_FUNCTION__, __LINE__)
304
305// Log Level 1: Error
306#define TU_LOG1 tu_printf
307#define TU_LOG1_MEM tu_print_mem
308#define TU_LOG1_VAR(_x) tu_print_var((uint8_t const*)(_x), sizeof(*(_x)))
309#define TU_LOG1_INT(_x) tu_printf(#_x " = %ld\r\n", (unsigned long) (_x) )
310#define TU_LOG1_HEX(_x) tu_printf(#_x " = %lX\r\n", (unsigned long) (_x) )
311
312// Log Level 2: Warn
313#if CFG_TUSB_DEBUG >= 2
314 #define TU_LOG2 TU_LOG1
315 #define TU_LOG2_MEM TU_LOG1_MEM
316 #define TU_LOG2_VAR TU_LOG1_VAR
317 #define TU_LOG2_INT TU_LOG1_INT
318 #define TU_LOG2_HEX TU_LOG1_HEX
319#endif
320
321// Log Level 3: Info
322#if CFG_TUSB_DEBUG >= 3
323 #define TU_LOG3 TU_LOG1
324 #define TU_LOG3_MEM TU_LOG1_MEM
325 #define TU_LOG3_VAR TU_LOG1_VAR
326 #define TU_LOG3_INT TU_LOG1_INT
327 #define TU_LOG3_HEX TU_LOG1_HEX
328#endif
329
330typedef struct
331{
332 uint32_t key;
333 const char* data;
334} tu_lookup_entry_t;
335
336typedef struct
337{
338 uint16_t count;
339 tu_lookup_entry_t const* items;
340} tu_lookup_table_t;
341
342static inline const char* tu_lookup_find(tu_lookup_table_t const* p_table, uint32_t key)
343{
344 static char not_found[11];
345
346 for(uint16_t i=0; i<p_table->count; i++)
347 {
348 if (p_table->items[i].key == key) return p_table->items[i].data;
349 }
350
351 // not found return the key value in hex
352 sprintf(not_found, "0x%08lX", (unsigned long) key);
353
354 return not_found;
355}
356
357#endif // CFG_TUSB_DEBUG
358
359#ifndef TU_LOG
360#define TU_LOG(n, ...)
361#define TU_LOG_MEM(n, ...)
362#define TU_LOG_VAR(n, ...)
363#define TU_LOG_INT(n, ...)
364#define TU_LOG_HEX(n, ...)
365#define TU_LOG_LOCATION()
366#define TU_LOG_FAILED()
367#endif
368
369// TODO replace all TU_LOGn with TU_LOG(n)
370
371#define TU_LOG0(...)
372#define TU_LOG0_MEM(...)
373#define TU_LOG0_VAR(...)
374#define TU_LOG0_INT(...)
375#define TU_LOG0_HEX(...)
376
377
378#ifndef TU_LOG1
379 #define TU_LOG1(...)
380 #define TU_LOG1_MEM(...)
381 #define TU_LOG1_VAR(...)
382 #define TU_LOG1_INT(...)
383 #define TU_LOG1_HEX(...)
384#endif
385
386#ifndef TU_LOG2
387 #define TU_LOG2(...)
388 #define TU_LOG2_MEM(...)
389 #define TU_LOG2_VAR(...)
390 #define TU_LOG2_INT(...)
391 #define TU_LOG2_HEX(...)
392#endif
393
394#ifndef TU_LOG3
395 #define TU_LOG3(...)
396 #define TU_LOG3_MEM(...)
397 #define TU_LOG3_VAR(...)
398 #define TU_LOG3_INT(...)
399 #define TU_LOG3_HEX(...)
400#endif
401
402#ifdef __cplusplus
403 }
404#endif
405
406#endif /* _TUSB_COMMON_H_ */