1 /**
2   ******************************************************************************
3   * @file    stm32h5xx_hal_cortex.c
4   * @author  MCD Application Team
5   * @brief   CORTEX HAL module driver.
6   *          This file provides firmware functions to manage the following
7   *          functionalities of the CORTEX:
8   *           + Initialization and Configuration functions
9   *           + Peripheral Control functions
10   *
11   ******************************************************************************
12   * @attention
13   *
14   * Copyright (c) 2022 STMicroelectronics.
15   * All rights reserved.
16   *
17   * This software is licensed under terms that can be found in the LICENSE file
18   * in the root directory of this software component.
19   * If no LICENSE file comes with this software, it is provided AS-IS.
20   *
21   ******************************************************************************
22   @verbatim
23   ==============================================================================
24                         ##### How to use this driver #####
25   ==============================================================================
26 
27     [..]
28     *** How to configure Interrupts using CORTEX HAL driver ***
29     ===========================================================
30     [..]
31     This section provides functions allowing to configure the NVIC interrupts (IRQ).
32     The Cortex-M33 exceptions are managed by CMSIS functions.
33 
34     (#) Configure the NVIC Priority Grouping using HAL_NVIC_SetPriorityGrouping() function.
35     (#) Configure the priority of the selected IRQ Channels using HAL_NVIC_SetPriority().
36     (#) Enable the selected IRQ Channels using HAL_NVIC_EnableIRQ().
37 
38      -@- When the NVIC_PRIORITYGROUP_0 is selected, IRQ pre-emption is no more possible.
39          The pending IRQ priority will be managed only by the sub priority.
40 
41      -@- IRQ priority order (sorted by highest to lowest priority):
42         (+@) Lowest pre-emption priority
43         (+@) Lowest sub priority
44         (+@) Lowest hardware priority (IRQ number)
45 
46     [..]
47     *** How to configure SysTick using CORTEX HAL driver ***
48     ========================================================
49     [..]
50     Setup SysTick Timer for time base.
51 
52    (+) The HAL_SYSTICK_Config() function calls the SysTick_Config() function which
53        is a CMSIS function that:
54         (++) Configures the SysTick Reload register with value passed as function parameter.
55         (++) Configures the SysTick IRQ priority to the lowest value (0x0F).
56         (++) Resets the SysTick Counter register.
57         (++) Configures the SysTick Counter clock source to be Core Clock Source (HCLK).
58         (++) Enables the SysTick Interrupt.
59         (++) Starts the SysTick Counter.
60 
61    (+) You can change the SysTick Clock source to be HCLK_Div8 by calling the macro
62        __HAL_CORTEX_SYSTICKCLK_CONFIG(SYSTICK_CLKSOURCE_HCLK_DIV8) just after the
63        HAL_SYSTICK_Config() function call. The __HAL_CORTEX_SYSTICKCLK_CONFIG() macro is defined
64        inside the stm32h5xx_hal_cortex.h file.
65 
66    (+) You can change the SysTick IRQ priority by calling the
67        HAL_NVIC_SetPriority(SysTick_IRQn,...) function just after the HAL_SYSTICK_Config() function
68        call. The HAL_NVIC_SetPriority() call the NVIC_SetPriority() function which is a CMSIS function.
69 
70    (+) To adjust the SysTick time base, use the following formula:
71 
72        Reload Value = SysTick Counter Clock (Hz) x  Desired Time base (s)
73        (++) Reload Value is the parameter to be passed for HAL_SYSTICK_Config() function
74        (++) Reload Value should not exceed 0xFFFFFF
75 
76     [..]
77     *** How to configure MPU (secure and non secure) using CORTEX HAL driver ***
78     ===========================================================
79     [..]
80     This section provides functions allowing to Enable and configure the MPU secure and non-secure.
81 
82     (#) Enable the MPU using HAL_MPU_Enable() function.
83     (#) Disable the MPU using HAL_MPU_Disable() function.
84     (#) Enable the MPU using HAL_MPU_Enable_NS() function to address the non secure MPU.
85     (#) Disable the MPU using HAL_MPU_Disable_NS() function to address the non secure MPU.
86     (#) Configure the MPU region using HAL_MPU_ConfigRegion()
87         and HAL_MPU_ConfigRegion_NS() to address the non secure MPU.
88     (#) Configure the MPU Memory attributes using HAL_MPU_ConfigMemoryAttributes()
89         and HAL_MPU_ConfigMemoryAttributes_NS() to address the non secure MPU.
90 
91   @endverbatim
92   ******************************************************************************
93 
94   The table below gives the allowed values of the pre-emption priority and subpriority according
95   to the Priority Grouping configuration performed by HAL_NVIC_SetPriorityGrouping() function.
96 
97 ========================================================================================================================
98   NVIC_PriorityGroup  | NVIC_IRQChannelPreemptionPriority | NVIC_IRQChannelSubPriority |       Description
99 ========================================================================================================================
100  NVIC_PRIORITYGROUP_0 |                0                  |            0-15            | 0 bit for pre-emption priority
101                       |                                   |                            | 4 bits for subpriority
102 ------------------------------------------------------------------------------------------------------------------------
103  NVIC_PRIORITYGROUP_1 |                0-1                |            0-7             | 1 bit for pre-emption priority
104                       |                                   |                            | 3 bits for subpriority
105 ------------------------------------------------------------------------------------------------------------------------
106  NVIC_PRIORITYGROUP_2 |                0-3                |            0-3             | 2 bits for pre-emption priority
107                       |                                   |                            | 2 bits for subpriority
108 ------------------------------------------------------------------------------------------------------------------------
109  NVIC_PRIORITYGROUP_3 |                0-7                |            0-1             | 3 bits for pre-emption priority
110                       |                                   |                            | 1 bit for subpriority
111 ------------------------------------------------------------------------------------------------------------------------
112  NVIC_PRIORITYGROUP_4 |                0-15               |            0               | 4 bits for pre-emption priority
113                       |                                   |                            | 0 bit for subpriority
114 ========================================================================================================================
115   */
116 
117 /* Includes ------------------------------------------------------------------*/
118 #include "stm32h5xx_hal.h"
119 
120 /** @addtogroup STM32H5xx_HAL_Driver
121   * @{
122   */
123 
124 /** @addtogroup CORTEX
125   * @{
126   */
127 
128 #ifdef HAL_CORTEX_MODULE_ENABLED
129 
130 /* Private types -------------------------------------------------------------*/
131 /* Private variables ---------------------------------------------------------*/
132 /* Private constants ---------------------------------------------------------*/
133 /* Private macros ------------------------------------------------------------*/
134 /* Private functions ---------------------------------------------------------*/
135 /** @defgroup CORTEX_Private_Functions CORTEX Private Functions
136   * @{
137   */
138 static void MPU_ConfigRegion(MPU_Type *MPUx, const MPU_Region_InitTypeDef *const pMPU_RegionInit);
139 static void MPU_ConfigMemoryAttributes(MPU_Type *MPUx, const MPU_Attributes_InitTypeDef *const pMPU_AttributesInit);
140 /**
141   * @}
142   */
143 /* Exported functions --------------------------------------------------------*/
144 
145 /** @addtogroup CORTEX_Exported_Functions
146   * @{
147   */
148 
149 
150 /** @addtogroup CORTEX_Exported_Functions_Group1
151   *  @brief    NVIC functions
152   *
153 @verbatim
154   ==============================================================================
155                           ##### NVIC functions #####
156   ==============================================================================
157     [..]
158       This section provides the CORTEX HAL driver functions for NVIC functionalities
159 
160 @endverbatim
161   * @{
162   */
163 
164 
165 /**
166   * @brief  Set the priority grouping field (pre-emption priority and subpriority)
167   *         using the required unlock sequence.
168   * @param  PriorityGroup: The priority grouping bits length.
169   *         This parameter can be one of the following values:
170   *         @arg NVIC_PRIORITYGROUP_0: 0 bit  for pre-emption priority,
171   *                                    4 bits for subpriority
172   *         @arg NVIC_PRIORITYGROUP_1: 1 bit  for pre-emption priority,
173   *                                    3 bits for subpriority
174   *         @arg NVIC_PRIORITYGROUP_2: 2 bits for pre-emption priority,
175   *                                    2 bits for subpriority
176   *         @arg NVIC_PRIORITYGROUP_3: 3 bits for pre-emption priority,
177   *                                    1 bit  for subpriority
178   *         @arg NVIC_PRIORITYGROUP_4: 4 bits for pre-emption priority,
179   *                                    0 bit  for subpriority
180   * @note   When the NVIC_PRIORITYGROUP_0 is selected, IRQ pre-emption is no more possible.
181   *         The pending IRQ priority will be managed only by the subpriority.
182   * @retval None
183   */
HAL_NVIC_SetPriorityGrouping(uint32_t PriorityGroup)184 void HAL_NVIC_SetPriorityGrouping(uint32_t PriorityGroup)
185 {
186   /* Check the parameters */
187   assert_param(IS_NVIC_PRIORITY_GROUP(PriorityGroup));
188 
189   /* Set the PRIGROUP[10:8] bits according to the PriorityGroup parameter value */
190   NVIC_SetPriorityGrouping(PriorityGroup);
191 }
192 
193 /**
194   * @brief  Set the priority of an interrupt.
195   * @param  IRQn: External interrupt number.
196   *         This parameter can be an enumerator of IRQn_Type enumeration
197   *         (For the complete STM32 Devices IRQ Channels list, please refer to the appropriate
198   *          CMSIS device file (stm32h5xxxx.h))
199   * @param  PreemptPriority: The pre-emption priority for the IRQn channel.
200   *         This parameter can be a value between 0 and 15
201   *         A lower priority value indicates a higher priority
202   * @param  SubPriority: the subpriority level for the IRQ channel.
203   *         This parameter can be a value between 0 and 15
204   *         A lower priority value indicates a higher priority.
205   * @retval None
206   */
HAL_NVIC_SetPriority(IRQn_Type IRQn,uint32_t PreemptPriority,uint32_t SubPriority)207 void HAL_NVIC_SetPriority(IRQn_Type IRQn, uint32_t PreemptPriority, uint32_t SubPriority)
208 {
209   uint32_t prioritygroup;
210 
211   /* Check the parameters */
212   assert_param(IS_NVIC_SUB_PRIORITY(SubPriority));
213   assert_param(IS_NVIC_PREEMPTION_PRIORITY(PreemptPriority));
214 
215   prioritygroup = NVIC_GetPriorityGrouping();
216 
217   NVIC_SetPriority(IRQn, NVIC_EncodePriority(prioritygroup, PreemptPriority, SubPriority));
218 }
219 
220 /**
221   * @brief  Enable a device specific interrupt in the NVIC interrupt controller.
222   * @note   To configure interrupts priority correctly, the NVIC_PriorityGroupConfig()
223   *         function should be called before.
224   * @param  IRQn External interrupt number.
225   *         This parameter can be an enumerator of IRQn_Type enumeration
226   *         (For the complete STM32 Devices IRQ Channels list, please refer to the appropriate
227   *          CMSIS device file (stm32h5xxxx.h))
228   * @retval None
229   */
HAL_NVIC_EnableIRQ(IRQn_Type IRQn)230 void HAL_NVIC_EnableIRQ(IRQn_Type IRQn)
231 {
232   /* Check the parameters */
233   assert_param(IS_NVIC_DEVICE_IRQ(IRQn));
234 
235   /* Enable interrupt */
236   NVIC_EnableIRQ(IRQn);
237 }
238 
239 /**
240   * @brief  Disable a device specific interrupt in the NVIC interrupt controller.
241   * @param  IRQn External interrupt number.
242   *         This parameter can be an enumerator of IRQn_Type enumeration
243   *         (For the complete STM32 Devices IRQ Channels list, please refer to the appropriate
244   *          CMSIS device file (stm32h5xxxx.h))
245   * @retval None
246   */
HAL_NVIC_DisableIRQ(IRQn_Type IRQn)247 void HAL_NVIC_DisableIRQ(IRQn_Type IRQn)
248 {
249   /* Check the parameters */
250   assert_param(IS_NVIC_DEVICE_IRQ(IRQn));
251 
252   /* Disable interrupt */
253   NVIC_DisableIRQ(IRQn);
254 }
255 
256 /**
257   * @brief  Initiate a system reset request to reset the MCU.
258   * @retval None
259   */
HAL_NVIC_SystemReset(void)260 void HAL_NVIC_SystemReset(void)
261 {
262   /* System Reset */
263   NVIC_SystemReset();
264 }
265 
266 /**
267   * @brief  Get the priority grouping field from the NVIC Interrupt Controller.
268   * @retval Priority grouping field (SCB->AIRCR [10:8] PRIGROUP field)
269   */
HAL_NVIC_GetPriorityGrouping(void)270 uint32_t HAL_NVIC_GetPriorityGrouping(void)
271 {
272   /* Get the PRIGROUP[10:8] field value */
273   return NVIC_GetPriorityGrouping();
274 }
275 
276 /**
277   * @brief  Get the priority of an interrupt.
278   * @param  IRQn: External interrupt number.
279   *         This parameter can be an enumerator of IRQn_Type enumeration
280   *         (For the complete STM32 Devices IRQ Channels list, please refer to the appropriate
281   *          CMSIS device file (stm32h5xxxx.h))
282   * @param   PriorityGroup: the priority grouping bits length.
283   *         This parameter can be one of the following values:
284   *           @arg NVIC_PRIORITYGROUP_0: 0 bit for pre-emption priority,
285   *                                      4 bits for subpriority
286   *           @arg NVIC_PRIORITYGROUP_1: 1 bit for pre-emption priority,
287   *                                      3 bits for subpriority
288   *           @arg NVIC_PRIORITYGROUP_2: 2 bits for pre-emption priority,
289   *                                      2 bits for subpriority
290   *           @arg NVIC_PRIORITYGROUP_3: 3 bits for pre-emption priority,
291   *                                      1 bit for subpriority
292   *           @arg NVIC_PRIORITYGROUP_4: 4 bits for pre-emption priority,
293   *                                      0 bit for subpriority
294   * @param  pPreemptPriority: Pointer on the Preemptive priority value (starting from 0).
295   * @param  pSubPriority: Pointer on the Subpriority value (starting from 0).
296   * @retval None
297   */
HAL_NVIC_GetPriority(IRQn_Type IRQn,uint32_t PriorityGroup,uint32_t * const pPreemptPriority,uint32_t * const pSubPriority)298 void HAL_NVIC_GetPriority(IRQn_Type IRQn, uint32_t PriorityGroup, uint32_t *const pPreemptPriority,
299                           uint32_t *const pSubPriority)
300 {
301   /* Check the parameters */
302   assert_param(IS_NVIC_PRIORITY_GROUP(PriorityGroup));
303   /* Get priority for Cortex-M system or device specific interrupts */
304   NVIC_DecodePriority(NVIC_GetPriority(IRQn), PriorityGroup, pPreemptPriority, pSubPriority);
305 }
306 
307 /**
308   * @brief  Set Pending bit of an external interrupt.
309   * @param  IRQn External interrupt number
310   *         This parameter can be an enumerator of IRQn_Type enumeration
311   *         (For the complete STM32 Devices IRQ Channels list, please refer to the appropriate
312   *          CMSIS device file (stm32h5xxxx.h))
313   * @retval None
314   */
HAL_NVIC_SetPendingIRQ(IRQn_Type IRQn)315 void HAL_NVIC_SetPendingIRQ(IRQn_Type IRQn)
316 {
317   /* Set interrupt pending */
318   NVIC_SetPendingIRQ(IRQn);
319 }
320 
321 /**
322   * @brief  Get Pending Interrupt (read the pending register in the NVIC
323   *         and return the pending bit for the specified interrupt).
324   * @param  IRQn External interrupt number.
325   *         This parameter can be an enumerator of IRQn_Type enumeration
326   *         (For the complete STM32 Devices IRQ Channels list, please refer to the appropriate
327   *          CMSIS device file (stm32h5xxxx.h))
328   * @retval status: - 0  Interrupt status is not pending.
329   *                 - 1  Interrupt status is pending.
330   */
HAL_NVIC_GetPendingIRQ(IRQn_Type IRQn)331 uint32_t HAL_NVIC_GetPendingIRQ(IRQn_Type IRQn)
332 {
333   /* Return 1 if pending else 0 */
334   return NVIC_GetPendingIRQ(IRQn);
335 }
336 
337 /**
338   * @brief  Clear the pending bit of an external interrupt.
339   * @param  IRQn External interrupt number.
340   *         This parameter can be an enumerator of IRQn_Type enumeration
341   *         (For the complete STM32 Devices IRQ Channels list, please refer to the appropriate
342   *          CMSIS device file (stm32h5xxxx.h))
343   * @retval None
344   */
HAL_NVIC_ClearPendingIRQ(IRQn_Type IRQn)345 void HAL_NVIC_ClearPendingIRQ(IRQn_Type IRQn)
346 {
347   /* Clear pending interrupt */
348   NVIC_ClearPendingIRQ(IRQn);
349 }
350 
351 /**
352   * @brief Get active interrupt (read the active register in NVIC and return the active bit).
353   * @param IRQn External interrupt number
354   *         This parameter can be an enumerator of IRQn_Type enumeration
355   *         (For the complete STM32 Devices IRQ Channels list, please refer to the appropriate
356   *          CMSIS device file (stm32h5xxxx.h))
357   * @retval status: - 0  Interrupt status is not pending.
358   *                 - 1  Interrupt status is pending.
359   */
HAL_NVIC_GetActive(IRQn_Type IRQn)360 uint32_t HAL_NVIC_GetActive(IRQn_Type IRQn)
361 {
362   /* Return 1 if active else 0 */
363   return NVIC_GetActive(IRQn);
364 }
365 
366 /**
367   * @}
368   */
369 
370 
371 /** @addtogroup CORTEX_Exported_Functions_Group2
372   *  @brief   SYSTICK functions
373   *
374 @verbatim
375   ==============================================================================
376                           ##### SYSTICK functions #####
377   ==============================================================================
378     [..]
379       This section provides the CORTEX HAL driver functions for SYSTICK functionalities
380 
381 
382 @endverbatim
383   * @{
384   */
385 
386 /**
387   * @brief  Initialize the System Timer with interrupt enabled and start the System Tick Timer (SysTick):
388   *         Counter is in free running mode to generate periodic interrupts.
389   * @param  TicksNumb: Specifies the ticks Number of ticks between two interrupts.
390   * @retval status:  - 0  Function succeeded.
391   *                  - 1  Function failed.
392   */
HAL_SYSTICK_Config(uint32_t TicksNumb)393 uint32_t HAL_SYSTICK_Config(uint32_t TicksNumb)
394 {
395   return SysTick_Config(TicksNumb);
396 }
397 
398 /**
399   * @brief  Configure the SysTick clock source.
400   * @param  CLKSource: specifies the SysTick clock source.
401   *          This parameter can be one of the following values:
402   *             @arg SYSTICK_CLKSOURCE_LSI: LSI clock selected as SysTick clock source.
403   *             @arg SYSTICK_CLKSOURCE_LSE: LSE clock selected as SysTick clock source.
404   *             @arg SYSTICK_CLKSOURCE_HCLK: AHB clock selected as SysTick clock source.
405   *             @arg SYSTICK_CLKSOURCE_HCLK_DIV8: AHB clock divided by 8 selected as SysTick clock source.
406   * @retval None
407   */
HAL_SYSTICK_CLKSourceConfig(uint32_t CLKSource)408 void HAL_SYSTICK_CLKSourceConfig(uint32_t CLKSource)
409 {
410   /* Check the parameters */
411   assert_param(IS_SYSTICK_CLK_SOURCE(CLKSource));
412   switch (CLKSource)
413   {
414     /* Select HCLK as Systick clock source */
415     case SYSTICK_CLKSOURCE_HCLK:
416       SET_BIT(SysTick->CTRL, SYSTICK_CLKSOURCE_HCLK);
417       break;
418     /* Select HCLK_DIV8 as Systick clock source */
419     case SYSTICK_CLKSOURCE_HCLK_DIV8:
420       CLEAR_BIT(SysTick->CTRL, SYSTICK_CLKSOURCE_HCLK);
421       MODIFY_REG(RCC->CCIPR4, RCC_CCIPR4_SYSTICKSEL, (0x00000000U));
422       break;
423     /* Select LSI as Systick clock source */
424     case SYSTICK_CLKSOURCE_LSI:
425       CLEAR_BIT(SysTick->CTRL, SYSTICK_CLKSOURCE_HCLK);
426       MODIFY_REG(RCC->CCIPR4, RCC_CCIPR4_SYSTICKSEL, RCC_CCIPR4_SYSTICKSEL_0);
427       break;
428     /* Select LSE as Systick clock source */
429     case SYSTICK_CLKSOURCE_LSE:
430       CLEAR_BIT(SysTick->CTRL, SYSTICK_CLKSOURCE_HCLK);
431       MODIFY_REG(RCC->CCIPR4, RCC_CCIPR4_SYSTICKSEL, RCC_CCIPR4_SYSTICKSEL_1);
432       break;
433     default:
434       /* Nothing to do */
435       break;
436   }
437 }
438 
439 /**
440   * @brief  Handle SYSTICK interrupt request.
441   * @retval None
442   */
HAL_SYSTICK_IRQHandler(void)443 void HAL_SYSTICK_IRQHandler(void)
444 {
445   HAL_SYSTICK_Callback();
446 }
447 
448 /**
449   * @brief  SYSTICK callback.
450   * @retval None
451   */
HAL_SYSTICK_Callback(void)452 __weak void HAL_SYSTICK_Callback(void)
453 {
454   /* NOTE : This function should not be modified, when the callback is needed,
455             the HAL_SYSTICK_Callback could be implemented in the user file
456    */
457 }
458 
459 /**
460   * @}
461   */
462 
463 /** @addtogroup CORTEX_Exported_Functions_Group3
464   *  @brief   MPU functions
465   *
466 @verbatim
467   ==============================================================================
468                          ##### MPU functions #####
469   ==============================================================================
470     [..]
471       This section provides the CORTEX HAL driver functions for MPU functionalities
472 
473 
474 @endverbatim
475   * @{
476   */
477 
478 /**
479   * @brief  Enable the MPU.
480   * @param  MPU_Control: Specifies the control mode of the MPU during hard fault,
481   *          NMI, FAULTMASK and privileged access to the default memory
482   *          This parameter can be one of the following values:
483   *            @arg MPU_HFNMI_PRIVDEF_NONE
484   *            @arg MPU_HARDFAULT_NMI
485   *            @arg MPU_PRIVILEGED_DEFAULT
486   *            @arg MPU_HFNMI_PRIVDEF
487   * @retval None
488   */
HAL_MPU_Enable(uint32_t MPU_Control)489 void HAL_MPU_Enable(uint32_t MPU_Control)
490 {
491   /* Enable the MPU */
492   MPU->CTRL   = MPU_Control | MPU_CTRL_ENABLE_Msk;
493 
494   /* Enable fault exceptions */
495   SCB->SHCSR |= SCB_SHCSR_MEMFAULTENA_Msk;
496 
497   /* Follow ARM recommendation with */
498   /* - Data Memory Barrier and Instruction Synchronization to insure MPU usage */
499   __DMB(); /* Force memory writes before continuing */
500   __ISB(); /* Flush and refill pipeline with updated permissions */
501 }
502 
503 #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
504 /**
505   * @brief  Enable the non-secure MPU.
506   * @param  MPU_Control: Specifies the control mode of the MPU during hard fault,
507   *          NMI, FAULTMASK and privileged access to the default memory
508   *          This parameter can be one of the following values:
509   *            @arg MPU_HFNMI_PRIVDEF_NONE
510   *            @arg MPU_HARDFAULT_NMI
511   *            @arg MPU_PRIVILEGED_DEFAULT
512   *            @arg MPU_HFNMI_PRIVDEF
513   * @retval None
514   */
HAL_MPU_Enable_NS(uint32_t MPU_Control)515 void HAL_MPU_Enable_NS(uint32_t MPU_Control)
516 {
517   /* Enable the MPU */
518   MPU_NS->CTRL   = MPU_Control | MPU_CTRL_ENABLE_Msk;
519 
520   /* Enable fault exceptions */
521   SCB_NS->SHCSR |= SCB_SHCSR_MEMFAULTENA_Msk;
522 
523   /* Follow ARM recommendation with */
524   /* - Data Memory Barrier and Instruction Synchronization to insure MPU usage */
525   __DMB(); /* Force memory writes before continuing */
526   __ISB(); /* Flush and refill pipeline with updated permissions */
527 }
528 #endif /* __ARM_FEATURE_CMSE */
529 
530 /**
531   * @brief  Disable the MPU.
532   * @retval None
533   */
HAL_MPU_Disable(void)534 void HAL_MPU_Disable(void)
535 {
536   __DMB(); /* Force any outstanding transfers to complete before disabling MPU */
537 
538   /* Disable the MPU */
539   MPU->CTRL  &= ~MPU_CTRL_ENABLE_Msk;
540 }
541 
542 #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
543 /**
544   * @brief  Disable the non-secure MPU.
545   * @retval None
546   */
HAL_MPU_Disable_NS(void)547 void HAL_MPU_Disable_NS(void)
548 {
549   __DMB(); /* Force any outstanding transfers to complete before disabling MPU */
550 
551   /* Disable the MPU */
552   MPU_NS->CTRL  &= ~MPU_CTRL_ENABLE_Msk;
553 }
554 #endif /* __ARM_FEATURE_CMSE */
555 
556 /**
557   * @brief  Initialize and configure the Region and the memory to be protected.
558   * @param  pMPU_RegionInit: Pointer to a MPU_Region_InitTypeDef structure that contains
559   *                the initialization and configuration information.
560   * @retval None
561   */
HAL_MPU_ConfigRegion(const MPU_Region_InitTypeDef * const pMPU_RegionInit)562 void HAL_MPU_ConfigRegion(const MPU_Region_InitTypeDef *const pMPU_RegionInit)
563 {
564   MPU_ConfigRegion(MPU, pMPU_RegionInit);
565 }
566 
567 #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
568 /**
569   * @brief  Initialize and configure the Region and the memory to be protected for non-secure MPU.
570   * @param  pMPU_RegionInit: Pointer to a MPU_Region_InitTypeDef structure that contains
571   *                the initialization and configuration information.
572   * @retval None
573   */
HAL_MPU_ConfigRegion_NS(const MPU_Region_InitTypeDef * const pMPU_RegionInit)574 void HAL_MPU_ConfigRegion_NS(const MPU_Region_InitTypeDef *const pMPU_RegionInit)
575 {
576   MPU_ConfigRegion(MPU_NS, pMPU_RegionInit);
577 }
578 #endif /* __ARM_FEATURE_CMSE */
579 
580 /**
581   * @brief  Initialize and configure the memory attributes.
582   * @param  pMPU_AttributesInit: Pointer to a MPU_Attributes_InitTypeDef structure that contains
583   *                the initialization and configuration information.
584   * @retval None
585   */
HAL_MPU_ConfigMemoryAttributes(const MPU_Attributes_InitTypeDef * const pMPU_AttributesInit)586 void HAL_MPU_ConfigMemoryAttributes(const MPU_Attributes_InitTypeDef *const pMPU_AttributesInit)
587 {
588   MPU_ConfigMemoryAttributes(MPU, pMPU_AttributesInit);
589 }
590 
591 #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
592 /**
593   * @brief  Initialize and configure the memory attributes for non-secure MPU.
594   * @param  pMPU_AttributesInit: Pointer to a MPU_Attributes_InitTypeDef structure that contains
595   *                the initialization and configuration information.
596   * @retval None
597   */
HAL_MPU_ConfigMemoryAttributes_NS(const MPU_Attributes_InitTypeDef * const pMPU_AttributesInit)598 void HAL_MPU_ConfigMemoryAttributes_NS(const MPU_Attributes_InitTypeDef *const pMPU_AttributesInit)
599 {
600   MPU_ConfigMemoryAttributes(MPU_NS, pMPU_AttributesInit);
601 }
602 #endif /* __ARM_FEATURE_CMSE */
603 
604 /**
605   * @}
606   */
607 
608 /**
609   * @}
610   */
611 
612 /** @addtogroup CORTEX_Private_Functions
613   * @{
614   */
615 /**
616   * @brief  Initialize and configure the Region and the memory to be protected for MPU.
617   * @param  MPUx: Pointer to MPU_Type structure
618   *          This parameter can be one of the following values:
619   *            @arg MPU
620   *            @arg MPU_NS
621   * @param  pMPU_RegionInit: Pointer to a MPU_Region_InitTypeDef structure that contains
622   *                the initialization and configuration information.
623   * @retval None
624   */
MPU_ConfigRegion(MPU_Type * MPUx,const MPU_Region_InitTypeDef * const pMPU_RegionInit)625 static void MPU_ConfigRegion(MPU_Type *MPUx, const MPU_Region_InitTypeDef *const pMPU_RegionInit)
626 {
627   /* Check the parameters */
628 #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
629   assert_param(IS_MPU_INSTANCE(MPUx));
630 #endif /* __ARM_FEATURE_CMSE */
631   assert_param(IS_MPU_REGION_NUMBER(pMPU_RegionInit->Number));
632   assert_param(IS_MPU_REGION_ENABLE(pMPU_RegionInit->Enable));
633 
634   /* Follow ARM recommendation with Data Memory Barrier prior to MPU configuration */
635   __DMB();
636 
637   /* Set the Region number */
638   MPUx->RNR = pMPU_RegionInit->Number;
639 
640   if (pMPU_RegionInit->Enable != MPU_REGION_DISABLE)
641   {
642     /* Check the parameters */
643     assert_param(IS_MPU_INSTRUCTION_ACCESS(pMPU_RegionInit->DisableExec));
644     assert_param(IS_MPU_REGION_PERMISSION_ATTRIBUTE(pMPU_RegionInit->AccessPermission));
645     assert_param(IS_MPU_ACCESS_SHAREABLE(pMPU_RegionInit->IsShareable));
646 
647     MPUx->RBAR = (((uint32_t)pMPU_RegionInit->BaseAddress               & 0xFFFFFFE0UL)  |
648                   ((uint32_t)pMPU_RegionInit->IsShareable           << MPU_RBAR_SH_Pos)  |
649                   ((uint32_t)pMPU_RegionInit->AccessPermission      << MPU_RBAR_AP_Pos)  |
650                   ((uint32_t)pMPU_RegionInit->DisableExec           << MPU_RBAR_XN_Pos));
651 
652     MPUx->RLAR = (((uint32_t)pMPU_RegionInit->LimitAddress                    & 0xFFFFFFE0UL) |
653                   ((uint32_t)pMPU_RegionInit->AttributesIndex       << MPU_RLAR_AttrIndx_Pos) |
654                   ((uint32_t)pMPU_RegionInit->Enable                << MPU_RLAR_EN_Pos));
655   }
656   else
657   {
658     MPUx->RBAR = 0U;
659     MPUx->RLAR = 0U;
660   }
661 }
662 
663 /**
664   * @brief  Initialize and configure the memory attributes for MPU.
665   * @param  MPUx: Pointer to MPU_Type structure
666   *          This parameter can be one of the following values:
667   *            @arg MPU
668   *            @arg MPU_NS
669   * @param  pMPU_AttributesInit: Pointer to a MPU_Attributes_InitTypeDef structure that contains
670   *                the initialization and configuration information.
671   * @retval None
672   */
MPU_ConfigMemoryAttributes(MPU_Type * MPUx,const MPU_Attributes_InitTypeDef * const pMPU_AttributesInit)673 static void MPU_ConfigMemoryAttributes(MPU_Type *MPUx, const MPU_Attributes_InitTypeDef *const pMPU_AttributesInit)
674 {
675   __IO uint32_t *p_mair;
676   uint32_t      attr_values;
677   uint32_t      attr_number;
678 
679   /* Check the parameters */
680 #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
681   assert_param(IS_MPU_INSTANCE(MPUx));
682 #endif /* __ARM_FEATURE_CMSE */
683   assert_param(IS_MPU_ATTRIBUTES_NUMBER(pMPU_AttributesInit->Number));
684   /* No need to check Attributes value as all 0x0..0xFF possible */
685 
686   /* Follow ARM recommendation with Data Memory Barrier prior to MPUx configuration */
687   __DMB();
688 
689   if (pMPU_AttributesInit->Number < MPU_ATTRIBUTES_NUMBER4)
690   {
691     /* Program MPU_MAIR0 */
692     p_mair = &(MPUx->MAIR0);
693     attr_number = pMPU_AttributesInit->Number;
694   }
695   else
696   {
697     /* Program MPU_MAIR1 */
698     p_mair = &(MPUx->MAIR1);
699     attr_number = (uint32_t)pMPU_AttributesInit->Number - 4U;
700   }
701 
702   attr_values = *(p_mair);
703   attr_values &=  ~(0xFFUL << (attr_number * 8U));
704   *(p_mair) = attr_values | ((uint32_t)pMPU_AttributesInit->Attributes << (attr_number * 8U));
705 }
706 /**
707   * @}
708   */
709 
710 #endif /* HAL_CORTEX_MODULE_ENABLED */
711 /**
712   * @}
713   */
714 
715 /**
716   * @}
717   */
718 
719