]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32L1/stm32l1xx_hal_cortex.c
Merge commit '1fe4406f374291ab2e86e95a97341fd9c475fcb8'
[qmk_firmware.git] / tmk_core / tool / mbed / mbed-sdk / libraries / mbed / targets / cmsis / TARGET_STM / TARGET_STM32L1 / stm32l1xx_hal_cortex.c
1 /**
2   ******************************************************************************
3   * @file    stm32l1xx_hal_cortex.c
4   * @author  MCD Application Team
5   * @version V1.0.0
6   * @date    5-September-2014
7   * @brief   CORTEX HAL module driver.
8   *
9   *          This file provides firmware functions to manage the following
10   *          functionalities of the CORTEX:
11   *           + Initialization and de-initialization functions
12   *           + Peripheral Control functions
13   *          
14   *  @verbatim    
15   ==============================================================================
16                         ##### How to use this driver #####
17   ==============================================================================
18
19     [..]  
20     *** How to configure Interrupts using Cortex HAL driver ***
21     ===========================================================
22     [..]     
23     This section provide functions allowing to configure the NVIC interrupts (IRQ).
24     The Cortex-M3 exceptions are managed by CMSIS functions.
25    
26     (#) Configure the NVIC Priority Grouping using HAL_NVIC_SetPriorityGrouping()
27         function according to the following table.
28
29      The table below gives the allowed values of the pre-emption priority and subpriority according
30      to the Priority Grouping configuration performed by HAL_NVIC_SetPriorityGrouping() function.
31        ==========================================================================================================================
32          NVIC_PriorityGroup   | NVIC_IRQChannelPreemptionPriority | NVIC_IRQChannelSubPriority  |       Description
33        ==========================================================================================================================
34         NVIC_PRIORITYGROUP_0  |                0                  |            0-15             | 0 bits for pre-emption priority
35                               |                                   |                             | 4 bits for subpriority
36        --------------------------------------------------------------------------------------------------------------------------
37         NVIC_PRIORITYGROUP_1  |                0-1                |            0-7              | 1 bits for pre-emption priority
38                               |                                   |                             | 3 bits for subpriority
39        --------------------------------------------------------------------------------------------------------------------------    
40         NVIC_PRIORITYGROUP_2  |                0-3                |            0-3              | 2 bits for pre-emption priority
41                               |                                   |                             | 2 bits for subpriority
42        --------------------------------------------------------------------------------------------------------------------------    
43         NVIC_PRIORITYGROUP_3  |                0-7                |            0-1              | 3 bits for pre-emption priority
44                               |                                   |                             | 1 bits for subpriority
45        --------------------------------------------------------------------------------------------------------------------------    
46         NVIC_PRIORITYGROUP_4  |                0-15               |            0                | 4 bits for pre-emption priority
47                               |                                   |                             | 0 bits for subpriority                       
48        ==========================================================================================================================
49      (#)  Configure the priority of the selected IRQ Channels using HAL_NVIC_SetPriority() 
50
51      (#)  Enable the selected IRQ Channels using HAL_NVIC_EnableIRQ() 
52       
53
54      -@- When the NVIC_PRIORITYGROUP_0 is selected, IRQ pre-emption is no more possible. 
55          The pending IRQ priority will be managed only by the sub priority.
56    
57      -@- IRQ priority order (sorted by highest to lowest priority):
58         (+@) Lowest pre-emption priority
59         (+@) Lowest sub priority
60         (+@) Lowest hardware priority (IRQ number)
61  
62     [..]  
63     *** How to configure Systick using Cortex HAL driver ***
64     ========================================================
65     [..]
66     Setup SysTick Timer for 1 msec interrupts.
67            
68    (+) The HAL_SYSTICK_Config()function calls the SysTick_Config() function which
69        is a CMSIS function that:
70         (++) Configures the SysTick Reload register with value passed as function parameter.
71         (++) Configures the SysTick IRQ priority to the lowest value (0x0F).
72         (++) Resets the SysTick Counter register.
73         (++) Configures the SysTick Counter clock source to be Core Clock Source (HCLK).
74         (++) Enables the SysTick Interrupt.
75         (++) Starts the SysTick Counter.
76     
77    (+) You can change the SysTick Clock source to be HCLK_Div8 by calling the macro
78        __HAL_CORTEX_SYSTICKCLK_CONFIG(SYSTICK_CLKSOURCE_HCLK_DIV8) just after the
79        HAL_SYSTICK_Config() function call. The __HAL_CORTEX_SYSTICKCLK_CONFIG() macro is defined
80        inside the stm32l1xx_hal_cortex.h file.
81
82    (+) You can change the SysTick IRQ priority by calling the
83        HAL_NVIC_SetPriority(SysTick_IRQn,...) function just after the HAL_SYSTICK_Config() function 
84        call. The HAL_NVIC_SetPriority() call the NVIC_SetPriority() function which is a CMSIS function.
85
86    (+) To adjust the SysTick time base, use the following formula:
87                             
88        Reload Value = SysTick Counter Clock (Hz) x  Desired Time base (s)
89        (++) Reload Value is the parameter to be passed for HAL_SYSTICK_Config() function
90        (++) Reload Value should not exceed 0xFFFFFF
91    
92   @endverbatim
93   ******************************************************************************
94   * @attention
95   *
96   * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
97   *
98   * Redistribution and use in source and binary forms, with or without modification,
99   * are permitted provided that the following conditions are met:
100   *   1. Redistributions of source code must retain the above copyright notice,
101   *      this list of conditions and the following disclaimer.
102   *   2. Redistributions in binary form must reproduce the above copyright notice,
103   *      this list of conditions and the following disclaimer in the documentation
104   *      and/or other materials provided with the distribution.
105   *   3. Neither the name of STMicroelectronics nor the names of its contributors
106   *      may be used to endorse or promote products derived from this software
107   *      without specific prior written permission.
108   *
109   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
110   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
111   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
112   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
113   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
114   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
115   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
116   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
117   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
118   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
119   *
120   ******************************************************************************
121   */
122
123 /* Includes ------------------------------------------------------------------*/
124 #include "stm32l1xx_hal.h"
125
126 /** @addtogroup STM32L1xx_HAL_Driver
127   * @{
128   */
129
130 /** @defgroup CORTEX CORTEX
131   * @brief CORTEX HAL module driver
132   * @{
133   */
134
135 #ifdef HAL_CORTEX_MODULE_ENABLED
136
137 /* Private typedef -----------------------------------------------------------*/
138 /* Private define ------------------------------------------------------------*/
139 /* Private macro -------------------------------------------------------------*/
140 /* Private variables ---------------------------------------------------------*/
141 /* Private function prototypes -----------------------------------------------*/
142 /* Private functions ---------------------------------------------------------*/
143
144 /** @defgroup CORTEX_Exported_Functions CORTEX Exported Functions
145   * @{
146   */
147
148
149 /** @defgroup CORTEX_Exported_Functions_Group1 Initialization and de-initialization functions
150  *  @brief    Initialization and Configuration functions 
151  *
152 @verbatim    
153   ==============================================================================
154               ##### Initialization and de-initialization functions #####
155   ==============================================================================
156     [..]
157       This section provide the Cortex HAL driver functions allowing to configure Interrupts
158       Systick functionalities 
159
160 @endverbatim
161   * @{
162   */
163
164
165 /**
166   * @brief  Sets the priority grouping field (pre-emption priority and subpriority)
167   *         using the required unlock sequence.
168   * @param  PriorityGroup: The priority grouping bits length. 
169   *         This parameter can be one of the following values:
170   *         @arg NVIC_PRIORITYGROUP_0: 0 bits for pre-emption priority
171   *                                    4 bits for subpriority
172   *         @arg NVIC_PRIORITYGROUP_1: 1 bits for pre-emption priority
173   *                                    3 bits for subpriority
174   *         @arg NVIC_PRIORITYGROUP_2: 2 bits for pre-emption priority
175   *                                    2 bits for subpriority
176   *         @arg NVIC_PRIORITYGROUP_3: 3 bits for pre-emption priority
177   *                                    1 bits for subpriority
178   *         @arg NVIC_PRIORITYGROUP_4: 4 bits for pre-emption priority
179   *                                    0 bits for subpriority
180   * @note   When the NVIC_PriorityGroup_0 is selected, IRQ pre-emption is no more possible. 
181   *         The pending IRQ priority will be managed only by the subpriority. 
182   * @retval None
183   */
184 void HAL_NVIC_SetPriorityGrouping(uint32_t PriorityGroup)
185 {
186   /* Check the parameters */
187   assert_param(IS_NVIC_PRIORITY_GROUP(PriorityGroup));
188   
189   /* Set the PRIGROUP[10:8] bits according to the PriorityGroup parameter value */
190   NVIC_SetPriorityGrouping(PriorityGroup);
191 }
192
193 /**
194   * @brief  Sets the priority of an interrupt.
195   * @param  IRQn: External interrupt number
196   *         This parameter can be an enumerator of IRQn_Type enumeration
197   *         (For the complete STM32 Devices IRQ Channels list, please refer to the appropriate CMSIS device file (stm32l1xx.h))
198   * @param  PreemptPriority: The pre-emption priority for the IRQn channel.
199   *         This parameter can be a value between 0 and 15
200   *         A lower priority value indicates a higher priority 
201   * @param  SubPriority: the subpriority level for the IRQ channel.
202   *         This parameter can be a value between 0 and 15
203   *         A lower priority value indicates a higher priority.          
204   * @retval None
205   */
206 void HAL_NVIC_SetPriority(IRQn_Type IRQn, uint32_t PreemptPriority, uint32_t SubPriority)
207 {
208   uint32_t prioritygroup = 0x00;
209   
210   /* Check the parameters */
211   assert_param(IS_NVIC_SUB_PRIORITY(SubPriority));
212   assert_param(IS_NVIC_PREEMPTION_PRIORITY(PreemptPriority));
213   
214   prioritygroup = NVIC_GetPriorityGrouping();
215   
216   NVIC_SetPriority(IRQn, NVIC_EncodePriority(prioritygroup, PreemptPriority, SubPriority));
217 }
218
219 /**
220   * @brief  Enables a device specific interrupt in the NVIC interrupt controller.
221   * @note   To configure interrupts priority correctly, the NVIC_PriorityGroupConfig()
222   *         function should be called before. 
223   * @param  IRQn External interrupt number
224   *         This parameter can be an enumerator of IRQn_Type enumeration
225   *         (For the complete STM32 Devices IRQ Channels list, please refer to the appropriate CMSIS device file (stm32l1xx.h))
226   * @retval None
227   */
228 void HAL_NVIC_EnableIRQ(IRQn_Type IRQn)
229 {
230   /* Enable interrupt */
231   NVIC_EnableIRQ(IRQn);
232 }
233
234 /**
235   * @brief  Disables a device specific interrupt in the NVIC interrupt controller.
236   * @param  IRQn External interrupt number
237   *         This parameter can be an enumerator of IRQn_Type enumeration
238   *         (For the complete STM32 Devices IRQ Channels list, please refer to the appropriate CMSIS device file (stm32l1xxxx.h))  
239   * @retval None
240   */
241 void HAL_NVIC_DisableIRQ(IRQn_Type IRQn)
242 {
243   /* Disable interrupt */
244   NVIC_DisableIRQ(IRQn);
245 }
246
247 /**
248   * @brief  Initiates a system reset request to reset the MCU.
249   * @retval None
250   */
251 void HAL_NVIC_SystemReset(void)
252 {
253   /* System Reset */
254   NVIC_SystemReset();
255 }
256
257 /**
258   * @brief  Initializes the System Timer and its interrupt, and starts the System Tick Timer.
259   *         Counter is in free running mode to generate periodic interrupts.
260   * @param  TicksNumb: Specifies the ticks Number of ticks between two interrupts.
261   * @retval status:  - 0  Function succeeded.
262   *                  - 1  Function failed.
263   */
264 uint32_t HAL_SYSTICK_Config(uint32_t TicksNumb)
265 {
266    return SysTick_Config(TicksNumb);
267 }
268 /**
269   * @}
270   */
271
272 /** @defgroup CORTEX_Exported_Functions_Group2 Peripheral Control functions
273  *  @brief    Cortex control functions 
274  *
275 @verbatim   
276   ==============================================================================
277                       ##### Peripheral Control functions #####
278   ==============================================================================
279     [..]
280       This subsection provides a set of functions allowing to control the CORTEX
281       (NVIC, SYSTICK) functionalities. 
282  
283       
284 @endverbatim
285   * @{
286   */
287
288 /**
289   * @brief  Gets the priority grouping field from the NVIC Interrupt Controller.
290   * @retval Priority grouping field (SCB->AIRCR [10:8] PRIGROUP field)
291   */
292 uint32_t HAL_NVIC_GetPriorityGrouping(void)
293 {
294   /* Get the PRIGROUP[10:8] field value */
295   return NVIC_GetPriorityGrouping();
296 }
297
298 /**
299   * @brief  Gets the priority of an interrupt.
300   * @param  IRQn: External interrupt number
301   *         This parameter can be an enumerator of IRQn_Type enumeration
302   *         (For the complete STM32 Devices IRQ Channels list, please refer to the appropriate CMSIS device file (stm32l1xxxx.h))
303   * @param   PriorityGroup: the priority grouping bits length.
304   *         This parameter can be one of the following values:
305   *           @arg NVIC_PRIORITYGROUP_0: 0 bits for pre-emption priority
306   *                                      4 bits for subpriority
307   *           @arg NVIC_PRIORITYGROUP_1: 1 bits for pre-emption priority
308   *                                      3 bits for subpriority
309   *           @arg NVIC_PRIORITYGROUP_2: 2 bits for pre-emption priority
310   *                                      2 bits for subpriority
311   *           @arg NVIC_PRIORITYGROUP_3: 3 bits for pre-emption priority
312   *                                      1 bits for subpriority
313   *           @arg NVIC_PRIORITYGROUP_4: 4 bits for pre-emption priority
314   *                                      0 bits for subpriority
315   * @param  pPreemptPriority: Pointer on the Preemptive priority value (starting from 0).
316   * @param  pSubPriority: Pointer on the Subpriority value (starting from 0).
317   * @retval None
318   */
319 void HAL_NVIC_GetPriority(IRQn_Type IRQn, uint32_t PriorityGroup, uint32_t* pPreemptPriority, uint32_t* pSubPriority)
320 {
321   /* Check the parameters */
322   assert_param(IS_NVIC_PRIORITY_GROUP(PriorityGroup));
323  /* Get priority for Cortex-M system or device specific interrupts */
324   NVIC_DecodePriority(NVIC_GetPriority(IRQn), PriorityGroup, pPreemptPriority, pSubPriority);
325 }
326
327 /**
328   * @brief  Sets Pending bit of an external interrupt.
329   * @param  IRQn External interrupt number
330   *         This parameter can be an enumerator of IRQn_Type enumeration
331   *         (For the complete STM32 Devices IRQ Channels list, please refer to the appropriate CMSIS device file (stm32l1xxxx.h))  
332   * @retval None
333   */
334 void HAL_NVIC_SetPendingIRQ(IRQn_Type IRQn)
335
336   /* Set interrupt pending */
337   NVIC_SetPendingIRQ(IRQn);
338 }
339
340 /**
341   * @brief Gets Pending Interrupt (reads the pending register in the NVIC 
342   *         and returns the pending bit for the specified interrupt).
343   * @param IRQn External interrupt number
344   *         This parameter can be an enumerator of IRQn_Type enumeration
345   *         (For the complete STM32 Devices IRQ Channels list, please refer to the appropriate CMSIS device file (stm32l1xxxx.h))  
346   * @retval status: - 0  Interrupt status is not pending.
347   *                 - 1  Interrupt status is pending.
348   */
349 uint32_t HAL_NVIC_GetPendingIRQ(IRQn_Type IRQn)
350
351   /* Return 1 if pending else 0 */
352   return NVIC_GetPendingIRQ(IRQn);
353 }
354
355 /**
356   * @brief Clears the pending bit of an external interrupt.
357   * @param IRQn External interrupt number
358   *         This parameter can be an enumerator of IRQn_Type enumeration
359   *         (For the complete STM32 Devices IRQ Channels list, please refer to the appropriate CMSIS device file (stm32l1xxxx.h))  
360   * @retval None
361   */
362 void HAL_NVIC_ClearPendingIRQ(IRQn_Type IRQn)
363
364   /* Clear pending interrupt */
365   NVIC_ClearPendingIRQ(IRQn);
366 }
367
368 /**
369   * @brief Gets active interrupt ( reads the active register in NVIC and returns the active bit).
370   * @param IRQn External interrupt number
371   *         This parameter can be an enumerator of IRQn_Type enumeration
372   *         (For the complete STM32 Devices IRQ Channels list, please refer to the appropriate CMSIS device file (stm32l1xxxx.h))  
373   * @retval status: - 0  Interrupt status is not pending.
374   *                 - 1  Interrupt status is pending.
375   */
376 uint32_t HAL_NVIC_GetActive(IRQn_Type IRQn)
377
378   /* Return 1 if active else 0 */
379   return NVIC_GetActive(IRQn);
380 }
381
382 /**
383   * @brief  Configures the SysTick clock source.
384   * @param  CLKSource: specifies the SysTick clock source.
385   *         This parameter can be one of the following values:
386   *             @arg SYSTICK_CLKSOURCE_HCLK_DIV8: AHB clock divided by 8 selected as SysTick clock source.
387   *             @arg SYSTICK_CLKSOURCE_HCLK: AHB clock selected as SysTick clock source.
388   * @retval None
389   */
390 void HAL_SYSTICK_CLKSourceConfig(uint32_t CLKSource)
391 {
392   /* Check the parameters */
393   assert_param(IS_SYSTICK_CLK_SOURCE(CLKSource));
394   if (CLKSource == SYSTICK_CLKSOURCE_HCLK)
395   {
396     SysTick->CTRL |= SYSTICK_CLKSOURCE_HCLK;
397   }
398   else
399   {
400     SysTick->CTRL &= ~SYSTICK_CLKSOURCE_HCLK;
401   }
402 }
403
404 /**
405   * @brief  This function handles SYSTICK interrupt request.
406   * @retval None
407   */
408 void HAL_SYSTICK_IRQHandler(void)
409 {
410   HAL_SYSTICK_Callback();
411 }
412
413 /**
414   * @brief  SYSTICK callback.
415   * @retval None
416   */
417 __weak void HAL_SYSTICK_Callback(void)
418 {
419   /* NOTE : This function Should not be modified, when the callback is needed,
420             the HAL_SYSTICK_Callback could be implemented in the user file
421    */
422 }
423
424 /**
425   * @}
426   */
427
428 /**
429   * @}
430   */
431
432 #endif /* HAL_CORTEX_MODULE_ENABLED */
433 /**
434   * @}
435   */
436
437 /**
438   * @}
439   */
440
441 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/