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