You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

stm32l1xx_hal_cortex.c 20KB

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