• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2023, sakumisu
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 #ifndef USB_UTIL_H
7 #define USB_UTIL_H
8 
9 #if defined(__CC_ARM)
10 #ifndef __USED
11 #define __USED __attribute__((used))
12 #endif
13 #ifndef __WEAK
14 #define __WEAK __attribute__((weak))
15 #endif
16 #ifndef __PACKED
17 #define __PACKED __attribute__((packed))
18 #endif
19 #ifndef __PACKED_STRUCT
20 #define __PACKED_STRUCT __packed struct
21 #endif
22 #ifndef __PACKED_UNION
23 #define __PACKED_UNION __packed union
24 #endif
25 #ifndef __ALIGNED
26 #define __ALIGNED(x) __attribute__((aligned(x)))
27 #endif
28 #elif defined(__GNUC__)
29 #ifndef __USED
30 #define __USED __attribute__((used))
31 #endif
32 #ifndef __WEAK
33 #define __WEAK __attribute__((weak))
34 #endif
35 #ifndef __PACKED
36 #define __PACKED __attribute__((packed, aligned(1)))
37 #endif
38 #ifndef __PACKED_STRUCT
39 #define __PACKED_STRUCT struct __attribute__((packed, aligned(1)))
40 #endif
41 #ifndef __PACKED_UNION
42 #define __PACKED_UNION union __attribute__((packed, aligned(1)))
43 #endif
44 #ifndef __ALIGNED
45 #define __ALIGNED(x) __attribute__((aligned(x)))
46 #endif
47 #elif defined(__ICCARM__) || defined(__ICCRX__) || defined(__ICCRISCV__)
48 #ifndef __USED
49 #if defined(__ICCARM_V8) || defined (__ICCRISCV__)
50 #define __USED __attribute__((used))
51 #else
52 #define __USED __root
53 #endif
54 #endif
55 
56 #ifndef __WEAK
57 #if defined(__ICCARM_V8) || defined (__ICCRISCV__)
58 #define __WEAK __attribute__((weak))
59 #else
60 #define __WEAK _Pragma("__weak")
61 #endif
62 #endif
63 
64 #ifndef __PACKED
65 #if defined(__ICCARM_V8) || defined (__ICCRISCV__)
66 #define __PACKED __attribute__((packed, aligned(1)))
67 #else
68 /* Needs IAR language extensions */
69 #define __PACKED __packed
70 #endif
71 #endif
72 
73 #ifndef __PACKED_STRUCT
74 #if defined(__ICCARM_V8) || defined (__ICCRISCV__)
75 #define __PACKED_STRUCT struct __attribute__((packed, aligned(1)))
76 #else
77 /* Needs IAR language extensions */
78 #define __PACKED_STRUCT __packed struct
79 #endif
80 #endif
81 
82 #ifndef __PACKED_UNION
83 #if defined(__ICCARM_V8) || defined (__ICCRISCV__)
84 #define __PACKED_UNION union __attribute__((packed, aligned(1)))
85 #else
86 /* Needs IAR language extensions */
87 #define __PACKED_UNION __packed union
88 #endif
89 #endif
90 
91 #ifndef __ALIGNED
92 #if defined(__ICCARM_V8) || defined (__ICCRISCV__)
93 #define __ALIGNED(x) __attribute__((aligned(x)))
94 #elif (__VER__ >= 7080000)
95 /* Needs IAR language extensions */
96 #define __ALIGNED(x) __attribute__((aligned(x)))
97 #else
98 #warning No compiler specific solution for __ALIGNED.__ALIGNED is ignored.
99 #define __ALIGNED(x)
100 #endif
101 #endif
102 
103 #endif
104 
105 #ifndef __ALIGN_BEGIN
106 #define __ALIGN_BEGIN
107 #endif
108 #ifndef __ALIGN_END
109 #define __ALIGN_END __attribute__((aligned(4)))
110 #endif
111 
112 #ifndef ARG_UNUSED
113 #define ARG_UNUSED(x) (void)(x)
114 #endif
115 
116 #ifndef LO_BYTE
117 #define LO_BYTE(x) ((uint8_t)(x & 0x00FF))
118 #endif
119 
120 #ifndef HI_BYTE
121 #define HI_BYTE(x) ((uint8_t)((x & 0xFF00) >> 8))
122 #endif
123 
124 #ifndef MAX
125 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
126 #endif
127 
128 #ifndef MIN
129 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
130 #endif
131 
132 #ifndef BCD
133 #define BCD(x) ((((x) / 10) << 4) | ((x) % 10))
134 #endif
135 
136 #ifdef BIT
137 #undef BIT
138 #define BIT(n) (1UL << (n))
139 #else
140 #define BIT(n) (1UL << (n))
141 #endif
142 
143 #ifndef ARRAY_SIZE
144 #define ARRAY_SIZE(array) \
145     ((int)((sizeof(array) / sizeof((array)[0]))))
146 #endif
147 
148 #ifndef BSWAP16
149 #define BSWAP16(u16) (__builtin_bswap16(u16))
150 #endif
151 #ifndef BSWAP32
152 #define BSWAP32(u32) (__builtin_bswap32(u32))
153 #endif
154 
155 #define GET_BE16(field) \
156     (((uint16_t)(field)[0] << 8) | ((uint16_t)(field)[1]))
157 
158 #define GET_BE32(field) \
159     (((uint32_t)(field)[0] << 24) | ((uint32_t)(field)[1] << 16) | ((uint32_t)(field)[2] << 8) | ((uint32_t)(field)[3] << 0))
160 
161 #define SET_BE16(field, value)                \
162     do {                                      \
163         (field)[0] = (uint8_t)((value) >> 8); \
164         (field)[1] = (uint8_t)((value) >> 0); \
165     } while (0)
166 
167 #define SET_BE24(field, value)                 \
168     do {                                       \
169         (field)[0] = (uint8_t)((value) >> 16); \
170         (field)[1] = (uint8_t)((value) >> 8);  \
171         (field)[2] = (uint8_t)((value) >> 0);  \
172     } while (0)
173 
174 #define SET_BE32(field, value)                 \
175     do {                                       \
176         (field)[0] = (uint8_t)((value) >> 24); \
177         (field)[1] = (uint8_t)((value) >> 16); \
178         (field)[2] = (uint8_t)((value) >> 8);  \
179         (field)[3] = (uint8_t)((value) >> 0);  \
180     } while (0)
181 
182 #define WBVAL(x) (x & 0xFF), ((x >> 8) & 0xFF)
183 #define DBVAL(x) (x & 0xFF), ((x >> 8) & 0xFF), ((x >> 16) & 0xFF), ((x >> 24) & 0xFF)
184 
185 #define PP_NARG(...) \
186     PP_NARG_(__VA_ARGS__, PP_RSEQ_N())
187 #define PP_NARG_(...) \
188     PP_ARG_N(__VA_ARGS__)
189 #define PP_ARG_N(                                     \
190     _1, _2, _3, _4, _5, _6, _7, _8, _9, _10,          \
191     _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, \
192     _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, \
193     _31, _32, _33, _34, _35, _36, _37, _38, _39, _40, \
194     _41, _42, _43, _44, _45, _46, _47, _48, _49, _50, \
195     _51, _52, _53, _54, _55, _56, _57, _58, _59, _60, \
196     _61, _62, _63, N, ...) N
197 #define PP_RSEQ_N()                             \
198     63, 62, 61, 60,                             \
199         59, 58, 57, 56, 55, 54, 53, 52, 51, 50, \
200         49, 48, 47, 46, 45, 44, 43, 42, 41, 40, \
201         39, 38, 37, 36, 35, 34, 33, 32, 31, 30, \
202         29, 28, 27, 26, 25, 24, 23, 22, 21, 20, \
203         19, 18, 17, 16, 15, 14, 13, 12, 11, 10, \
204         9, 8, 7, 6, 5, 4, 3, 2, 1, 0
205 
206 #endif /* USB_UTIL_H */
207