Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:23:06

0001 /**
0002   ******************************************************************************
0003   * @file    stm32h7xx_hal_cortex.c
0004   * @author  MCD Application Team
0005   * @brief   CORTEX HAL module driver.
0006   *          This file provides firmware functions to manage the following
0007   *          functionalities of the CORTEX:
0008   *           + Initialization and de-initialization functions
0009   *           + Peripheral Control functions
0010   *
0011   @verbatim
0012   ==============================================================================
0013                         ##### How to use this driver #####
0014   ==============================================================================
0015 
0016     [..]
0017     *** How to configure Interrupts using CORTEX HAL driver ***
0018     ===========================================================
0019     [..]
0020     This section provides functions allowing to configure the NVIC interrupts (IRQ).
0021     The Cortex-M exceptions are managed by CMSIS functions.
0022 
0023     (#) Configure the NVIC Priority Grouping using HAL_NVIC_SetPriorityGrouping()
0024         function according to the following table.
0025     (#) Configure the priority of the selected IRQ Channels using HAL_NVIC_SetPriority().
0026     (#) Enable the selected IRQ Channels using HAL_NVIC_EnableIRQ().
0027     (#) please refer to programming manual for details in how to configure priority.
0028 
0029      -@- When the NVIC_PRIORITYGROUP_0 is selected, IRQ preemption is no more possible.
0030          The pending IRQ priority will be managed only by the sub priority.
0031 
0032      -@- IRQ priority order (sorted by highest to lowest priority):
0033         (+@) Lowest preemption priority
0034         (+@) Lowest sub priority
0035         (+@) Lowest hardware priority (IRQ number)
0036 
0037     [..]
0038     *** How to configure Systick using CORTEX HAL driver ***
0039     ========================================================
0040     [..]
0041     Setup SysTick Timer for time base.
0042 
0043    (+) The HAL_SYSTICK_Config() function calls the SysTick_Config() function which
0044        is a CMSIS function that:
0045         (++) Configures the SysTick Reload register with value passed as function parameter.
0046         (++) Configures the SysTick IRQ priority to the lowest value (0x0F).
0047         (++) Resets the SysTick Counter register.
0048         (++) Configures the SysTick Counter clock source to be Core Clock Source (HCLK).
0049         (++) Enables the SysTick Interrupt.
0050         (++) Starts the SysTick Counter.
0051 
0052    (+) You can change the SysTick Clock source to be HCLK_Div8 by calling the macro
0053        HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK_DIV8) just after the
0054        HAL_SYSTICK_Config() function call. The HAL_SYSTICK_CLKSourceConfig() macro is defined
0055        inside the stm32h7xx_hal_cortex.h file.
0056 
0057    (+) You can change the SysTick IRQ priority by calling the
0058        HAL_NVIC_SetPriority(SysTick_IRQn,...) function just after the HAL_SYSTICK_Config() function
0059        call. The HAL_NVIC_SetPriority() call the NVIC_SetPriority() function which is a CMSIS function.
0060 
0061    (+) To adjust the SysTick time base, use the following formula:
0062 
0063        Reload Value = SysTick Counter Clock (Hz) x  Desired Time base (s)
0064        (++) Reload Value is the parameter to be passed for HAL_SYSTICK_Config() function
0065        (++) Reload Value should not exceed 0xFFFFFF
0066 
0067   @endverbatim
0068   ******************************************************************************
0069   * @attention
0070   *
0071   * Copyright (c) 2017 STMicroelectronics.
0072   * All rights reserved.
0073   *
0074   * This software is licensed under terms that can be found in the LICENSE file in
0075   * the root directory of this software component.
0076   * If no LICENSE file comes with this software, it is provided AS-IS.
0077   *
0078   ******************************************************************************
0079   */
0080 
0081 /* Includes ------------------------------------------------------------------*/
0082 #include "stm32h7xx_hal.h"
0083 
0084 /** @addtogroup STM32H7xx_HAL_Driver
0085   * @{
0086   */
0087 
0088 /** @defgroup CORTEX CORTEX
0089   * @ingroup RTEMSBSPsARMSTM32H7
0090   * @brief CORTEX HAL module driver
0091   * @{
0092   */
0093 
0094 #ifdef HAL_CORTEX_MODULE_ENABLED
0095 
0096 /* Private types -------------------------------------------------------------*/
0097 /* Private variables ---------------------------------------------------------*/
0098 /* Private constants ---------------------------------------------------------*/
0099 /* Private macros ------------------------------------------------------------*/
0100 /* Private functions ---------------------------------------------------------*/
0101 /* Exported functions --------------------------------------------------------*/
0102 
0103 /** @defgroup CORTEX_Exported_Functions CORTEX Exported Functions
0104   * @ingroup RTEMSBSPsARMSTM32H7
0105   * @{
0106   */
0107 
0108 
0109 /** @defgroup CORTEX_Exported_Functions_Group1 Initialization and de-initialization functions
0110   * @ingroup RTEMSBSPsARMSTM32H7
0111  *  @brief    Initialization and Configuration functions
0112  *
0113 @verbatim
0114   ==============================================================================
0115               ##### Initialization and de-initialization functions #####
0116   ==============================================================================
0117     [..]
0118       This section provides the CORTEX HAL driver functions allowing to configure Interrupts
0119       Systick functionalities
0120 
0121 @endverbatim
0122   * @{
0123   */
0124 
0125 
0126 /**
0127   * @brief  Sets the priority grouping field (preemption priority and subpriority)
0128   *         using the required unlock sequence.
0129   * @param  PriorityGroup The priority grouping bits length.
0130   *         This parameter can be one of the following values:
0131   *         @arg NVIC_PRIORITYGROUP_0: 0 bits for preemption priority
0132   *                                    4 bits for subpriority
0133   *         @arg NVIC_PRIORITYGROUP_1: 1 bits for preemption priority
0134   *                                    3 bits for subpriority
0135   *         @arg NVIC_PRIORITYGROUP_2: 2 bits for preemption priority
0136   *                                    2 bits for subpriority
0137   *         @arg NVIC_PRIORITYGROUP_3: 3 bits for preemption priority
0138   *                                    1 bits for subpriority
0139   *         @arg NVIC_PRIORITYGROUP_4: 4 bits for preemption priority
0140   *                                    0 bits for subpriority
0141   * @note   When the NVIC_PriorityGroup_0 is selected, IRQ preemption is no more possible.
0142   *         The pending IRQ priority will be managed only by the subpriority.
0143   * @retval None
0144   */
0145 void HAL_NVIC_SetPriorityGrouping(uint32_t PriorityGroup)
0146 {
0147   /* Check the parameters */
0148   assert_param(IS_NVIC_PRIORITY_GROUP(PriorityGroup));
0149 
0150   /* Set the PRIGROUP[10:8] bits according to the PriorityGroup parameter value */
0151   NVIC_SetPriorityGrouping(PriorityGroup);
0152 }
0153 
0154 /**
0155   * @brief  Sets the priority of an interrupt.
0156   * @param  IRQn External interrupt number.
0157   *         This parameter can be an enumerator of IRQn_Type enumeration
0158   *         (For the complete STM32 Devices IRQ Channels list, please refer to the appropriate CMSIS device file (stm32h7xxxx.h))
0159   * @param  PreemptPriority The preemption priority for the IRQn channel.
0160   *         This parameter can be a value between 0 and 15
0161   *         A lower priority value indicates a higher priority
0162   * @param  SubPriority the subpriority level for the IRQ channel.
0163   *         This parameter can be a value between 0 and 15
0164   *         A lower priority value indicates a higher priority.
0165   * @retval None
0166   */
0167 void HAL_NVIC_SetPriority(IRQn_Type IRQn, uint32_t PreemptPriority, uint32_t SubPriority)
0168 {
0169   uint32_t prioritygroup;
0170 
0171   /* Check the parameters */
0172   assert_param(IS_NVIC_SUB_PRIORITY(SubPriority));
0173   assert_param(IS_NVIC_PREEMPTION_PRIORITY(PreemptPriority));
0174 
0175   prioritygroup = NVIC_GetPriorityGrouping();
0176 
0177   NVIC_SetPriority(IRQn, NVIC_EncodePriority(prioritygroup, PreemptPriority, SubPriority));
0178 }
0179 
0180 /**
0181   * @brief  Enables a device specific interrupt in the NVIC interrupt controller.
0182   * @note   To configure interrupts priority correctly, the NVIC_PriorityGroupConfig()
0183   *         function should be called before.
0184   * @param  IRQn External interrupt number.
0185   *         This parameter can be an enumerator of IRQn_Type enumeration
0186   *         (For the complete STM32 Devices IRQ Channels list, please refer to the appropriate CMSIS device file (stm32h7xxxx.h))
0187   * @retval None
0188   */
0189 void HAL_NVIC_EnableIRQ(IRQn_Type IRQn)
0190 {
0191   /* Check the parameters */
0192   assert_param(IS_NVIC_DEVICE_IRQ(IRQn));
0193 
0194   /* Enable interrupt */
0195   NVIC_EnableIRQ(IRQn);
0196 }
0197 
0198 /**
0199   * @brief  Disables a device specific interrupt in the NVIC interrupt controller.
0200   * @param  IRQn External interrupt number.
0201   *         This parameter can be an enumerator of IRQn_Type enumeration
0202   *         (For the complete STM32 Devices IRQ Channels list, please refer to the appropriate CMSIS device file (stm32h7xxxx.h))
0203   * @retval None
0204   */
0205 void HAL_NVIC_DisableIRQ(IRQn_Type IRQn)
0206 {
0207   /* Check the parameters */
0208   assert_param(IS_NVIC_DEVICE_IRQ(IRQn));
0209 
0210   /* Disable interrupt */
0211   NVIC_DisableIRQ(IRQn);
0212 }
0213 
0214 /**
0215   * @brief  Initiates a system reset request to reset the MCU.
0216   * @retval None
0217   */
0218 void HAL_NVIC_SystemReset(void)
0219 {
0220   /* System Reset */
0221   NVIC_SystemReset();
0222 }
0223 
0224 /**
0225   * @brief  Initializes the System Timer and its interrupt, and starts the System Tick Timer.
0226   *         Counter is in free running mode to generate periodic interrupts.
0227   * @param  TicksNumb Specifies the ticks Number of ticks between two interrupts.
0228   * @retval status   - 0  Function succeeded.
0229   *                  - 1  Function failed.
0230   */
0231 uint32_t HAL_SYSTICK_Config(uint32_t TicksNumb)
0232 {
0233    return SysTick_Config(TicksNumb);
0234 }
0235 /**
0236   * @}
0237   */
0238 
0239 /** @defgroup CORTEX_Exported_Functions_Group2 Peripheral Control functions
0240   * @ingroup RTEMSBSPsARMSTM32H7
0241  *  @brief   Cortex control functions
0242  *
0243 @verbatim
0244   ==============================================================================
0245                       ##### Peripheral Control functions #####
0246   ==============================================================================
0247     [..]
0248       This subsection provides a set of functions allowing to control the CORTEX
0249       (NVIC, SYSTICK, MPU) functionalities.
0250 
0251 
0252 @endverbatim
0253   * @{
0254   */
0255 #if (__MPU_PRESENT == 1)
0256 /**
0257   * @brief  Disables the MPU
0258   * @retval None
0259   */
0260 void HAL_MPU_Disable(void)
0261 {
0262   /* Make sure outstanding transfers are done */
0263   __DMB();
0264 
0265   /* Disable fault exceptions */
0266   SCB->SHCSR &= ~SCB_SHCSR_MEMFAULTENA_Msk;
0267 
0268   /* Disable the MPU and clear the control register*/
0269   MPU->CTRL = 0;
0270 }
0271 
0272 /**
0273   * @brief  Enables the MPU
0274   * @param  MPU_Control Specifies the control mode of the MPU during hard fault,
0275   *         NMI, FAULTMASK and privileged access to the default memory
0276   *         This parameter can be one of the following values:
0277   *            @arg MPU_HFNMI_PRIVDEF_NONE
0278   *            @arg MPU_HARDFAULT_NMI
0279   *            @arg MPU_PRIVILEGED_DEFAULT
0280   *            @arg MPU_HFNMI_PRIVDEF
0281   * @retval None
0282   */
0283 void HAL_MPU_Enable(uint32_t MPU_Control)
0284 {
0285   /* Enable the MPU */
0286   MPU->CTRL = MPU_Control | MPU_CTRL_ENABLE_Msk;
0287 
0288   /* Enable fault exceptions */
0289   SCB->SHCSR |= SCB_SHCSR_MEMFAULTENA_Msk;
0290 
0291   /* Ensure MPU setting take effects */
0292   __DSB();
0293   __ISB();
0294 }
0295 
0296 /**
0297   * @brief  Enables the MPU Region.
0298   * @retval None
0299   */
0300 void HAL_MPU_EnableRegion(uint32_t RegionNumber)
0301 {
0302   /* Check the parameters */
0303   assert_param(IS_MPU_REGION_NUMBER(RegionNumber));
0304 
0305   /* Set the Region number */
0306   MPU->RNR = RegionNumber;
0307 
0308   /* Enable the Region */
0309   SET_BIT(MPU->RASR, MPU_RASR_ENABLE_Msk);
0310 }
0311 
0312 /**
0313   * @brief  Disables the MPU Region.
0314   * @retval None
0315   */
0316 void HAL_MPU_DisableRegion(uint32_t RegionNumber)
0317 {
0318   /* Check the parameters */
0319   assert_param(IS_MPU_REGION_NUMBER(RegionNumber));
0320 
0321   /* Set the Region number */
0322   MPU->RNR = RegionNumber;
0323 
0324   /* Disable the Region */
0325   CLEAR_BIT(MPU->RASR, MPU_RASR_ENABLE_Msk);
0326 }
0327 
0328 /**
0329   * @brief  Initializes and configures the Region and the memory to be protected.
0330   * @param MPU_Init Pointer to a MPU_Region_InitTypeDef structure that contains
0331   *                the initialization and configuration information.
0332   * @retval None
0333   */
0334 void HAL_MPU_ConfigRegion(MPU_Region_InitTypeDef *MPU_Init)
0335 {
0336   /* Check the parameters */
0337   assert_param(IS_MPU_REGION_NUMBER(MPU_Init->Number));
0338   assert_param(IS_MPU_REGION_ENABLE(MPU_Init->Enable));
0339   assert_param(IS_MPU_INSTRUCTION_ACCESS(MPU_Init->DisableExec));
0340   assert_param(IS_MPU_REGION_PERMISSION_ATTRIBUTE(MPU_Init->AccessPermission));
0341   assert_param(IS_MPU_TEX_LEVEL(MPU_Init->TypeExtField));
0342   assert_param(IS_MPU_ACCESS_SHAREABLE(MPU_Init->IsShareable));
0343   assert_param(IS_MPU_ACCESS_CACHEABLE(MPU_Init->IsCacheable));
0344   assert_param(IS_MPU_ACCESS_BUFFERABLE(MPU_Init->IsBufferable));
0345   assert_param(IS_MPU_SUB_REGION_DISABLE(MPU_Init->SubRegionDisable));
0346   assert_param(IS_MPU_REGION_SIZE(MPU_Init->Size));
0347 
0348   /* Set the Region number */
0349   MPU->RNR = MPU_Init->Number;
0350 
0351   /* Disable the Region */
0352   CLEAR_BIT(MPU->RASR, MPU_RASR_ENABLE_Msk);
0353 
0354   /* Apply configuration */
0355   MPU->RBAR = MPU_Init->BaseAddress;
0356   MPU->RASR = ((uint32_t)MPU_Init->DisableExec             << MPU_RASR_XN_Pos)   |
0357               ((uint32_t)MPU_Init->AccessPermission        << MPU_RASR_AP_Pos)   |
0358               ((uint32_t)MPU_Init->TypeExtField            << MPU_RASR_TEX_Pos)  |
0359               ((uint32_t)MPU_Init->IsShareable             << MPU_RASR_S_Pos)    |
0360               ((uint32_t)MPU_Init->IsCacheable             << MPU_RASR_C_Pos)    |
0361               ((uint32_t)MPU_Init->IsBufferable            << MPU_RASR_B_Pos)    |
0362               ((uint32_t)MPU_Init->SubRegionDisable        << MPU_RASR_SRD_Pos)  |
0363               ((uint32_t)MPU_Init->Size                    << MPU_RASR_SIZE_Pos) |
0364               ((uint32_t)MPU_Init->Enable                  << MPU_RASR_ENABLE_Pos);
0365 }
0366 #endif /* __MPU_PRESENT */
0367 
0368 /**
0369   * @brief  Gets the priority grouping field from the NVIC Interrupt Controller.
0370   * @retval Priority grouping field (SCB->AIRCR [10:8] PRIGROUP field)
0371   */
0372 uint32_t HAL_NVIC_GetPriorityGrouping(void)
0373 {
0374   /* Get the PRIGROUP[10:8] field value */
0375   return NVIC_GetPriorityGrouping();
0376 }
0377 
0378 /**
0379   * @brief  Gets the priority of an interrupt.
0380   * @param  IRQn External interrupt number.
0381   *         This parameter can be an enumerator of IRQn_Type enumeration
0382   *         (For the complete STM32 Devices IRQ Channels list, please refer to the appropriate CMSIS device file (stm32h7xxxx.h))
0383   * @param   PriorityGroup the priority grouping bits length.
0384   *         This parameter can be one of the following values:
0385   *           @arg NVIC_PRIORITYGROUP_0: 0 bits for preemption priority
0386   *                                      4 bits for subpriority
0387   *           @arg NVIC_PRIORITYGROUP_1: 1 bits for preemption priority
0388   *                                      3 bits for subpriority
0389   *           @arg NVIC_PRIORITYGROUP_2: 2 bits for preemption priority
0390   *                                      2 bits for subpriority
0391   *           @arg NVIC_PRIORITYGROUP_3: 3 bits for preemption priority
0392   *                                      1 bits for subpriority
0393   *           @arg NVIC_PRIORITYGROUP_4: 4 bits for preemption priority
0394   *                                      0 bits for subpriority
0395   * @param  pPreemptPriority Pointer on the Preemptive priority value (starting from 0).
0396   * @param  pSubPriority Pointer on the Subpriority value (starting from 0).
0397   * @retval None
0398   */
0399 void HAL_NVIC_GetPriority(IRQn_Type IRQn, uint32_t PriorityGroup, uint32_t *pPreemptPriority, uint32_t *pSubPriority)
0400 {
0401   /* Check the parameters */
0402   assert_param(IS_NVIC_PRIORITY_GROUP(PriorityGroup));
0403  /* Get priority for Cortex-M system or device specific interrupts */
0404   NVIC_DecodePriority(NVIC_GetPriority(IRQn), PriorityGroup, pPreemptPriority, pSubPriority);
0405 }
0406 
0407 /**
0408   * @brief  Sets Pending bit of an external interrupt.
0409   * @param  IRQn External interrupt number
0410   *         This parameter can be an enumerator of IRQn_Type enumeration
0411   *         (For the complete STM32 Devices IRQ Channels list, please refer to the appropriate CMSIS device file (stm32h7xxxx.h))
0412   * @retval None
0413   */
0414 void HAL_NVIC_SetPendingIRQ(IRQn_Type IRQn)
0415 {
0416   /* Check the parameters */
0417   assert_param(IS_NVIC_DEVICE_IRQ(IRQn));
0418 
0419   /* Set interrupt pending */
0420   NVIC_SetPendingIRQ(IRQn);
0421 }
0422 
0423 /**
0424   * @brief  Gets Pending Interrupt (reads the pending register in the NVIC
0425   *         and returns the pending bit for the specified interrupt).
0426   * @param  IRQn External interrupt number.
0427   *          This parameter can be an enumerator of IRQn_Type enumeration
0428   *         (For the complete STM32 Devices IRQ Channels list, please refer to the appropriate CMSIS device file (stm32h7xxxx.h))
0429   * @retval status  - 0  Interrupt status is not pending.
0430   *                 - 1  Interrupt status is pending.
0431   */
0432 uint32_t HAL_NVIC_GetPendingIRQ(IRQn_Type IRQn)
0433 {
0434   /* Check the parameters */
0435   assert_param(IS_NVIC_DEVICE_IRQ(IRQn));
0436 
0437   /* Return 1 if pending else 0 */
0438   return NVIC_GetPendingIRQ(IRQn);
0439 }
0440 
0441 /**
0442   * @brief  Clears the pending bit of an external interrupt.
0443   * @param  IRQn External interrupt number.
0444   *         This parameter can be an enumerator of IRQn_Type enumeration
0445   *         (For the complete STM32 Devices IRQ Channels list, please refer to the appropriate CMSIS device file (stm32h7xxxx.h))
0446   * @retval None
0447   */
0448 void HAL_NVIC_ClearPendingIRQ(IRQn_Type IRQn)
0449 {
0450   /* Check the parameters */
0451   assert_param(IS_NVIC_DEVICE_IRQ(IRQn));
0452 
0453   /* Clear pending interrupt */
0454   NVIC_ClearPendingIRQ(IRQn);
0455 }
0456 
0457 /**
0458   * @brief Gets active interrupt ( reads the active register in NVIC and returns the active bit).
0459   * @param IRQn External interrupt number
0460   *         This parameter can be an enumerator of IRQn_Type enumeration
0461   *         (For the complete STM32 Devices IRQ Channels list, please refer to the appropriate CMSIS device file (stm32h7xxxx.h))
0462   * @retval status  - 0  Interrupt status is not pending.
0463   *                 - 1  Interrupt status is pending.
0464   */
0465 uint32_t HAL_NVIC_GetActive(IRQn_Type IRQn)
0466 {
0467   /* Check the parameters */
0468   assert_param(IS_NVIC_DEVICE_IRQ(IRQn));
0469 
0470   /* Return 1 if active else 0 */
0471   return NVIC_GetActive(IRQn);
0472 }
0473 
0474 /**
0475   * @brief  Configures the SysTick clock source.
0476   * @param  CLKSource specifies the SysTick clock source.
0477   *         This parameter can be one of the following values:
0478   *             @arg SYSTICK_CLKSOURCE_HCLK_DIV8: AHB clock divided by 8 selected as SysTick clock source.
0479   *             @arg SYSTICK_CLKSOURCE_HCLK: AHB clock selected as SysTick clock source.
0480   * @retval None
0481   */
0482 void HAL_SYSTICK_CLKSourceConfig(uint32_t CLKSource)
0483 {
0484   /* Check the parameters */
0485   assert_param(IS_SYSTICK_CLK_SOURCE(CLKSource));
0486   if (CLKSource == SYSTICK_CLKSOURCE_HCLK)
0487   {
0488     SysTick->CTRL |= SYSTICK_CLKSOURCE_HCLK;
0489   }
0490   else
0491   {
0492     SysTick->CTRL &= ~SYSTICK_CLKSOURCE_HCLK;
0493   }
0494 }
0495 
0496 /**
0497   * @brief  This function handles SYSTICK interrupt request.
0498   * @retval None
0499   */
0500 void HAL_SYSTICK_IRQHandler(void)
0501 {
0502   HAL_SYSTICK_Callback();
0503 }
0504 
0505 /**
0506   * @brief  SYSTICK callback.
0507   * @retval None
0508   */
0509 __weak void HAL_SYSTICK_Callback(void)
0510 {
0511   /* NOTE : This function Should not be modified, when the callback is needed,
0512             the HAL_SYSTICK_Callback could be implemented in the user file
0513    */
0514 }
0515 
0516 #if defined(DUAL_CORE)
0517 
0518 /**
0519   * @brief  Returns the current CPU ID.
0520   * @retval CPU identifier
0521   */
0522 uint32_t HAL_GetCurrentCPUID(void)
0523 {
0524   if (((SCB->CPUID & 0x000000F0U) >> 4 )== 0x7U)
0525   {
0526     return  CM7_CPUID;
0527   }
0528   else
0529   {
0530     return CM4_CPUID;
0531   }
0532 }
0533 
0534 #else
0535 
0536 /**
0537 * @brief  Returns the current CPU ID.
0538 * @retval CPU identifier
0539 */
0540 uint32_t HAL_GetCurrentCPUID(void)
0541 {
0542   return  CM7_CPUID;
0543 }
0544 
0545 #endif /*DUAL_CORE*/
0546 /**
0547   * @}
0548   */
0549 
0550 /**
0551   * @}
0552   */
0553 
0554 #endif /* HAL_CORTEX_MODULE_ENABLED */
0555 /**
0556   * @}
0557   */
0558 
0559 /**
0560   * @}
0561   */
0562