usb_util.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #ifndef _USB_UTIL_H
  2. #define _USB_UTIL_H
  3. #include "stdbool.h"
  4. #include "string.h"
  5. #include "stdint.h"
  6. #include "stdio.h"
  7. #include "usb_slist.h"
  8. #ifndef __packed
  9. #define __packed __attribute__((__packed__))
  10. #endif
  11. #ifndef __aligned
  12. #define __aligned(x) __attribute__((__aligned__(x)))
  13. #endif
  14. #define __may_alias __attribute__((__may_alias__))
  15. #ifndef __printf_like
  16. #define __printf_like(f, a) __attribute__((format(printf, f, a)))
  17. #endif
  18. #define __used __attribute__((__used__))
  19. #ifndef __deprecated
  20. #define __deprecated __attribute__((deprecated))
  21. #endif
  22. #define ARG_UNUSED(x) (void)(x)
  23. // #define likely(x) __builtin_expect((bool)!!(x), true)
  24. // #define unlikely(x) __builtin_expect((bool)!!(x), false)
  25. #define popcount(x) __builtin_popcount(x)
  26. #ifndef __no_optimization
  27. #define __no_optimization __attribute__((optimize("-O0")))
  28. #endif
  29. #ifndef __weak
  30. #define __weak __attribute__((__weak__))
  31. #endif
  32. #define __unused __attribute__((__unused__))
  33. #define __ALIGN_END __attribute__((aligned(4)))
  34. #define __ALIGN_BEGIN
  35. #ifndef LO_BYTE
  36. #define LO_BYTE(x) ((uint8_t)(x & 0x00FF))
  37. #endif
  38. #ifndef HI_BYTE
  39. #define HI_BYTE(x) ((uint8_t)((x & 0xFF00) >> 8))
  40. #endif
  41. /**
  42. * @def MAX
  43. * @brief The larger value between @p a and @p b.
  44. * @note Arguments are evaluated twice.
  45. */
  46. #ifndef MAX
  47. /* Use Z_MAX for a GCC-only, single evaluation version */
  48. #define MAX(a, b) (((a) > (b)) ? (a) : (b))
  49. #endif
  50. /**
  51. * @def MIN
  52. * @brief The smaller value between @p a and @p b.
  53. * @note Arguments are evaluated twice.
  54. */
  55. #ifndef MIN
  56. /* Use Z_MIN for a GCC-only, single evaluation version */
  57. #define MIN(a, b) (((a) < (b)) ? (a) : (b))
  58. #endif
  59. #define BCD(x) ((((x) / 10) << 4) | ((x) % 10))
  60. #define BIT(x) (1 << (x))
  61. #define ARRAY_SIZE(array) \
  62. ((int)((sizeof(array) / sizeof((array)[0]))))
  63. #define USB_DESC_SECTION __attribute__((section("usb_desc"))) __used __aligned(1)
  64. #define BSWAP16(u16) (__builtin_bswap16(u16))
  65. #define BSWAP32(u32) (__builtin_bswap32(u32))
  66. #define GET_BE16(field) \
  67. (((uint16_t)(field)[0] << 8) | ((uint16_t)(field)[1]))
  68. #define GET_BE32(field) \
  69. (((uint32_t)(field)[0] << 24) | ((uint32_t)(field)[1] << 16) | ((uint32_t)(field)[2] << 8) | ((uint32_t)(field)[3] << 0))
  70. #define SET_BE16(field, value) \
  71. do { \
  72. (field)[0] = (uint8_t)((value) >> 8); \
  73. (field)[1] = (uint8_t)((value) >> 0); \
  74. } while (0)
  75. #define SET_BE24(field, value) \
  76. do { \
  77. (field)[0] = (uint8_t)((value) >> 16); \
  78. (field)[1] = (uint8_t)((value) >> 8); \
  79. (field)[2] = (uint8_t)((value) >> 0); \
  80. } while (0)
  81. #define SET_BE32(field, value) \
  82. do { \
  83. (field)[0] = (uint8_t)((value) >> 24); \
  84. (field)[1] = (uint8_t)((value) >> 16); \
  85. (field)[2] = (uint8_t)((value) >> 8); \
  86. (field)[3] = (uint8_t)((value) >> 0); \
  87. } while (0)
  88. #define REQTYPE_GET_DIR(x) (((x) >> 7) & 0x01)
  89. #define REQTYPE_GET_TYPE(x) (((x) >> 5) & 0x03U)
  90. #define REQTYPE_GET_RECIP(x) ((x)&0x1F)
  91. #define GET_DESC_TYPE(x) (((x) >> 8) & 0xFFU)
  92. #define GET_DESC_INDEX(x) ((x)&0xFFU)
  93. #define WBVAL(x) (x & 0xFF), ((x >> 8) & 0xFF)
  94. #define DBVAL(x) (x & 0xFF), ((x >> 8) & 0xFF), ((x >> 16) & 0xFF), ((x >> 24) & 0xFF)
  95. #if 0
  96. #define USBD_LOG_WRN(a, ...) bflb_platform_printf(a, ##__VA_ARGS__)
  97. #define USBD_LOG_DBG(a, ...) bflb_platform_printf(a, ##__VA_ARGS__)
  98. #define USBD_LOG_ERR(a, ...) bflb_platform_printf(a, ##__VA_ARGS__)
  99. #else
  100. #define USBD_LOG_WRN(a, ...) bflb_platform_printf(a, ##__VA_ARGS__)
  101. #define USBD_LOG_DBG(a, ...)
  102. #define USBD_LOG_ERR(a, ...) bflb_platform_printf(a, ##__VA_ARGS__)
  103. #define USBD_LOG(a, ...) bflb_platform_printf(a, ##__VA_ARGS__)
  104. #endif
  105. #endif