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