common: Add NXP's USB serial code.
[gps-watch.git] / src / common / usb_misc.h
1 /*
2  * The Clear BSD License
3  * Copyright (c) 2015 - 2016, Freescale Semiconductor, Inc.
4  * Copyright 2016 NXP
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without modification,
8  * are permitted (subject to the limitations in the disclaimer below) provided
9  * that the following conditions are met:
10  *
11  * o Redistributions of source code must retain the above copyright notice, this list
12  *   of conditions and the following disclaimer.
13  *
14  * o Redistributions in binary form must reproduce the above copyright notice, this
15  *   list of conditions and the following disclaimer in the documentation and/or
16  *   other materials provided with the distribution.
17  *
18  * o Neither the name of the copyright holder nor the names of its
19  *   contributors may be used to endorse or promote products derived from this
20  *   software without specific prior written permission.
21  *
22  * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
27  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
30  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34
35 #ifndef __USB_MISC_H__
36 #define __USB_MISC_H__
37
38 #ifndef ENDIANNESS
39
40 #error ENDIANNESS should be defined, and then rebulid the project.
41
42 #endif
43
44 /*******************************************************************************
45  * Definitions
46  ******************************************************************************/
47
48 /*! @brief Define USB printf */
49 #if defined(__cplusplus)
50 extern "C" {
51 #endif /* __cplusplus */
52
53 extern int DbgConsole_Printf(const char *fmt_s, ...);
54
55 #if defined(__cplusplus)
56 }
57 #endif /* __cplusplus */
58
59 #if defined(SDK_DEBUGCONSOLE) && (SDK_DEBUGCONSOLE < 1)
60 #define usb_echo printf
61 #else
62 #define usb_echo DbgConsole_Printf
63 #endif
64
65 #if defined(__ICCARM__)
66
67 #ifndef STRUCT_PACKED
68 #define STRUCT_PACKED __packed
69 #endif
70
71 #ifndef STRUCT_UNPACKED
72 #define STRUCT_UNPACKED
73 #endif
74
75 #elif defined(__GNUC__)
76
77 #ifndef STRUCT_PACKED
78 #define STRUCT_PACKED
79 #endif
80
81 #ifndef STRUCT_UNPACKED
82 #define STRUCT_UNPACKED __attribute__((__packed__))
83 #endif
84
85 #elif defined(__CC_ARM)
86
87 #ifndef STRUCT_PACKED
88 #define STRUCT_PACKED _Pragma("pack(1U)")
89 #endif
90
91 #ifndef STRUCT_UNPACKED
92 #define STRUCT_UNPACKED _Pragma("pack()")
93 #endif
94
95 #endif
96
97 #define USB_SHORT_GET_LOW(x) (((uint16_t)x) & 0xFFU)
98 #define USB_SHORT_GET_HIGH(x) ((uint8_t)(((uint16_t)x) >> 8U) & 0xFFU)
99
100 #define USB_LONG_GET_BYTE0(x) ((uint8_t)(((uint32_t)(x))) & 0xFFU)
101 #define USB_LONG_GET_BYTE1(x) ((uint8_t)(((uint32_t)(x)) >> 8U) & 0xFFU)
102 #define USB_LONG_GET_BYTE2(x) ((uint8_t)(((uint32_t)(x)) >> 16U) & 0xFFU)
103 #define USB_LONG_GET_BYTE3(x) ((uint8_t)(((uint32_t)(x)) >> 24U) & 0xFFU)
104
105 #define USB_MEM4_ALIGN_MASK (0x03U)
106
107 /* accessory macro */
108 #define USB_MEM4_ALIGN(n) ((n + 3U) & (0xFFFFFFFCu))
109 #define USB_MEM32_ALIGN(n) ((n + 31U) & (0xFFFFFFE0u))
110 #define USB_MEM64_ALIGN(n) ((n + 63U) & (0xFFFFFFC0u))
111
112 /* big/little endian */
113 #define SWAP2BYTE_CONST(n) ((((n)&0x00FFU) << 8U) | (((n)&0xFF00U) >> 8U))
114 #define SWAP4BYTE_CONST(n) \
115     ((((n)&0x000000FFU) << 24U) | (((n)&0x0000FF00U) << 8U) | (((n)&0x00FF0000U) >> 8U) | (((n)&0xFF000000U) >> 24U))
116
117 #define USB_ASSIGN_VALUE_ADDRESS_LONG_BY_BYTE(n, m)      \
118     {                                                    \
119         *((uint8_t *)&(n)) = *((uint8_t *)&(m));         \
120         *((uint8_t *)&(n) + 1) = *((uint8_t *)&(m) + 1); \
121         *((uint8_t *)&(n) + 2) = *((uint8_t *)&(m) + 2); \
122         *((uint8_t *)&(n) + 3) = *((uint8_t *)&(m) + 3); \
123     }
124
125 #define USB_ASSIGN_VALUE_ADDRESS_SHORT_BY_BYTE(n, m)     \
126     {                                                    \
127         *((uint8_t *)&(n)) = *((uint8_t *)&(m));         \
128         *((uint8_t *)&(n) + 1) = *((uint8_t *)&(m) + 1); \
129     }
130
131 #define USB_ASSIGN_MACRO_VALUE_ADDRESS_LONG_BY_BYTE(n, m) \
132     {                                                     \
133         *((uint8_t *)&(n)) = (uint8_t)m;                  \
134         *((uint8_t *)&(n) + 1) = (uint8_t)(m >> 8);       \
135         *((uint8_t *)&(n) + 2) = (uint8_t)(m >> 16);      \
136         *((uint8_t *)&(n) + 3) = (uint8_t)(m >> 24);      \
137     }
138
139 #define USB_ASSIGN_MACRO_VALUE_ADDRESS_SHORT_BY_BYTE(n, m) \
140     {                                                      \
141         *((uint8_t *)&(n)) = (uint8_t)m;                   \
142         *((uint8_t *)&(n) + 1) = (uint8_t)(m >> 8);        \
143     }
144
145 #if (ENDIANNESS == USB_BIG_ENDIAN)
146
147 #define USB_SHORT_TO_LITTLE_ENDIAN(n) SWAP2BYTE_CONST(n)
148 #define USB_LONG_TO_LITTLE_ENDIAN(n) SWAP4BYTE_CONST(n)
149 #define USB_SHORT_FROM_LITTLE_ENDIAN(n) SWAP2BYTE_CONST(n)
150 #define USB_LONG_FROM_LITTLE_ENDIAN(n) SWAP2BYTE_CONST(n)
151
152 #define USB_SHORT_TO_BIG_ENDIAN(n) (n)
153 #define USB_LONG_TO_BIG_ENDIAN(n) (n)
154 #define USB_SHORT_FROM_BIG_ENDIAN(n) (n)
155 #define USB_LONG_FROM_BIG_ENDIAN(n) (n)
156
157 #define USB_LONG_TO_LITTLE_ENDIAN_ADDRESS(n, m)    \
158     {                                              \
159         m[3] = ((((uint32_t)(n)) >> 24U) & 0xFFU); \
160         m[2] = ((((uint32_t)(n)) >> 16U) & 0xFFU); \
161         m[1] = ((((uint32_t)(n)) >> 8U) & 0xFFU);  \
162         m[0] = (((uint32_t)(n)) & 0xFFU);          \
163     }
164
165 #define USB_LONG_FROM_LITTLE_ENDIAN_ADDRESS(n)                                                  \
166     ((uint32_t)((((uint8_t)n[3]) << 24U) | (((uint8_t)n[2]) << 16U) | (((uint8_t)n[1]) << 8U) | \
167                 (((uint8_t)n[0]) << 0U)))
168
169 #define USB_LONG_TO_BIG_ENDIAN_ADDRESS(n, m)       \
170     {                                              \
171         m[0] = ((((uint32_t)(n)) >> 24U) & 0xFFU); \
172         m[1] = ((((uint32_t)(n)) >> 16U) & 0xFFU); \
173         m[2] = ((((uint32_t)(n)) >> 8U) & 0xFFU);  \
174         m[3] = (((uint32_t)(n)) & 0xFFU);          \
175     }
176
177 #define USB_LONG_FROM_BIG_ENDIAN_ADDRESS(n)                                                     \
178     ((uint32_t)((((uint8_t)n[0]) << 24U) | (((uint8_t)n[1]) << 16U) | (((uint8_t)n[2]) << 8U) | \
179                 (((uint8_t)n[3]) << 0U)))
180
181 #define USB_SHORT_TO_LITTLE_ENDIAN_ADDRESS(n, m)  \
182     {                                             \
183         m[1] = ((((uint16_t)(n)) >> 8U) & 0xFFU); \
184         m[0] = (((uint16_t)(n)) & 0xFFU);         \
185     }
186
187 #define USB_SHORT_FROM_LITTLE_ENDIAN_ADDRESS(n) ((uint32_t)((((uint8_t)n[1]) << 8U) | (((uint8_t)n[0]) << 0U)))
188
189 #define USB_SHORT_TO_BIG_ENDIAN_ADDRESS(n, m)     \
190     {                                             \
191         m[0] = ((((uint16_t)(n)) >> 8U) & 0xFFU); \
192         m[1] = (((uint16_t)(n)) & 0xFFU);         \
193     }
194
195 #define USB_SHORT_FROM_BIG_ENDIAN_ADDRESS(n) ((uint32_t)((((uint8_t)n[0]) << 8U) | (((uint8_t)n[1]) << 0U)))
196
197 #define USB_LONG_TO_LITTLE_ENDIAN_DATA(n, m)                         \
198     {                                                                \
199         *((uint8_t *)&(m) + 3) = ((((uint32_t)(n)) >> 24U) & 0xFFU); \
200         *((uint8_t *)&(m) + 2) = ((((uint32_t)(n)) >> 16U) & 0xFFU); \
201         *((uint8_t *)&(m) + 1) = ((((uint32_t)(n)) >> 8U) & 0xFFU);  \
202         *((uint8_t *)&(m) + 0) = (((uint32_t)(n)) & 0xFFU);          \
203     }
204
205 #define USB_LONG_FROM_LITTLE_ENDIAN_DATA(n)                                             \
206     ((uint32_t)(((*((uint8_t *)&(n) + 3)) << 24U) | ((*((uint8_t *)&(n) + 2)) << 16U) | \
207                 ((*((uint8_t *)&(n) + 1)) << 8U) | ((*((uint8_t *)&(n))) << 0U)))
208
209 #define USB_SHORT_TO_LITTLE_ENDIAN_DATA(n, m)                       \
210     {                                                               \
211         *((uint8_t *)&(m) + 1) = ((((uint16_t)(n)) >> 8U) & 0xFFU); \
212         *((uint8_t *)&(m)) = ((((uint16_t)(n))) & 0xFFU);           \
213     }
214
215 #define USB_SHORT_FROM_LITTLE_ENDIAN_DATA(n) ((uint32_t)(((*((uint8_t *)&(n) + 1)) << 8U) | ((*((uint8_t *)&(n))))))
216
217 #else
218
219 #define USB_SHORT_TO_LITTLE_ENDIAN(n) (n)
220 #define USB_LONG_TO_LITTLE_ENDIAN(n) (n)
221 #define USB_SHORT_FROM_LITTLE_ENDIAN(n) (n)
222 #define USB_LONG_FROM_LITTLE_ENDIAN(n) (n)
223
224 #define USB_SHORT_TO_BIG_ENDIAN(n) SWAP2BYTE_CONST(n)
225 #define USB_LONG_TO_BIG_ENDIAN(n) SWAP4BYTE_CONST(n)
226 #define USB_SHORT_FROM_BIG_ENDIAN(n) SWAP2BYTE_CONST(n)
227 #define USB_LONG_FROM_BIG_ENDIAN(n) SWAP4BYTE_CONST(n)
228
229 #define USB_LONG_TO_LITTLE_ENDIAN_ADDRESS(n, m)    \
230     {                                              \
231         m[3] = ((((uint32_t)(n)) >> 24U) & 0xFFU); \
232         m[2] = ((((uint32_t)(n)) >> 16U) & 0xFFU); \
233         m[1] = ((((uint32_t)(n)) >> 8U) & 0xFFU);  \
234         m[0] = (((uint32_t)(n)) & 0xFFU);          \
235     }
236
237 #define USB_LONG_FROM_LITTLE_ENDIAN_ADDRESS(n)                                                  \
238     ((uint32_t)((((uint8_t)n[3]) << 24U) | (((uint8_t)n[2]) << 16U) | (((uint8_t)n[1]) << 8U) | \
239                 (((uint8_t)n[0]) << 0U)))
240
241 #define USB_LONG_TO_BIG_ENDIAN_ADDRESS(n, m)       \
242     {                                              \
243         m[0] = ((((uint32_t)(n)) >> 24U) & 0xFFU); \
244         m[1] = ((((uint32_t)(n)) >> 16U) & 0xFFU); \
245         m[2] = ((((uint32_t)(n)) >> 8U) & 0xFFU);  \
246         m[3] = (((uint32_t)(n)) & 0xFFU);          \
247     }
248
249 #define USB_LONG_FROM_BIG_ENDIAN_ADDRESS(n)                                                     \
250     ((uint32_t)((((uint8_t)n[0]) << 24U) | (((uint8_t)n[1]) << 16U) | (((uint8_t)n[2]) << 8U) | \
251                 (((uint8_t)n[3]) << 0U)))
252
253 #define USB_SHORT_TO_LITTLE_ENDIAN_ADDRESS(n, m)  \
254     {                                             \
255         m[1] = ((((uint16_t)(n)) >> 8U) & 0xFFU); \
256         m[0] = (((uint16_t)(n)) & 0xFFU);         \
257     }
258
259 #define USB_SHORT_FROM_LITTLE_ENDIAN_ADDRESS(n) ((uint32_t)((((uint8_t)n[1]) << 8U) | (((uint8_t)n[0]) << 0U)))
260
261 #define USB_SHORT_TO_BIG_ENDIAN_ADDRESS(n, m)     \
262     {                                             \
263         m[0] = ((((uint16_t)(n)) >> 8U) & 0xFFU); \
264         m[1] = (((uint16_t)(n)) & 0xFFU);         \
265     }
266
267 #define USB_SHORT_FROM_BIG_ENDIAN_ADDRESS(n) ((uint32_t)((((uint8_t)n[0]) << 8U) | (((uint8_t)n[1]) << 0U)))
268
269 #define USB_LONG_TO_LITTLE_ENDIAN_DATA(n, m)                         \
270     {                                                                \
271         *((uint8_t *)&(m) + 3) = ((((uint32_t)(n)) >> 24U) & 0xFFU); \
272         *((uint8_t *)&(m) + 2) = ((((uint32_t)(n)) >> 16U) & 0xFFU); \
273         *((uint8_t *)&(m) + 1) = ((((uint32_t)(n)) >> 8U) & 0xFFU);  \
274         *((uint8_t *)&(m) + 0) = (((uint32_t)(n)) & 0xFFU);          \
275     }
276
277 #define USB_LONG_FROM_LITTLE_ENDIAN_DATA(n)                                             \
278     ((uint32_t)(((*((uint8_t *)&(n) + 3)) << 24U) | ((*((uint8_t *)&(n) + 2)) << 16U) | \
279                 ((*((uint8_t *)&(n) + 1)) << 8U) | ((*((uint8_t *)&(n))) << 0U)))
280
281 #define USB_SHORT_TO_LITTLE_ENDIAN_DATA(n, m)                       \
282     {                                                               \
283         *((uint8_t *)&(m) + 1) = ((((uint16_t)(n)) >> 8U) & 0xFFU); \
284         *((uint8_t *)&(m)) = ((((uint16_t)(n))) & 0xFFU);           \
285     }
286
287 #define USB_SHORT_FROM_LITTLE_ENDIAN_DATA(n) ((uint32_t)(((*((uint8_t *)&(n) + 1)) << 8U) | ((*((uint8_t *)&(n))))))
288
289 #endif
290
291 /*
292  * The following MACROs (USB_GLOBAL, USB_BDT, USB_RAM_ADDRESS_ALIGNMENT, etc) are only used for USB device stack.
293  * The USB device global variables are put into the section m_usb_global and m_usb_bdt or the section
294  * .bss.m_usb_global and .bss.m_usb_bdt by using the MACRO USB_GLOBAL and USB_BDT. In this way, the USB device
295  * global variables can be linked into USB dedicated RAM by USB_STACK_USE_DEDICATED_RAM.
296  * The MACRO USB_STACK_USE_DEDICATED_RAM is used to decide the USB stack uses dedicated RAM or not. The value of
297  * the marco can be set as 0, USB_STACK_DEDICATED_RAM_TYPE_BDT_GLOBAL, or USB_STACK_DEDICATED_RAM_TYPE_BDT.
298  * The MACRO USB_STACK_DEDICATED_RAM_TYPE_BDT_GLOBAL means USB device global variables, including USB_BDT and
299  * USB_GLOBAL, are put into the USB dedicated RAM. This feature can only be enabled when the USB dedicated RAM
300  * is not less than 2K Bytes.
301  * The MACRO USB_STACK_DEDICATED_RAM_TYPE_BDT means USB device global variables, only including USB_BDT, are put
302  * into the USB dedicated RAM, the USB_GLOBAL will be put into .bss section. This feature is used for some SOCs,
303  * the USB dedicated RAM size is not more than 512 Bytes.
304  */
305 #define USB_STACK_DEDICATED_RAM_TYPE_BDT_GLOBAL 1
306 #define USB_STACK_DEDICATED_RAM_TYPE_BDT 2
307
308 #if defined(__ICCARM__)
309
310 #define USB_WEAK_VAR __attribute__((weak))
311 #define USB_WEAK_FUN __attribute__((weak))
312 /* disable misra 19.13 */
313 _Pragma("diag_suppress=Pm120")
314 #define USB_ALIGN_PRAGMA(x) _Pragma(#x)
315     _Pragma("diag_default=Pm120")
316
317 #define USB_RAM_ADDRESS_ALIGNMENT(n) USB_ALIGN_PRAGMA(data_alignment = n)
318         _Pragma("diag_suppress=Pm120")
319 #define USB_LINK_SECTION_PART(str) _Pragma(#str)
320 #define USB_LINK_DMA_INIT_DATA(sec) USB_LINK_SECTION_PART(location = #sec)
321 #define USB_LINK_USB_GLOBAL _Pragma("location = \"m_usb_global\"")
322 #define USB_LINK_USB_BDT _Pragma("location = \"m_usb_bdt\"")
323 #define USB_LINK_USB_GLOBAL_BSS _Pragma("location = \".bss.m_usb_global\"")
324 #define USB_LINK_USB_BDT_BSS _Pragma("location = \".bss.m_usb_bdt\"")
325             _Pragma("diag_default=Pm120")
326 #define USB_LINK_DMA_NONINIT_DATA _Pragma("location = \"m_usb_dma_noninit_data\"")
327 #define USB_LINK_NONCACHE_NONINIT_DATA _Pragma("location = \"NonCacheable\"")
328 #elif defined(__CC_ARM)
329
330 #define USB_WEAK_VAR __attribute__((weak))
331 #define USB_WEAK_FUN __weak
332 #define USB_RAM_ADDRESS_ALIGNMENT(n) __attribute__((aligned(n)))
333 #define USB_LINK_DMA_INIT_DATA(sec) __attribute__((section(#sec)))
334 #define USB_LINK_USB_GLOBAL __attribute__((section("m_usb_global"))) __attribute__((zero_init))
335 #define USB_LINK_USB_BDT __attribute__((section("m_usb_bdt"))) __attribute__((zero_init))
336 #define USB_LINK_USB_GLOBAL_BSS __attribute__((section(".bss.m_usb_global"))) __attribute__((zero_init))
337 #define USB_LINK_USB_BDT_BSS __attribute__((section(".bss.m_usb_bdt"))) __attribute__((zero_init))
338 #define USB_LINK_DMA_NONINIT_DATA __attribute__((section("m_usb_dma_noninit_data"))) __attribute__((zero_init))
339 #define USB_LINK_NONCACHE_NONINIT_DATA __attribute__((section("NonCacheable"))) __attribute__((zero_init))
340
341 #elif defined(__GNUC__)
342
343 #define USB_WEAK_VAR __attribute__((weak))
344 #define USB_WEAK_FUN __attribute__((weak))
345 #define USB_RAM_ADDRESS_ALIGNMENT(n) __attribute__((aligned(n)))
346 #define USB_LINK_DMA_INIT_DATA(sec) __attribute__((section(#sec)))
347 #define USB_LINK_USB_GLOBAL __attribute__((section("m_usb_global, \"aw\", %nobits @")))
348 #define USB_LINK_USB_BDT __attribute__((section("m_usb_bdt, \"aw\", %nobits @")))
349 #define USB_LINK_USB_GLOBAL_BSS __attribute__((section(".bss.m_usb_global, \"aw\", %nobits @")))
350 #define USB_LINK_USB_BDT_BSS __attribute__((section(".bss.m_usb_bdt, \"aw\", %nobits @")))
351 #define USB_LINK_DMA_NONINIT_DATA __attribute__((section("m_usb_dma_noninit_data, \"aw\", %nobits @")))
352 #define USB_LINK_NONCACHE_NONINIT_DATA __attribute__((section("NonCacheable, \"aw\", %nobits @")))
353
354 #else
355 #error The tool-chain is not supported.
356 #endif
357
358 #if (defined(USB_DEVICE_CONFIG_BUFFER_PROPERTY_CACHEABLE) && (USB_DEVICE_CONFIG_BUFFER_PROPERTY_CACHEABLE)) || \
359     (defined(USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE) && (USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE))
360
361 #if ((defined(FSL_FEATURE_L2CACHE_LINESIZE_BYTE)) && (defined(FSL_FEATURE_L1DCACHE_LINESIZE_BYTE)))
362 #define USB_CACHE_LINESIZE MAX(FSL_FEATURE_L2CACHE_LINESIZE_BYTE, FSL_FEATURE_L1DCACHE_LINESIZE_BYTE)
363 #elif(defined(FSL_FEATURE_L2CACHE_LINESIZE_BYTE))
364 #define USB_CACHE_LINESIZE MAX(FSL_FEATURE_L2CACHE_LINESIZE_BYTE, 0)
365 #elif(defined(FSL_FEATURE_L1DCACHE_LINESIZE_BYTE))
366 #define USB_CACHE_LINESIZE MAX(0, FSL_FEATURE_L1DCACHE_LINESIZE_BYTE)
367 #else
368 #define USB_CACHE_LINESIZE 4
369 #endif
370
371 #else
372 #define USB_CACHE_LINESIZE 4
373 #endif
374
375 #if (((defined(USB_DEVICE_CONFIG_LPCIP3511FS)) && (USB_DEVICE_CONFIG_LPCIP3511FS > 0U)) || \
376      ((defined(USB_DEVICE_CONFIG_LPCIP3511HS)) && (USB_DEVICE_CONFIG_LPCIP3511HS > 0U)))
377 #define USB_DATA_ALIGN 64
378 #else
379 #define USB_DATA_ALIGN 4
380 #endif
381
382 #define USB_DATA_ALIGN_SIZE MAX(USB_CACHE_LINESIZE, USB_DATA_ALIGN)
383
384 #define USB_DATA_ALIGN_SIZE_MULTIPLE(n) ((n + USB_DATA_ALIGN_SIZE - 1) & (~(USB_DATA_ALIGN_SIZE - 1)))
385
386 #if defined(USB_STACK_USE_DEDICATED_RAM) && (USB_STACK_USE_DEDICATED_RAM == USB_STACK_DEDICATED_RAM_TYPE_BDT_GLOBAL)
387
388 #define USB_GLOBAL USB_LINK_USB_GLOBAL
389 #define USB_BDT USB_LINK_USB_BDT
390
391 #if (defined(USB_DEVICE_CONFIG_BUFFER_PROPERTY_CACHEABLE) && (USB_DEVICE_CONFIG_BUFFER_PROPERTY_CACHEABLE)) || \
392     (defined(USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE) && (USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE))
393 #define USB_DMA_DATA_NONINIT_SUB USB_LINK_DMA_NONINIT_DATA
394 #define USB_DMA_DATA_INIT_SUB USB_LINK_DMA_INIT_DATA(m_usb_dma_init_data)
395 #define USB_CONTROLLER_DATA USB_LINK_NONCACHE_NONINIT_DATA
396 #else
397 #define USB_DMA_DATA_NONINIT_SUB
398 #define USB_DMA_DATA_INIT_SUB
399 #define USB_CONTROLLER_DATA USB_LINK_USB_GLOBAL
400 #endif
401
402 #elif defined(USB_STACK_USE_DEDICATED_RAM) && (USB_STACK_USE_DEDICATED_RAM == USB_STACK_DEDICATED_RAM_TYPE_BDT)
403
404 #define USB_BDT USB_LINK_USB_BDT
405
406 #if (defined(USB_DEVICE_CONFIG_BUFFER_PROPERTY_CACHEABLE) && (USB_DEVICE_CONFIG_BUFFER_PROPERTY_CACHEABLE)) || \
407     (defined(USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE) && (USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE))
408 #define USB_GLOBAL USB_LINK_DMA_NONINIT_DATA
409 #define USB_DMA_DATA_NONINIT_SUB USB_LINK_DMA_NONINIT_DATA
410 #define USB_DMA_DATA_INIT_SUB USB_LINK_DMA_INIT_DATA(m_usb_dma_init_data)
411 #define USB_CONTROLLER_DATA USB_LINK_NONCACHE_NONINIT_DATA
412 #else
413 #define USB_GLOBAL USB_LINK_USB_GLOBAL_BSS
414 #define USB_DMA_DATA_NONINIT_SUB
415 #define USB_DMA_DATA_INIT_SUB
416 #define USB_CONTROLLER_DATA
417 #endif
418
419 #else
420
421 #if (defined(USB_DEVICE_CONFIG_BUFFER_PROPERTY_CACHEABLE) && (USB_DEVICE_CONFIG_BUFFER_PROPERTY_CACHEABLE)) || \
422     (defined(USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE) && (USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE))
423
424 #define USB_GLOBAL USB_LINK_DMA_NONINIT_DATA
425 #define USB_BDT USB_LINK_NONCACHE_NONINIT_DATA
426 #define USB_DMA_DATA_NONINIT_SUB USB_LINK_DMA_NONINIT_DATA
427 #define USB_DMA_DATA_INIT_SUB USB_LINK_DMA_INIT_DATA(m_usb_dma_init_data)
428 #define USB_CONTROLLER_DATA USB_LINK_NONCACHE_NONINIT_DATA
429
430 #else
431 #define USB_GLOBAL USB_LINK_USB_GLOBAL_BSS
432 #define USB_BDT USB_LINK_USB_BDT_BSS
433 #define USB_DMA_DATA_NONINIT_SUB
434 #define USB_DMA_DATA_INIT_SUB
435 #define USB_CONTROLLER_DATA
436 #endif
437
438 #endif
439
440 #define USB_DMA_NONINIT_DATA_ALIGN(n) USB_RAM_ADDRESS_ALIGNMENT(n) USB_DMA_DATA_NONINIT_SUB
441 #define USB_DMA_INIT_DATA_ALIGN(n) USB_RAM_ADDRESS_ALIGNMENT(n) USB_DMA_DATA_INIT_SUB
442
443 #if (defined(USB_DEVICE_CONFIG_BUFFER_PROPERTY_CACHEABLE) && (USB_DEVICE_CONFIG_BUFFER_PROPERTY_CACHEABLE)) || \
444     (defined(USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE) && (USB_HOST_CONFIG_BUFFER_PROPERTY_CACHEABLE))
445 #define USB_DMA_DATA_NONCACHEABLE USB_LINK_NONCACHE_NONINIT_DATA
446
447 #else
448 #define USB_DMA_DATA_NONCACHEABLE
449 #endif
450
451 #define USB_GLOBAL_DEDICATED_RAM USB_LINK_USB_GLOBAL
452
453 /* #define USB_RAM_ADDRESS_NONCACHEREG_ALIGNMENT(n, var) AT_NONCACHEABLE_SECTION_ALIGN(var, n) */
454 /* #define USB_RAM_ADDRESS_NONCACHEREG(var) AT_NONCACHEABLE_SECTION(var) */
455
456 #endif /* __USB_MISC_H__ */