stm32wbxx_hal_cortex.c 17 KB

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