hc32_ll_def.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /**
  2. *******************************************************************************
  3. * @file hc32_ll_def.h
  4. * @brief This file contains LL common definitions: enumeration, macros and
  5. * structures definitions.
  6. @verbatim
  7. Change Logs:
  8. Date Author Notes
  9. 2022-12-31 CDT First version
  10. @endverbatim
  11. *******************************************************************************
  12. * Copyright (C) 2022, Xiaohua Semiconductor Co., Ltd. All rights reserved.
  13. *
  14. * This software component is licensed by XHSC under BSD 3-Clause license
  15. * (the "License"); You may not use this file except in compliance with the
  16. * License. You may obtain a copy of the License at:
  17. * opensource.org/licenses/BSD-3-Clause
  18. *
  19. *******************************************************************************
  20. */
  21. #ifndef __HC32_LL_DEF_H__
  22. #define __HC32_LL_DEF_H__
  23. /* C binding of definitions if building with C++ compiler */
  24. #ifdef __cplusplus
  25. extern "C"
  26. {
  27. #endif
  28. /*******************************************************************************
  29. * Include files
  30. ******************************************************************************/
  31. #include <stddef.h>
  32. #include <stdint.h>
  33. /**
  34. * @addtogroup LL_Common
  35. * @{
  36. */
  37. /*******************************************************************************
  38. * Global type definitions ('typedef')
  39. ******************************************************************************/
  40. /**
  41. * @defgroup LL_Common_Global_Types LL Common Global Types
  42. * @{
  43. */
  44. /**
  45. * @brief Single precision floating point number (4 byte)
  46. */
  47. typedef float float32_t;
  48. /**
  49. * @brief Double precision floating point number (8 byte)
  50. */
  51. typedef double float64_t;
  52. /**
  53. * @brief Function pointer type to void/void function
  54. */
  55. typedef void (*func_ptr_t)(void);
  56. /**
  57. * @brief Functional state
  58. */
  59. typedef enum {
  60. DISABLE = 0U,
  61. ENABLE = 1U,
  62. } en_functional_state_t;
  63. /**
  64. * @brief Flag status
  65. */
  66. typedef enum {
  67. RESET = 0U,
  68. SET = 1U,
  69. } en_flag_status_t, en_int_status_t;
  70. /**
  71. * @}
  72. */
  73. /*******************************************************************************
  74. * Global pre-processor symbols/macros ('#define')
  75. ******************************************************************************/
  76. /**
  77. * @defgroup LL_Common_Global_Macros LL Common Global Macros
  78. * @{
  79. */
  80. /**
  81. * @defgroup Compiler_Macros Compiler Macros
  82. * @{
  83. */
  84. #ifndef __UNUSED
  85. #define __UNUSED __attribute__((unused))
  86. #endif /* __UNUSED */
  87. #if defined (__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)
  88. #ifndef __WEAKDEF
  89. #define __WEAKDEF __attribute__((weak))
  90. #endif /* __WEAKDEF */
  91. #ifndef __ALIGN_BEGIN
  92. #define __ALIGN_BEGIN __attribute__((aligned(4)))
  93. #endif /* __ALIGN_BEGIN */
  94. #ifndef __NOINLINE
  95. #define __NOINLINE __attribute__((noinline))
  96. #endif /* __NOINLINE */
  97. /* RAM functions are defined using the toolchain options.
  98. Functions that are executed in RAM should reside in a separate source module.
  99. Using the 'Options for File' dialog you can simply change the 'Code / Const'
  100. area of a module to a memory space in physical RAM. */
  101. #ifndef __RAM_FUNC
  102. #define __RAM_FUNC
  103. #endif /* __RAM_FUNC */
  104. #ifndef __NO_INIT
  105. #define __NO_INIT __attribute__((section(".bss.noinit")))
  106. #endif /* __NO_INIT */
  107. #elif defined ( __GNUC__ ) && !defined (__CC_ARM) /*!< GNU Compiler */
  108. #ifndef __WEAKDEF
  109. #define __WEAKDEF __attribute__((weak))
  110. #endif /* __WEAKDEF */
  111. #ifndef __ALIGN_BEGIN
  112. #define __ALIGN_BEGIN __attribute__((aligned (4)))
  113. #endif /* __ALIGN_BEGIN */
  114. #ifndef __NOINLINE
  115. #define __NOINLINE __attribute__((noinline))
  116. #endif /* __NOINLINE */
  117. #ifndef __RAM_FUNC
  118. #define __RAM_FUNC __attribute__((long_call, section(".ramfunc")))
  119. /* Usage: __RAM_FUNC void foo(void) */
  120. #endif /* __RAM_FUNC */
  121. #ifndef __NO_INIT
  122. #define __NO_INIT __attribute__((section(".noinit")))
  123. #endif /* __NO_INIT */
  124. #elif defined (__ICCARM__) /*!< IAR Compiler */
  125. #ifndef __WEAKDEF
  126. #define __WEAKDEF __weak
  127. #endif /* __WEAKDEF */
  128. #ifndef __ALIGN_BEGIN
  129. #define __ALIGN_BEGIN _Pragma("data_alignment=4")
  130. #endif /* __ALIGN_BEGIN */
  131. #ifndef __NOINLINE
  132. #define __NOINLINE _Pragma("optimize = no_inline")
  133. #endif /* __NOINLINE */
  134. #ifndef __RAM_FUNC
  135. #define __RAM_FUNC __ramfunc
  136. #endif /* __RAM_FUNC */
  137. #ifndef __NO_INIT
  138. #define __NO_INIT __no_init
  139. #endif /* __NO_INIT */
  140. #elif defined (__CC_ARM) /*!< ARM Compiler */
  141. #ifndef __WEAKDEF
  142. #define __WEAKDEF __attribute__((weak))
  143. #endif /* __WEAKDEF */
  144. #ifndef __ALIGN_BEGIN
  145. #define __ALIGN_BEGIN __align(4)
  146. #endif /* __ALIGN_BEGIN */
  147. #ifndef __NOINLINE
  148. #define __NOINLINE __attribute__((noinline))
  149. #endif /* __NOINLINE */
  150. #ifndef __NO_INIT
  151. #define __NO_INIT __attribute__((section(".bss.noinit"), zero_init))
  152. #endif /* __NO_INIT */
  153. /* RAM functions are defined using the toolchain options.
  154. Functions that are executed in RAM should reside in a separate source module.
  155. Using the 'Options for File' dialog you can simply change the 'Code / Const'
  156. area of a module to a memory space in physical RAM. */
  157. #ifndef __RAM_FUNC
  158. #define __RAM_FUNC __attribute__((section("RAMCODE")))
  159. #endif /* __RAM_FUNC */
  160. /* Suppress warning message: extended constant initialiser used */
  161. #pragma diag_suppress 1296
  162. #else
  163. #error "unsupported compiler!!"
  164. #endif
  165. /**
  166. * @}
  167. */
  168. /**
  169. * @defgroup Extend_Macros Extend Macros
  170. * @{
  171. */
  172. /* Decimal to BCD */
  173. #define DEC2BCD(x) ((((x) / 10U) << 4U) + ((x) % 10U))
  174. /* BCD to decimal */
  175. #define BCD2DEC(x) ((((x) >> 4U) * 10U) + ((x) & 0x0FU))
  176. /* Returns the dimension of an array */
  177. #define ARRAY_SZ(x) ((sizeof(x)) / (sizeof((x)[0])))
  178. /* Returns the minimum value out of two values */
  179. #define LL_MIN(x, y) ((x) < (y) ? (x) : (y))
  180. /* Returns the maximum value out of two values */
  181. #define LL_MAX(x, y) ((x) > (y) ? (x) : (y))
  182. /**
  183. * @}
  184. */
  185. /**
  186. * @defgroup Check_Parameters_Validity Check Parameters Validity
  187. * @{
  188. */
  189. /* Check Functional State */
  190. #define IS_FUNCTIONAL_STATE(state) (((state) == DISABLE) || ((state) == ENABLE))
  191. /**
  192. * @defgroup Check_Address_Align_Validity Check Address Align Validity
  193. * @{
  194. */
  195. #define IS_ADDR_ALIGN(addr, align) (0UL == (((uint32_t)(addr)) & (((uint32_t)(align)) - 1UL)))
  196. #define IS_ADDR_ALIGN_HALFWORD(addr) (0UL == (((uint32_t)(addr)) & 0x1UL))
  197. #define IS_ADDR_ALIGN_WORD(addr) (0UL == (((uint32_t)(addr)) & 0x3UL))
  198. /**
  199. * @}
  200. */
  201. /**
  202. * @}
  203. */
  204. /**
  205. * @defgroup Peripheral_Bit_Band Peripheral Bit Band
  206. * @{
  207. */
  208. #define __PERIPH_BIT_BAND_BASE (0x42000000UL)
  209. #define __PERIPH_BASE (0x40000000UL)
  210. #define __REG_OFS(regAddr) ((regAddr) - __PERIPH_BASE)
  211. #define __BIT_BAND_ADDR(regAddr, pos) ((__REG_OFS(regAddr) << 5U) + ((uint32_t)(pos) << 2U) + __PERIPH_BIT_BAND_BASE)
  212. #define PERIPH_BIT_BAND(regAddr, pos) (*(__IO uint32_t *)__BIT_BAND_ADDR((regAddr), (pos)))
  213. /**
  214. * @}
  215. */
  216. /**
  217. * @defgroup Generic_Error_Codes Generic Error Codes
  218. * @{
  219. */
  220. #define LL_OK (0) /*!< No error */
  221. #define LL_ERR (-1) /*!< Non-specific error code */
  222. #define LL_ERR_UNINIT (-2) /*!< Module (or part of it) was not initialized properly */
  223. #define LL_ERR_INVD_PARAM (-3) /*!< Provided parameter is not valid */
  224. #define LL_ERR_INVD_MD (-4) /*!< Operation not allowed in current mode */
  225. #define LL_ERR_NOT_RDY (-5) /*!< A requested final state is not reached */
  226. #define LL_ERR_BUSY (-6) /*!< A conflicting or requested operation is still in progress */
  227. #define LL_ERR_ADDR_ALIGN (-7) /*!< Address alignment does not match */
  228. #define LL_ERR_TIMEOUT (-8) /*!< Time Out error occurred (e.g. I2C arbitration lost, Flash time-out, etc.) */
  229. #define LL_ERR_BUF_EMPTY (-9) /*!< Circular buffer can not be read because the buffer is empty */
  230. #define LL_ERR_BUF_FULL (-10) /*!< Circular buffer can not be written because the buffer is full */
  231. /**
  232. * @}
  233. */
  234. /**
  235. * @defgroup Chip_Module_Switch Chip Module Switch
  236. * @{
  237. */
  238. #define DDL_ON (1U)
  239. #define DDL_OFF (0U)
  240. /**
  241. * @}
  242. */
  243. /**
  244. * @defgroup Bit_Mask_Macros Bit Mask Macros
  245. * @{
  246. */
  247. #define BIT_MASK_00 (1UL << 0U)
  248. #define BIT_MASK_01 (1UL << 1U)
  249. #define BIT_MASK_02 (1UL << 2U)
  250. #define BIT_MASK_03 (1UL << 3U)
  251. #define BIT_MASK_04 (1UL << 4U)
  252. #define BIT_MASK_05 (1UL << 5U)
  253. #define BIT_MASK_06 (1UL << 6U)
  254. #define BIT_MASK_07 (1UL << 7U)
  255. #define BIT_MASK_08 (1UL << 8U)
  256. #define BIT_MASK_09 (1UL << 9U)
  257. #define BIT_MASK_10 (1UL << 10U)
  258. #define BIT_MASK_11 (1UL << 11U)
  259. #define BIT_MASK_12 (1UL << 12U)
  260. #define BIT_MASK_13 (1UL << 13U)
  261. #define BIT_MASK_14 (1UL << 14U)
  262. #define BIT_MASK_15 (1UL << 15U)
  263. #define BIT_MASK_16 (1UL << 16U)
  264. #define BIT_MASK_17 (1UL << 17U)
  265. #define BIT_MASK_18 (1UL << 18U)
  266. #define BIT_MASK_19 (1UL << 19U)
  267. #define BIT_MASK_20 (1UL << 20U)
  268. #define BIT_MASK_21 (1UL << 21U)
  269. #define BIT_MASK_22 (1UL << 22U)
  270. #define BIT_MASK_23 (1UL << 23U)
  271. #define BIT_MASK_24 (1UL << 24U)
  272. #define BIT_MASK_25 (1UL << 25U)
  273. #define BIT_MASK_26 (1UL << 26U)
  274. #define BIT_MASK_27 (1UL << 27U)
  275. #define BIT_MASK_28 (1UL << 28U)
  276. #define BIT_MASK_29 (1UL << 29U)
  277. #define BIT_MASK_30 (1UL << 30U)
  278. #define BIT_MASK_31 (1UL << 31U)
  279. /**
  280. * @}
  281. */
  282. /**
  283. * @defgroup Register_Macros Register Macros
  284. * @{
  285. */
  286. #define RW_MEM8(addr) (*(volatile uint8_t *)(addr))
  287. #define RW_MEM16(addr) (*(volatile uint16_t *)(addr))
  288. #define RW_MEM32(addr) (*(volatile uint32_t *)(addr))
  289. #define SET_REG_BIT(REG, BIT) ((REG) |= (BIT))
  290. #define SET_REG8_BIT(REG, BIT) ((REG) |= ((uint8_t)(BIT)))
  291. #define SET_REG16_BIT(REG, BIT) ((REG) |= ((uint16_t)(BIT)))
  292. #define SET_REG32_BIT(REG, BIT) ((REG) |= ((uint32_t)(BIT)))
  293. #define CLR_REG_BIT(REG, BIT) ((REG) &= (~(BIT)))
  294. #define CLR_REG8_BIT(REG, BIT) ((REG) &= ((uint8_t)(~((uint8_t)(BIT)))))
  295. #define CLR_REG16_BIT(REG, BIT) ((REG) &= ((uint16_t)(~((uint16_t)(BIT)))))
  296. #define CLR_REG32_BIT(REG, BIT) ((REG) &= ((uint32_t)(~((uint32_t)(BIT)))))
  297. #define READ_REG_BIT(REG, BIT) ((REG) & (BIT))
  298. #define READ_REG8_BIT(REG, BIT) ((REG) & ((uint8_t)(BIT)))
  299. #define READ_REG16_BIT(REG, BIT) ((REG) & ((uint16_t)(BIT)))
  300. #define READ_REG32_BIT(REG, BIT) ((REG) & ((uint32_t)(BIT)))
  301. #define CLR_REG(REG) ((REG) = (0U))
  302. #define CLR_REG8(REG) ((REG) = ((uint8_t)(0U)))
  303. #define CLR_REG16(REG) ((REG) = ((uint16_t)(0U)))
  304. #define CLR_REG32(REG) ((REG) = ((uint32_t)(0UL)))
  305. #define WRITE_REG(REG, VAL) ((REG) = (VAL))
  306. #define WRITE_REG8(REG, VAL) ((REG) = ((uint8_t)(VAL)))
  307. #define WRITE_REG16(REG, VAL) ((REG) = ((uint16_t)(VAL)))
  308. #define WRITE_REG32(REG, VAL) ((REG) = ((uint32_t)(VAL)))
  309. #define READ_REG(REG) (REG)
  310. #define READ_REG8(REG) (REG)
  311. #define READ_REG16(REG) (REG)
  312. #define READ_REG32(REG) (REG)
  313. #define MODIFY_REG(REGS, CLRMASK, SETMASK) (WRITE_REG((REGS), (((READ_REG(REGS)) & (~(CLRMASK))) | ((SETMASK) & (CLRMASK)))))
  314. #define MODIFY_REG8(REGS, CLRMASK, SETMASK) (WRITE_REG8((REGS), (((READ_REG8((REGS))) & ((uint8_t)(~((uint8_t)(CLRMASK))))) | ((uint8_t)(SETMASK) & (uint8_t)(CLRMASK)))))
  315. #define MODIFY_REG16(REGS, CLRMASK, SETMASK) (WRITE_REG16((REGS), (((READ_REG16((REGS))) & ((uint16_t)(~((uint16_t)(CLRMASK))))) | ((uint16_t)(SETMASK) & (uint16_t)(CLRMASK)))))
  316. #define MODIFY_REG32(REGS, CLRMASK, SETMASK) (WRITE_REG32((REGS), (((READ_REG32((REGS))) & ((uint32_t)(~((uint32_t)(CLRMASK))))) | ((uint32_t)(SETMASK) & (uint32_t)(CLRMASK)))))
  317. /**
  318. * @}
  319. */
  320. /**
  321. * @}
  322. */
  323. /*******************************************************************************
  324. * Global variable definitions ('extern')
  325. ******************************************************************************/
  326. /*******************************************************************************
  327. * Global function prototypes (definition in C source)
  328. ******************************************************************************/
  329. /**
  330. * @}
  331. */
  332. #ifdef __cplusplus
  333. }
  334. #endif
  335. #endif /* __HC32_LL_DEF_H__ */
  336. /*******************************************************************************
  337. * EOF (not truncated)
  338. ******************************************************************************/