ec_util.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Copyright (c) 2025, sakumisu
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #ifndef EC_UTIL_H
  7. #define EC_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 MAX
  103. #define MAX(a, b) (((a) > (b)) ? (a) : (b))
  104. #endif
  105. #ifndef MIN
  106. #define MIN(a, b) (((a) < (b)) ? (a) : (b))
  107. #endif
  108. #define EC_ALIGN_UP(size, align) (((size) + (align)-1) & ~((align)-1))
  109. #define EC_WRITE_U8(DATA, VAL) \
  110. do { \
  111. *((uint8_t *)(DATA)) = ((uint8_t)(VAL)); \
  112. } while (0)
  113. #define EC_WRITE_U16(DATA, VAL) \
  114. do { \
  115. *((uint16_t *)(DATA)) = ((uint16_t)(VAL)); \
  116. } while (0)
  117. #define EC_WRITE_U32(DATA, VAL) \
  118. do { \
  119. *((uint32_t *)(DATA)) = ((uint32_t)(VAL)); \
  120. } while (0)
  121. #define EC_WRITE_U64(DATA, VAL) \
  122. do { \
  123. *((uint64_t *)(DATA)) = ((uint64_t)(VAL)); \
  124. } while (0)
  125. #define EC_READ_U8(DATA) \
  126. ((uint8_t) * ((uint8_t *)(DATA)))
  127. #define EC_READ_U16(DATA) \
  128. ((uint16_t) * ((uint16_t *)(DATA)))
  129. #define EC_READ_U32(DATA) \
  130. ((uint32_t) * ((uint32_t *)(DATA)))
  131. #define EC_READ_U64(DATA) \
  132. ((uint64_t) * ((uint64_t *)(DATA)))
  133. #define ec_htons(A) ((((uint16_t)(A)&0xff00) >> 8) | \
  134. (((uint16_t)(A)&0x00ff) << 8))
  135. #define ec_htonl(A) ((((uint32_t)(A)&0xff000000) >> 24) | \
  136. (((uint32_t)(A)&0x00ff0000) >> 8) | \
  137. (((uint32_t)(A)&0x0000ff00) << 8) | \
  138. (((uint32_t)(A)&0x000000ff) << 24))
  139. #endif