1 /**
2   ******************************************************************************
3   * @file    stm32h7xx_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 de-initialization functions
9   *           + Peripheral Control functions
10   *
11   @verbatim
12   ==============================================================================
13                         ##### How to use this driver #####
14   ==============================================================================
15 
16     [..]
17     *** How to configure Interrupts using CORTEX HAL driver ***
18     ===========================================================
19     [..]
20     This section provides functions allowing to configure the NVIC interrupts (IRQ).
21     The Cortex-M exceptions are managed by CMSIS functions.
22 
23     (#) Configure the NVIC Priority Grouping using HAL_NVIC_SetPriorityGrouping()
24         function according to the following table.
25     (#) Configure the priority of the selected IRQ Channels using HAL_NVIC_SetPriority().
26     (#) Enable the selected IRQ Channels using HAL_NVIC_EnableIRQ().
27     (#) please refer to programming manual for details in how to configure priority.
28 
29      -@- When the NVIC_PRIORITYGROUP_0 is selected, IRQ preemption is no more possible.
30          The pending IRQ priority will be managed only by the sub priority.
31 
32      -@- IRQ priority order (sorted by highest to lowest priority):
33         (+@) Lowest preemption priority
34         (+@) Lowest sub priority
35         (+@) Lowest hardware priority (IRQ number)
36 
37     [..]
38     *** How to configure Systick using CORTEX HAL driver ***
39     ========================================================
40     [..]
41     Setup SysTick Timer for time base.
42 
43    (+) The HAL_SYSTICK_Config() function calls the SysTick_Config() function which
44        is a CMSIS function that:
45         (++) Configures the SysTick Reload register with value passed as function parameter.
46         (++) Configures the SysTick IRQ priority to the lowest value (0x0F).
47         (++) Resets the SysTick Counter register.
48         (++) Configures the SysTick Counter clock source to be Core Clock Source (HCLK).
49         (++) Enables the SysTick Interrupt.
50         (++) Starts the SysTick Counter.
51 
52    (+) You can change the SysTick Clock source to be HCLK_Div8 by calling the macro
53        HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK_DIV8) just after the
54        HAL_SYSTICK_Config() function call. The HAL_SYSTICK_CLKSourceConfig() macro is defined
55        inside the stm32h7xx_hal_cortex.h file.
56 
57    (+) You can change the SysTick IRQ priority by calling the
58        HAL_NVIC_SetPriority(SysTick_IRQn,...) function just after the HAL_SYSTICK_Config() function
59        call. The HAL_NVIC_SetPriority() call the NVIC_SetPriority() function which is a CMSIS function.
60 
61    (+) To adjust the SysTick time base, use the following formula:
62 
63        Reload Value = SysTick Counter Clock (Hz) x  Desired Time base (s)
64        (++) Reload Value is the parameter to be passed for HAL_SYSTICK_Config() function
65        (++) Reload Value should not exceed 0xFFFFFF
66 
67   @endverbatim
68   ******************************************************************************
69   * @attention
70   *
71   * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics.
72   * All rights reserved.</center></h2>
73   *
74   * This software component is licensed by ST under BSD 3-Clause license,
75   * the "License"; You may not use this file except in compliance with the
76   * License. You may obtain a copy of the License at:
77   *                        opensource.org/licenses/BSD-3-Clause
78   *
79   ******************************************************************************
80   */
81 
82 /* Includes ------------------------------------------------------------------*/
83 #include "stm32h7xx_hal.h"
84 
85 /** @addtogroup STM32H7xx_HAL_Driver
86   * @{
87   */
88 
89 /** @defgroup CORTEX CORTEX
90   * @brief CORTEX HAL module driver
91   * @{
92   */
93 
94 #ifdef HAL_CORTEX_MODULE_ENABLED
95 
96 /* Private types -------------------------------------------------------------*/
97 /* Private variables ---------------------------------------------------------*/
98 /* Private constants ---------------------------------------------------------*/
99 /* Private macros ------------------------------------------------------------*/
100 /* Private functions ---------------------------------------------------------*/
101 /* Exported functions --------------------------------------------------------*/
102 
103 /** @defgroup CORTEX_Exported_Functions CORTEX Exported Functions
104   * @{
105   */
106 
107 
108 /** @defgroup CORTEX_Exported_Functions_Group1 Initialization and de-initialization functions
109  *  @brief    Initialization and Configuration functions
110  *
111 @verbatim
112   ==============================================================================
113               ##### Initialization and de-initialization functions #####
114   ==============================================================================
115     [..]
116       This section provides the CORTEX HAL driver functions allowing to configure Interrupts
117       Systick functionalities
118 
119 @endverbatim
120   * @{
121   */
122 
123 
124 /**
125   * @brief  Sets the priority grouping field (preemption priority and subpriority)
126   *         using the required unlock sequence.
127   * @param  PriorityGroup The priority grouping bits length.
128   *         This parameter can be one of the following values:
129   *         @arg NVIC_PRIORITYGROUP_0: 0 bits for preemption priority
130   *                                    4 bits for subpriority
131   *         @arg NVIC_PRIORITYGROUP_1: 1 bits for preemption priority
132   *                                    3 bits for subpriority
133   *         @arg NVIC_PRIORITYGROUP_2: 2 bits for preemption priority
134   *                                    2 bits for subpriority
135   *         @arg NVIC_PRIORITYGROUP_3: 3 bits for preemption priority
136   *                                    1 bits for subpriority
137   *         @arg NVIC_PRIORITYGROUP_4: 4 bits for preemption priority
138   *                                    0 bits for subpriority
139   * @note   When the NVIC_PriorityGroup_0 is selected, IRQ preemption is no more possible.
140   *         The pending IRQ priority will be managed only by the subpriority.
141   * @retval None
142   */
HAL_NVIC_SetPriorityGrouping(uint32_t PriorityGroup)143 void HAL_NVIC_SetPriorityGrouping(uint32_t PriorityGroup)
144 {
145   /* Check the parameters */
146   assert_param(IS_NVIC_PRIORITY_GROUP(PriorityGroup));
147 
148   /* Set the PRIGROUP[10:8] bits according to the PriorityGroup parameter value */
149   NVIC_SetPriorityGrouping(PriorityGroup);
150 }
151 
152 /**
153   * @brief  Sets the priority of an interrupt.
154   * @param  IRQn External interrupt number.
155   *         This parameter can be an enumerator of IRQn_Type enumeration
156   *         (For the complete STM32 Devices IRQ Channels list, please refer to the appropriate CMSIS device file (stm32h7xxxx.h))
157   * @param  PreemptPriority The preemption priority for the IRQn channel.
158   *         This parameter can be a value between 0 and 15
159   *         A lower priority value indicates a higher priority
160   * @param  SubPriority the subpriority level for the IRQ channel.
161   *         This parameter can be a value between 0 and 15
162   *         A lower priority value indicates a higher priority.
163   * @retval None
164   */
HAL_NVIC_SetPriority(IRQn_Type IRQn,uint32_t PreemptPriority,uint32_t SubPriority)165 void HAL_NVIC_SetPriority(IRQn_Type IRQn, uint32_t PreemptPriority, uint32_t SubPriority)
166 {
167   uint32_t prioritygroup;
168 
169   /* Check the parameters */
170   assert_param(IS_NVIC_SUB_PRIORITY(SubPriority));
171   assert_param(IS_NVIC_PREEMPTION_PRIORITY(PreemptPriority));
172 
173   prioritygroup = NVIC_GetPriorityGrouping();
174 
175   NVIC_SetPriority(IRQn, NVIC_EncodePriority(prioritygroup, PreemptPriority, SubPriority));
176 }
177 
178 /**
179   * @brief  Enables a device specific interrupt in the NVIC interrupt controller.
180   * @note   To configure interrupts priority correctly, the NVIC_PriorityGroupConfig()
181   *         function should be called before.
182   * @param  IRQn External interrupt number.
183   *         This parameter can be an enumerator of IRQn_Type enumeration
184   *         (For the complete STM32 Devices IRQ Channels list, please refer to the appropriate CMSIS device file (stm32h7xxxx.h))
185   * @retval None
186   */
HAL_NVIC_EnableIRQ(IRQn_Type IRQn)187 void HAL_NVIC_EnableIRQ(IRQn_Type IRQn)
188 {
189   /* Check the parameters */
190   assert_param(IS_NVIC_DEVICE_IRQ(IRQn));
191 
192   /* Enable interrupt */
193   NVIC_EnableIRQ(IRQn);
194 }
195 
196 /**
197   * @brief  Disables a device specific interrupt in the NVIC interrupt controller.
198   * @param  IRQn External interrupt number.
199   *         This parameter can be an enumerator of IRQn_Type enumeration
200   *         (For the complete STM32 Devices IRQ Channels list, please refer to the appropriate CMSIS device file (stm32h7xxxx.h))
201   * @retval None
202   */
HAL_NVIC_DisableIRQ(IRQn_Type IRQn)203 void HAL_NVIC_DisableIRQ(IRQn_Type IRQn)
204 {
205   /* Check the parameters */
206   assert_param(IS_NVIC_DEVICE_IRQ(IRQn));
207 
208   /* Disable interrupt */
209   NVIC_DisableIRQ(IRQn);
210 }
211 
212 /**
213   * @brief  Initiates a system reset request to reset the MCU.
214   * @retval None
215   */
HAL_NVIC_SystemReset(void)216 void HAL_NVIC_SystemReset(void)
217 {
218   /* System Reset */
219   NVIC_SystemReset();
220 }
221 
222 /**
223   * @brief  Initializes the System Timer and its interrupt, and starts the System Tick Timer.
224   *         Counter is in free running mode to generate periodic interrupts.
225   * @param  TicksNumb Specifies the ticks Number of ticks between two interrupts.
226   * @retval status   - 0  Function succeeded.
227   *                  - 1  Function failed.
228   */
HAL_SYSTICK_Config(uint32_t TicksNumb)229 uint32_t HAL_SYSTICK_Config(uint32_t TicksNumb)
230 {
231    return SysTick_Config(TicksNumb);
232 }
233 /**
234   * @}
235   */
236 
237 /** @defgroup CORTEX_Exported_Functions_Group2 Peripheral Control functions
238  *  @brief   Cortex control functions
239  *
240 @verbatim
241   ==============================================================================
242                       ##### Peripheral Control functions #####
243   ==============================================================================
244     [..]
245       This subsection provides a set of functions allowing to control the CORTEX
246       (NVIC, SYSTICK, MPU) functionalities.
247 
248 
249 @endverbatim
250   * @{
251   */
252 #if (__MPU_PRESENT == 1)
253 /**
254   * @brief  Disables the MPU
255   * @retval None
256   */
HAL_MPU_Disable(void)257 void HAL_MPU_Disable(void)
258 {
259   /* Make sure outstanding transfers are done */
260   __DMB();
261 
262   /* Disable fault exceptions */
263   SCB->SHCSR &= ~SCB_SHCSR_MEMFAULTENA_Msk;
264 
265   /* Disable the MPU and clear the control register*/
266   MPU->CTRL = 0;
267 }
268 
269 /**
270   * @brief  Enables the MPU
271   * @param  MPU_Control Specifies the control mode of the MPU during hard fault,
272   *         NMI, FAULTMASK and privileged access to the default memory
273   *         This parameter can be one of the following values:
274   *            @arg MPU_HFNMI_PRIVDEF_NONE
275   *            @arg MPU_HARDFAULT_NMI
276   *            @arg MPU_PRIVILEGED_DEFAULT
277   *            @arg MPU_HFNMI_PRIVDEF
278   * @retval None
279   */
HAL_MPU_Enable(uint32_t MPU_Control)280 void HAL_MPU_Enable(uint32_t MPU_Control)
281 {
282   /* Enable the MPU */
283   MPU->CTRL = MPU_Control | MPU_CTRL_ENABLE_Msk;
284 
285   /* Enable fault exceptions */
286   SCB->SHCSR |= SCB_SHCSR_MEMFAULTENA_Msk;
287 
288   /* Ensure MPU setting take effects */
289   __DSB();
290   __ISB();
291 }
292 /**
293   * @brief  Initializes and configures the Region and the memory to be protected.
294   * @param  MPU_Init Pointer to a MPU_Region_InitTypeDef structure that contains
295   *                  the initialization and configuration information.
296   * @retval None
297   */
HAL_MPU_ConfigRegion(MPU_Region_InitTypeDef * MPU_Init)298 void HAL_MPU_ConfigRegion(MPU_Region_InitTypeDef *MPU_Init)
299 {
300   /* Check the parameters */
301   assert_param(IS_MPU_REGION_NUMBER(MPU_Init->Number));
302   assert_param(IS_MPU_REGION_ENABLE(MPU_Init->Enable));
303 
304   /* Set the Region number */
305   MPU->RNR = MPU_Init->Number;
306 
307   if ((MPU_Init->Enable) != 0UL)
308   {
309     /* Check the parameters */
310     assert_param(IS_MPU_INSTRUCTION_ACCESS(MPU_Init->DisableExec));
311     assert_param(IS_MPU_REGION_PERMISSION_ATTRIBUTE(MPU_Init->AccessPermission));
312     assert_param(IS_MPU_TEX_LEVEL(MPU_Init->TypeExtField));
313     assert_param(IS_MPU_ACCESS_SHAREABLE(MPU_Init->IsShareable));
314     assert_param(IS_MPU_ACCESS_CACHEABLE(MPU_Init->IsCacheable));
315     assert_param(IS_MPU_ACCESS_BUFFERABLE(MPU_Init->IsBufferable));
316     assert_param(IS_MPU_SUB_REGION_DISABLE(MPU_Init->SubRegionDisable));
317     assert_param(IS_MPU_REGION_SIZE(MPU_Init->Size));
318 
319     MPU->RBAR = MPU_Init->BaseAddress;
320     MPU->RASR = ((uint32_t)MPU_Init->DisableExec             << MPU_RASR_XN_Pos)   |
321                 ((uint32_t)MPU_Init->AccessPermission        << MPU_RASR_AP_Pos)   |
322                 ((uint32_t)MPU_Init->TypeExtField            << MPU_RASR_TEX_Pos)  |
323                 ((uint32_t)MPU_Init->IsShareable             << MPU_RASR_S_Pos)    |
324                 ((uint32_t)MPU_Init->IsCacheable             << MPU_RASR_C_Pos)    |
325                 ((uint32_t)MPU_Init->IsBufferable            << MPU_RASR_B_Pos)    |
326                 ((uint32_t)MPU_Init->SubRegionDisable        << MPU_RASR_SRD_Pos)  |
327                 ((uint32_t)MPU_Init->Size                    << MPU_RASR_SIZE_Pos) |
328                 ((uint32_t)MPU_Init->Enable                  << MPU_RASR_ENABLE_Pos);
329   }
330   else
331   {
332     MPU->RBAR = 0x00;
333     MPU->RASR = 0x00;
334   }
335 }
336 #endif /* __MPU_PRESENT */
337 
338 /**
339   * @brief  Gets the priority grouping field from the NVIC Interrupt Controller.
340   * @retval Priority grouping field (SCB->AIRCR [10:8] PRIGROUP field)
341   */
HAL_NVIC_GetPriorityGrouping(void)342 uint32_t HAL_NVIC_GetPriorityGrouping(void)
343 {
344   /* Get the PRIGROUP[10:8] field value */
345   return NVIC_GetPriorityGrouping();
346 }
347 
348 /**
349   * @brief  Gets the priority of an interrupt.
350   * @param  IRQn External interrupt number.
351   *         This parameter can be an enumerator of IRQn_Type enumeration
352   *         (For the complete STM32 Devices IRQ Channels list, please refer to the appropriate CMSIS device file (stm32h7xxxx.h))
353   * @param   PriorityGroup the priority grouping bits length.
354   *         This parameter can be one of the following values:
355   *           @arg NVIC_PRIORITYGROUP_0: 0 bits for preemption priority
356   *                                      4 bits for subpriority
357   *           @arg NVIC_PRIORITYGROUP_1: 1 bits for preemption priority
358   *                                      3 bits for subpriority
359   *           @arg NVIC_PRIORITYGROUP_2: 2 bits for preemption priority
360   *                                      2 bits for subpriority
361   *           @arg NVIC_PRIORITYGROUP_3: 3 bits for preemption priority
362   *                                      1 bits for subpriority
363   *           @arg NVIC_PRIORITYGROUP_4: 4 bits for preemption priority
364   *                                      0 bits for subpriority
365   * @param  pPreemptPriority Pointer on the Preemptive priority value (starting from 0).
366   * @param  pSubPriority Pointer on the Subpriority value (starting from 0).
367   * @retval None
368   */
HAL_NVIC_GetPriority(IRQn_Type IRQn,uint32_t PriorityGroup,uint32_t * pPreemptPriority,uint32_t * pSubPriority)369 void HAL_NVIC_GetPriority(IRQn_Type IRQn, uint32_t PriorityGroup, uint32_t *pPreemptPriority, uint32_t *pSubPriority)
370 {
371   /* Check the parameters */
372   assert_param(IS_NVIC_PRIORITY_GROUP(PriorityGroup));
373  /* Get priority for Cortex-M system or device specific interrupts */
374   NVIC_DecodePriority(NVIC_GetPriority(IRQn), PriorityGroup, pPreemptPriority, pSubPriority);
375 }
376 
377 /**
378   * @brief  Sets Pending bit of an external interrupt.
379   * @param  IRQn External interrupt number
380   *         This parameter can be an enumerator of IRQn_Type enumeration
381   *         (For the complete STM32 Devices IRQ Channels list, please refer to the appropriate CMSIS device file (stm32h7xxxx.h))
382   * @retval None
383   */
HAL_NVIC_SetPendingIRQ(IRQn_Type IRQn)384 void HAL_NVIC_SetPendingIRQ(IRQn_Type IRQn)
385 {
386   /* Check the parameters */
387   assert_param(IS_NVIC_DEVICE_IRQ(IRQn));
388 
389   /* Set interrupt pending */
390   NVIC_SetPendingIRQ(IRQn);
391 }
392 
393 /**
394   * @brief  Gets Pending Interrupt (reads the pending register in the NVIC
395   *         and returns the pending bit for the specified interrupt).
396   * @param  IRQn External interrupt number.
397   *          This parameter can be an enumerator of IRQn_Type enumeration
398   *         (For the complete STM32 Devices IRQ Channels list, please refer to the appropriate CMSIS device file (stm32h7xxxx.h))
399   * @retval status  - 0  Interrupt status is not pending.
400   *                 - 1  Interrupt status is pending.
401   */
HAL_NVIC_GetPendingIRQ(IRQn_Type IRQn)402 uint32_t HAL_NVIC_GetPendingIRQ(IRQn_Type IRQn)
403 {
404   /* Check the parameters */
405   assert_param(IS_NVIC_DEVICE_IRQ(IRQn));
406 
407   /* Return 1 if pending else 0 */
408   return NVIC_GetPendingIRQ(IRQn);
409 }
410 
411 /**
412   * @brief  Clears the pending bit of an external interrupt.
413   * @param  IRQn External interrupt number.
414   *         This parameter can be an enumerator of IRQn_Type enumeration
415   *         (For the complete STM32 Devices IRQ Channels list, please refer to the appropriate CMSIS device file (stm32h7xxxx.h))
416   * @retval None
417   */
HAL_NVIC_ClearPendingIRQ(IRQn_Type IRQn)418 void HAL_NVIC_ClearPendingIRQ(IRQn_Type IRQn)
419 {
420   /* Check the parameters */
421   assert_param(IS_NVIC_DEVICE_IRQ(IRQn));
422 
423   /* Clear pending interrupt */
424   NVIC_ClearPendingIRQ(IRQn);
425 }
426 
427 /**
428   * @brief Gets active interrupt ( reads the active register in NVIC and returns the active bit).
429   * @param IRQn External interrupt number
430   *         This parameter can be an enumerator of IRQn_Type enumeration
431   *         (For the complete STM32 Devices IRQ Channels list, please refer to the appropriate CMSIS device file (stm32h7xxxx.h))
432   * @retval status  - 0  Interrupt status is not pending.
433   *                 - 1  Interrupt status is pending.
434   */
HAL_NVIC_GetActive(IRQn_Type IRQn)435 uint32_t HAL_NVIC_GetActive(IRQn_Type IRQn)
436 {
437   /* Check the parameters */
438   assert_param(IS_NVIC_DEVICE_IRQ(IRQn));
439 
440   /* Return 1 if active else 0 */
441   return NVIC_GetActive(IRQn);
442 }
443 
444 /**
445   * @brief  Configures the SysTick clock source.
446   * @param  CLKSource specifies the SysTick clock source.
447   *         This parameter can be one of the following values:
448   *             @arg SYSTICK_CLKSOURCE_HCLK_DIV8: AHB clock divided by 8 selected as SysTick clock source.
449   *             @arg SYSTICK_CLKSOURCE_HCLK: AHB clock selected as SysTick clock source.
450   * @retval None
451   */
HAL_SYSTICK_CLKSourceConfig(uint32_t CLKSource)452 void HAL_SYSTICK_CLKSourceConfig(uint32_t CLKSource)
453 {
454   /* Check the parameters */
455   assert_param(IS_SYSTICK_CLK_SOURCE(CLKSource));
456   if (CLKSource == SYSTICK_CLKSOURCE_HCLK)
457   {
458     SysTick->CTRL |= SYSTICK_CLKSOURCE_HCLK;
459   }
460   else
461   {
462     SysTick->CTRL &= ~SYSTICK_CLKSOURCE_HCLK;
463   }
464 }
465 
466 /**
467   * @brief  This function handles SYSTICK interrupt request.
468   * @retval None
469   */
HAL_SYSTICK_IRQHandler(void)470 void HAL_SYSTICK_IRQHandler(void)
471 {
472   HAL_SYSTICK_Callback();
473 }
474 
475 /**
476   * @brief  SYSTICK callback.
477   * @retval None
478   */
HAL_SYSTICK_Callback(void)479 __weak void HAL_SYSTICK_Callback(void)
480 {
481   /* NOTE : This function Should not be modified, when the callback is needed,
482             the HAL_SYSTICK_Callback could be implemented in the user file
483    */
484 }
485 
486 #if defined(DUAL_CORE)
487 
488 /**
489   * @brief  Returns the current CPU ID.
490   * @retval CPU identifier
491   */
HAL_GetCurrentCPUID(void)492 uint32_t HAL_GetCurrentCPUID(void)
493 {
494   if (((SCB->CPUID & 0x000000F0U) >> 4 )== 0x7U)
495   {
496     return  CM7_CPUID;
497   }
498   else
499   {
500     return CM4_CPUID;
501   }
502 }
503 
504 #else
505 
506 /**
507 * @brief  Returns the current CPU ID.
508 * @retval CPU identifier
509 */
HAL_GetCurrentCPUID(void)510 uint32_t HAL_GetCurrentCPUID(void)
511 {
512   return  CM7_CPUID;
513 }
514 
515 #endif /*DUAL_CORE*/
516 /**
517   * @}
518   */
519 
520 /**
521   * @}
522   */
523 
524 #endif /* HAL_CORTEX_MODULE_ENABLED */
525 /**
526   * @}
527   */
528 
529 /**
530   * @}
531   */
532 
533 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
534