stm32f4xx_hal_cortex.c 19 KB

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