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