stm32h5xx_hal_icache.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. /**
  2. ******************************************************************************
  3. * @file stm32h5xx_hal_icache.c
  4. * @author MCD Application Team
  5. * @brief ICACHE HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the Instruction Cache (ICACHE).
  8. * + Initialization and Configuration
  9. * + Invalidate functions
  10. * + Monitoring management
  11. * + Memory address remap management
  12. ******************************************************************************
  13. * @attention
  14. *
  15. * Copyright (c) 2023 STMicroelectronics.
  16. * All rights reserved.
  17. *
  18. * This software is licensed under terms that can be found in the LICENSE file
  19. * in the root directory of this software component.
  20. * If no LICENSE file comes with this software, it is provided AS-IS.
  21. *
  22. ******************************************************************************
  23. @verbatim
  24. ==============================================================================
  25. ##### ICACHE main features #####
  26. ==============================================================================
  27. [..]
  28. The Instruction Cache (ICACHE) is introduced on C-AHB code bus of
  29. Cortex-M33 processor to improve performance when fetching instruction
  30. and data from both internal and external memories. It allows close to
  31. zero wait states performance.
  32. (+) The ICACHE provides two performance counters (Hit and Miss),
  33. cache invalidate maintenance operation, error management and TrustZone
  34. security support.
  35. (+) The ICACHE provides additionally the possibility to remap input address
  36. falling into up to four memory regions (used to remap aliased code in
  37. external memories to the internal Code region, for execution)
  38. ===============================================================================
  39. ##### How to use this driver #####
  40. ===============================================================================
  41. [..]
  42. The ICACHE HAL driver can be used as follows:
  43. (#) Optionally configure the Instruction Cache mode with
  44. HAL_ICACHE_ConfigAssociativityMode() if the default configuration
  45. does not suit the application requirements.
  46. (#) Enable and disable the Instruction Cache with respectively
  47. HAL_ICACHE_Enable() and HAL_ICACHE_Disable().
  48. Use HAL_ICACHE_IsEnabled() to get the Instruction Cache status.
  49. To ensure a deterministic cache behavior after power on, system reset or after
  50. a call to @ref HAL_ICACHE_Disable(), the application must call
  51. @ref HAL_ICACHE_WaitForInvalidateComplete(). Indeed on power on, system reset
  52. or cache disable, an automatic cache invalidation procedure is launched and the
  53. cache is bypassed until the operation completes.
  54. (#) Initiate the cache maintenance invalidation procedure with either
  55. HAL_ICACHE_Invalidate() (blocking mode) or HAL_ICACHE_Invalidate_IT()
  56. (interrupt mode). When interrupt mode is used, the callback function
  57. HAL_ICACHE_InvalidateCompleteCallback() is called when the invalidate
  58. procedure is complete. The function HAL_ICACHE_WaitForInvalidateComplete()
  59. may be called to wait for the end of the invalidate procedure automatically
  60. initiated when disabling the Instruction Cache with HAL_ICACHE_Disable().
  61. The cache operation is bypassed during the invalidation procedure.
  62. (#) Use the performance monitoring counters for Hit and Miss with the following
  63. functions: HAL_ICACHE_Monitor_Start(), HAL_ICACHE_Monitor_Stop(),
  64. HAL_ICACHE_Monitor_Reset(), HAL_ICACHE_Monitor_GetHitValue() and
  65. HAL_ICACHE_Monitor_GetMissValue()
  66. (#) Enable and disable up to four regions to remap input address from external
  67. memories to the internal Code region for execution with
  68. HAL_ICACHE_EnableRemapRegion() and HAL_ICACHE_DisableRemapRegion()
  69. @endverbatim
  70. */
  71. /* Includes ------------------------------------------------------------------*/
  72. #include "stm32h5xx_hal.h"
  73. /** @addtogroup STM32H5xx_HAL_Driver
  74. * @{
  75. */
  76. /** @defgroup ICACHE ICACHE
  77. * @brief HAL ICACHE module driver
  78. * @{
  79. */
  80. #if defined(ICACHE) && defined (HAL_ICACHE_MODULE_ENABLED)
  81. /* Private typedef -----------------------------------------------------------*/
  82. /* Private constants ---------------------------------------------------------*/
  83. /** @addtogroup ICACHE_Private_Constants ICACHE Private Constants
  84. * @{
  85. */
  86. #define ICACHE_INVALIDATE_TIMEOUT_VALUE 1U /* 1ms */
  87. #define ICACHE_DISABLE_TIMEOUT_VALUE 1U /* 1ms */
  88. /**
  89. * @}
  90. */
  91. /* Private macros ------------------------------------------------------------*/
  92. /** @defgroup ICACHE_Private_Macros ICACHE Private Macros
  93. * @{
  94. */
  95. #define IS_ICACHE_ASSOCIATIVITY_MODE(__MODE__) (((__MODE__) == ICACHE_1WAY) || \
  96. ((__MODE__) == ICACHE_2WAYS))
  97. #define IS_ICACHE_MONITOR_TYPE(__TYPE__) (((__TYPE__) == ICACHE_MONITOR_HIT_MISS) || \
  98. ((__TYPE__) == ICACHE_MONITOR_HIT) || \
  99. ((__TYPE__) == ICACHE_MONITOR_MISS))
  100. #if defined(ICACHE_CRRx_REN)
  101. #define IS_ICACHE_REGION_NUMBER(__NUMBER__) ((__NUMBER__) < 4U)
  102. #define IS_ICACHE_REGION_SIZE(__SIZE__) (((__SIZE__) == ICACHE_REGIONSIZE_2MB) || \
  103. ((__SIZE__) == ICACHE_REGIONSIZE_4MB) || \
  104. ((__SIZE__) == ICACHE_REGIONSIZE_8MB) || \
  105. ((__SIZE__) == ICACHE_REGIONSIZE_16MB) || \
  106. ((__SIZE__) == ICACHE_REGIONSIZE_32MB) || \
  107. ((__SIZE__) == ICACHE_REGIONSIZE_64MB) || \
  108. ((__SIZE__) == ICACHE_REGIONSIZE_128MB))
  109. #define IS_ICACHE_REGION_TRAFFIC_ROUTE(__TRAFFICROUTE__) (((__TRAFFICROUTE__) == ICACHE_MASTER1_PORT) || \
  110. ((__TRAFFICROUTE__) == ICACHE_MASTER2_PORT))
  111. #define IS_ICACHE_REGION_OUTPUT_BURST_TYPE(__OUTPUTBURSTTYPE_) (((__OUTPUTBURSTTYPE_) == ICACHE_OUTPUT_BURST_WRAP) || \
  112. ((__OUTPUTBURSTTYPE_) == ICACHE_OUTPUT_BURST_INCR))
  113. #endif /* ICACHE_CRRx_REN */
  114. /**
  115. * @}
  116. */
  117. /* Private variables ---------------------------------------------------------*/
  118. /* Private function prototypes -----------------------------------------------*/
  119. /* Exported functions --------------------------------------------------------*/
  120. /** @defgroup ICACHE_Exported_Functions ICACHE Exported Functions
  121. * @{
  122. */
  123. /** @defgroup ICACHE_Exported_Functions_Group1 Initialization and control functions
  124. * @brief Initialization and control functions
  125. *
  126. @verbatim
  127. ==============================================================================
  128. ##### Initialization and control functions #####
  129. ==============================================================================
  130. [..]
  131. This section provides functions allowing to initialize and control the
  132. Instruction Cache (mode, invalidate procedure, performance counters).
  133. @endverbatim
  134. * @{
  135. */
  136. /**
  137. * @brief Configure the Instruction Cache cache associativity mode selection.
  138. * @param AssociativityMode Associativity mode selection
  139. * This parameter can be one of the following values:
  140. * @arg ICACHE_1WAY 1-way cache (direct mapped cache)
  141. * @arg ICACHE_2WAYS 2-ways set associative cache (default)
  142. * @retval HAL status (HAL_OK/HAL_ERROR)
  143. */
  144. HAL_StatusTypeDef HAL_ICACHE_ConfigAssociativityMode(uint32_t AssociativityMode)
  145. {
  146. HAL_StatusTypeDef status = HAL_OK;
  147. /* Check the parameters */
  148. assert_param(IS_ICACHE_ASSOCIATIVITY_MODE(AssociativityMode));
  149. /* Check cache is not enabled */
  150. if (READ_BIT(ICACHE->CR, ICACHE_CR_EN) != 0U)
  151. {
  152. status = HAL_ERROR;
  153. }
  154. else
  155. {
  156. MODIFY_REG(ICACHE->CR, ICACHE_CR_WAYSEL, AssociativityMode);
  157. }
  158. return status;
  159. }
  160. /**
  161. * @brief DeInitialize the Instruction Cache.
  162. * @retval HAL status (HAL_OK)
  163. */
  164. HAL_StatusTypeDef HAL_ICACHE_DeInit(void)
  165. {
  166. /* Reset interrupt enable value */
  167. WRITE_REG(ICACHE->IER, 0U);
  168. /* Clear any pending flags */
  169. WRITE_REG(ICACHE->FCR, ICACHE_FCR_CBSYENDF | ICACHE_FCR_CERRF);
  170. /* Disable cache then set default associative mode value */
  171. CLEAR_BIT(ICACHE->CR, ICACHE_CR_EN);
  172. WRITE_REG(ICACHE->CR, ICACHE_CR_WAYSEL);
  173. /* Stop monitor and reset monitor values */
  174. CLEAR_BIT(ICACHE->CR, ICACHE_MONITOR_HIT_MISS);
  175. SET_BIT(ICACHE->CR, (ICACHE_MONITOR_HIT_MISS << 2U));
  176. CLEAR_BIT(ICACHE->CR, (ICACHE_MONITOR_HIT_MISS << 2U));
  177. #if defined(ICACHE_CRRx_REN)
  178. /* Reset regions configuration values */
  179. WRITE_REG(ICACHE->CRR0, ICACHE_REGIONSIZE_2MB << ICACHE_CRRx_RSIZE_Pos);
  180. WRITE_REG(ICACHE->CRR1, ICACHE_REGIONSIZE_2MB << ICACHE_CRRx_RSIZE_Pos);
  181. WRITE_REG(ICACHE->CRR2, ICACHE_REGIONSIZE_2MB << ICACHE_CRRx_RSIZE_Pos);
  182. WRITE_REG(ICACHE->CRR3, ICACHE_REGIONSIZE_2MB << ICACHE_CRRx_RSIZE_Pos);
  183. #endif /* ICACHE_CRRx_REN */
  184. return HAL_OK;
  185. }
  186. /**
  187. * @brief Enable the Instruction Cache.
  188. * @note This function always returns HAL_OK even if there is any ongoing
  189. * cache operation. The Instruction Cache is bypassed until the
  190. * cache operation completes.
  191. * @retval HAL status (HAL_OK)
  192. */
  193. HAL_StatusTypeDef HAL_ICACHE_Enable(void)
  194. {
  195. SET_BIT(ICACHE->CR, ICACHE_CR_EN);
  196. return HAL_OK;
  197. }
  198. /**
  199. * @brief Disable the Instruction Cache.
  200. * @note This function waits for the cache being disabled but
  201. * not for the end of the automatic cache invalidation procedure.
  202. * @retval HAL status (HAL_OK/HAL_TIMEOUT)
  203. */
  204. HAL_StatusTypeDef HAL_ICACHE_Disable(void)
  205. {
  206. HAL_StatusTypeDef status = HAL_OK;
  207. uint32_t tickstart;
  208. /* Make sure BSYENDF is reset before to disable the instruction cache */
  209. /* as it automatically starts a cache invalidation procedure */
  210. WRITE_REG(ICACHE->FCR, ICACHE_FCR_CBSYENDF);
  211. CLEAR_BIT(ICACHE->CR, ICACHE_CR_EN);
  212. /* Get tick */
  213. tickstart = HAL_GetTick();
  214. /* Wait for instruction cache being disabled */
  215. while (READ_BIT(ICACHE->CR, ICACHE_CR_EN) != 0U)
  216. {
  217. if ((HAL_GetTick() - tickstart) > ICACHE_DISABLE_TIMEOUT_VALUE)
  218. {
  219. /* New check to avoid false timeout detection in case of preemption */
  220. if (READ_BIT(ICACHE->CR, ICACHE_CR_EN) != 0U)
  221. {
  222. status = HAL_TIMEOUT;
  223. break;
  224. }
  225. }
  226. }
  227. return status;
  228. }
  229. /**
  230. * @brief Check whether the Instruction Cache is enabled or not.
  231. * @retval Status (0: disabled, 1: enabled)
  232. */
  233. uint32_t HAL_ICACHE_IsEnabled(void)
  234. {
  235. return ((READ_BIT(ICACHE->CR, ICACHE_CR_EN) != 0U) ? 1UL : 0UL);
  236. }
  237. /**
  238. * @brief Invalidate the Instruction Cache.
  239. * @note This function waits for the end of cache invalidation procedure
  240. * and clears the associated BSYENDF flag.
  241. * @retval HAL status (HAL_OK/HAL_ERROR/HAL_TIMEOUT)
  242. */
  243. HAL_StatusTypeDef HAL_ICACHE_Invalidate(void)
  244. {
  245. HAL_StatusTypeDef status;
  246. /* Check if no ongoing operation */
  247. if (READ_BIT(ICACHE->SR, ICACHE_SR_BUSYF) == 0U)
  248. {
  249. /* Launch cache invalidation */
  250. SET_BIT(ICACHE->CR, ICACHE_CR_CACHEINV);
  251. }
  252. status = HAL_ICACHE_WaitForInvalidateComplete();
  253. return status;
  254. }
  255. /**
  256. * @brief Invalidate the Instruction Cache with interrupt.
  257. * @note This function launches cache invalidation and returns.
  258. * User application shall resort to interrupt generation to check
  259. * the end of the cache invalidation with the BSYENDF flag and the
  260. * HAL_ICACHE_InvalidateCompleteCallback() callback.
  261. * @retval HAL status (HAL_OK/HAL_ERROR)
  262. */
  263. HAL_StatusTypeDef HAL_ICACHE_Invalidate_IT(void)
  264. {
  265. HAL_StatusTypeDef status = HAL_OK;
  266. /* Check no ongoing operation */
  267. if (READ_BIT(ICACHE->SR, ICACHE_SR_BUSYF) != 0U)
  268. {
  269. status = HAL_ERROR;
  270. }
  271. else
  272. {
  273. /* Make sure BSYENDF is reset before to start cache invalidation */
  274. WRITE_REG(ICACHE->FCR, ICACHE_FCR_CBSYENDF);
  275. /* Enable end of cache invalidation interrupt */
  276. SET_BIT(ICACHE->IER, ICACHE_IER_BSYENDIE);
  277. /* Launch cache invalidation */
  278. SET_BIT(ICACHE->CR, ICACHE_CR_CACHEINV);
  279. }
  280. return status;
  281. }
  282. /**
  283. * @brief Wait for the end of the Instruction Cache invalidate procedure.
  284. * @note This function checks and clears the BSYENDF flag when set.
  285. * @retval HAL status (HAL_OK/HAL_TIMEOUT)
  286. */
  287. HAL_StatusTypeDef HAL_ICACHE_WaitForInvalidateComplete(void)
  288. {
  289. HAL_StatusTypeDef status = HAL_OK;
  290. uint32_t tickstart;
  291. /* Check if ongoing invalidation operation */
  292. if (READ_BIT(ICACHE->SR, ICACHE_SR_BUSYF) != 0U)
  293. {
  294. /* Get tick */
  295. tickstart = HAL_GetTick();
  296. /* Wait for end of cache invalidation */
  297. while (READ_BIT(ICACHE->SR, ICACHE_SR_BSYENDF) == 0U)
  298. {
  299. if ((HAL_GetTick() - tickstart) > ICACHE_INVALIDATE_TIMEOUT_VALUE)
  300. {
  301. /* New check to avoid false timeout detection in case of preemption */
  302. if (READ_BIT(ICACHE->SR, ICACHE_SR_BSYENDF) == 0U)
  303. {
  304. status = HAL_TIMEOUT;
  305. break;
  306. }
  307. }
  308. }
  309. }
  310. /* Clear BSYENDF */
  311. WRITE_REG(ICACHE->FCR, ICACHE_FCR_CBSYENDF);
  312. return status;
  313. }
  314. /**
  315. * @brief Start the Instruction Cache performance monitoring.
  316. * @param MonitorType Monitoring type
  317. * This parameter can be one of the following values:
  318. * @arg ICACHE_MONITOR_HIT_MISS Hit & Miss monitoring
  319. * @arg ICACHE_MONITOR_HIT Hit monitoring
  320. * @arg ICACHE_MONITOR_MISS Miss monitoring
  321. * @retval HAL status (HAL_OK)
  322. */
  323. HAL_StatusTypeDef HAL_ICACHE_Monitor_Start(uint32_t MonitorType)
  324. {
  325. /* Check the parameters */
  326. assert_param(IS_ICACHE_MONITOR_TYPE(MonitorType));
  327. SET_BIT(ICACHE->CR, MonitorType);
  328. return HAL_OK;
  329. }
  330. /**
  331. * @brief Stop the Instruction Cache performance monitoring.
  332. * @note Stopping the monitoring does not reset the values.
  333. * @param MonitorType Monitoring type
  334. * This parameter can be one of the following values:
  335. * @arg ICACHE_MONITOR_HIT_MISS Hit & Miss monitoring
  336. * @arg ICACHE_MONITOR_HIT Hit monitoring
  337. * @arg ICACHE_MONITOR_MISS Miss monitoring
  338. * @retval HAL status (HAL_OK)
  339. */
  340. HAL_StatusTypeDef HAL_ICACHE_Monitor_Stop(uint32_t MonitorType)
  341. {
  342. /* Check the parameters */
  343. assert_param(IS_ICACHE_MONITOR_TYPE(MonitorType));
  344. CLEAR_BIT(ICACHE->CR, MonitorType);
  345. return HAL_OK;
  346. }
  347. /**
  348. * @brief Reset the Instruction Cache performance monitoring values.
  349. * @param MonitorType Monitoring type
  350. * This parameter can be one of the following values:
  351. * @arg ICACHE_MONITOR_HIT_MISS Hit & Miss monitoring
  352. * @arg ICACHE_MONITOR_HIT Hit monitoring
  353. * @arg ICACHE_MONITOR_MISS Miss monitoring
  354. * @retval HAL status (HAL_OK)
  355. */
  356. HAL_StatusTypeDef HAL_ICACHE_Monitor_Reset(uint32_t MonitorType)
  357. {
  358. /* Check the parameters */
  359. assert_param(IS_ICACHE_MONITOR_TYPE(MonitorType));
  360. /* Force/Release reset */
  361. SET_BIT(ICACHE->CR, (MonitorType << 2U));
  362. CLEAR_BIT(ICACHE->CR, (MonitorType << 2U));
  363. return HAL_OK;
  364. }
  365. /**
  366. * @brief Get the Instruction Cache performance Hit monitoring value.
  367. * @note Upon reaching the 32-bit maximum value, monitor does not wrap.
  368. * @retval Hit monitoring value
  369. */
  370. uint32_t HAL_ICACHE_Monitor_GetHitValue(void)
  371. {
  372. return (ICACHE->HMONR);
  373. }
  374. /**
  375. * @brief Get the Instruction Cache performance Miss monitoring value.
  376. * @note Upon reaching the 32-bit maximum value, monitor does not wrap.
  377. * @retval Miss monitoring value
  378. */
  379. uint32_t HAL_ICACHE_Monitor_GetMissValue(void)
  380. {
  381. return (ICACHE->MMONR);
  382. }
  383. /**
  384. * @}
  385. */
  386. /** @defgroup ICACHE_Exported_Functions_Group2 IRQ and callback functions
  387. * @brief IRQ and callback functions
  388. *
  389. @verbatim
  390. ==============================================================================
  391. ##### IRQ and callback functions #####
  392. ==============================================================================
  393. [..]
  394. This section provides functions allowing to handle ICACHE global interrupt
  395. and the associated callback functions.
  396. @endverbatim
  397. * @{
  398. */
  399. /**
  400. * @brief Handle the Instruction Cache interrupt request.
  401. * @note This function should be called under the ICACHE_IRQHandler().
  402. * @note This function respectively disables the interrupt and clears the
  403. * flag of any pending flag before calling the associated user callback.
  404. * @retval None
  405. */
  406. void HAL_ICACHE_IRQHandler(void)
  407. {
  408. /* Get current interrupt flags and interrupt sources value */
  409. uint32_t itflags = READ_REG(ICACHE->SR);
  410. uint32_t itsources = READ_REG(ICACHE->IER);
  411. /* Check Instruction cache Error interrupt flag */
  412. if (((itflags & itsources) & ICACHE_FLAG_ERROR) != 0U)
  413. {
  414. /* Disable error interrupt */
  415. CLEAR_BIT(ICACHE->IER, ICACHE_IER_ERRIE);
  416. /* Clear ERR pending flag */
  417. WRITE_REG(ICACHE->FCR, ICACHE_FCR_CERRF);
  418. /* Instruction cache error interrupt user callback */
  419. HAL_ICACHE_ErrorCallback();
  420. }
  421. /* Check Instruction cache BusyEnd interrupt flag */
  422. if (((itflags & itsources) & ICACHE_FLAG_BUSYEND) != 0U)
  423. {
  424. /* Disable end of cache invalidation interrupt */
  425. CLEAR_BIT(ICACHE->IER, ICACHE_IER_BSYENDIE);
  426. /* Clear BSYENDF pending flag */
  427. WRITE_REG(ICACHE->FCR, ICACHE_FCR_CBSYENDF);
  428. /* Instruction cache busyend interrupt user callback */
  429. HAL_ICACHE_InvalidateCompleteCallback();
  430. }
  431. }
  432. /**
  433. * @brief Cache invalidation complete callback.
  434. */
  435. __weak void HAL_ICACHE_InvalidateCompleteCallback(void)
  436. {
  437. /* NOTE : This function should not be modified, when the callback is needed,
  438. the HAL_ICACHE_InvalidateCompleteCallback() should be implemented in the user file
  439. */
  440. }
  441. /**
  442. * @brief Error callback.
  443. */
  444. __weak void HAL_ICACHE_ErrorCallback(void)
  445. {
  446. /* NOTE : This function should not be modified, when the callback is needed,
  447. the HAL_ICACHE_ErrorCallback() should be implemented in the user file
  448. */
  449. }
  450. /**
  451. * @}
  452. */
  453. #if defined(ICACHE_CRRx_REN)
  454. /** @defgroup ICACHE_Exported_Functions_Group3 Memory remapped regions functions
  455. * @brief Memory remapped regions functions
  456. *
  457. @verbatim
  458. ==============================================================================
  459. ##### Memory remapped regions functions #####
  460. ==============================================================================
  461. [..]
  462. This section provides functions allowing to manage the remapping of
  463. external memories to internal Code for execution.
  464. @endverbatim
  465. * @{
  466. */
  467. /**
  468. * @brief Configure and enable a region for memory remapping.
  469. * @note The Instruction Cache and the region must be disabled.
  470. * @param Region Region number
  471. This parameter can be a value of @arg @ref ICACHE_Region
  472. * @param pRegionConfig Pointer to structure of ICACHE region configuration parameters
  473. * @retval HAL status (HAL_OK/HAL_ERROR)
  474. */
  475. HAL_StatusTypeDef HAL_ICACHE_EnableRemapRegion(uint32_t Region, const ICACHE_RegionConfigTypeDef *const pRegionConfig)
  476. {
  477. HAL_StatusTypeDef status = HAL_OK;
  478. __IO uint32_t *p_reg;
  479. uint32_t value;
  480. /* Check the parameters */
  481. assert_param(IS_ICACHE_REGION_NUMBER(Region));
  482. assert_param(IS_ICACHE_REGION_SIZE(pRegionConfig->Size));
  483. assert_param(IS_ICACHE_REGION_TRAFFIC_ROUTE(pRegionConfig->TrafficRoute));
  484. assert_param(IS_ICACHE_REGION_OUTPUT_BURST_TYPE(pRegionConfig->OutputBurstType));
  485. /* Check cache is not enabled */
  486. if (READ_BIT(ICACHE->CR, ICACHE_CR_EN) != 0U)
  487. {
  488. status = HAL_ERROR;
  489. }
  490. else
  491. {
  492. /* Get region control register address */
  493. p_reg = &(ICACHE->CRR0) + (1U * Region);
  494. /* Check region is not already enabled */
  495. if ((*p_reg & ICACHE_CRRx_REN) != 0U)
  496. {
  497. status = HAL_ERROR;
  498. }
  499. else
  500. {
  501. /* Region 2MB: BaseAddress size 8 bits, RemapAddress size 11 bits */
  502. /* Region 4MB: BaseAddress size 7 bits, RemapAddress size 10 bits */
  503. /* Region 8MB: BaseAddress size 6 bits, RemapAddress size 9 bits */
  504. /* Region 16MB: BaseAddress size 5 bits, RemapAddress size 8 bits */
  505. /* Region 32MB: BaseAddress size 4 bits, RemapAddress size 7 bits */
  506. /* Region 64MB: BaseAddress size 3 bits, RemapAddress size 6 bits */
  507. /* Region 128MB: BaseAddress size 2 bits, RemapAddress size 5 bits */
  508. value = ((pRegionConfig->BaseAddress & 0x1FFFFFFFU) >> 21U) & \
  509. (0xFFU & ~(pRegionConfig->Size - 1U));
  510. value |= ((pRegionConfig->RemapAddress >> 5U) & \
  511. ((uint32_t)(0x7FFU & ~(pRegionConfig->Size - 1U)) << ICACHE_CRRx_REMAPADDR_Pos));
  512. value |= (pRegionConfig->Size << ICACHE_CRRx_RSIZE_Pos) | pRegionConfig->TrafficRoute | \
  513. pRegionConfig->OutputBurstType;
  514. *p_reg = (value | ICACHE_CRRx_REN);
  515. }
  516. }
  517. return status;
  518. }
  519. /**
  520. * @brief Disable the memory remapping for a predefined region.
  521. * @param Region Region number
  522. This parameter can be a value of @arg @ref ICACHE_Region
  523. * @retval HAL status (HAL_OK/HAL_ERROR)
  524. */
  525. HAL_StatusTypeDef HAL_ICACHE_DisableRemapRegion(uint32_t Region)
  526. {
  527. HAL_StatusTypeDef status = HAL_OK;
  528. __IO uint32_t *p_reg;
  529. /* Check the parameters */
  530. assert_param(IS_ICACHE_REGION_NUMBER(Region));
  531. /* Check cache is not enabled */
  532. if (READ_BIT(ICACHE->CR, ICACHE_CR_EN) != 0U)
  533. {
  534. status = HAL_ERROR;
  535. }
  536. else
  537. {
  538. /* Get region control register address */
  539. p_reg = &(ICACHE->CRR0) + (1U * Region);
  540. *p_reg &= ~ICACHE_CRRx_REN;
  541. }
  542. return status;
  543. }
  544. /**
  545. * @}
  546. */
  547. #endif /* ICACHE_CRRx_REN */
  548. /**
  549. * @}
  550. */
  551. #endif /* ICACHE && HAL_ICACHE_MODULE_ENABLED */
  552. /**
  553. * @}
  554. */
  555. /**
  556. * @}
  557. */