usb_util.h 5.4 KB

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