hc32_ll_mpu.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. /**
  2. *******************************************************************************
  3. * @file hc32_ll_mpu.h
  4. * @brief This file contains all the functions prototypes of the MPU driver
  5. * library.
  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_MPU_H__
  22. #define __HC32_LL_MPU_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 "hc32_ll_def.h"
  32. #include "hc32f4xx.h"
  33. #include "hc32f4xx_conf.h"
  34. /**
  35. * @addtogroup LL_Driver
  36. * @{
  37. */
  38. /**
  39. * @addtogroup LL_MPU
  40. * @{
  41. */
  42. #if (LL_MPU_ENABLE == DDL_ON)
  43. /*******************************************************************************
  44. * Global type definitions ('typedef')
  45. ******************************************************************************/
  46. /**
  47. * @defgroup MPU_Global_Types MPU Global Types
  48. * @{
  49. */
  50. /**
  51. * @brief MPU Unit configure structure definition
  52. */
  53. typedef struct {
  54. uint32_t u32ExceptionType; /*!< Specifies the type of exception that occurs when the unit accesses a protected region.
  55. This parameter can be a value of @ref MPU_Exception_Type */
  56. uint32_t u32BackgroundWrite; /*!< Specifies the unit's write permission for the background space.
  57. This parameter can be a value of @ref MPU_Background_Write_Permission */
  58. uint32_t u32BackgroundRead; /*!< Specifies the unit's read permission for the background space
  59. This parameter can be a value of @ref MPU_Background_Read_Permission */
  60. } stc_mpu_unit_config_t;
  61. /**
  62. * @brief MPU Init structure definition
  63. */
  64. typedef struct {
  65. stc_mpu_unit_config_t stcDma1; /*!< Configure storage protection unit of DMA1 */
  66. stc_mpu_unit_config_t stcDma2; /*!< Configure storage protection unit of DMA2 */
  67. } stc_mpu_init_t;
  68. /**
  69. * @brief MPU Region Permission structure definition
  70. */
  71. typedef struct {
  72. uint32_t u32RegionWrite; /*!< Specifies the unit's write permission for the region.
  73. This parameter can be a value of @ref MPU_Region_Write_Permission */
  74. uint32_t u32RegionRead; /*!< Specifies the unit's read permission for the region.
  75. This parameter can be a value of @ref MPU_Region_Read_Permission */
  76. } stc_mpu_region_permission_t;
  77. /**
  78. * @brief MPU region initialization structure definition
  79. * @note The effective bits of the 'u32BaseAddr' are related to the 'u32Size' of the region,
  80. * and the low 'u32Size+1' bits are fixed at 0.
  81. */
  82. typedef struct {
  83. uint32_t u32BaseAddr; /*!< Specifies the base address of the region.
  84. This parameter can be a number between 0UL and 0xFFFFFFE0UL */
  85. uint32_t u32Size; /*!< Specifies the size of the region.
  86. This parameter can be a value of @ref MPU_Region_Size */
  87. stc_mpu_region_permission_t stcDma1; /*!< Specifies the DMA1 access permission for the region */
  88. stc_mpu_region_permission_t stcDma2; /*!< Specifies the DMA2 access permission for the region */
  89. } stc_mpu_region_init_t;
  90. /**
  91. * @brief MPU Stack Pointer initialization structure definition
  92. * @note The parameters 'u32Addr' and 'u32Size' must satisfy four-byte alignment.
  93. */
  94. typedef struct {
  95. uint32_t u32ExceptionType; /*!< Specifies the type of exception that occurs when the SP access outside specified range.
  96. This parameter can be a value of @ref MPU_SP_Exception_Type */
  97. uint32_t u32Addr; /*!< Specifies the start address that the SP is allowed to access.
  98. This parameter can be a number between 0UL and 0xFFFFFFFCUL */
  99. uint32_t u32Size; /*!< Specifies the allowed access range for the SP.
  100. This parameter can be a number between 4UL and 65536UL */
  101. } stc_mpu_sp_init_t;
  102. /**
  103. * @}
  104. */
  105. /*******************************************************************************
  106. * Global pre-processor symbols/macros ('#define')
  107. ******************************************************************************/
  108. /**
  109. * @defgroup MPU_Global_Macros MPU Global Macros
  110. * @{
  111. */
  112. /**
  113. * @defgroup MPU_Unit_Type MPU Unit Type
  114. * @{
  115. */
  116. #define MPU_UNIT_DMA1 (0x01UL) /*!< System DMA_1 MPU */
  117. #define MPU_UNIT_DMA2 (0x02UL) /*!< System DMA_2 MPU */
  118. #define MPU_UNIT_ALL (MPU_UNIT_DMA1 | MPU_UNIT_DMA2)
  119. /**
  120. * @}
  121. */
  122. /**
  123. * @defgroup MPU_Region_Number MPU Region Number
  124. * @note 'MPU_REGION_NUM8' to 'MPU_REGION_NUM15' are only valid when the MPU unit is 'MPU_UNIT_DMA1' or 'MPU_UNIT_DMA2'.
  125. * @{
  126. */
  127. #define MPU_REGION_NUM0 (0x00UL) /*!< MPU region number 0 */
  128. #define MPU_REGION_NUM1 (0x01UL) /*!< MPU region number 1 */
  129. #define MPU_REGION_NUM2 (0x02UL) /*!< MPU region number 2 */
  130. #define MPU_REGION_NUM3 (0x03UL) /*!< MPU region number 3 */
  131. #define MPU_REGION_NUM4 (0x04UL) /*!< MPU region number 4 */
  132. #define MPU_REGION_NUM5 (0x05UL) /*!< MPU region number 5 */
  133. #define MPU_REGION_NUM6 (0x06UL) /*!< MPU region number 6 */
  134. #define MPU_REGION_NUM7 (0x07UL) /*!< MPU region number 7 */
  135. #define MPU_REGION_NUM8 (0x08UL) /*!< MPU region number 8 */
  136. #define MPU_REGION_NUM9 (0x09UL) /*!< MPU region number 9 */
  137. #define MPU_REGION_NUM10 (0x0AUL) /*!< MPU region number 10 */
  138. #define MPU_REGION_NUM11 (0x0BUL) /*!< MPU region number 11 */
  139. #define MPU_REGION_NUM12 (0x0CUL) /*!< MPU region number 12 */
  140. #define MPU_REGION_NUM13 (0x0DUL) /*!< MPU region number 13 */
  141. #define MPU_REGION_NUM14 (0x0EUL) /*!< MPU region number 14 */
  142. #define MPU_REGION_NUM15 (0x0FUL) /*!< MPU region number 15 */
  143. /**
  144. * @}
  145. */
  146. /**
  147. * @defgroup MPU_Background_Write_Permission MPU Background Write Permission
  148. * @{
  149. */
  150. #define MPU_BACKGROUND_WR_DISABLE (MPU_SCR_SMPUBWP) /*!< Disable write the background space */
  151. #define MPU_BACKGROUND_WR_ENABLE (0UL) /*!< Enable write the background space */
  152. /**
  153. * @}
  154. */
  155. /**
  156. * @defgroup MPU_Background_Read_Permission MPU Background Read Permission
  157. * @{
  158. */
  159. #define MPU_BACKGROUND_RD_DISABLE (MPU_SCR_SMPUBRP) /*!< Disable read the background space */
  160. #define MPU_BACKGROUND_RD_ENABLE (0UL) /*!< Enable read the background space */
  161. /**
  162. * @}
  163. */
  164. /**
  165. * @defgroup MPU_Exception_Type MPU Exception Type
  166. * @{
  167. */
  168. #define MPU_EXP_TYPE_NONE (0UL) /*!< The host unit access protection regions will be ignored */
  169. #define MPU_EXP_TYPE_BUS_ERR (MPU_SCR_SMPUACT_0) /*!< The host unit access protection regions will be ignored and a bus error will be triggered */
  170. #define MPU_EXP_TYPE_NMI (MPU_SCR_SMPUACT_1) /*!< The host unit access protection regions will be ignored and a NMI interrupt will be triggered */
  171. #define MPU_EXP_TYPE_RST (MPU_SCR_SMPUACT) /*!< The host unit access protection regions will trigger the reset */
  172. /**
  173. * @}
  174. */
  175. /**
  176. * @defgroup MPU_Region_Write_Permission MPU Region Write Permission
  177. * @{
  178. */
  179. #define MPU_REGION_WR_DISABLE (MPU_SRGWP_RG0WP) /*!< Disable write the region */
  180. #define MPU_REGION_WR_ENABLE (0UL) /*!< Enable write the region */
  181. /**
  182. * @}
  183. */
  184. /**
  185. * @defgroup MPU_Region_Read_Permission MPU Region Read Permission
  186. * @{
  187. */
  188. #define MPU_REGION_RD_DISABLE (MPU_SRGRP_RG0RP) /*!< Disable read the region */
  189. #define MPU_REGION_RD_ENABLE (0UL) /*!< Enable read the region */
  190. /**
  191. * @}
  192. */
  193. /**
  194. * @defgroup MPU_Region_Size MPU Region Size
  195. * @{
  196. */
  197. #define MPU_REGION_SIZE_32BYTE (0x04UL) /*!< 32 Byte */
  198. #define MPU_REGION_SIZE_64BYTE (0x05UL) /*!< 64 Byte */
  199. #define MPU_REGION_SIZE_128BYTE (0x06UL) /*!< 126 Byte */
  200. #define MPU_REGION_SIZE_256BYTE (0x07UL) /*!< 256 Byte */
  201. #define MPU_REGION_SIZE_512BYTE (0x08UL) /*!< 512 Byte */
  202. #define MPU_REGION_SIZE_1KBYTE (0x09UL) /*!< 1K Byte */
  203. #define MPU_REGION_SIZE_2KBYTE (0x0AUL) /*!< 2K Byte */
  204. #define MPU_REGION_SIZE_4KBYTE (0x0BUL) /*!< 4K Byte */
  205. #define MPU_REGION_SIZE_8KBYTE (0x0CUL) /*!< 8K Byte */
  206. #define MPU_REGION_SIZE_16KBYTE (0x0DUL) /*!< 16K Byte */
  207. #define MPU_REGION_SIZE_32KBYTE (0x0EUL) /*!< 32K Byte */
  208. #define MPU_REGION_SIZE_64KBYTE (0x0FUL) /*!< 64K Byte */
  209. #define MPU_REGION_SIZE_128KBYTE (0x10UL) /*!< 128K Byte */
  210. #define MPU_REGION_SIZE_256KBYTE (0x11UL) /*!< 256K Byte */
  211. #define MPU_REGION_SIZE_512KBYTE (0x12UL) /*!< 512K Byte */
  212. #define MPU_REGION_SIZE_1MBYTE (0x13UL) /*!< 1M Byte */
  213. #define MPU_REGION_SIZE_2MBYTE (0x14UL) /*!< 2M Byte */
  214. #define MPU_REGION_SIZE_4MBYTE (0x15UL) /*!< 4M Byte */
  215. #define MPU_REGION_SIZE_8MBYTE (0x16UL) /*!< 8M Byte */
  216. #define MPU_REGION_SIZE_16MBYTE (0x17UL) /*!< 16M Byte */
  217. #define MPU_REGION_SIZE_32MBYTE (0x18UL) /*!< 32M Byte */
  218. #define MPU_REGION_SIZE_64MBYTE (0x19UL) /*!< 64M Byte */
  219. #define MPU_REGION_SIZE_128MBYTE (0x1AUL) /*!< 128M Byte */
  220. #define MPU_REGION_SIZE_256MBYTE (0x1BUL) /*!< 256M Byte */
  221. #define MPU_REGION_SIZE_512MBYTE (0x1CUL) /*!< 512M Byte */
  222. #define MPU_REGION_SIZE_1GBYTE (0x1DUL) /*!< 1G Byte */
  223. #define MPU_REGION_SIZE_2GBYTE (0x1EUL) /*!< 2G Byte */
  224. #define MPU_REGION_SIZE_4GBYTE (0x1FUL) /*!< 4G Byte */
  225. /**
  226. * @}
  227. */
  228. /**
  229. * @defgroup MPU_SP_Exception_Type MPU SP Exception Type
  230. * @{
  231. */
  232. #define MPU_SP_EXP_TYPE_NMI (0UL) /*!< The SP access outside specified range will trigger a NMI interrupt */
  233. #define MPU_SP_EXP_TYPE_RST (MPU_MSPPCTL_MSPPACT) /*!< The SP access outside specified range will trigger a reset */
  234. /**
  235. * @}
  236. */
  237. /**
  238. * @defgroup MPU_SP_Unit_Type MPU SP Unit Type
  239. * @{
  240. */
  241. #define MPU_SP_UNIT_MSP (0x01UL) /*!< MSP */
  242. #define MPU_SP_UNIT_PSP (0x02UL) /*!< PSP */
  243. #define MPU_SP_UNIT_ALL (MPU_SP_UNIT_MSP | MPU_SP_UNIT_PSP)
  244. /**
  245. * @}
  246. */
  247. /**
  248. * @defgroup MPU_Flag MPU Flag
  249. * @{
  250. */
  251. #define MPU_FLAG_SMPU1EAF (MPU_SR_SMPU1EAF) /*!< System DMA_1 error flag */
  252. #define MPU_FLAG_SMPU2EAF (MPU_SR_SMPU2EAF) /*!< System DMA_2 error flag */
  253. #define MPU_FLAG_PSPEF (MPU_SR_PSPEF) /*!< PSP error flag */
  254. #define MPU_FLAG_MSPEF (MPU_SR_MSPEF) /*!< MSP error flag */
  255. #define MPU_FLAG_ALL (MPU_FLAG_SMPU1EAF | MPU_FLAG_SMPU2EAF | \
  256. MPU_FLAG_PSPEF | MPU_FLAG_MSPEF)
  257. /**
  258. * @}
  259. */
  260. /**
  261. * @defgroup MPU_IP_Type MPU IP Type
  262. * @note IP access protection is not available in privileged mode.
  263. * @{
  264. */
  265. #define MPU_IP_AES (MPU_IPPR_AESRDP) /*!< AES module */
  266. #define MPU_IP_HASH (MPU_IPPR_HASHRDP) /*!< HASH module */
  267. #define MPU_IP_TRNG (MPU_IPPR_TRNGRDP) /*!< TRNG module */
  268. #define MPU_IP_CRC (MPU_IPPR_CRCRDP) /*!< CRC module */
  269. #define MPU_IP_EFM (MPU_IPPR_EFMRDP) /*!< EFM module */
  270. #define MPU_IP_WDT (MPU_IPPR_WDTRDP) /*!< WDT module */
  271. #define MPU_IP_SWDT (MPU_IPPR_SWDTRDP) /*!< SWDT module */
  272. #define MPU_IP_BKSRAM (MPU_IPPR_BKSRAMRDP) /*!< BKSRAM module */
  273. #define MPU_IP_RTC (MPU_IPPR_RTCRDP) /*!< RTC module */
  274. #define MPU_IP_MPU (MPU_IPPR_DMPURDP) /*!< MPU module */
  275. #define MPU_IP_SRAMC (MPU_IPPR_SRAMCRDP) /*!< SRAMC module */
  276. #define MPU_IP_INTC (MPU_IPPR_INTCRDP) /*!< INTC module */
  277. #define MPU_IP_RMU_CMU_PWC (MPU_IPPR_SYSCRDP) /*!< RMU, CMU and PWC modules */
  278. #define MPU_IP_FCG (MPU_IPPR_MSTPRDP) /*!< PWR_FCG0/1/2/3 and PWR_FCG0PC registers */
  279. #define MPU_IP_ALL (MPU_IP_AES | MPU_IP_HASH | MPU_IP_TRNG | MPU_IP_CRC | \
  280. MPU_IP_EFM | MPU_IP_WDT | MPU_IP_SWDT | MPU_IP_BKSRAM | \
  281. MPU_IP_RTC | MPU_IP_MPU | MPU_IP_SRAMC | MPU_IP_INTC | \
  282. MPU_IP_FCG | MPU_IP_RMU_CMU_PWC)
  283. /**
  284. * @}
  285. */
  286. /**
  287. * @defgroup MPU_IP_Exception_Type MPU IP Exception Type
  288. * @{
  289. */
  290. #define MPU_IP_EXP_TYPE_NONE (0UL) /*!< Access to the protected IP will be ignored */
  291. #define MPU_IP_EXP_TYPE_BUS_ERR (MPU_IPPR_BUSERRE) /*!< Access to the protected IP will trigger a bus error */
  292. /**
  293. * @}
  294. */
  295. /**
  296. * @defgroup MPU_Register_Protect_Key INTC Registers Protect Key
  297. * @{
  298. */
  299. #define MPU_REG_LOCK_KEY (0x96A4UL)
  300. #define MPU_REG_UNLOCK_KEY (0x96A5UL)
  301. /**
  302. * @}
  303. */
  304. /**
  305. * @}
  306. */
  307. /*******************************************************************************
  308. * Global variable definitions ('extern')
  309. ******************************************************************************/
  310. /*******************************************************************************
  311. Global function prototypes (definition in C source)
  312. ******************************************************************************/
  313. /**
  314. * @addtogroup MPU_Global_Functions
  315. * @{
  316. */
  317. /**
  318. * @brief MPU write protect unlock.
  319. * @param None
  320. * @retval None
  321. */
  322. __STATIC_INLINE void MPU_REG_Unlock(void)
  323. {
  324. WRITE_REG32(CM_MPU->WP, MPU_REG_UNLOCK_KEY);
  325. }
  326. /**
  327. * @brief MPU write protect lock.
  328. * @param None
  329. * @retval None
  330. */
  331. __STATIC_INLINE void MPU_REG_Lock(void)
  332. {
  333. WRITE_REG32(CM_MPU->WP, MPU_REG_LOCK_KEY);
  334. }
  335. void MPU_REG_Unlock(void);
  336. void MPU_REG_Lock(void);
  337. void MPU_DeInit(void);
  338. int32_t MPU_Init(const stc_mpu_init_t *pstcMpuInit);
  339. int32_t MPU_StructInit(stc_mpu_init_t *pstcMpuInit);
  340. void MPU_SetExceptionType(uint32_t u32Unit, uint32_t u32Type);
  341. void MPU_BackgroundWriteCmd(uint32_t u32Unit, en_functional_state_t enNewState);
  342. void MPU_BackgroundReadCmd(uint32_t u32Unit, en_functional_state_t enNewState);
  343. void MPU_UnitCmd(uint32_t u32Unit, en_functional_state_t enNewState);
  344. en_flag_status_t MPU_GetStatus(uint32_t u32Flag);
  345. void MPU_ClearStatus(uint32_t u32Flag);
  346. int32_t MPU_RegionInit(uint32_t u32Num, const stc_mpu_region_init_t *pstcRegionInit);
  347. int32_t MPU_RegionStructInit(stc_mpu_region_init_t *pstcRegionInit);
  348. void MPU_SetRegionBaseAddr(uint32_t u32Num, uint32_t u32Addr);
  349. void MPU_SetRegionSize(uint32_t u32Num, uint32_t u32Size);
  350. void MPU_RegionWriteCmd(uint32_t u32Num, uint32_t u32Unit, en_functional_state_t enNewState);
  351. void MPU_RegionReadCmd(uint32_t u32Num, uint32_t u32Unit, en_functional_state_t enNewState);
  352. void MPU_RegionCmd(uint32_t u32Num, uint32_t u32Unit, en_functional_state_t enNewState);
  353. int32_t MPU_SPInit(uint32_t u32Unit, const stc_mpu_sp_init_t *pstcSpInit);
  354. int32_t MPU_SPStructInit(stc_mpu_sp_init_t *pstcSpInit);
  355. void MPU_SetSPStartAddr(uint32_t u32Unit, uint32_t u32Addr);
  356. void MPU_SetSPSize(uint32_t u32Unit, uint32_t u32Size);
  357. void MPU_SetSPExceptionType(uint32_t u32Unit, uint32_t u32Type);
  358. void MPU_SPCmd(uint32_t u32Unit, en_functional_state_t enNewState);
  359. void MPU_IP_SetExceptionType(uint32_t u32Type);
  360. void MPU_IP_WriteCmd(uint32_t u32Periph, en_functional_state_t enNewState);
  361. void MPU_IP_ReadCmd(uint32_t u32Periph, en_functional_state_t enNewState);
  362. /**
  363. * @}
  364. */
  365. #endif /* LL_MPU_ENABLE */
  366. /**
  367. * @}
  368. */
  369. /**
  370. * @}
  371. */
  372. #ifdef __cplusplus
  373. }
  374. #endif
  375. #endif /* __HC32_LL_MPU_H__ */
  376. /*******************************************************************************
  377. * EOF (not truncated)
  378. ******************************************************************************/