misc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /**
  2. ******************************************************************************
  3. * @file misc.c
  4. * @author MCD Application Team
  5. * @version V1.5.0
  6. * @date 06-March-2015
  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. * <h2><center>&copy; COPYRIGHT 2015 STMicroelectronics</center></h2>
  58. *
  59. * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  60. * You may not use this file except in compliance with the License.
  61. * You may obtain a copy of the License at:
  62. *
  63. * http://www.st.com/software_license_agreement_liberty_v2
  64. *
  65. * Unless required by applicable law or agreed to in writing, software
  66. * distributed under the License is distributed on an "AS IS" BASIS,
  67. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  68. * See the License for the specific language governing permissions and
  69. * limitations under the License.
  70. *
  71. ******************************************************************************
  72. */
  73. /* Includes ------------------------------------------------------------------*/
  74. #include "misc.h"
  75. /** @addtogroup STM32F4xx_StdPeriph_Driver
  76. * @{
  77. */
  78. /** @defgroup MISC
  79. * @brief MISC driver modules
  80. * @{
  81. */
  82. /* Private typedef -----------------------------------------------------------*/
  83. /* Private define ------------------------------------------------------------*/
  84. #define AIRCR_VECTKEY_MASK ((uint32_t)0x05FA0000)
  85. /* Private macro -------------------------------------------------------------*/
  86. /* Private variables ---------------------------------------------------------*/
  87. /* Private function prototypes -----------------------------------------------*/
  88. /* Private functions ---------------------------------------------------------*/
  89. /** @defgroup MISC_Private_Functions
  90. * @{
  91. */
  92. /**
  93. * @brief Configures the priority grouping: pre-emption priority and subpriority.
  94. * @param NVIC_PriorityGroup: specifies the priority grouping bits length.
  95. * This parameter can be one of the following values:
  96. * @arg NVIC_PriorityGroup_0: 0 bits for pre-emption priority
  97. * 4 bits for subpriority
  98. * @arg NVIC_PriorityGroup_1: 1 bits for pre-emption priority
  99. * 3 bits for subpriority
  100. * @arg NVIC_PriorityGroup_2: 2 bits for pre-emption priority
  101. * 2 bits for subpriority
  102. * @arg NVIC_PriorityGroup_3: 3 bits for pre-emption priority
  103. * 1 bits for subpriority
  104. * @arg NVIC_PriorityGroup_4: 4 bits for pre-emption priority
  105. * 0 bits for subpriority
  106. * @note When the NVIC_PriorityGroup_0 is selected, IRQ pre-emption is no more possible.
  107. * The pending IRQ priority will be managed only by the subpriority.
  108. * @retval None
  109. */
  110. void NVIC_PriorityGroupConfig(uint32_t NVIC_PriorityGroup)
  111. {
  112. /* Check the parameters */
  113. assert_param(IS_NVIC_PRIORITY_GROUP(NVIC_PriorityGroup));
  114. /* Set the PRIGROUP[10:8] bits according to NVIC_PriorityGroup value */
  115. SCB->AIRCR = AIRCR_VECTKEY_MASK | NVIC_PriorityGroup;
  116. }
  117. /**
  118. * @brief Initializes the NVIC peripheral according to the specified
  119. * parameters in the NVIC_InitStruct.
  120. * @note To configure interrupts priority correctly, the NVIC_PriorityGroupConfig()
  121. * function should be called before.
  122. * @param NVIC_InitStruct: pointer to a NVIC_InitTypeDef structure that contains
  123. * the configuration information for the specified NVIC peripheral.
  124. * @retval None
  125. */
  126. void NVIC_Init(NVIC_InitTypeDef* NVIC_InitStruct)
  127. {
  128. uint8_t tmppriority = 0x00, tmppre = 0x00, tmpsub = 0x0F;
  129. /* Check the parameters */
  130. assert_param(IS_FUNCTIONAL_STATE(NVIC_InitStruct->NVIC_IRQChannelCmd));
  131. assert_param(IS_NVIC_PREEMPTION_PRIORITY(NVIC_InitStruct->NVIC_IRQChannelPreemptionPriority));
  132. assert_param(IS_NVIC_SUB_PRIORITY(NVIC_InitStruct->NVIC_IRQChannelSubPriority));
  133. if (NVIC_InitStruct->NVIC_IRQChannelCmd != DISABLE)
  134. {
  135. /* Compute the Corresponding IRQ Priority --------------------------------*/
  136. tmppriority = (0x700 - ((SCB->AIRCR) & (uint32_t)0x700))>> 0x08;
  137. tmppre = (0x4 - tmppriority);
  138. tmpsub = tmpsub >> tmppriority;
  139. tmppriority = NVIC_InitStruct->NVIC_IRQChannelPreemptionPriority << tmppre;
  140. tmppriority |= (uint8_t)(NVIC_InitStruct->NVIC_IRQChannelSubPriority & tmpsub);
  141. tmppriority = tmppriority << 0x04;
  142. NVIC->IP[NVIC_InitStruct->NVIC_IRQChannel] = tmppriority;
  143. /* Enable the Selected IRQ Channels --------------------------------------*/
  144. NVIC->ISER[NVIC_InitStruct->NVIC_IRQChannel >> 0x05] =
  145. (uint32_t)0x01 << (NVIC_InitStruct->NVIC_IRQChannel & (uint8_t)0x1F);
  146. }
  147. else
  148. {
  149. /* Disable the Selected IRQ Channels -------------------------------------*/
  150. NVIC->ICER[NVIC_InitStruct->NVIC_IRQChannel >> 0x05] =
  151. (uint32_t)0x01 << (NVIC_InitStruct->NVIC_IRQChannel & (uint8_t)0x1F);
  152. }
  153. }
  154. /**
  155. * @brief Sets the vector table location and Offset.
  156. * @param NVIC_VectTab: specifies if the vector table is in RAM or FLASH memory.
  157. * This parameter can be one of the following values:
  158. * @arg NVIC_VectTab_RAM: Vector Table in internal SRAM.
  159. * @arg NVIC_VectTab_FLASH: Vector Table in internal FLASH.
  160. * @param Offset: Vector Table base offset field. This value must be a multiple of 0x200.
  161. * @retval None
  162. */
  163. void NVIC_SetVectorTable(uint32_t NVIC_VectTab, uint32_t Offset)
  164. {
  165. /* Check the parameters */
  166. assert_param(IS_NVIC_VECTTAB(NVIC_VectTab));
  167. assert_param(IS_NVIC_OFFSET(Offset));
  168. SCB->VTOR = NVIC_VectTab | (Offset & (uint32_t)0x1FFFFF80);
  169. }
  170. /**
  171. * @brief Selects the condition for the system to enter low power mode.
  172. * @param LowPowerMode: Specifies the new mode for the system to enter low power mode.
  173. * This parameter can be one of the following values:
  174. * @arg NVIC_LP_SEVONPEND: Low Power SEV on Pend.
  175. * @arg NVIC_LP_SLEEPDEEP: Low Power DEEPSLEEP request.
  176. * @arg NVIC_LP_SLEEPONEXIT: Low Power Sleep on Exit.
  177. * @param NewState: new state of LP condition. This parameter can be: ENABLE or DISABLE.
  178. * @retval None
  179. */
  180. void NVIC_SystemLPConfig(uint8_t LowPowerMode, FunctionalState NewState)
  181. {
  182. /* Check the parameters */
  183. assert_param(IS_NVIC_LP(LowPowerMode));
  184. assert_param(IS_FUNCTIONAL_STATE(NewState));
  185. if (NewState != DISABLE)
  186. {
  187. SCB->SCR |= LowPowerMode;
  188. }
  189. else
  190. {
  191. SCB->SCR &= (uint32_t)(~(uint32_t)LowPowerMode);
  192. }
  193. }
  194. /**
  195. * @brief Configures the SysTick clock source.
  196. * @param SysTick_CLKSource: specifies the SysTick clock source.
  197. * This parameter can be one of the following values:
  198. * @arg SysTick_CLKSource_HCLK_Div8: AHB clock divided by 8 selected as SysTick clock source.
  199. * @arg SysTick_CLKSource_HCLK: AHB clock selected as SysTick clock source.
  200. * @retval None
  201. */
  202. void SysTick_CLKSourceConfig(uint32_t SysTick_CLKSource)
  203. {
  204. /* Check the parameters */
  205. assert_param(IS_SYSTICK_CLK_SOURCE(SysTick_CLKSource));
  206. if (SysTick_CLKSource == SysTick_CLKSource_HCLK)
  207. {
  208. SysTick->CTRL |= SysTick_CLKSource_HCLK;
  209. }
  210. else
  211. {
  212. SysTick->CTRL &= SysTick_CLKSource_HCLK_Div8;
  213. }
  214. }
  215. /**
  216. * @}
  217. */
  218. /**
  219. * @}
  220. */
  221. /**
  222. * @}
  223. */
  224. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/