misc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /**
  2. ******************************************************************************
  3. * @file misc.c
  4. * @author MCD Application Team
  5. * @version V1.8.1
  6. * @date 27-January-2022
  7. * @brief This file provides all the miscellaneous firmware functions (add-on
  8. * to CMSIS functions).
  9. *
  10. * @verbatim
  11. *
  12. * ===================================================================
  13. * How to configure Interrupts using driver
  14. * ===================================================================
  15. *
  16. * This section provide functions allowing to configure the NVIC interrupts (IRQ).
  17. * The Cortex-M4 exceptions are managed by CMSIS functions.
  18. *
  19. * 1. Configure the NVIC Priority Grouping using NVIC_PriorityGroupConfig()
  20. * function according to the following table.
  21. * The table below gives the allowed values of the pre-emption priority and subpriority according
  22. * to the Priority Grouping configuration performed by NVIC_PriorityGroupConfig function
  23. * ==========================================================================================================================
  24. * NVIC_PriorityGroup | NVIC_IRQChannelPreemptionPriority | NVIC_IRQChannelSubPriority | Description
  25. * ==========================================================================================================================
  26. * NVIC_PriorityGroup_0 | 0 | 0-15 | 0 bits for pre-emption priority
  27. * | | | 4 bits for subpriority
  28. * --------------------------------------------------------------------------------------------------------------------------
  29. * NVIC_PriorityGroup_1 | 0-1 | 0-7 | 1 bits for pre-emption priority
  30. * | | | 3 bits for subpriority
  31. * --------------------------------------------------------------------------------------------------------------------------
  32. * NVIC_PriorityGroup_2 | 0-3 | 0-3 | 2 bits for pre-emption priority
  33. * | | | 2 bits for subpriority
  34. * --------------------------------------------------------------------------------------------------------------------------
  35. * NVIC_PriorityGroup_3 | 0-7 | 0-1 | 3 bits for pre-emption priority
  36. * | | | 1 bits for subpriority
  37. * --------------------------------------------------------------------------------------------------------------------------
  38. * NVIC_PriorityGroup_4 | 0-15 | 0 | 4 bits for pre-emption priority
  39. * | | | 0 bits for subpriority
  40. * ==========================================================================================================================
  41. *
  42. * 2. Enable and Configure the priority of the selected IRQ Channels using NVIC_Init()
  43. *
  44. * @note When the NVIC_PriorityGroup_0 is selected, IRQ pre-emption is no more possible.
  45. * The pending IRQ priority will be managed only by the subpriority.
  46. *
  47. * @note IRQ priority order (sorted by highest to lowest priority):
  48. * - Lowest pre-emption priority
  49. * - Lowest subpriority
  50. * - Lowest hardware priority (IRQ number)
  51. *
  52. * @endverbatim
  53. *
  54. ******************************************************************************
  55. * @attention
  56. *
  57. * Copyright (c) 2016 STMicroelectronics.
  58. * All rights reserved.
  59. *
  60. * This software is licensed under terms that can be found in the LICENSE file
  61. * in the root directory of this software component.
  62. * If no LICENSE file comes with this software, it is provided AS-IS.
  63. *
  64. ******************************************************************************
  65. */
  66. /* Includes ------------------------------------------------------------------*/
  67. #include "misc.h"
  68. /** @addtogroup STM32F4xx_StdPeriph_Driver
  69. * @{
  70. */
  71. /** @defgroup MISC
  72. * @brief MISC driver modules
  73. * @{
  74. */
  75. /* Private typedef -----------------------------------------------------------*/
  76. /* Private define ------------------------------------------------------------*/
  77. #define AIRCR_VECTKEY_MASK ((uint32_t)0x05FA0000)
  78. /* Private macro -------------------------------------------------------------*/
  79. /* Private variables ---------------------------------------------------------*/
  80. /* Private function prototypes -----------------------------------------------*/
  81. /* Private functions ---------------------------------------------------------*/
  82. /** @defgroup MISC_Private_Functions
  83. * @{
  84. */
  85. /**
  86. * @brief Configures the priority grouping: pre-emption priority and subpriority.
  87. * @param NVIC_PriorityGroup: specifies the priority grouping bits length.
  88. * This parameter can be one of the following values:
  89. * @arg NVIC_PriorityGroup_0: 0 bits for pre-emption priority
  90. * 4 bits for subpriority
  91. * @arg NVIC_PriorityGroup_1: 1 bits for pre-emption priority
  92. * 3 bits for subpriority
  93. * @arg NVIC_PriorityGroup_2: 2 bits for pre-emption priority
  94. * 2 bits for subpriority
  95. * @arg NVIC_PriorityGroup_3: 3 bits for pre-emption priority
  96. * 1 bits for subpriority
  97. * @arg NVIC_PriorityGroup_4: 4 bits for pre-emption priority
  98. * 0 bits for subpriority
  99. * @note When the NVIC_PriorityGroup_0 is selected, IRQ pre-emption is no more possible.
  100. * The pending IRQ priority will be managed only by the subpriority.
  101. * @retval None
  102. */
  103. void NVIC_PriorityGroupConfig(uint32_t NVIC_PriorityGroup)
  104. {
  105. /* Check the parameters */
  106. assert_param(IS_NVIC_PRIORITY_GROUP(NVIC_PriorityGroup));
  107. /* Set the PRIGROUP[10:8] bits according to NVIC_PriorityGroup value */
  108. SCB->AIRCR = AIRCR_VECTKEY_MASK | NVIC_PriorityGroup;
  109. }
  110. /**
  111. * @brief Initializes the NVIC peripheral according to the specified
  112. * parameters in the NVIC_InitStruct.
  113. * @note To configure interrupts priority correctly, the NVIC_PriorityGroupConfig()
  114. * function should be called before.
  115. * @param NVIC_InitStruct: pointer to a NVIC_InitTypeDef structure that contains
  116. * the configuration information for the specified NVIC peripheral.
  117. * @retval None
  118. */
  119. void NVIC_Init(NVIC_InitTypeDef* NVIC_InitStruct)
  120. {
  121. uint8_t tmppriority = 0x00, tmppre = 0x00, tmpsub = 0x0F;
  122. /* Check the parameters */
  123. assert_param(IS_FUNCTIONAL_STATE(NVIC_InitStruct->NVIC_IRQChannelCmd));
  124. assert_param(IS_NVIC_PREEMPTION_PRIORITY(NVIC_InitStruct->NVIC_IRQChannelPreemptionPriority));
  125. assert_param(IS_NVIC_SUB_PRIORITY(NVIC_InitStruct->NVIC_IRQChannelSubPriority));
  126. if (NVIC_InitStruct->NVIC_IRQChannelCmd != DISABLE)
  127. {
  128. /* Compute the Corresponding IRQ Priority --------------------------------*/
  129. tmppriority = (0x700 - ((SCB->AIRCR) & (uint32_t)0x700))>> 0x08;
  130. tmppre = (0x4 - tmppriority);
  131. tmpsub = tmpsub >> tmppriority;
  132. tmppriority = NVIC_InitStruct->NVIC_IRQChannelPreemptionPriority << tmppre;
  133. tmppriority |= (uint8_t)(NVIC_InitStruct->NVIC_IRQChannelSubPriority & tmpsub);
  134. tmppriority = tmppriority << 0x04;
  135. NVIC->IP[NVIC_InitStruct->NVIC_IRQChannel] = tmppriority;
  136. /* Enable the Selected IRQ Channels --------------------------------------*/
  137. NVIC->ISER[NVIC_InitStruct->NVIC_IRQChannel >> 0x05] =
  138. (uint32_t)0x01 << (NVIC_InitStruct->NVIC_IRQChannel & (uint8_t)0x1F);
  139. }
  140. else
  141. {
  142. /* Disable the Selected IRQ Channels -------------------------------------*/
  143. NVIC->ICER[NVIC_InitStruct->NVIC_IRQChannel >> 0x05] =
  144. (uint32_t)0x01 << (NVIC_InitStruct->NVIC_IRQChannel & (uint8_t)0x1F);
  145. }
  146. }
  147. /**
  148. * @brief Sets the vector table location and Offset.
  149. * @param NVIC_VectTab: specifies if the vector table is in RAM or FLASH memory.
  150. * This parameter can be one of the following values:
  151. * @arg NVIC_VectTab_RAM: Vector Table in internal SRAM.
  152. * @arg NVIC_VectTab_FLASH: Vector Table in internal FLASH.
  153. * @param Offset: Vector Table base offset field. This value must be a multiple of 0x200.
  154. * @retval None
  155. */
  156. void NVIC_SetVectorTable(uint32_t NVIC_VectTab, uint32_t Offset)
  157. {
  158. /* Check the parameters */
  159. assert_param(IS_NVIC_VECTTAB(NVIC_VectTab));
  160. assert_param(IS_NVIC_OFFSET(Offset));
  161. SCB->VTOR = NVIC_VectTab | (Offset & (uint32_t)0x1FFFFF80);
  162. }
  163. /**
  164. * @brief Selects the condition for the system to enter low power mode.
  165. * @param LowPowerMode: Specifies the new mode for the system to enter low power mode.
  166. * This parameter can be one of the following values:
  167. * @arg NVIC_LP_SEVONPEND: Low Power SEV on Pend.
  168. * @arg NVIC_LP_SLEEPDEEP: Low Power DEEPSLEEP request.
  169. * @arg NVIC_LP_SLEEPONEXIT: Low Power Sleep on Exit.
  170. * @param NewState: new state of LP condition. This parameter can be: ENABLE or DISABLE.
  171. * @retval None
  172. */
  173. void NVIC_SystemLPConfig(uint8_t LowPowerMode, FunctionalState NewState)
  174. {
  175. /* Check the parameters */
  176. assert_param(IS_NVIC_LP(LowPowerMode));
  177. assert_param(IS_FUNCTIONAL_STATE(NewState));
  178. if (NewState != DISABLE)
  179. {
  180. SCB->SCR |= LowPowerMode;
  181. }
  182. else
  183. {
  184. SCB->SCR &= (uint32_t)(~(uint32_t)LowPowerMode);
  185. }
  186. }
  187. /**
  188. * @brief Configures the SysTick clock source.
  189. * @param SysTick_CLKSource: specifies the SysTick clock source.
  190. * This parameter can be one of the following values:
  191. * @arg SysTick_CLKSource_HCLK_Div8: AHB clock divided by 8 selected as SysTick clock source.
  192. * @arg SysTick_CLKSource_HCLK: AHB clock selected as SysTick clock source.
  193. * @retval None
  194. */
  195. void SysTick_CLKSourceConfig(uint32_t SysTick_CLKSource)
  196. {
  197. /* Check the parameters */
  198. assert_param(IS_SYSTICK_CLK_SOURCE(SysTick_CLKSource));
  199. if (SysTick_CLKSource == SysTick_CLKSource_HCLK)
  200. {
  201. SysTick->CTRL |= SysTick_CLKSource_HCLK;
  202. }
  203. else
  204. {
  205. SysTick->CTRL &= SysTick_CLKSource_HCLK_Div8;
  206. }
  207. }
  208. /**
  209. * @}
  210. */
  211. /**
  212. * @}
  213. */
  214. /**
  215. * @}
  216. */