hpm_common.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * Copyright (c) 2021-2022 HPMicro
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. */
  7. #ifndef _HPM_COMMON_H
  8. #define _HPM_COMMON_H
  9. #include <assert.h>
  10. #include <stdbool.h>
  11. #include <stdint.h>
  12. #include <string.h>
  13. #include <stdlib.h>
  14. /**
  15. *
  16. * @brief COMMON driver APIs
  17. * @defgroup common_interface COMMON driver APIs
  18. * @{
  19. *
  20. */
  21. #define __R volatile const /* Define "read-only" permission */
  22. #define __RW volatile /* Define "read-write" permission */
  23. #define __W volatile /* Define "write-only" permission */
  24. #ifndef __I
  25. #define __I __R
  26. #endif
  27. #ifndef __IO
  28. #define __IO __RW
  29. #endif
  30. #ifndef __O
  31. #define __O __W
  32. #endif
  33. #ifndef ARRAY_SIZE
  34. #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
  35. #endif
  36. #ifndef MAX
  37. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  38. #endif
  39. #ifndef MIN
  40. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  41. #endif
  42. #define HPM_BITSMASK(val, offset) ((uint32_t)(val) << (offset))
  43. #define IS_HPM_BITMASK_SET(val, mask) (((uint32_t)(val) & (uint32_t)(mask)) != 0U)
  44. #define IS_HPM_BIT_SET(val, offset) (((uint32_t)(val) & (1UL << (offset))) != 0U)
  45. #define IS_HPM_BITMASK_CLR(val, mask) (((uint32_t)(val) & (uint32_t)(mask)) == 0U)
  46. #define IS_HPM_BIT_CLR(val, offset) (((uint32_t)(val) & (1UL << (offset))) == 0U)
  47. #define HPM_BREAK_IF(cond) if (cond) { break; }
  48. #define HPM_CONTINUE_IF(cond) if (cond) { continue; }
  49. #define HPM_CHECK_RET(x) \
  50. do { \
  51. stat = (x); \
  52. if (status_success != stat) { \
  53. return stat; \
  54. } \
  55. } while (false)
  56. #define SIZE_1KB (1024UL)
  57. #define SIZE_1MB (1048576UL)
  58. typedef uint32_t hpm_stat_t;
  59. /* @brief Enum definition for the Status group
  60. * Rule:
  61. * [Group] 0-999 for the SoC driver and the corresponding components
  62. * 1000 or above for the application status group
  63. * [Code] Valid value: 0-999
  64. *
  65. */
  66. #define MAKE_STATUS(group, code) ((uint32_t)(group)*1000U + (uint32_t)(code))
  67. /* @brief System status group definitions */
  68. enum {
  69. status_group_common = 0,
  70. status_group_uart = 1,
  71. status_group_i2c = 2,
  72. status_group_spi = 3,
  73. status_group_usb = 4,
  74. status_group_i2s = 5,
  75. status_group_xpi = 6,
  76. status_group_l1c,
  77. status_group_dma,
  78. status_group_femc,
  79. status_group_sdp,
  80. status_group_xpi_nor,
  81. status_group_otp,
  82. status_group_lcdc,
  83. status_group_mbx,
  84. status_group_rng,
  85. status_group_pdma,
  86. status_group_wdg,
  87. status_group_pmic_sec,
  88. status_group_can,
  89. status_group_sdxc,
  90. status_group_pcfg,
  91. status_group_clk,
  92. status_group_pllctl,
  93. status_group_pllctlv2,
  94. status_group_ffa,
  95. status_group_mcan,
  96. status_group_middleware_start = 500,
  97. status_group_sdmmc = status_group_middleware_start,
  98. status_group_audio_codec,
  99. status_group_dma_manager,
  100. };
  101. /* @brief Common status code definitions */
  102. enum {
  103. status_success = MAKE_STATUS(status_group_common, 0),
  104. status_fail = MAKE_STATUS(status_group_common, 1),
  105. status_invalid_argument = MAKE_STATUS(status_group_common, 2),
  106. status_timeout = MAKE_STATUS(status_group_common, 3),
  107. };
  108. #if defined(__GNUC__)
  109. /* alway_inline */
  110. #define ATTR_ALWAYS_INLINE __attribute__((always_inline))
  111. /* weak */
  112. #define ATTR_WEAK __attribute__((weak))
  113. /* alignment */
  114. #define ATTR_ALIGN(alignment) __attribute__((aligned(alignment)))
  115. /* place var_declare at section_name, e.x. PLACE_AT(".target_section", var); */
  116. #define ATTR_PLACE_AT(section_name) __attribute__((section(section_name)))
  117. #define ATTR_PLACE_AT_WITH_ALIGNMENT(section_name, alignment) \
  118. ATTR_PLACE_AT(section_name) ATTR_ALIGN(alignment)
  119. #define ATTR_PLACE_AT_NONCACHEABLE ATTR_PLACE_AT(".noncacheable.bss")
  120. #define ATTR_PLACE_AT_NONCACHEABLE_WITH_ALIGNMENT(alignment) \
  121. ATTR_PLACE_AT_NONCACHEABLE ATTR_ALIGN(alignment)
  122. #define ATTR_PLACE_AT_NONCACHEABLE_BSS ATTR_PLACE_AT(".noncacheable.bss")
  123. #define ATTR_PLACE_AT_NONCACHEABLE_BSS_WITH_ALIGNMENT(alignment) \
  124. ATTR_PLACE_AT_NONCACHEABLE_BSS ATTR_ALIGN(alignment)
  125. /* initialize variable x with y using PLACE_AT_NONCACHEABLE_INIT(x) = {y}; */
  126. #define ATTR_PLACE_AT_NONCACHEABLE_INIT ATTR_PLACE_AT(".noncacheable.init")
  127. #define ATTR_PLACE_AT_NONCACHEABLE_INIT_WITH_ALIGNMENT(alignment) \
  128. ATTR_PLACE_AT_NONCACHEABLE_INIT ATTR_ALIGN(alignment)
  129. #define ATTR_RAMFUNC ATTR_PLACE_AT(".fast")
  130. #define ATTR_RAMFUNC_WITH_ALIGNMENT(alignment) \
  131. ATTR_RAMFUNC ATTR_ALIGN(alignment)
  132. #define ATTR_SHARE_MEM ATTR_PLACE_AT(".sh_mem")
  133. #define NOP() __asm volatile("nop")
  134. #define WFI() __asm volatile("wfi")
  135. #else
  136. #error Unknown toolchain
  137. #endif
  138. #ifdef __cplusplus
  139. extern "C" {
  140. #endif
  141. /**
  142. * @brief Count bits set to 1
  143. *
  144. * @param value Data to be counted
  145. *
  146. * @return number of bits set to 1
  147. */
  148. static inline uint32_t count_set_bits(uint32_t value)
  149. {
  150. if (value == 0) {
  151. return 0;
  152. }
  153. return 1 + count_set_bits(value & (value - 1));
  154. }
  155. /**
  156. * @brief Count bits set to 1 from least significant bit
  157. *
  158. * @param value Data to be counted
  159. *
  160. * @return number of bits set to 1
  161. * @return 0xFFFFFFFF if no bit was set to 1
  162. */
  163. static inline uint32_t get_first_set_bit_from_lsb(uint32_t value)
  164. {
  165. uint32_t i = 0;
  166. if (!value) {
  167. return 0xFFFFFFFFUL;
  168. }
  169. while (value && !(value & 0x1)) {
  170. value >>= 1;
  171. i++;
  172. }
  173. return i;
  174. }
  175. /**
  176. * @brief Count bits set to 1 from most significant bit
  177. *
  178. * @param value Data to be counted
  179. *
  180. * @return number of bits set to 1
  181. * @return 0xFFFFFFFF if no bit was set to 1
  182. */
  183. static inline uint32_t get_first_set_bit_from_msb(uint32_t value)
  184. {
  185. uint32_t i = 31;
  186. if (!value) {
  187. return 0xFFFFFFFFUL;
  188. }
  189. while (value && !(value & 0x80000000)) {
  190. value <<= 1;
  191. value &= ~1;
  192. i--;
  193. }
  194. return i;
  195. }
  196. #ifdef __cplusplus
  197. }
  198. #endif
  199. /**
  200. * @}
  201. */
  202. #endif /* _HPM_COMMON_H */