Open FFBoard
Open source force feedback firmware
tusb_compiler.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
32
#ifndef _TUSB_COMPILER_H_
33
#define _TUSB_COMPILER_H_
34
35
#define TU_TOKEN(x) x
36
#define TU_STRING(x) #x
37
#define TU_XSTRING(x) TU_STRING(x)
38
39
#define TU_STRCAT(a, b) a##b
40
#define TU_STRCAT3(a, b, c) a##b##c
41
42
#define TU_XSTRCAT(a, b) TU_STRCAT(a, b)
43
#define TU_XSTRCAT3(a, b, c) TU_STRCAT3(a, b, c)
44
45
#define TU_INCLUDE_PATH(_dir,_file) TU_XSTRING( TU_TOKEN(_dir)TU_TOKEN(_file) )
46
47
#if defined __COUNTER__ && __COUNTER__ != __COUNTER__
48
#define _TU_COUNTER_ __COUNTER__
49
#else
50
#define _TU_COUNTER_ __LINE__
51
#endif
52
53
// Compile-time Assert
54
#if defined (__cplusplus) && __cplusplus >= 201103L
55
#define TU_VERIFY_STATIC static_assert
56
#elif defined (__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
57
#define TU_VERIFY_STATIC _Static_assert
58
#elif defined(__CCRX__)
59
#define TU_VERIFY_STATIC(const_expr, _mess) typedef char TU_XSTRCAT(_verify_static_, _TU_COUNTER_)[(const_expr) ? 1 : 0];
60
#else
61
#define TU_VERIFY_STATIC(const_expr, _mess) enum { TU_XSTRCAT(_verify_static_, _TU_COUNTER_) = 1/(!!(const_expr)) }
62
#endif
63
64
/* --------------------- Fuzzing types -------------------------------------- */
65
#ifdef _FUZZ
66
#define tu_static static __thread
67
#else
68
#define tu_static static
69
#endif
70
71
// for declaration of reserved field, make use of _TU_COUNTER_
72
#define TU_RESERVED TU_XSTRCAT(reserved, _TU_COUNTER_)
73
74
#define TU_LITTLE_ENDIAN (0x12u)
75
#define TU_BIG_ENDIAN (0x21u)
76
77
/*------------------------------------------------------------------*/
78
/* Count number of arguments of __VA_ARGS__
79
* - reference https://stackoverflow.com/questions/2124339/c-preprocessor-va-args-number-of-arguments
80
* - _GET_NTH_ARG() takes args >= N (64) but only expand to Nth one (64th)
81
* - _RSEQ_N() is reverse sequential to N to add padding to have
82
* Nth position is the same as the number of arguments
83
* - ##__VA_ARGS__ is used to deal with 0 paramerter (swallows comma)
84
*------------------------------------------------------------------*/
85
#if !defined(__CCRX__)
86
#define TU_ARGS_NUM(...) _TU_NARG(_0, ##__VA_ARGS__, _RSEQ_N())
87
#else
88
#define TU_ARGS_NUM(...) _TU_NARG(_0, __VA_ARGS__, _RSEQ_N())
89
#endif
90
91
#define _TU_NARG(...) _GET_NTH_ARG(__VA_ARGS__)
92
#define _GET_NTH_ARG( \
93
_1, _2, _3, _4, _5, _6, _7, _8, _9,_10, \
94
_11,_12,_13,_14,_15,_16,_17,_18,_19,_20, \
95
_21,_22,_23,_24,_25,_26,_27,_28,_29,_30, \
96
_31,_32,_33,_34,_35,_36,_37,_38,_39,_40, \
97
_41,_42,_43,_44,_45,_46,_47,_48,_49,_50, \
98
_51,_52,_53,_54,_55,_56,_57,_58,_59,_60, \
99
_61,_62,_63,N,...) N
100
#define _RSEQ_N() \
101
62,61,60, \
102
59,58,57,56,55,54,53,52,51,50, \
103
49,48,47,46,45,44,43,42,41,40, \
104
39,38,37,36,35,34,33,32,31,30, \
105
29,28,27,26,25,24,23,22,21,20, \
106
19,18,17,16,15,14,13,12,11,10, \
107
9,8,7,6,5,4,3,2,1,0
108
109
// Apply an macro X to each of the arguments with an separated of choice
110
#define TU_ARGS_APPLY(_X, _s, ...) TU_XSTRCAT(_TU_ARGS_APPLY_, TU_ARGS_NUM(__VA_ARGS__))(_X, _s, __VA_ARGS__)
111
112
#define _TU_ARGS_APPLY_1(_X, _s, _a1) _X(_a1)
113
#define _TU_ARGS_APPLY_2(_X, _s, _a1, _a2) _X(_a1) _s _X(_a2)
114
#define _TU_ARGS_APPLY_3(_X, _s, _a1, _a2, _a3) _X(_a1) _s _TU_ARGS_APPLY_2(_X, _s, _a2, _a3)
115
#define _TU_ARGS_APPLY_4(_X, _s, _a1, _a2, _a3, _a4) _X(_a1) _s _TU_ARGS_APPLY_3(_X, _s, _a2, _a3, _a4)
116
#define _TU_ARGS_APPLY_5(_X, _s, _a1, _a2, _a3, _a4, _a5) _X(_a1) _s _TU_ARGS_APPLY_4(_X, _s, _a2, _a3, _a4, _a5)
117
#define _TU_ARGS_APPLY_6(_X, _s, _a1, _a2, _a3, _a4, _a5, _a6) _X(_a1) _s _TU_ARGS_APPLY_5(_X, _s, _a2, _a3, _a4, _a5, _a6)
118
#define _TU_ARGS_APPLY_7(_X, _s, _a1, _a2, _a3, _a4, _a5, _a6, _a7) _X(_a1) _s _TU_ARGS_APPLY_6(_X, _s, _a2, _a3, _a4, _a5, _a6, _a7)
119
#define _TU_ARGS_APPLY_8(_X, _s, _a1, _a2, _a3, _a4, _a5, _a6, _a7, _a8) _X(_a1) _s _TU_ARGS_APPLY_7(_X, _s, _a2, _a3, _a4, _a5, _a6, _a7, _a8)
120
121
//--------------------------------------------------------------------+
122
// Macro for function default arguments
123
//--------------------------------------------------------------------+
124
#define TU_GET_3RD_ARG(arg1, arg2, arg3, ...) arg3
125
126
// function expand with number of arguments
127
#define TU_FUNC_OPTIONAL_ARG(func, ...) TU_XSTRCAT(func##_arg, TU_ARGS_NUM(__VA_ARGS__))(__VA_ARGS__)
128
129
//--------------------------------------------------------------------+
130
// Compiler porting with Attribute and Endian
131
//--------------------------------------------------------------------+
132
133
// TODO refactor since __attribute__ is supported across many compiler
134
#if defined(__GNUC__)
135
#define TU_ATTR_ALIGNED(Bytes) __attribute__ ((aligned(Bytes)))
136
#define TU_ATTR_SECTION(sec_name) __attribute__ ((section(#sec_name)))
137
#define TU_ATTR_PACKED __attribute__ ((packed))
138
#define TU_ATTR_WEAK __attribute__ ((weak))
139
// #define TU_ATTR_WEAK_ALIAS(f) __attribute__ ((weak, alias(#f))
140
#ifndef TU_ATTR_ALWAYS_INLINE
// allow to override for debug
141
#define TU_ATTR_ALWAYS_INLINE __attribute__ ((always_inline))
142
#endif
143
#define TU_ATTR_DEPRECATED(mess) __attribute__ ((deprecated(mess)))
// warn if function with this attribute is used
144
#define TU_ATTR_UNUSED __attribute__ ((unused))
// Function/Variable is meant to be possibly unused
145
#define TU_ATTR_USED __attribute__ ((used))
// Function/Variable is meant to be used
146
147
#define TU_ATTR_PACKED_BEGIN
148
#define TU_ATTR_PACKED_END
149
#define TU_ATTR_BIT_FIELD_ORDER_BEGIN
150
#define TU_ATTR_BIT_FIELD_ORDER_END
151
152
#if __GNUC__ < 5
153
#define TU_ATTR_FALLTHROUGH do {} while (0)
/* fallthrough */
154
#else
155
#if __has_attribute(__fallthrough__)
156
#define TU_ATTR_FALLTHROUGH __attribute__((fallthrough))
157
#else
158
#define TU_ATTR_FALLTHROUGH do {} while (0)
/* fallthrough */
159
#endif
160
#endif
161
162
// Endian conversion use well-known host to network (big endian) naming
163
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
164
#define TU_BYTE_ORDER TU_LITTLE_ENDIAN
165
#else
166
#define TU_BYTE_ORDER TU_BIG_ENDIAN
167
#endif
168
169
// Unfortunately XC16 doesn't provide builtins for 32bit endian conversion
170
#if defined(__XC16)
171
#define TU_BSWAP16(u16) (__builtin_swap(u16))
172
#define TU_BSWAP32(u32) ((((u32) & 0xff000000) >> 24) | \
173
(((u32) & 0x00ff0000) >> 8) | \
174
(((u32) & 0x0000ff00) << 8) | \
175
(((u32) & 0x000000ff) << 24))
176
#else
177
#define TU_BSWAP16(u16) (__builtin_bswap16(u16))
178
#define TU_BSWAP32(u32) (__builtin_bswap32(u32))
179
#endif
180
181
#ifndef __ARMCC_VERSION
182
// List of obsolete callback function that is renamed and should not be defined.
183
// Put it here since only gcc support this pragma
184
#pragma GCC poison tud_vendor_control_request_cb
185
#endif
186
187
#elif defined(__TI_COMPILER_VERSION__)
188
#define TU_ATTR_ALIGNED(Bytes) __attribute__ ((aligned(Bytes)))
189
#define TU_ATTR_SECTION(sec_name) __attribute__ ((section(#sec_name)))
190
#define TU_ATTR_PACKED __attribute__ ((packed))
191
#define TU_ATTR_WEAK __attribute__ ((weak))
192
#define TU_ATTR_ALWAYS_INLINE __attribute__ ((always_inline))
193
#define TU_ATTR_DEPRECATED(mess) __attribute__ ((deprecated(mess)))
// warn if function with this attribute is used
194
#define TU_ATTR_UNUSED __attribute__ ((unused))
// Function/Variable is meant to be possibly unused
195
#define TU_ATTR_USED __attribute__ ((used))
196
#define TU_ATTR_FALLTHROUGH __attribute__((fallthrough))
197
198
#define TU_ATTR_PACKED_BEGIN
199
#define TU_ATTR_PACKED_END
200
#define TU_ATTR_BIT_FIELD_ORDER_BEGIN
201
#define TU_ATTR_BIT_FIELD_ORDER_END
202
203
// __BYTE_ORDER is defined in the TI ARM compiler, but not MSP430 (which is little endian)
204
#if ((__BYTE_ORDER__) == (__ORDER_LITTLE_ENDIAN__)) || defined(__MSP430__)
205
#define TU_BYTE_ORDER TU_LITTLE_ENDIAN
206
#else
207
#define TU_BYTE_ORDER TU_BIG_ENDIAN
208
#endif
209
210
#define TU_BSWAP16(u16) (__builtin_bswap16(u16))
211
#define TU_BSWAP32(u32) (__builtin_bswap32(u32))
212
213
#elif defined(__ICCARM__)
214
#include <intrinsics.h>
215
#define TU_ATTR_ALIGNED(Bytes) __attribute__ ((aligned(Bytes)))
216
#define TU_ATTR_SECTION(sec_name) __attribute__ ((section(#sec_name)))
217
#define TU_ATTR_PACKED __attribute__ ((packed))
218
#define TU_ATTR_WEAK __attribute__ ((weak))
219
#ifndef TU_ATTR_ALWAYS_INLINE
// allow to override for debug
220
#define TU_ATTR_ALWAYS_INLINE __attribute__ ((always_inline))
221
#endif
222
#define TU_ATTR_DEPRECATED(mess) __attribute__ ((deprecated(mess)))
// warn if function with this attribute is used
223
#define TU_ATTR_UNUSED __attribute__ ((unused))
// Function/Variable is meant to be possibly unused
224
#define TU_ATTR_USED __attribute__ ((used))
// Function/Variable is meant to be used
225
#define TU_ATTR_FALLTHROUGH do {} while (0)
/* fallthrough */
226
227
#define TU_ATTR_PACKED_BEGIN
228
#define TU_ATTR_PACKED_END
229
#define TU_ATTR_BIT_FIELD_ORDER_BEGIN
230
#define TU_ATTR_BIT_FIELD_ORDER_END
231
232
// Endian conversion use well-known host to network (big endian) naming
233
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
234
#define TU_BYTE_ORDER TU_LITTLE_ENDIAN
235
#else
236
#define TU_BYTE_ORDER TU_BIG_ENDIAN
237
#endif
238
239
#define TU_BSWAP16(u16) (__iar_builtin_REV16(u16))
240
#define TU_BSWAP32(u32) (__iar_builtin_REV(u32))
241
242
#elif defined(__CCRX__)
243
#define TU_ATTR_ALIGNED(Bytes)
244
#define TU_ATTR_SECTION(sec_name)
245
#define TU_ATTR_PACKED
246
#define TU_ATTR_WEAK
247
#define TU_ATTR_ALWAYS_INLINE
248
#define TU_ATTR_DEPRECATED(mess)
249
#define TU_ATTR_UNUSED
250
#define TU_ATTR_USED
251
#define TU_ATTR_FALLTHROUGH do {} while (0)
/* fallthrough */
252
253
#define TU_ATTR_PACKED_BEGIN _Pragma("pack"
)
254
#define TU_ATTR_PACKED_END _Pragma("packoption"
)
255
#define TU_ATTR_BIT_FIELD_ORDER_BEGIN _Pragma("bit_order right"
)
256
#define TU_ATTR_BIT_FIELD_ORDER_END _Pragma("bit_order"
)
257
258
// Endian conversion use well-known host to network (big endian) naming
259
#if defined(__LIT)
260
#define TU_BYTE_ORDER TU_LITTLE_ENDIAN
261
#else
262
#define TU_BYTE_ORDER TU_BIG_ENDIAN
263
#endif
264
265
#define TU_BSWAP16(u16) ((unsigned short)_builtin_revw((unsigned long)u16))
266
#define TU_BSWAP32(u32) (_builtin_revl(u32))
267
268
#else
269
#error "Compiler attribute porting is required"
270
#endif
271
272
273
#if (TU_BYTE_ORDER == TU_LITTLE_ENDIAN)
274
275
#define tu_htons(u16) (TU_BSWAP16(u16))
276
#define tu_ntohs(u16) (TU_BSWAP16(u16))
277
278
#define tu_htonl(u32) (TU_BSWAP32(u32))
279
#define tu_ntohl(u32) (TU_BSWAP32(u32))
280
281
#define tu_htole16(u16) (u16)
282
#define tu_le16toh(u16) (u16)
283
284
#define tu_htole32(u32) (u32)
285
#define tu_le32toh(u32) (u32)
286
287
#elif (TU_BYTE_ORDER == TU_BIG_ENDIAN)
288
289
#define tu_htons(u16) (u16)
290
#define tu_ntohs(u16) (u16)
291
292
#define tu_htonl(u32) (u32)
293
#define tu_ntohl(u32) (u32)
294
295
#define tu_htole16(u16) (TU_BSWAP16(u16))
296
#define tu_le16toh(u16) (TU_BSWAP16(u16))
297
298
#define tu_htole32(u32) (TU_BSWAP32(u32))
299
#define tu_le32toh(u32) (TU_BSWAP32(u32))
300
301
#else
302
#error Byte order is undefined
303
#endif
304
305
#endif
/* _TUSB_COMPILER_H_ */
306
Firmware
FFBoard
USB
common
tusb_compiler.h
Generated by
1.9.2