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