hal_def.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. /*
  3. * Copyright (c) 2020-2021 Rockchip Electronics Co., Ltd.
  4. */
  5. /** @addtogroup RK_HAL_Driver
  6. * @{
  7. */
  8. /** @addtogroup HAL_DEF
  9. * @{
  10. */
  11. #ifndef _HAL_DEF_H_
  12. #define _HAL_DEF_H_
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <stdarg.h>
  17. #include <stdint.h>
  18. #include <stdbool.h>
  19. #include "cmsis_compiler.h"
  20. #include "soc.h"
  21. #include "hal_list.h"
  22. /***************************** MACRO Definition ******************************/
  23. /** @defgroup HAL_DEF_Exported_Definition_Group1 Basic Definition
  24. * @{
  25. */
  26. #define SET_BIT(REG, BIT) ((*(volatile uint32_t *)&(REG)) |= (BIT)) /**< Set 1 to the register specific bit field */
  27. #define CLEAR_BIT(REG, MASK) ((*(volatile uint32_t *)&(REG)) &= ~(MASK)) /**< Clear the specific bits filed from the register */
  28. #define READ_BIT(REG, MASK) ((*(volatile const uint32_t *)&(REG)) & (MASK)) /**< Read the value of a specific bits field from the register */
  29. #define CLEAR_REG(REG) ((*(volatile uint32_t *)&(REG)) = (0x0)) /**< Write 0 to the register */
  30. #define WRITE_REG(REG, VAL) ((*(volatile uint32_t *)&(REG)) = (VAL)) /**< Write the register */
  31. #define READ_REG(REG) ((*(volatile const uint32_t *)&(REG))) /**< Read the register */
  32. #define MODIFY_REG(REG, CLEARMASK, SETMASK) \
  33. WRITE_REG((REG), (((READ_REG(REG)) & (~(CLEARMASK))) | (SETMASK))) /**< Clear and set the value of a specific bits field from the register */
  34. #define POSITION_VAL(VAL) (__CLZ(__RBIT(VAL)))
  35. #if defined(__GNUC__) || defined(__CC_ARM)
  36. #define MASK_TO_WE(msk) (__builtin_constant_p(msk) ? ((msk) > 0xFFFFU ? 0 : ((msk) << 16)) : ((msk) << 16))
  37. #else
  38. #define MASK_TO_WE(msk) ((msk) << 16)
  39. #endif
  40. #define VAL_MASK_WE(msk, val) ((MASK_TO_WE(msk)) | (val))
  41. #define WRITE_REG_MASK_WE(reg, msk, val) WRITE_REG(reg, (VAL_MASK_WE(msk, val)))
  42. /* Misc OPS Marco */
  43. #define HAL_MAX_DELAY 0xFFFFFFFFU
  44. #define RESET 0
  45. #define HAL_IS_BIT_SET(REG, MASK) (((*(volatile uint32_t *)&(REG)) & (MASK)) != RESET) /**< Check if the the specific bits filed from the register is valid */
  46. #define HAL_IS_BIT_CLR(REG, MASK) (((*(volatile uint32_t *)&(REG)) & (MASK)) == RESET) /**< Check if the the specific bits filed from the register is isvalid */
  47. #define HAL_BIT(nr) (1UL << (nr))
  48. #define HAL_ARRAY_SIZE(a) (sizeof((a)) / sizeof((a)[0]))
  49. #define HAL_MAX(x, y) ((x) > (y) ? (x) : (y))
  50. #define HAL_MIN(x, y) ((x) < (y) ? (x) : (y))
  51. #define HAL_DIV_ROUND_UP(x, y) (((x) + (y) - 1) / (y))
  52. #define HAL_IS_ALIGNED(x, a) (((x) & (a - 1)) == 0)
  53. #ifdef CACHE_LINE_SIZE
  54. #define HAL_IS_CACHELINE_ALIGNED(x) HAL_IS_ALIGNED((uint32_t)(x), CACHE_LINE_SIZE)
  55. #else
  56. #define HAL_IS_CACHELINE_ALIGNED(x) HAL_IS_ALIGNED((uint32_t)(x), 4)
  57. #endif
  58. /* Compiller Macro */
  59. #if defined(__GNUC__) || defined(__clang__) || defined(__CC_ARM) || defined(__ICCARM__)
  60. #define HAL_UNUSED __attribute__((__unused__))
  61. #else
  62. #define HAL_UNUSED
  63. #endif
  64. #ifdef CACHE_LINE_SIZE
  65. #define HAL_CACHELINE_ALIGNED __ALIGNED(CACHE_LINE_SIZE)
  66. #else
  67. #define HAL_CACHELINE_ALIGNED
  68. #endif
  69. #ifdef HAL_SRAM_SECTION_ENABLED
  70. #define HAL_SECTION_SRAM_CODE __attribute__((section(".sram_code")))
  71. #define HAL_SECTION_SRAM_RODATA __attribute__((section(".sram_rodata")))
  72. #define HAL_SECTION_SRAM_DATA __attribute__((section(".sram_data")))
  73. #define HAL_SECTION_SRAM_BSS __attribute__((section(".sram_bss")))
  74. #else
  75. #define HAL_SECTION_SRAM_CODE
  76. #define HAL_SECTION_SRAM_RODATA
  77. #define HAL_SECTION_SRAM_DATA
  78. #define HAL_SECTION_SRAM_BSS
  79. #endif
  80. #ifdef HAL_PSRAM_SECTION_ENABLED
  81. #define HAL_SECTION_PSRAM_CODE __attribute__((section(".psram_code")))
  82. #define HAL_SECTION_PSRAM_RODATA __attribute__((section(".psram_rodata")))
  83. #define HAL_SECTION_PSRAM_DATA __attribute__((section(".psram_data")))
  84. #define HAL_SECTION_PSRAM_BSS __attribute__((section(".psram_bss")))
  85. #else
  86. #define HAL_SECTION_PSRAM_CODE
  87. #define HAL_SECTION_PSRAM_RODATA
  88. #define HAL_SECTION_PSRAM_DATA
  89. #define HAL_SECTION_PSRAM_BSS
  90. #endif
  91. #ifdef HAL_XIP_SECTION_ENABLED
  92. #define HAL_SECTION_XIP_CODE __attribute__((section(".xip_code")))
  93. #define HAL_SECTION_XIP_RODATA __attribute__((section(".xip_rodata")))
  94. #else
  95. #define HAL_SECTION_XIP_CODE
  96. #define HAL_SECTION_XIP_RODATA
  97. #endif
  98. /** MCU systick clock source */
  99. typedef enum {
  100. HAL_SYSTICK_CLKSRC_CORE,
  101. HAL_SYSTICK_CLKSRC_EXT
  102. } eHAL_systickClkSource;
  103. /** Check if is MCU systick clock source */
  104. #define IS_SYSTICK_SOURCE(s) (((s) == HAL_SYSTICK_CLKSRC_CORE) || ((s) == HAL_SYSTICK_CLKSRC_EXT))
  105. /***************************** Structure Definition **************************/
  106. /** HAL boolean type definition */
  107. typedef enum {
  108. HAL_FALSE = 0x00U,
  109. HAL_TRUE = 0x01U
  110. } HAL_Check;
  111. /** HAL error code definition */
  112. typedef enum {
  113. HAL_OK = 0x00U,
  114. HAL_ERROR = (-1),
  115. HAL_BUSY = (-16),
  116. HAL_NODEV = (-19),
  117. HAL_INVAL = (-22),
  118. HAL_NOSYS = (-38),
  119. HAL_TIMEOUT = (-110)
  120. } HAL_Status;
  121. /** HAL functional status definition */
  122. typedef enum {
  123. HAL_DISABLE = 0x00U,
  124. HAL_ENABLE = 0x01U
  125. } HAL_FuncStatus;
  126. /** HAL lock structures definition */
  127. typedef enum {
  128. HAL_UNLOCKED = 0x00U,
  129. HAL_LOCKED = 0x01U
  130. } HAL_LockStatus;
  131. /** RK GPIO bank definition */
  132. typedef enum {
  133. #ifdef GPIO0
  134. GPIO_BANK0 = 0,
  135. #endif
  136. #ifdef GPIO1
  137. GPIO_BANK1 = 1,
  138. #endif
  139. #ifdef GPIO2
  140. GPIO_BANK2 = 2,
  141. #endif
  142. #ifdef GPIO3
  143. GPIO_BANK3 = 3,
  144. #endif
  145. #ifdef GPIO4
  146. GPIO_BANK4 = 4,
  147. #endif
  148. GPIO_BANK_NUM
  149. } eGPIO_bankId;
  150. /** HAL function type definition */
  151. typedef void (*pFunc)(void);
  152. /** @} */
  153. /***************************** Function Declare ******************************/
  154. #endif
  155. /** @} */
  156. /** @} */