]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M4F_STM32F407ZG-SK/Libraries/STM32F4xx_StdPeriph_Driver/src/misc.c
Add FreeRTOS-Plus directory.
[freertos] / FreeRTOS / Demo / CORTEX_M4F_STM32F407ZG-SK / Libraries / STM32F4xx_StdPeriph_Driver / src / misc.c
1 /**\r
2   ******************************************************************************\r
3   * @file    misc.c\r
4   * @author  MCD Application Team\r
5   * @version V1.0.0\r
6   * @date    30-September-2011\r
7   * @brief   This file provides all the miscellaneous firmware functions (add-on\r
8   *          to CMSIS functions).\r
9   *          \r
10   *  @verbatim   \r
11   *                               \r
12   *          ===================================================================      \r
13   *                        How to configure Interrupts using driver \r
14   *          ===================================================================      \r
15   * \r
16   *            This section provide functions allowing to configure the NVIC interrupts (IRQ).\r
17   *            The Cortex-M4 exceptions are managed by CMSIS functions.\r
18   *\r
19   *            1. Configure the NVIC Priority Grouping using NVIC_PriorityGroupConfig()\r
20   *                function according to the following table.\r
21  \r
22   *  The table below gives the allowed values of the pre-emption priority and subpriority according\r
23   *  to the Priority Grouping configuration performed by NVIC_PriorityGroupConfig function\r
24   *    ==========================================================================================================================\r
25   *      NVIC_PriorityGroup   | NVIC_IRQChannelPreemptionPriority | NVIC_IRQChannelSubPriority  |       Description\r
26   *    ==========================================================================================================================\r
27   *     NVIC_PriorityGroup_0  |                0                  |            0-15             | 0 bits for pre-emption priority\r
28   *                           |                                   |                             | 4 bits for subpriority\r
29   *    --------------------------------------------------------------------------------------------------------------------------\r
30   *     NVIC_PriorityGroup_1  |                0-1                |            0-7              | 1 bits for pre-emption priority\r
31   *                           |                                   |                             | 3 bits for subpriority\r
32   *    --------------------------------------------------------------------------------------------------------------------------    \r
33   *     NVIC_PriorityGroup_2  |                0-3                |            0-3              | 2 bits for pre-emption priority\r
34   *                           |                                   |                             | 2 bits for subpriority\r
35   *    --------------------------------------------------------------------------------------------------------------------------    \r
36   *     NVIC_PriorityGroup_3  |                0-7                |            0-1              | 3 bits for pre-emption priority\r
37   *                           |                                   |                             | 1 bits for subpriority\r
38   *    --------------------------------------------------------------------------------------------------------------------------    \r
39   *     NVIC_PriorityGroup_4  |                0-15               |            0                | 4 bits for pre-emption priority\r
40   *                           |                                   |                             | 0 bits for subpriority                       \r
41   *    ==========================================================================================================================     \r
42   *\r
43   *            2. Enable and Configure the priority of the selected IRQ Channels using NVIC_Init()  \r
44   *\r
45   * @note  When the NVIC_PriorityGroup_0 is selected, IRQ pre-emption is no more possible. \r
46   *        The pending IRQ priority will be managed only by the subpriority.\r
47   *\r
48   * @note  IRQ priority order (sorted by highest to lowest priority):\r
49   *         - Lowest pre-emption priority\r
50   *         - Lowest subpriority\r
51   *         - Lowest hardware priority (IRQ number)\r
52   *\r
53   *  @endverbatim\r
54   *\r
55   ******************************************************************************\r
56   * @attention\r
57   *\r
58   * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS\r
59   * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE\r
60   * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY\r
61   * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING\r
62   * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE\r
63   * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.\r
64   *\r
65   * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>\r
66   ******************************************************************************\r
67   */\r
68 \r
69 /* Includes ------------------------------------------------------------------*/\r
70 #include "misc.h"\r
71 \r
72 /** @addtogroup STM32F4xx_StdPeriph_Driver\r
73   * @{\r
74   */\r
75 \r
76 /** @defgroup MISC \r
77   * @brief MISC driver modules\r
78   * @{\r
79   */\r
80 \r
81 /* Private typedef -----------------------------------------------------------*/\r
82 /* Private define ------------------------------------------------------------*/\r
83 #define AIRCR_VECTKEY_MASK    ((uint32_t)0x05FA0000)\r
84 \r
85 /* Private macro -------------------------------------------------------------*/\r
86 /* Private variables ---------------------------------------------------------*/\r
87 /* Private function prototypes -----------------------------------------------*/\r
88 /* Private functions ---------------------------------------------------------*/\r
89 \r
90 /** @defgroup MISC_Private_Functions\r
91   * @{\r
92   */\r
93 \r
94 /**\r
95   * @brief  Configures the priority grouping: pre-emption priority and subpriority.\r
96   * @param  NVIC_PriorityGroup: specifies the priority grouping bits length. \r
97   *   This parameter can be one of the following values:\r
98   *     @arg NVIC_PriorityGroup_0: 0 bits for pre-emption priority\r
99   *                                4 bits for subpriority\r
100   *     @arg NVIC_PriorityGroup_1: 1 bits for pre-emption priority\r
101   *                                3 bits for subpriority\r
102   *     @arg NVIC_PriorityGroup_2: 2 bits for pre-emption priority\r
103   *                                2 bits for subpriority\r
104   *     @arg NVIC_PriorityGroup_3: 3 bits for pre-emption priority\r
105   *                                1 bits for subpriority\r
106   *     @arg NVIC_PriorityGroup_4: 4 bits for pre-emption priority\r
107   *                                0 bits for subpriority\r
108   * @note   When the NVIC_PriorityGroup_0 is selected, IRQ pre-emption is no more possible. \r
109   *         The pending IRQ priority will be managed only by the subpriority. \r
110   * @retval None\r
111   */\r
112 void NVIC_PriorityGroupConfig(uint32_t NVIC_PriorityGroup)\r
113 {\r
114   /* Check the parameters */\r
115   assert_param(IS_NVIC_PRIORITY_GROUP(NVIC_PriorityGroup));\r
116   \r
117   /* Set the PRIGROUP[10:8] bits according to NVIC_PriorityGroup value */\r
118   SCB->AIRCR = AIRCR_VECTKEY_MASK | NVIC_PriorityGroup;\r
119 }\r
120 \r
121 /**\r
122   * @brief  Initializes the NVIC peripheral according to the specified\r
123   *         parameters in the NVIC_InitStruct.\r
124   * @note   To configure interrupts priority correctly, the NVIC_PriorityGroupConfig()\r
125   *         function should be called before. \r
126   * @param  NVIC_InitStruct: pointer to a NVIC_InitTypeDef structure that contains\r
127   *         the configuration information for the specified NVIC peripheral.\r
128   * @retval None\r
129   */\r
130 void NVIC_Init(NVIC_InitTypeDef* NVIC_InitStruct)\r
131 {\r
132   uint8_t tmppriority = 0x00, tmppre = 0x00, tmpsub = 0x0F;\r
133   \r
134   /* Check the parameters */\r
135   assert_param(IS_FUNCTIONAL_STATE(NVIC_InitStruct->NVIC_IRQChannelCmd));\r
136   assert_param(IS_NVIC_PREEMPTION_PRIORITY(NVIC_InitStruct->NVIC_IRQChannelPreemptionPriority));  \r
137   assert_param(IS_NVIC_SUB_PRIORITY(NVIC_InitStruct->NVIC_IRQChannelSubPriority));\r
138     \r
139   if (NVIC_InitStruct->NVIC_IRQChannelCmd != DISABLE)\r
140   {\r
141     /* Compute the Corresponding IRQ Priority --------------------------------*/    \r
142     tmppriority = (0x700 - ((SCB->AIRCR) & (uint32_t)0x700))>> 0x08;\r
143     tmppre = (0x4 - tmppriority);\r
144     tmpsub = tmpsub >> tmppriority;\r
145 \r
146     tmppriority = NVIC_InitStruct->NVIC_IRQChannelPreemptionPriority << tmppre;\r
147     tmppriority |=  (uint8_t)(NVIC_InitStruct->NVIC_IRQChannelSubPriority & tmpsub);\r
148         \r
149     tmppriority = tmppriority << 0x04;\r
150         \r
151     NVIC->IP[NVIC_InitStruct->NVIC_IRQChannel] = tmppriority;\r
152     \r
153     /* Enable the Selected IRQ Channels --------------------------------------*/\r
154     NVIC->ISER[NVIC_InitStruct->NVIC_IRQChannel >> 0x05] =\r
155       (uint32_t)0x01 << (NVIC_InitStruct->NVIC_IRQChannel & (uint8_t)0x1F);\r
156   }\r
157   else\r
158   {\r
159     /* Disable the Selected IRQ Channels -------------------------------------*/\r
160     NVIC->ICER[NVIC_InitStruct->NVIC_IRQChannel >> 0x05] =\r
161       (uint32_t)0x01 << (NVIC_InitStruct->NVIC_IRQChannel & (uint8_t)0x1F);\r
162   }\r
163 }\r
164 \r
165 /**\r
166   * @brief  Sets the vector table location and Offset.\r
167   * @param  NVIC_VectTab: specifies if the vector table is in RAM or FLASH memory.\r
168   *   This parameter can be one of the following values:\r
169   *     @arg NVIC_VectTab_RAM: Vector Table in internal SRAM.\r
170   *     @arg NVIC_VectTab_FLASH: Vector Table in internal FLASH.\r
171   * @param  Offset: Vector Table base offset field. This value must be a multiple of 0x200.\r
172   * @retval None\r
173   */\r
174 void NVIC_SetVectorTable(uint32_t NVIC_VectTab, uint32_t Offset)\r
175\r
176   /* Check the parameters */\r
177   assert_param(IS_NVIC_VECTTAB(NVIC_VectTab));\r
178   assert_param(IS_NVIC_OFFSET(Offset));  \r
179    \r
180   SCB->VTOR = NVIC_VectTab | (Offset & (uint32_t)0x1FFFFF80);\r
181 }\r
182 \r
183 /**\r
184   * @brief  Selects the condition for the system to enter low power mode.\r
185   * @param  LowPowerMode: Specifies the new mode for the system to enter low power mode.\r
186   *   This parameter can be one of the following values:\r
187   *     @arg NVIC_LP_SEVONPEND: Low Power SEV on Pend.\r
188   *     @arg NVIC_LP_SLEEPDEEP: Low Power DEEPSLEEP request.\r
189   *     @arg NVIC_LP_SLEEPONEXIT: Low Power Sleep on Exit.\r
190   * @param  NewState: new state of LP condition. This parameter can be: ENABLE or DISABLE.\r
191   * @retval None\r
192   */\r
193 void NVIC_SystemLPConfig(uint8_t LowPowerMode, FunctionalState NewState)\r
194 {\r
195   /* Check the parameters */\r
196   assert_param(IS_NVIC_LP(LowPowerMode));\r
197   assert_param(IS_FUNCTIONAL_STATE(NewState));  \r
198   \r
199   if (NewState != DISABLE)\r
200   {\r
201     SCB->SCR |= LowPowerMode;\r
202   }\r
203   else\r
204   {\r
205     SCB->SCR &= (uint32_t)(~(uint32_t)LowPowerMode);\r
206   }\r
207 }\r
208 \r
209 /**\r
210   * @brief  Configures the SysTick clock source.\r
211   * @param  SysTick_CLKSource: specifies the SysTick clock source.\r
212   *   This parameter can be one of the following values:\r
213   *     @arg SysTick_CLKSource_HCLK_Div8: AHB clock divided by 8 selected as SysTick clock source.\r
214   *     @arg SysTick_CLKSource_HCLK: AHB clock selected as SysTick clock source.\r
215   * @retval None\r
216   */\r
217 void SysTick_CLKSourceConfig(uint32_t SysTick_CLKSource)\r
218 {\r
219   /* Check the parameters */\r
220   assert_param(IS_SYSTICK_CLK_SOURCE(SysTick_CLKSource));\r
221   if (SysTick_CLKSource == SysTick_CLKSource_HCLK)\r
222   {\r
223     SysTick->CTRL |= SysTick_CLKSource_HCLK;\r
224   }\r
225   else\r
226   {\r
227     SysTick->CTRL &= SysTick_CLKSource_HCLK_Div8;\r
228   }\r
229 }\r
230 \r
231 /**\r
232   * @}\r
233   */\r
234 \r
235 /**\r
236   * @}\r
237   */\r
238 \r
239 /**\r
240   * @}\r
241   */\r
242 \r
243 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/\r