usb_util.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /**
  2. * @file usb_util.h
  3. * @brief
  4. *
  5. * Copyright (c) 2022 sakumisu
  6. *
  7. * Licensed to the Apache Software Foundation (ASF) under one or more
  8. * contributor license agreements. See the NOTICE file distributed with
  9. * this work for additional information regarding copyright ownership. The
  10. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  11. * "License"); you may not use this file except in compliance with the
  12. * License. You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  18. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  19. * License for the specific language governing permissions and limitations
  20. * under the License.
  21. *
  22. */
  23. #ifndef _USB_UTIL_H
  24. #define _USB_UTIL_H
  25. #if defined(__CC_ARM)
  26. #ifndef __USED
  27. #define __USED __attribute__((used))
  28. #endif
  29. #ifndef __WEAK
  30. #define __WEAK __attribute__((weak))
  31. #endif
  32. #ifndef __PACKED
  33. #define __PACKED __attribute__((packed))
  34. #endif
  35. #ifndef __PACKED_STRUCT
  36. #define __PACKED_STRUCT __packed struct
  37. #endif
  38. #ifndef __PACKED_UNION
  39. #define __PACKED_UNION __packed union
  40. #endif
  41. #ifndef __ALIGNED
  42. #define __ALIGNED(x) __attribute__((aligned(x)))
  43. #endif
  44. #elif defined(__GNUC__)
  45. #ifndef __USED
  46. #define __USED __attribute__((used))
  47. #endif
  48. #ifndef __WEAK
  49. #define __WEAK __attribute__((weak))
  50. #endif
  51. #ifndef __PACKED
  52. #define __PACKED __attribute__((packed, aligned(1)))
  53. #endif
  54. #ifndef __PACKED_STRUCT
  55. #define __PACKED_STRUCT struct __attribute__((packed, aligned(1)))
  56. #endif
  57. #ifndef __PACKED_UNION
  58. #define __PACKED_UNION union __attribute__((packed, aligned(1)))
  59. #endif
  60. #ifndef __ALIGNED
  61. #define __ALIGNED(x) __attribute__((aligned(x)))
  62. #endif
  63. #elif defined(__ICCARM__)
  64. #ifndef __USED
  65. #if __ICCARM_V8
  66. #define __USED __attribute__((used))
  67. #else
  68. #define __USED _Pragma("__root")
  69. #endif
  70. #endif
  71. #ifndef __WEAK
  72. #if __ICCARM_V8
  73. #define __WEAK __attribute__((weak))
  74. #else
  75. #define __WEAK _Pragma("__weak")
  76. #endif
  77. #endif
  78. #ifndef __PACKED
  79. #if __ICCARM_V8
  80. #define __PACKED __attribute__((packed, aligned(1)))
  81. #else
  82. /* Needs IAR language extensions */
  83. #define __PACKED __packed
  84. #endif
  85. #endif
  86. #ifndef __PACKED_STRUCT
  87. #if __ICCARM_V8
  88. #define __PACKED_STRUCT struct __attribute__((packed, aligned(1)))
  89. #else
  90. /* Needs IAR language extensions */
  91. #define __PACKED_STRUCT __packed struct
  92. #endif
  93. #endif
  94. #ifndef __PACKED_UNION
  95. #if __ICCARM_V8
  96. #define __PACKED_UNION union __attribute__((packed, aligned(1)))
  97. #else
  98. /* Needs IAR language extensions */
  99. #define __PACKED_UNION __packed union
  100. #endif
  101. #endif
  102. #ifndef __ALIGNED
  103. #if __ICCARM_V8
  104. #define __ALIGNED(x) __attribute__((aligned(x)))
  105. #elif (__VER__ >= 7080000)
  106. /* Needs IAR language extensions */
  107. #define __ALIGNED(x) __attribute__((aligned(x)))
  108. #else
  109. #warning No compiler specific solution for __ALIGNED.__ALIGNED is ignored.
  110. #define __ALIGNED(x)
  111. #endif
  112. #endif
  113. #endif
  114. #ifndef __ALIGN_BEGIN
  115. #define __ALIGN_BEGIN
  116. #endif
  117. #ifndef __ALIGN_END
  118. #define __ALIGN_END __attribute__((aligned(4)))
  119. #endif
  120. #ifndef ARG_UNUSED
  121. #define ARG_UNUSED(x) (void)(x)
  122. #endif
  123. #ifndef LO_BYTE
  124. #define LO_BYTE(x) ((uint8_t)(x & 0x00FF))
  125. #endif
  126. #ifndef HI_BYTE
  127. #define HI_BYTE(x) ((uint8_t)((x & 0xFF00) >> 8))
  128. #endif
  129. /**
  130. * @def MAX
  131. * @brief The larger value between @p a and @p b.
  132. * @note Arguments are evaluated twice.
  133. */
  134. #ifndef MAX
  135. /* Use Z_MAX for a GCC-only, single evaluation version */
  136. #define MAX(a, b) (((a) > (b)) ? (a) : (b))
  137. #endif
  138. /**
  139. * @def MIN
  140. * @brief The smaller value between @p a and @p b.
  141. * @note Arguments are evaluated twice.
  142. */
  143. #ifndef MIN
  144. /* Use Z_MIN for a GCC-only, single evaluation version */
  145. #define MIN(a, b) (((a) < (b)) ? (a) : (b))
  146. #endif
  147. #ifndef BCD
  148. #define BCD(x) ((((x) / 10) << 4) | ((x) % 10))
  149. #endif
  150. #ifdef BIT
  151. #undef BIT
  152. #define BIT(n) (1UL << (n))
  153. #else
  154. #define BIT(n) (1UL << (n))
  155. #endif
  156. #ifndef ARRAY_SIZE
  157. #define ARRAY_SIZE(array) \
  158. ((int)((sizeof(array) / sizeof((array)[0]))))
  159. #endif
  160. #ifndef BSWAP16
  161. #define BSWAP16(u16) (__builtin_bswap16(u16))
  162. #endif
  163. #ifndef BSWAP32
  164. #define BSWAP32(u32) (__builtin_bswap32(u32))
  165. #endif
  166. #define GET_BE16(field) \
  167. (((uint16_t)(field)[0] << 8) | ((uint16_t)(field)[1]))
  168. #define GET_BE32(field) \
  169. (((uint32_t)(field)[0] << 24) | ((uint32_t)(field)[1] << 16) | ((uint32_t)(field)[2] << 8) | ((uint32_t)(field)[3] << 0))
  170. #define SET_BE16(field, value) \
  171. do { \
  172. (field)[0] = (uint8_t)((value) >> 8); \
  173. (field)[1] = (uint8_t)((value) >> 0); \
  174. } while (0)
  175. #define SET_BE24(field, value) \
  176. do { \
  177. (field)[0] = (uint8_t)((value) >> 16); \
  178. (field)[1] = (uint8_t)((value) >> 8); \
  179. (field)[2] = (uint8_t)((value) >> 0); \
  180. } while (0)
  181. #define SET_BE32(field, value) \
  182. do { \
  183. (field)[0] = (uint8_t)((value) >> 24); \
  184. (field)[1] = (uint8_t)((value) >> 16); \
  185. (field)[2] = (uint8_t)((value) >> 8); \
  186. (field)[3] = (uint8_t)((value) >> 0); \
  187. } while (0)
  188. #define REQTYPE_GET_DIR(x) (((x) >> 7) & 0x01)
  189. #define REQTYPE_GET_TYPE(x) (((x) >> 5) & 0x03U)
  190. #define REQTYPE_GET_RECIP(x) ((x)&0x1F)
  191. #define GET_DESC_TYPE(x) (((x) >> 8) & 0xFFU)
  192. #define GET_DESC_INDEX(x) ((x)&0xFFU)
  193. #define WBVAL(x) (x & 0xFF), ((x >> 8) & 0xFF)
  194. #define DBVAL(x) (x & 0xFF), ((x >> 8) & 0xFF), ((x >> 16) & 0xFF), ((x >> 24) & 0xFF)
  195. #define PP_NARG(...) \
  196. PP_NARG_(__VA_ARGS__, PP_RSEQ_N())
  197. #define PP_NARG_(...) \
  198. PP_ARG_N(__VA_ARGS__)
  199. #define PP_ARG_N( \
  200. _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, \
  201. _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, \
  202. _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, \
  203. _31, _32, _33, _34, _35, _36, _37, _38, _39, _40, \
  204. _41, _42, _43, _44, _45, _46, _47, _48, _49, _50, \
  205. _51, _52, _53, _54, _55, _56, _57, _58, _59, _60, \
  206. _61, _62, _63, N, ...) N
  207. #define PP_RSEQ_N() \
  208. 63, 62, 61, 60, \
  209. 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, \
  210. 49, 48, 47, 46, 45, 44, 43, 42, 41, 40, \
  211. 39, 38, 37, 36, 35, 34, 33, 32, 31, 30, \
  212. 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, \
  213. 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, \
  214. 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
  215. #define USB_DESC_SECTION __attribute__((section("usb_desc"))) __USED __ALIGNED(1)
  216. #endif