misc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /**
  2. ******************************************************************************
  3. * @file misc.c
  4. * @author MCD Application Team
  5. * @version V1.0.0
  6. * @date 30-September-2011
  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. * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  58. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  59. * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  60. * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  61. * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  62. * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  63. *
  64. * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
  65. ******************************************************************************
  66. */
  67. /* Includes ------------------------------------------------------------------*/
  68. #include "misc.h"
  69. /** @addtogroup STM32F4xx_StdPeriph_Driver
  70. * @{
  71. */
  72. /** @defgroup MISC
  73. * @brief MISC driver modules
  74. * @{
  75. */
  76. /* Private typedef -----------------------------------------------------------*/
  77. /* Private define ------------------------------------------------------------*/
  78. #define AIRCR_VECTKEY_MASK ((uint32_t)0x05FA0000)
  79. /* Private macro -------------------------------------------------------------*/
  80. /* Private variables ---------------------------------------------------------*/
  81. /* Private function prototypes -----------------------------------------------*/
  82. /* Private functions ---------------------------------------------------------*/
  83. /** @defgroup MISC_Private_Functions
  84. * @{
  85. */
  86. /**
  87. * @brief Configures the priority grouping: pre-emption priority and subpriority.
  88. * @param NVIC_PriorityGroup: specifies the priority grouping bits length.
  89. * This parameter can be one of the following values:
  90. * @arg NVIC_PriorityGroup_0: 0 bits for pre-emption priority
  91. * 4 bits for subpriority
  92. * @arg NVIC_PriorityGroup_1: 1 bits for pre-emption priority
  93. * 3 bits for subpriority
  94. * @arg NVIC_PriorityGroup_2: 2 bits for pre-emption priority
  95. * 2 bits for subpriority
  96. * @arg NVIC_PriorityGroup_3: 3 bits for pre-emption priority
  97. * 1 bits for subpriority
  98. * @arg NVIC_PriorityGroup_4: 4 bits for pre-emption priority
  99. * 0 bits for subpriority
  100. * @note When the NVIC_PriorityGroup_0 is selected, IRQ pre-emption is no more possible.
  101. * The pending IRQ priority will be managed only by the subpriority.
  102. * @retval None
  103. */
  104. void NVIC_PriorityGroupConfig(uint32_t NVIC_PriorityGroup)
  105. {
  106. /* Check the parameters */
  107. assert_param(IS_NVIC_PRIORITY_GROUP(NVIC_PriorityGroup));
  108. /* Set the PRIGROUP[10:8] bits according to NVIC_PriorityGroup value */
  109. SCB->AIRCR = AIRCR_VECTKEY_MASK | NVIC_PriorityGroup;
  110. }
  111. /**
  112. * @brief Initializes the NVIC peripheral according to the specified
  113. * parameters in the NVIC_InitStruct.
  114. * @note To configure interrupts priority correctly, the NVIC_PriorityGroupConfig()
  115. * function should be called before.
  116. * @param NVIC_InitStruct: pointer to a NVIC_InitTypeDef structure that contains
  117. * the configuration information for the specified NVIC peripheral.
  118. * @retval None
  119. */
  120. void NVIC_Init(NVIC_InitTypeDef* NVIC_InitStruct)
  121. {
  122. uint8_t tmppriority = 0x00, tmppre = 0x00, tmpsub = 0x0F;
  123. /* Check the parameters */
  124. assert_param(IS_FUNCTIONAL_STATE(NVIC_InitStruct->NVIC_IRQChannelCmd));
  125. assert_param(IS_NVIC_PREEMPTION_PRIORITY(NVIC_InitStruct->NVIC_IRQChannelPreemptionPriority));
  126. assert_param(IS_NVIC_SUB_PRIORITY(NVIC_InitStruct->NVIC_IRQChannelSubPriority));
  127. if (NVIC_InitStruct->NVIC_IRQChannelCmd != DISABLE)
  128. {
  129. /* Compute the Corresponding IRQ Priority --------------------------------*/
  130. tmppriority = (0x700 - ((SCB->AIRCR) & (uint32_t)0x700))>> 0x08;
  131. tmppre = (0x4 - tmppriority);
  132. tmpsub = tmpsub >> tmppriority;
  133. tmppriority = NVIC_InitStruct->NVIC_IRQChannelPreemptionPriority << tmppre;
  134. tmppriority |= (uint8_t)(NVIC_InitStruct->NVIC_IRQChannelSubPriority & tmpsub);
  135. tmppriority = tmppriority << 0x04;
  136. NVIC->IP[NVIC_InitStruct->NVIC_IRQChannel] = tmppriority;
  137. /* Enable the Selected IRQ Channels --------------------------------------*/
  138. NVIC->ISER[NVIC_InitStruct->NVIC_IRQChannel >> 0x05] =
  139. (uint32_t)0x01 << (NVIC_InitStruct->NVIC_IRQChannel & (uint8_t)0x1F);
  140. }
  141. else
  142. {
  143. /* Disable the Selected IRQ Channels -------------------------------------*/
  144. NVIC->ICER[NVIC_InitStruct->NVIC_IRQChannel >> 0x05] =
  145. (uint32_t)0x01 << (NVIC_InitStruct->NVIC_IRQChannel & (uint8_t)0x1F);
  146. }
  147. }
  148. /**
  149. * @brief Sets the vector table location and Offset.
  150. * @param NVIC_VectTab: specifies if the vector table is in RAM or FLASH memory.
  151. * This parameter can be one of the following values:
  152. * @arg NVIC_VectTab_RAM: Vector Table in internal SRAM.
  153. * @arg NVIC_VectTab_FLASH: Vector Table in internal FLASH.
  154. * @param Offset: Vector Table base offset field. This value must be a multiple of 0x200.
  155. * @retval None
  156. */
  157. void NVIC_SetVectorTable(uint32_t NVIC_VectTab, uint32_t Offset)
  158. {
  159. /* Check the parameters */
  160. assert_param(IS_NVIC_VECTTAB(NVIC_VectTab));
  161. assert_param(IS_NVIC_OFFSET(Offset));
  162. SCB->VTOR = NVIC_VectTab | (Offset & (uint32_t)0x1FFFFF80);
  163. }
  164. /**
  165. * @brief Selects the condition for the system to enter low power mode.
  166. * @param LowPowerMode: Specifies the new mode for the system to enter low power mode.
  167. * This parameter can be one of the following values:
  168. * @arg NVIC_LP_SEVONPEND: Low Power SEV on Pend.
  169. * @arg NVIC_LP_SLEEPDEEP: Low Power DEEPSLEEP request.
  170. * @arg NVIC_LP_SLEEPONEXIT: Low Power Sleep on Exit.
  171. * @param NewState: new state of LP condition. This parameter can be: ENABLE or DISABLE.
  172. * @retval None
  173. */
  174. void NVIC_SystemLPConfig(uint8_t LowPowerMode, FunctionalState NewState)
  175. {
  176. /* Check the parameters */
  177. assert_param(IS_NVIC_LP(LowPowerMode));
  178. assert_param(IS_FUNCTIONAL_STATE(NewState));
  179. if (NewState != DISABLE)
  180. {
  181. SCB->SCR |= LowPowerMode;
  182. }
  183. else
  184. {
  185. SCB->SCR &= (uint32_t)(~(uint32_t)LowPowerMode);
  186. }
  187. }
  188. /**
  189. * @brief Configures the SysTick clock source.
  190. * @param SysTick_CLKSource: specifies the SysTick clock source.
  191. * This parameter can be one of the following values:
  192. * @arg SysTick_CLKSource_HCLK_Div8: AHB clock divided by 8 selected as SysTick clock source.
  193. * @arg SysTick_CLKSource_HCLK: AHB clock selected as SysTick clock source.
  194. * @retval None
  195. */
  196. void SysTick_CLKSourceConfig(uint32_t SysTick_CLKSource)
  197. {
  198. /* Check the parameters */
  199. assert_param(IS_SYSTICK_CLK_SOURCE(SysTick_CLKSource));
  200. if (SysTick_CLKSource == SysTick_CLKSource_HCLK)
  201. {
  202. SysTick->CTRL |= SysTick_CLKSource_HCLK;
  203. }
  204. else
  205. {
  206. SysTick->CTRL &= SysTick_CLKSource_HCLK_Div8;
  207. }
  208. }
  209. /**
  210. * @}
  211. */
  212. /**
  213. * @}
  214. */
  215. /**
  216. * @}
  217. */
  218. /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/