usb_util.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. #include <stdbool.h>
  26. #include <string.h>
  27. #include <stdint.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include "usb_errno.h"
  31. #include "usb_list.h"
  32. #include "usb_mem.h"
  33. #if defined(__CC_ARM)
  34. #ifndef __USED
  35. #define __USED __attribute__((used))
  36. #endif
  37. #ifndef __WEAK
  38. #define __WEAK __attribute__((weak))
  39. #endif
  40. #ifndef __PACKED
  41. #define __PACKED __attribute__((packed))
  42. #endif
  43. #ifndef __PACKED_STRUCT
  44. #define __PACKED_STRUCT __packed struct
  45. #endif
  46. #ifndef __PACKED_UNION
  47. #define __PACKED_UNION __packed union
  48. #endif
  49. #ifndef __ALIGNED
  50. #define __ALIGNED(x) __attribute__((aligned(x)))
  51. #endif
  52. #elif defined(__GNUC__)
  53. #ifndef __USED
  54. #define __USED __attribute__((used))
  55. #endif
  56. #ifndef __WEAK
  57. #define __WEAK __attribute__((weak))
  58. #endif
  59. #ifndef __PACKED
  60. #define __PACKED __attribute__((packed, aligned(1)))
  61. #endif
  62. #ifndef __PACKED_STRUCT
  63. #define __PACKED_STRUCT struct __attribute__((packed, aligned(1)))
  64. #endif
  65. #ifndef __PACKED_UNION
  66. #define __PACKED_UNION union __attribute__((packed, aligned(1)))
  67. #endif
  68. #ifndef __ALIGNED
  69. #define __ALIGNED(x) __attribute__((aligned(x)))
  70. #endif
  71. #elif defined(__ICCARM__)
  72. #ifndef __USED
  73. #if __ICCARM_V8
  74. #define __USED __attribute__((used))
  75. #else
  76. #define __USED _Pragma("__root")
  77. #endif
  78. #endif
  79. #ifndef __WEAK
  80. #if __ICCARM_V8
  81. #define __WEAK __attribute__((weak))
  82. #else
  83. #define __WEAK _Pragma("__weak")
  84. #endif
  85. #endif
  86. #ifndef __PACKED
  87. #if __ICCARM_V8
  88. #define __PACKED __attribute__((packed, aligned(1)))
  89. #else
  90. /* Needs IAR language extensions */
  91. #define __PACKED __packed
  92. #endif
  93. #endif
  94. #ifndef __PACKED_STRUCT
  95. #if __ICCARM_V8
  96. #define __PACKED_STRUCT struct __attribute__((packed, aligned(1)))
  97. #else
  98. /* Needs IAR language extensions */
  99. #define __PACKED_STRUCT __packed struct
  100. #endif
  101. #endif
  102. #ifndef __PACKED_UNION
  103. #if __ICCARM_V8
  104. #define __PACKED_UNION union __attribute__((packed, aligned(1)))
  105. #else
  106. /* Needs IAR language extensions */
  107. #define __PACKED_UNION __packed union
  108. #endif
  109. #endif
  110. #ifndef __ALIGNED
  111. #if __ICCARM_V8
  112. #define __ALIGNED(x) __attribute__((aligned(x)))
  113. #elif (__VER__ >= 7080000)
  114. /* Needs IAR language extensions */
  115. #define __ALIGNED(x) __attribute__((aligned(x)))
  116. #else
  117. #warning No compiler specific solution for __ALIGNED.__ALIGNED is ignored.
  118. #define __ALIGNED(x)
  119. #endif
  120. #endif
  121. #endif
  122. #ifndef __ALIGN_BEGIN
  123. #define __ALIGN_BEGIN
  124. #endif
  125. #ifndef __ALIGN_END
  126. #define __ALIGN_END __attribute__((aligned(4)))
  127. #endif
  128. #ifndef ARG_UNUSED
  129. #define ARG_UNUSED(x) (void)(x)
  130. #endif
  131. #ifndef LO_BYTE
  132. #define LO_BYTE(x) ((uint8_t)(x & 0x00FF))
  133. #endif
  134. #ifndef HI_BYTE
  135. #define HI_BYTE(x) ((uint8_t)((x & 0xFF00) >> 8))
  136. #endif
  137. /**
  138. * @def MAX
  139. * @brief The larger value between @p a and @p b.
  140. * @note Arguments are evaluated twice.
  141. */
  142. #ifndef MAX
  143. /* Use Z_MAX for a GCC-only, single evaluation version */
  144. #define MAX(a, b) (((a) > (b)) ? (a) : (b))
  145. #endif
  146. /**
  147. * @def MIN
  148. * @brief The smaller value between @p a and @p b.
  149. * @note Arguments are evaluated twice.
  150. */
  151. #ifndef MIN
  152. /* Use Z_MIN for a GCC-only, single evaluation version */
  153. #define MIN(a, b) (((a) < (b)) ? (a) : (b))
  154. #endif
  155. #ifndef BCD
  156. #define BCD(x) ((((x) / 10) << 4) | ((x) % 10))
  157. #endif
  158. #ifdef BIT
  159. #undef BIT
  160. #define BIT(n) (1UL << (n))
  161. #else
  162. #define BIT(n) (1UL << (n))
  163. #endif
  164. #ifndef ARRAY_SIZE
  165. #define ARRAY_SIZE(array) \
  166. ((int)((sizeof(array) / sizeof((array)[0]))))
  167. #endif
  168. #ifndef BSWAP16
  169. #define BSWAP16(u16) (__builtin_bswap16(u16))
  170. #endif
  171. #ifndef BSWAP32
  172. #define BSWAP32(u32) (__builtin_bswap32(u32))
  173. #endif
  174. #define GET_BE16(field) \
  175. (((uint16_t)(field)[0] << 8) | ((uint16_t)(field)[1]))
  176. #define GET_BE32(field) \
  177. (((uint32_t)(field)[0] << 24) | ((uint32_t)(field)[1] << 16) | ((uint32_t)(field)[2] << 8) | ((uint32_t)(field)[3] << 0))
  178. #define SET_BE16(field, value) \
  179. do { \
  180. (field)[0] = (uint8_t)((value) >> 8); \
  181. (field)[1] = (uint8_t)((value) >> 0); \
  182. } while (0)
  183. #define SET_BE24(field, value) \
  184. do { \
  185. (field)[0] = (uint8_t)((value) >> 16); \
  186. (field)[1] = (uint8_t)((value) >> 8); \
  187. (field)[2] = (uint8_t)((value) >> 0); \
  188. } while (0)
  189. #define SET_BE32(field, value) \
  190. do { \
  191. (field)[0] = (uint8_t)((value) >> 24); \
  192. (field)[1] = (uint8_t)((value) >> 16); \
  193. (field)[2] = (uint8_t)((value) >> 8); \
  194. (field)[3] = (uint8_t)((value) >> 0); \
  195. } while (0)
  196. #define REQTYPE_GET_DIR(x) (((x) >> 7) & 0x01)
  197. #define REQTYPE_GET_TYPE(x) (((x) >> 5) & 0x03U)
  198. #define REQTYPE_GET_RECIP(x) ((x)&0x1F)
  199. #define GET_DESC_TYPE(x) (((x) >> 8) & 0xFFU)
  200. #define GET_DESC_INDEX(x) ((x)&0xFFU)
  201. #define WBVAL(x) (x & 0xFF), ((x >> 8) & 0xFF)
  202. #define DBVAL(x) (x & 0xFF), ((x >> 8) & 0xFF), ((x >> 16) & 0xFF), ((x >> 24) & 0xFF)
  203. #define USB_DESC_SECTION __attribute__((section("usb_desc"))) __USED __ALIGNED(1)
  204. /* DEBUG level */
  205. #define USB_DBG_ERROR 0
  206. #define USB_DBG_WARNING 1
  207. #define USB_DBG_INFO 2
  208. #define USB_DBG_LOG 3
  209. #ifndef USB_DBG_LEVEL
  210. #define USB_DBG_LEVEL USB_DBG_INFO
  211. #endif
  212. #ifndef USB_DBG_TAG
  213. #define USB_DBG_TAG "USB"
  214. #endif
  215. /*
  216. * The color for terminal (foreground)
  217. * BLACK 30
  218. * RED 31
  219. * GREEN 32
  220. * YELLOW 33
  221. * BLUE 34
  222. * PURPLE 35
  223. * CYAN 36
  224. * WHITE 37
  225. */
  226. #define _USB_DBG_COLOR(n) printf("\033[" #n "m")
  227. #define _USB_DBG_LOG_HDR(lvl_name, color_n) \
  228. printf("\033[" #color_n "m[" lvl_name "/" USB_DBG_TAG "] ")
  229. #define _USB_DBG_LOG_X_END \
  230. printf("\033[0m")
  231. #define usb_dbg_log_line(lvl, color_n, fmt, ...) \
  232. do { \
  233. _USB_DBG_LOG_HDR(lvl, color_n); \
  234. printf(fmt, ##__VA_ARGS__); \
  235. _USB_DBG_LOG_X_END; \
  236. } while (0)
  237. #if (USB_DBG_LEVEL >= USB_DBG_LOG)
  238. #define USB_LOG_DBG(fmt, ...) usb_dbg_log_line("D", 0, fmt, ##__VA_ARGS__)
  239. #else
  240. #define USB_LOG_DBG(...)
  241. #endif
  242. #if (USB_DBG_LEVEL >= USB_DBG_INFO)
  243. #define USB_LOG_INFO(fmt, ...) usb_dbg_log_line("I", 32, fmt, ##__VA_ARGS__)
  244. #else
  245. #define USB_LOG_INFO(...)
  246. #endif
  247. #if (USB_DBG_LEVEL >= USB_DBG_WARNING)
  248. #define USB_LOG_WRN(fmt, ...) usb_dbg_log_line("W", 33, fmt, ##__VA_ARGS__)
  249. #else
  250. #define USB_LOG_WRN(...)
  251. #endif
  252. #if (USB_DBG_LEVEL >= USB_DBG_ERROR)
  253. #define USB_LOG_ERR(fmt, ...) usb_dbg_log_line("E", 31, fmt, ##__VA_ARGS__)
  254. #else
  255. #define USB_LOG_ERR(...)
  256. #endif
  257. void usb_assert(const char *filename, int linenum);
  258. #define USB_ASSERT(f) \
  259. do { \
  260. if (!(f)) \
  261. usb_assert(__FILE__, __LINE__); \
  262. } while (0)
  263. #endif