usb_util.h 5.7 KB

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