2 ******************************************************************************
\r
3 * @file stm32l4xx_hal_cortex.c
\r
4 * @author MCD Application Team
\r
5 * @brief CORTEX HAL module driver.
\r
6 * This file provides firmware functions to manage the following
\r
7 * functionalities of the CORTEX:
\r
8 * + Initialization and Configuration functions
\r
9 * + Peripheral Control functions
\r
12 ==============================================================================
\r
13 ##### How to use this driver #####
\r
14 ==============================================================================
\r
17 *** How to configure Interrupts using CORTEX HAL driver ***
\r
18 ===========================================================
\r
20 This section provides functions allowing to configure the NVIC interrupts (IRQ).
\r
21 The Cortex-M4 exceptions are managed by CMSIS functions.
\r
23 (#) Configure the NVIC Priority Grouping using HAL_NVIC_SetPriorityGrouping() function.
\r
24 (#) Configure the priority of the selected IRQ Channels using HAL_NVIC_SetPriority().
\r
25 (#) Enable the selected IRQ Channels using HAL_NVIC_EnableIRQ().
\r
27 -@- When the NVIC_PRIORITYGROUP_0 is selected, IRQ pre-emption is no more possible.
\r
28 The pending IRQ priority will be managed only by the sub priority.
\r
30 -@- IRQ priority order (sorted by highest to lowest priority):
\r
31 (+@) Lowest pre-emption priority
\r
32 (+@) Lowest sub priority
\r
33 (+@) Lowest hardware priority (IRQ number)
\r
36 *** How to configure SysTick using CORTEX HAL driver ***
\r
37 ========================================================
\r
39 Setup SysTick Timer for time base.
\r
41 (+) The HAL_SYSTICK_Config() function calls the SysTick_Config() function which
\r
42 is a CMSIS function that:
\r
43 (++) Configures the SysTick Reload register with value passed as function parameter.
\r
44 (++) Configures the SysTick IRQ priority to the lowest value (0x0F).
\r
45 (++) Resets the SysTick Counter register.
\r
46 (++) Configures the SysTick Counter clock source to be Core Clock Source (HCLK).
\r
47 (++) Enables the SysTick Interrupt.
\r
48 (++) Starts the SysTick Counter.
\r
50 (+) You can change the SysTick Clock source to be HCLK_Div8 by calling the macro
\r
51 __HAL_CORTEX_SYSTICKCLK_CONFIG(SYSTICK_CLKSOURCE_HCLK_DIV8) just after the
\r
52 HAL_SYSTICK_Config() function call. The __HAL_CORTEX_SYSTICKCLK_CONFIG() macro is defined
\r
53 inside the stm32l4xx_hal_cortex.h file.
\r
55 (+) You can change the SysTick IRQ priority by calling the
\r
56 HAL_NVIC_SetPriority(SysTick_IRQn,...) function just after the HAL_SYSTICK_Config() function
\r
57 call. The HAL_NVIC_SetPriority() call the NVIC_SetPriority() function which is a CMSIS function.
\r
59 (+) To adjust the SysTick time base, use the following formula:
\r
61 Reload Value = SysTick Counter Clock (Hz) x Desired Time base (s)
\r
62 (++) Reload Value is the parameter to be passed for HAL_SYSTICK_Config() function
\r
63 (++) Reload Value should not exceed 0xFFFFFF
\r
66 ******************************************************************************
\r
68 The table below gives the allowed values of the pre-emption priority and subpriority according
\r
69 to the Priority Grouping configuration performed by HAL_NVIC_SetPriorityGrouping() function.
\r
71 ==========================================================================================================================
\r
72 NVIC_PriorityGroup | NVIC_IRQChannelPreemptionPriority | NVIC_IRQChannelSubPriority | Description
\r
73 ==========================================================================================================================
\r
74 NVIC_PRIORITYGROUP_0 | 0 | 0-15 | 0 bit for pre-emption priority
\r
75 | | | 4 bits for subpriority
\r
76 --------------------------------------------------------------------------------------------------------------------------
\r
77 NVIC_PRIORITYGROUP_1 | 0-1 | 0-7 | 1 bit for pre-emption priority
\r
78 | | | 3 bits for subpriority
\r
79 --------------------------------------------------------------------------------------------------------------------------
\r
80 NVIC_PRIORITYGROUP_2 | 0-3 | 0-3 | 2 bits for pre-emption priority
\r
81 | | | 2 bits for subpriority
\r
82 --------------------------------------------------------------------------------------------------------------------------
\r
83 NVIC_PRIORITYGROUP_3 | 0-7 | 0-1 | 3 bits for pre-emption priority
\r
84 | | | 1 bit for subpriority
\r
85 --------------------------------------------------------------------------------------------------------------------------
\r
86 NVIC_PRIORITYGROUP_4 | 0-15 | 0 | 4 bits for pre-emption priority
\r
87 | | | 0 bit for subpriority
\r
88 ==========================================================================================================================
\r
90 ******************************************************************************
\r
93 * <h2><center>© Copyright (c) 2017 STMicroelectronics.
\r
94 * All rights reserved.</center></h2>
\r
96 * This software component is licensed by ST under BSD 3-Clause license,
\r
97 * the "License"; You may not use this file except in compliance with the
\r
98 * License. You may obtain a copy of the License at:
\r
99 * opensource.org/licenses/BSD-3-Clause
\r
101 ******************************************************************************
\r
104 /* Includes ------------------------------------------------------------------*/
\r
105 #include "stm32l4xx_hal.h"
\r
107 /** @addtogroup STM32L4xx_HAL_Driver
\r
111 /** @addtogroup CORTEX
\r
115 #ifdef HAL_CORTEX_MODULE_ENABLED
\r
117 /* Private types -------------------------------------------------------------*/
\r
118 /* Private variables ---------------------------------------------------------*/
\r
119 /* Private constants ---------------------------------------------------------*/
\r
120 /* Private macros ------------------------------------------------------------*/
\r
121 /* Private functions ---------------------------------------------------------*/
\r
122 /* Exported functions --------------------------------------------------------*/
\r
124 /** @addtogroup CORTEX_Exported_Functions
\r
129 /** @addtogroup CORTEX_Exported_Functions_Group1
\r
130 * @brief Initialization and Configuration functions
\r
133 ==============================================================================
\r
134 ##### Initialization and Configuration functions #####
\r
135 ==============================================================================
\r
137 This section provides the CORTEX HAL driver functions allowing to configure Interrupts
\r
138 SysTick functionalities
\r
146 * @brief Set the priority grouping field (pre-emption priority and subpriority)
\r
147 * using the required unlock sequence.
\r
148 * @param PriorityGroup: The priority grouping bits length.
\r
149 * This parameter can be one of the following values:
\r
150 * @arg NVIC_PRIORITYGROUP_0: 0 bit for pre-emption priority,
\r
151 * 4 bits for subpriority
\r
152 * @arg NVIC_PRIORITYGROUP_1: 1 bit for pre-emption priority,
\r
153 * 3 bits for subpriority
\r
154 * @arg NVIC_PRIORITYGROUP_2: 2 bits for pre-emption priority,
\r
155 * 2 bits for subpriority
\r
156 * @arg NVIC_PRIORITYGROUP_3: 3 bits for pre-emption priority,
\r
157 * 1 bit for subpriority
\r
158 * @arg NVIC_PRIORITYGROUP_4: 4 bits for pre-emption priority,
\r
159 * 0 bit for subpriority
\r
160 * @note When the NVIC_PriorityGroup_0 is selected, IRQ pre-emption is no more possible.
\r
161 * The pending IRQ priority will be managed only by the subpriority.
\r
164 void HAL_NVIC_SetPriorityGrouping(uint32_t PriorityGroup)
\r
166 /* Check the parameters */
\r
167 assert_param(IS_NVIC_PRIORITY_GROUP(PriorityGroup));
\r
169 /* Set the PRIGROUP[10:8] bits according to the PriorityGroup parameter value */
\r
170 NVIC_SetPriorityGrouping(PriorityGroup);
\r
174 * @brief Set the priority of an interrupt.
\r
175 * @param IRQn: External interrupt number.
\r
176 * This parameter can be an enumerator of IRQn_Type enumeration
\r
177 * (For the complete STM32 Devices IRQ Channels list, please refer to the appropriate CMSIS device file (stm32l4xxxx.h))
\r
178 * @param PreemptPriority: The pre-emption priority for the IRQn channel.
\r
179 * This parameter can be a value between 0 and 15
\r
180 * A lower priority value indicates a higher priority
\r
181 * @param SubPriority: the subpriority level for the IRQ channel.
\r
182 * This parameter can be a value between 0 and 15
\r
183 * A lower priority value indicates a higher priority.
\r
186 void HAL_NVIC_SetPriority(IRQn_Type IRQn, uint32_t PreemptPriority, uint32_t SubPriority)
\r
188 uint32_t prioritygroup = 0x00;
\r
190 /* Check the parameters */
\r
191 assert_param(IS_NVIC_SUB_PRIORITY(SubPriority));
\r
192 assert_param(IS_NVIC_PREEMPTION_PRIORITY(PreemptPriority));
\r
194 prioritygroup = NVIC_GetPriorityGrouping();
\r
196 NVIC_SetPriority(IRQn, NVIC_EncodePriority(prioritygroup, PreemptPriority, SubPriority));
\r
200 * @brief Enable a device specific interrupt in the NVIC interrupt controller.
\r
201 * @note To configure interrupts priority correctly, the NVIC_PriorityGroupConfig()
\r
202 * function should be called before.
\r
203 * @param IRQn External interrupt number.
\r
204 * This parameter can be an enumerator of IRQn_Type enumeration
\r
205 * (For the complete STM32 Devices IRQ Channels list, please refer to the appropriate CMSIS device file (stm32l4xxxx.h))
\r
208 void HAL_NVIC_EnableIRQ(IRQn_Type IRQn)
\r
210 /* Check the parameters */
\r
211 assert_param(IS_NVIC_DEVICE_IRQ(IRQn));
\r
213 /* Enable interrupt */
\r
214 NVIC_EnableIRQ(IRQn);
\r
218 * @brief Disable a device specific interrupt in the NVIC interrupt controller.
\r
219 * @param IRQn External interrupt number.
\r
220 * This parameter can be an enumerator of IRQn_Type enumeration
\r
221 * (For the complete STM32 Devices IRQ Channels list, please refer to the appropriate CMSIS device file (stm32l4xxxx.h))
\r
224 void HAL_NVIC_DisableIRQ(IRQn_Type IRQn)
\r
226 /* Check the parameters */
\r
227 assert_param(IS_NVIC_DEVICE_IRQ(IRQn));
\r
229 /* Disable interrupt */
\r
230 NVIC_DisableIRQ(IRQn);
\r
234 * @brief Initiate a system reset request to reset the MCU.
\r
237 void HAL_NVIC_SystemReset(void)
\r
240 NVIC_SystemReset();
\r
244 * @brief Initialize the System Timer with interrupt enabled and start the System Tick Timer (SysTick):
\r
245 * Counter is in free running mode to generate periodic interrupts.
\r
246 * @param TicksNumb: Specifies the ticks Number of ticks between two interrupts.
\r
247 * @retval status: - 0 Function succeeded.
\r
248 * - 1 Function failed.
\r
250 uint32_t HAL_SYSTICK_Config(uint32_t TicksNumb)
\r
252 return SysTick_Config(TicksNumb);
\r
258 /** @addtogroup CORTEX_Exported_Functions_Group2
\r
259 * @brief Cortex control functions
\r
262 ==============================================================================
\r
263 ##### Peripheral Control functions #####
\r
264 ==============================================================================
\r
266 This subsection provides a set of functions allowing to control the CORTEX
\r
267 (NVIC, SYSTICK, MPU) functionalities.
\r
275 * @brief Get the priority grouping field from the NVIC Interrupt Controller.
\r
276 * @retval Priority grouping field (SCB->AIRCR [10:8] PRIGROUP field)
\r
278 uint32_t HAL_NVIC_GetPriorityGrouping(void)
\r
280 /* Get the PRIGROUP[10:8] field value */
\r
281 return NVIC_GetPriorityGrouping();
\r
285 * @brief Get the priority of an interrupt.
\r
286 * @param IRQn: External interrupt number.
\r
287 * This parameter can be an enumerator of IRQn_Type enumeration
\r
288 * (For the complete STM32 Devices IRQ Channels list, please refer to the appropriate CMSIS device file (stm32l4xxxx.h))
\r
289 * @param PriorityGroup: the priority grouping bits length.
\r
290 * This parameter can be one of the following values:
\r
291 * @arg NVIC_PRIORITYGROUP_0: 0 bit for pre-emption priority,
\r
292 * 4 bits for subpriority
\r
293 * @arg NVIC_PRIORITYGROUP_1: 1 bit for pre-emption priority,
\r
294 * 3 bits for subpriority
\r
295 * @arg NVIC_PRIORITYGROUP_2: 2 bits for pre-emption priority,
\r
296 * 2 bits for subpriority
\r
297 * @arg NVIC_PRIORITYGROUP_3: 3 bits for pre-emption priority,
\r
298 * 1 bit for subpriority
\r
299 * @arg NVIC_PRIORITYGROUP_4: 4 bits for pre-emption priority,
\r
300 * 0 bit for subpriority
\r
301 * @param pPreemptPriority: Pointer on the Preemptive priority value (starting from 0).
\r
302 * @param pSubPriority: Pointer on the Subpriority value (starting from 0).
\r
305 void HAL_NVIC_GetPriority(IRQn_Type IRQn, uint32_t PriorityGroup, uint32_t *pPreemptPriority, uint32_t *pSubPriority)
\r
307 /* Check the parameters */
\r
308 assert_param(IS_NVIC_PRIORITY_GROUP(PriorityGroup));
\r
309 /* Get priority for Cortex-M system or device specific interrupts */
\r
310 NVIC_DecodePriority(NVIC_GetPriority(IRQn), PriorityGroup, pPreemptPriority, pSubPriority);
\r
314 * @brief Set Pending bit of an external interrupt.
\r
315 * @param IRQn External interrupt number
\r
316 * This parameter can be an enumerator of IRQn_Type enumeration
\r
317 * (For the complete STM32 Devices IRQ Channels list, please refer to the appropriate CMSIS device file (stm32l4xxxx.h))
\r
320 void HAL_NVIC_SetPendingIRQ(IRQn_Type IRQn)
\r
322 /* Check the parameters */
\r
323 assert_param(IS_NVIC_DEVICE_IRQ(IRQn));
\r
325 /* Set interrupt pending */
\r
326 NVIC_SetPendingIRQ(IRQn);
\r
330 * @brief Get Pending Interrupt (read the pending register in the NVIC
\r
331 * and return the pending bit for the specified interrupt).
\r
332 * @param IRQn External interrupt number.
\r
333 * This parameter can be an enumerator of IRQn_Type enumeration
\r
334 * (For the complete STM32 Devices IRQ Channels list, please refer to the appropriate CMSIS device file (stm32l4xxxx.h))
\r
335 * @retval status: - 0 Interrupt status is not pending.
\r
336 * - 1 Interrupt status is pending.
\r
338 uint32_t HAL_NVIC_GetPendingIRQ(IRQn_Type IRQn)
\r
340 /* Check the parameters */
\r
341 assert_param(IS_NVIC_DEVICE_IRQ(IRQn));
\r
343 /* Return 1 if pending else 0 */
\r
344 return NVIC_GetPendingIRQ(IRQn);
\r
348 * @brief Clear the pending bit of an external interrupt.
\r
349 * @param IRQn External interrupt number.
\r
350 * This parameter can be an enumerator of IRQn_Type enumeration
\r
351 * (For the complete STM32 Devices IRQ Channels list, please refer to the appropriate CMSIS device file (stm32l4xxxx.h))
\r
354 void HAL_NVIC_ClearPendingIRQ(IRQn_Type IRQn)
\r
356 /* Check the parameters */
\r
357 assert_param(IS_NVIC_DEVICE_IRQ(IRQn));
\r
359 /* Clear pending interrupt */
\r
360 NVIC_ClearPendingIRQ(IRQn);
\r
364 * @brief Get active interrupt (read the active register in NVIC and return the active bit).
\r
365 * @param IRQn External interrupt number
\r
366 * This parameter can be an enumerator of IRQn_Type enumeration
\r
367 * (For the complete STM32 Devices IRQ Channels list, please refer to the appropriate CMSIS device file (stm32l4xxxx.h))
\r
368 * @retval status: - 0 Interrupt status is not pending.
\r
369 * - 1 Interrupt status is pending.
\r
371 uint32_t HAL_NVIC_GetActive(IRQn_Type IRQn)
\r
373 /* Return 1 if active else 0 */
\r
374 return NVIC_GetActive(IRQn);
\r
378 * @brief Configure the SysTick clock source.
\r
379 * @param CLKSource: specifies the SysTick clock source.
\r
380 * This parameter can be one of the following values:
\r
381 * @arg SYSTICK_CLKSOURCE_HCLK_DIV8: AHB clock divided by 8 selected as SysTick clock source.
\r
382 * @arg SYSTICK_CLKSOURCE_HCLK: AHB clock selected as SysTick clock source.
\r
385 void HAL_SYSTICK_CLKSourceConfig(uint32_t CLKSource)
\r
387 /* Check the parameters */
\r
388 assert_param(IS_SYSTICK_CLK_SOURCE(CLKSource));
\r
389 if (CLKSource == SYSTICK_CLKSOURCE_HCLK)
\r
391 SysTick->CTRL |= SYSTICK_CLKSOURCE_HCLK;
\r
395 SysTick->CTRL &= ~SYSTICK_CLKSOURCE_HCLK;
\r
400 * @brief Handle SYSTICK interrupt request.
\r
403 void HAL_SYSTICK_IRQHandler(void)
\r
405 HAL_SYSTICK_Callback();
\r
409 * @brief SYSTICK callback.
\r
412 __weak void HAL_SYSTICK_Callback(void)
\r
414 /* NOTE : This function should not be modified, when the callback is needed,
\r
415 the HAL_SYSTICK_Callback could be implemented in the user file
\r
419 #if (__MPU_PRESENT == 1)
\r
421 * @brief Disable the MPU.
\r
424 void HAL_MPU_Disable(void)
\r
426 /* Make sure outstanding transfers are done */
\r
429 /* Disable fault exceptions */
\r
430 SCB->SHCSR &= ~SCB_SHCSR_MEMFAULTENA_Msk;
\r
432 /* Disable the MPU and clear the control register*/
\r
437 * @brief Enable the MPU.
\r
438 * @param MPU_Control: Specifies the control mode of the MPU during hard fault,
\r
439 * NMI, FAULTMASK and privileged accessto the default memory
\r
440 * This parameter can be one of the following values:
\r
441 * @arg MPU_HFNMI_PRIVDEF_NONE
\r
442 * @arg MPU_HARDFAULT_NMI
\r
443 * @arg MPU_PRIVILEGED_DEFAULT
\r
444 * @arg MPU_HFNMI_PRIVDEF
\r
447 void HAL_MPU_Enable(uint32_t MPU_Control)
\r
449 /* Enable the MPU */
\r
450 MPU->CTRL = MPU_Control | MPU_CTRL_ENABLE_Msk;
\r
452 /* Enable fault exceptions */
\r
453 SCB->SHCSR |= SCB_SHCSR_MEMFAULTENA_Msk;
\r
455 /* Ensure MPU settings take effects */
\r
461 * @brief Initialize and configure the Region and the memory to be protected.
\r
462 * @param MPU_Init: Pointer to a MPU_Region_InitTypeDef structure that contains
\r
463 * the initialization and configuration information.
\r
466 void HAL_MPU_ConfigRegion(MPU_Region_InitTypeDef *MPU_Init)
\r
468 /* Check the parameters */
\r
469 assert_param(IS_MPU_REGION_NUMBER(MPU_Init->Number));
\r
470 assert_param(IS_MPU_REGION_ENABLE(MPU_Init->Enable));
\r
472 /* Set the Region number */
\r
473 MPU->RNR = MPU_Init->Number;
\r
475 if ((MPU_Init->Enable) != RESET)
\r
477 /* Check the parameters */
\r
478 assert_param(IS_MPU_INSTRUCTION_ACCESS(MPU_Init->DisableExec));
\r
479 assert_param(IS_MPU_REGION_PERMISSION_ATTRIBUTE(MPU_Init->AccessPermission));
\r
480 assert_param(IS_MPU_TEX_LEVEL(MPU_Init->TypeExtField));
\r
481 assert_param(IS_MPU_ACCESS_SHAREABLE(MPU_Init->IsShareable));
\r
482 assert_param(IS_MPU_ACCESS_CACHEABLE(MPU_Init->IsCacheable));
\r
483 assert_param(IS_MPU_ACCESS_BUFFERABLE(MPU_Init->IsBufferable));
\r
484 assert_param(IS_MPU_SUB_REGION_DISABLE(MPU_Init->SubRegionDisable));
\r
485 assert_param(IS_MPU_REGION_SIZE(MPU_Init->Size));
\r
487 MPU->RBAR = MPU_Init->BaseAddress;
\r
488 MPU->RASR = ((uint32_t)MPU_Init->DisableExec << MPU_RASR_XN_Pos) |
\r
489 ((uint32_t)MPU_Init->AccessPermission << MPU_RASR_AP_Pos) |
\r
490 ((uint32_t)MPU_Init->TypeExtField << MPU_RASR_TEX_Pos) |
\r
491 ((uint32_t)MPU_Init->IsShareable << MPU_RASR_S_Pos) |
\r
492 ((uint32_t)MPU_Init->IsCacheable << MPU_RASR_C_Pos) |
\r
493 ((uint32_t)MPU_Init->IsBufferable << MPU_RASR_B_Pos) |
\r
494 ((uint32_t)MPU_Init->SubRegionDisable << MPU_RASR_SRD_Pos) |
\r
495 ((uint32_t)MPU_Init->Size << MPU_RASR_SIZE_Pos) |
\r
496 ((uint32_t)MPU_Init->Enable << MPU_RASR_ENABLE_Pos);
\r
504 #endif /* __MPU_PRESENT */
\r
514 #endif /* HAL_CORTEX_MODULE_ENABLED */
\r
523 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
\r