]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32L0/stm32l0xx_hal.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / mbed / targets / cmsis / TARGET_STM / TARGET_STM32L0 / stm32l0xx_hal.c
1 /**
2   ******************************************************************************
3   * @file    stm32l0xx_hal.c
4   * @author  MCD Application Team
5   * @version V1.2.0
6   * @date    06-February-2015
7   * @brief   HAL module driver.
8   *          This is the common part of the HAL initialization
9   *
10   @verbatim
11   ==============================================================================
12                      ##### How to use this driver #####
13   ==============================================================================
14     [..]
15     The common HAL driver contains a set of generic and common APIs that can be
16     used by the PPP peripheral drivers and the user to start using the HAL. 
17     [..]
18     The HAL contains two APIs categories: 
19          (+) Common HAL APIs
20          (+) Services HAL APIs
21
22   @endverbatim
23   ******************************************************************************
24   * @attention
25   *
26   * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
27   *
28   * Redistribution and use in source and binary forms, with or without modification,
29   * are permitted provided that the following conditions are met:
30   *   1. Redistributions of source code must retain the above copyright notice,
31   *      this list of conditions and the following disclaimer.
32   *   2. Redistributions in binary form must reproduce the above copyright notice,
33   *      this list of conditions and the following disclaimer in the documentation
34   *      and/or other materials provided with the distribution.
35   *   3. Neither the name of STMicroelectronics nor the names of its contributors
36   *      may be used to endorse or promote products derived from this software
37   *      without specific prior written permission.
38   *
39   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
40   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
42   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
43   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
44   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
45   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
46   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
47   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
48   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
49   *
50   ******************************************************************************
51   */
52
53 /* Includes ------------------------------------------------------------------*/
54 #include "stm32l0xx_hal.h"
55
56 /** @addtogroup STM32L0xx_HAL_Driver
57   * @{
58   */
59
60 /** @addgroup HAL 
61   * @brief HAL module driver.
62   * @{
63   */
64 #ifdef HAL_MODULE_ENABLED
65
66
67 /** @addtogroup HAL_Exported_Constants
68   *
69   * @{
70   */
71
72 /** @defgroup HAL_Version HAL Version
73   * @{
74   */
75 #define SYSCFG_BOOT_MAINFLASH          ((uint32_t)0x00000000)
76 #define SYSCFG_BOOT_SYSTEMFLASH        ((uint32_t)SYSCFG_CFGR1_MEM_MODE_0)
77 #define SYSCFG_BOOT_SRAM               ((uint32_t)SYSCFG_CFGR1_BOOT_MODE)     
78
79
80 #define HAL_TIMEOUT_DMA_ABORT    ((uint32_t)1000)  /* 1s  */
81
82 /**
83  * @brief STM32L0xx HAL Driver version number V1.2.0
84    */
85 #define __STM32L0xx_HAL_VERSION_MAIN   (0x01) /*!< [31:24] main version */
86 #define __STM32L0xx_HAL_VERSION_SUB1   (0x02) /*!< [23:16] sub1 version */
87 #define __STM32L0xx_HAL_VERSION_SUB2   (0x00) /*!< [15:8]  sub2 version */
88 #define __STM32L0xx_HAL_VERSION_RC     (0x00) /*!< [7:0]  release candidate */
89 #define __STM32L0xx_HAL_VERSION         ((__STM32L0xx_HAL_VERSION_MAIN << 24)\
90                                         |(__STM32L0xx_HAL_VERSION_SUB1 << 16)\
91                                         |(__STM32L0xx_HAL_VERSION_SUB2 << 8 )\
92                                         |(__STM32L0xx_HAL_VERSION_RC))
93
94 #define IDCODE_DEVID_MASK    ((uint32_t)0x00000FFF)
95
96 /**
97   * @}
98   */ 
99   
100 /**
101   * @}
102   */
103 /** @defgroup HAL_Private_Data
104   * @{
105   */ 
106 static __IO uint32_t uwTick;
107
108 /**
109   * @}
110   */ 
111
112 /** @addtogroup HAL_Exported_Functions HAL Exported Functions
113   * @{
114   */
115
116 /** @addtogroup HAL_Exported_Functions_Group1 Initialization and de-initialization Functions 
117  *  @brief    Initialization and de-initialization functions
118  *
119 @verbatim
120  ===============================================================================
121               ##### Initialization and de-initialization functions #####
122  ===============================================================================
123    [..]  This section provides functions allowing to:
124       (+) Initializes the Flash interface, the NVIC allocation and initial clock 
125           configuration. It initializes the source of time base also when timeout 
126           is needed and the backup domain when enabled.
127       (+) de-Initializes common part of the HAL.
128       (+) Configure The time base source to have 1ms time base with a dedicated 
129           Tick interrupt priority. 
130         (++) Systick timer is used by default as source of time base, but user 
131              can eventually implement his proper time base source (a general purpose 
132              timer for example or other time source), keeping in mind that Time base 
133              duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and 
134              handled in milliseconds basis.
135         (++) Time base configuration function (HAL_InitTick ()) is called automatically 
136              at the beginning of the program after reset by HAL_Init() or at any time 
137              when clock is configured, by HAL_RCC_ClockConfig(). 
138         (++) Source of time base is configured  to generate interrupts at regular 
139              time intervals. Care must be taken if HAL_Delay() is called from a 
140              peripheral ISR process, the Tick interrupt line must have higher priority 
141             (numerically lower) than the peripheral interrupt. Otherwise the caller 
142             ISR process will be blocked. 
143        (++) functions affecting time base configurations are declared as __weak  
144              to make  override possible  in case of other  implementations in user file.
145  
146 @endverbatim
147   * @{
148   */
149
150 /**
151   * @brief This function configures the Flash prefetch, Flash preread and Buffer cache,
152   *        Configures time base source, NVIC and Low level hardware
153   * @note This function is called at the beginning of program after reset and before 
154   *       the clock configuration
155   * @note The time base configuration is based on MSI clock when exiting from Reset.
156   *       Once done, time base tick start incrementing.
157   *        In the default implementation,Systick is used as source of time base.
158   *        the tick variable is incremented each 1ms in its ISR.
159   * @param None
160   * @retval HAL status
161   */
162 HAL_StatusTypeDef HAL_Init(void)
163 {
164   /* Configure Buffer cache, Flash prefetch,  Flash preread */ 
165 #if (BUFFER_CACHE_DISABLE != 0)
166   __HAL_FLASH_BUFFER_CACHE_DISABLE();
167 #endif /* BUFFER_CACHE_DISABLE */
168
169 #if (PREREAD_ENABLE != 0)
170   __HAL_FLASH_PREREAD_BUFFER_ENABLE();
171 #endif /* PREREAD_ENABLE */
172
173 #if (PREFETCH_ENABLE != 0)
174   __HAL_FLASH_PREFETCH_BUFFER_ENABLE();
175 #endif /* PREFETCH_ENABLE */
176
177   /* Use systick as time base source and configure 1ms tick (default clock after Reset is MSI) */
178
179   HAL_InitTick(TICK_INT_PRIORITY);
180
181   /* Init the low level hardware */
182   HAL_MspInit();
183
184   /* Return function status */
185   return HAL_OK;
186 }
187
188 /**
189   * @brief This function de-Initializes common part of the HAL and stops the source
190   *        of time base.
191   * @note This function is optional.
192   * @param None
193   * @retval HAL status
194   */
195 HAL_StatusTypeDef HAL_DeInit(void)
196 {
197   /* Reset of all peripherals */
198   __HAL_RCC_APB1_FORCE_RESET();
199   __HAL_RCC_APB1_RELEASE_RESET();
200
201   __HAL_RCC_APB2_FORCE_RESET();
202   __HAL_RCC_APB2_RELEASE_RESET();
203
204   __HAL_RCC_AHB_FORCE_RESET();
205   __HAL_RCC_AHB_RELEASE_RESET();
206
207   __HAL_RCC_IOP_FORCE_RESET();
208   __HAL_RCC_IOP_RELEASE_RESET();
209
210   /* De-Init the low level hardware */
211   HAL_MspDeInit();
212     
213   /* Return function status */
214   return HAL_OK;
215 }
216
217 /**
218   * @brief  Initializes the MSP.
219   * @param  None
220   * @retval None
221   */
222 __weak void HAL_MspInit(void)
223 {
224   /* NOTE : This function Should not be modified, when the callback is needed,
225             the HAL_MspInit could be implemented in the user file
226    */
227 }
228
229 /**
230   * @brief  DeInitializes the MSP.
231   * @param  None  
232   * @retval None
233   */
234 __weak void HAL_MspDeInit(void)
235 {
236   /* NOTE : This function Should not be modified, when the callback is needed,
237             the HAL_MspDeInit could be implemented in the user file
238    */
239 }
240
241
242 /**
243   * @brief This function configures the source of the time base. 
244   *        The time source is configured  to have 1ms time base with a dedicated 
245   *        Tick interrupt priority.
246   * @note This function is called  automatically at the beginning of program after
247   *       reset by HAL_Init() or at any time when clock is reconfigured  by HAL_RCC_ClockConfig(). 
248   * @note In the default implementation, SysTick timer is the source of time base. 
249   *       It is used to generate interrupts at regular time intervals. 
250   *       Care must be taken if HAL_Delay() is called from a peripheral ISR process, 
251   *       The the SysTick interrupt must have higher priority (numerically lower) 
252   *       than the peripheral interrupt. Otherwise the caller ISR process will be blocked.
253   *       The function is declared as __Weak  to be overwritten  in case of other
254   *       implementation  in user file.
255   * @param TickPriority: Tick interrupt priority.
256   * @retval HAL status
257   */
258 __weak HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
259 {
260   /*Configure the SysTick to have interrupt in 1ms time basis*/
261   HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
262
263   /*Configure the SysTick IRQ priority */
264   HAL_NVIC_SetPriority(SysTick_IRQn, TickPriority ,0);
265
266    /* Return function status */
267   return HAL_OK;
268 }
269
270 /**
271   * @}
272   */
273
274 /** @addtogroup HAL_Exported_Functions_Group2 
275  *  @brief    Peripheral Control functions
276  *
277 @verbatim
278  ===============================================================================
279                       ##### HAL Control functions #####
280  ===============================================================================
281     [..]  This section provides functions allowing to:
282       (+) Provide a tick value in millisecond
283       (+) Provide a blocking delay in millisecond
284       (+) Suspend the time base source interrupt
285       (+) Resume the time base source interrupt
286       (+) Get the HAL API driver version
287       (+) Get the device identifier
288       (+) Get the device revision identifier
289       (+) Configures low power mode behavior when the MCU is in Debug mode
290       (+) Manages the VEREFINT feature (activation, lock, output selection)
291       
292 @endverbatim
293   * @{
294   */
295
296 /**
297   * @brief This function is called to increment  a global variable "uwTick"
298   *        used as application time base.
299   * @note In the default implementation, this variable is incremented each 1ms
300   *       in Systick ISR.
301  * @note This function is declared as __weak to be overwritten in case of other 
302   *      implementations in user file.
303   * @param None
304   * @retval None
305   */
306 __weak void HAL_IncTick(void)
307 {
308   uwTick++;
309 }
310
311 /**
312   * @brief Provides a tick value in millisecond.
313   * @note This function is declared as __weak to be overwritten in case of other 
314   *       implementations in user file.
315   * @param None
316   * @retval tick value
317   */
318 __weak uint32_t HAL_GetTick(void)
319 {
320   return uwTick;
321 }
322
323 /**
324   * @brief This function provides accurate delay (in ms) based on a variable incremented.
325   * @note In the default implementation , SysTick timer is the source of time base.
326   *       It is used to generate interrupts at regular time intervals where uwTick
327   *       is incremented.
328   * @note ThiS function is declared as __weak to be overwritten in case of other
329   *       implementations in user file.
330   * @param Delay: specifies the delay time length, in milliseconds.
331   * @retval None
332   */
333 __weak void HAL_Delay(__IO uint32_t Delay)
334 {
335   uint32_t tickstart = 0;
336   tickstart = HAL_GetTick();
337   while((HAL_GetTick() - tickstart) < Delay)
338   {
339   }
340 }
341
342 /**
343   * @brief Suspend Tick increment.
344   * @note In the default implementation , SysTick timer is the source of time base. It is
345   *       used to generate interrupts at regular time intervals. Once HAL_SuspendTick()
346   *       is called, the the SysTick interrupt will be disabled and so Tick increment 
347   *       is suspended.
348   * @note This function is declared as __weak to be overwritten in case of other
349   *       implementations in user file.
350   * @param None
351   * @retval None
352   */
353 __weak void HAL_SuspendTick(void)
354 {
355   /* Disable SysTick Interrupt */
356   SysTick->CTRL &= ~SysTick_CTRL_TICKINT_Msk;
357 }
358
359 /**
360   * @brief Resume Tick increment.
361   * @note In the default implementation , SysTick timer is the source of time base. It is
362   *       used to generate interrupts at regular time intervals. Once HAL_ResumeTick()
363   *       is called, the the SysTick interrupt will be enabled and so Tick increment 
364   *       is resumed.
365   * @note This function is declared as __weak to be overwritten in case of other
366   *       implementations in user file.
367   * @param None
368   * @retval None
369   */
370 __weak void HAL_ResumeTick(void)
371 {
372   /* Enable SysTick Interrupt */
373   SysTick->CTRL  |= SysTick_CTRL_TICKINT_Msk;
374 }
375
376 /**
377   * @brief Returns the HAL revision
378   * @param None
379   * @retval version: 0xXYZR (8bits for each decimal, R for RC)
380   */
381 uint32_t HAL_GetHalVersion(void)
382 {
383   return __STM32L0xx_HAL_VERSION;
384 }
385
386 /**
387   * @brief Returns the device revision identifier.
388   * @param None
389   * @retval Device revision identifier
390   */
391 uint32_t HAL_GetREVID(void)
392 {
393   return((DBGMCU->IDCODE) >> 16);
394 }
395
396 /**
397   * @brief  Returns the device identifier.
398   * @param  None
399   * @retval Device identifier
400   */
401 uint32_t HAL_GetDEVID(void)
402 {
403    return((DBGMCU->IDCODE) & IDCODE_DEVID_MASK);
404 }
405
406 /**
407   * @brief  Enable the Debug Module during SLEEP mode
408   * @param  None
409   * @retval None
410   */
411 void HAL_DBGMCU_EnableDBGSleepMode(void)
412 {
413   SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_SLEEP);
414 }
415
416 /**
417   * @brief  Disable the Debug Module during SLEEP mode
418   * @param  None
419   * @retval None
420   */
421 void HAL_DBGMCU_DisableDBGSleepMode(void)
422 {
423   CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_SLEEP);
424 }
425
426 /**
427   * @brief  Enable the Debug Module during STOP mode
428   * @param  None
429   * @retval None
430   */
431 void HAL_DBGMCU_EnableDBGStopMode(void)
432 {
433   SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
434 }
435
436 /**
437   * @brief  Disable the Debug Module during STOP mode
438   * @param  None
439   * @retval None
440   */
441 void HAL_DBGMCU_DisableDBGStopMode(void)
442 {
443   CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
444 }
445
446 /**
447   * @brief  Enable the Debug Module during STANDBY mode
448   * @param  None
449   * @retval None
450   */
451 void HAL_DBGMCU_EnableDBGStandbyMode(void)
452 {
453   SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
454 }
455
456 /**
457   * @brief  Disable the Debug Module during STANDBY mode
458   * @param  None
459   * @retval None
460   */
461 void HAL_DBGMCU_DisableDBGStandbyMode(void)
462 {
463   CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
464 }
465
466 /**
467   * @brief  Enable low power mode behavior when the MCU is in Debug mode.
468   * @param Periph: specifies the low power mode.
469   *   This parameter can be any combination of the following values:
470   *     @arg DBGMCU_SLEEP: Keep debugger connection during SLEEP mode
471   *     @arg DBGMCU_STOP: Keep debugger connection during STOP mode
472   *     @arg DBGMCU_STANDBY: Keep debugger connection during STANDBY mode
473   * @retval None
474   */
475 void HAL_DBGMCU_DBG_EnableLowPowerConfig(uint32_t Periph)
476 {
477   /* Check the parameters */
478   assert_param(IS_DBGMCU_PERIPH(Periph));
479   
480   DBGMCU->CR |= Periph;
481
482 }
483 /**
484   * @brief  Disable low power mode behavior when the MCU is in Debug mode.
485   * @param Periph: specifies the low power mode.
486   *   This parameter can be any combination of the following values:
487   *     @arg DBGMCU_SLEEP: Keep debugger connection during SLEEP mode
488   *     @arg DBGMCU_STOP: Keep debugger connection during STOP mode
489   *     @arg DBGMCU_STANDBY: Keep debugger connection during STANDBY mode
490   * @retval None
491   */
492 void HAL_DBGMCU_DBG_DisableLowPowerConfig(uint32_t Periph)
493 {
494   /* Check the parameters */
495   assert_param(IS_DBGMCU_PERIPH(Periph));
496   {
497     DBGMCU->CR &= ~Periph;
498   }
499 }
500
501 /**
502   * @brief  Returns the boot mode as configured by user.
503   * @param  None.
504   * @retval The boot mode as configured by user. The returned value can be one 
505   *         of the following values:
506   *              - 0x00000000: Boot is configured in Main Flash memory
507   *              - 0x00000100: Boot is configured in System Flash memory
508   *              - 0x00000300: Boot is configured in Embedded SRAM memory
509   */
510 uint32_t  HAL_SYSCFG_GetBootMode(void)
511 {
512   return (SYSCFG->CFGR1 & SYSCFG_CFGR1_BOOT_MODE);
513 }
514
515 /**
516   * @brief Enables the VREFINT.
517   * @param None
518   * @retval None
519   */
520 void HAL_SYSCFG_EnableVREFINT(void)
521 {
522     /* Enable the VREFINT by setting EN_VREFINT bit in the CFGR3 register */
523     SET_BIT(SYSCFG->CFGR3, SYSCFG_CFGR3_EN_VREFINT);
524 }
525
526 /**
527   * @brief Disables the VREFINT.
528   * @param None.
529   * @retval None
530   */
531 void HAL_SYSCFG_DisableVREFINT(void)
532 {
533     /* Disable the VREFINT by setting EN_VREFINT bit in the CFGR3 register */
534     CLEAR_BIT(SYSCFG->CFGR3,SYSCFG_CFGR3_EN_VREFINT); 
535 }
536 /**
537   * @brief Selects the output of internal reference voltage (VREFINT).
538   *        The VREFINT output can be routed to(PB0) or
539   *        (PB1) or both.
540   * @note   Kept for backward compatibility
541   *         We recommend to use the MACRO 
542   *         __HAL_SYSCFG_VREFINT_OUTPUT_SELECT(__VREFINT_OUTPUT__)
543   *         rather than this function
544   * @param SYSCFG_Vrefint_OUTPUT: new state of the Vrefint output.
545   *        This parameter can be one of the following values:
546   *     @arg SYSCFG_VREFINT_OUT_NONE
547   *     @arg SYSCFG_VREFINT_OUT_PB0
548   *     @arg SYSCFG_VREFINT_OUT_PB1
549   *     @arg SYSCFG_VREFINT_OUT_PB0_PB1
550   * @retval None
551   */
552 void HAL_SYSCFG_VREFINT_OutputSelect(uint32_t SYSCFG_Vrefint_OUTPUT)
553 {
554   /* Check the parameters */
555   assert_param(IS_SYSCFG_VREFINT_OUT_SELECT(SYSCFG_Vrefint_OUTPUT));
556
557   /* Set the output Vrefint pin */
558   SYSCFG->CFGR3 &= ~(SYSCFG_CFGR3_VREF_OUT);
559   SYSCFG->CFGR3 |= (uint32_t)(SYSCFG_Vrefint_OUTPUT);
560 }
561
562 /**
563   * @brief  Lock the SYSCFG VREF register values
564   * @retval None
565   */
566 void HAL_SYSCFG_Enable_Lock_VREFINT(void)
567 {
568     /* Enable the LOCK by setting REF_LOCK bit in the CFGR3 register */
569     SET_BIT(SYSCFG->CFGR3, SYSCFG_CFGR3_REF_LOCK);
570 }
571
572 /**
573   * @brief  Unlock the overall SYSCFG VREF register values
574   * @retval None
575   */
576 void HAL_SYSCFG_Disable_Lock_VREFINT(void)
577 {
578     /* Disable the LOCK by setting REF_LOCK bit in the CFGR3 register */
579     CLEAR_BIT(SYSCFG->CFGR3, SYSCFG_CFGR3_REF_LOCK);
580 }
581
582 /**
583   * @}
584   */
585
586 /**
587   * @}
588   */
589
590 #endif /* HAL_MODULE_ENABLED */
591 /**
592   * @}
593   */
594
595 /**
596   * @}
597   */
598
599 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
600