stm32h7xx_hal_cortex.c 19 KB

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