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