1 /**
2   ******************************************************************************
3   * @file    stm32l4xx_hal.c
4   * @author  MCD Application Team
5   * @brief   HAL module driver.
6   *          This is the common part of the HAL initialization
7   *
8   ******************************************************************************
9   * @attention
10   *
11   * Copyright (c) 2017 STMicroelectronics.
12   * All rights reserved.
13   *
14   * This software is licensed under terms that can be found in the LICENSE file
15   * in the root directory of this software component.
16   * If no LICENSE file comes with this software, it is provided AS-IS.
17   *
18   ******************************************************************************
19   @verbatim
20   ==============================================================================
21                      ##### How to use this driver #####
22   ==============================================================================
23     [..]
24     The common HAL driver contains a set of generic and common APIs that can be
25     used by the PPP peripheral drivers and the user to start using the HAL.
26     [..]
27     The HAL contains two APIs' categories:
28          (+) Common HAL APIs
29          (+) Services HAL APIs
30 
31   @endverbatim
32   ******************************************************************************
33   */
34 
35 /* Includes ------------------------------------------------------------------*/
36 #include "stm32l4xx_hal.h"
37 
38 /** @addtogroup STM32L4xx_HAL_Driver
39   * @{
40   */
41 
42 /** @defgroup HAL HAL
43   * @brief HAL module driver
44   * @{
45   */
46 
47 #ifdef HAL_MODULE_ENABLED
48 
49 /* Private typedef -----------------------------------------------------------*/
50 /* Private define ------------------------------------------------------------*/
51 /**
52  * @brief STM32L4xx HAL Driver version number
53    */
54 #define STM32L4XX_HAL_VERSION_MAIN   (0x01U) /*!< [31:24] main version */
55 #define STM32L4XX_HAL_VERSION_SUB1   (0x0DU) /*!< [23:16] sub1 version */
56 #define STM32L4XX_HAL_VERSION_SUB2   (0x05U) /*!< [15:8]  sub2 version */
57 #define STM32L4XX_HAL_VERSION_RC     (0x00U) /*!< [7:0]  release candidate */
58 #define STM32L4XX_HAL_VERSION        ((STM32L4XX_HAL_VERSION_MAIN  << 24U)\
59                                       |(STM32L4XX_HAL_VERSION_SUB1 << 16U)\
60                                       |(STM32L4XX_HAL_VERSION_SUB2 << 8U)\
61                                       |(STM32L4XX_HAL_VERSION_RC))
62 
63 #if defined(VREFBUF)
64 #define VREFBUF_TIMEOUT_VALUE     10U   /* 10 ms (to be confirmed) */
65 #endif /* VREFBUF */
66 
67 /* ------------ SYSCFG registers bit address in the alias region ------------ */
68 #define SYSCFG_OFFSET             (SYSCFG_BASE - PERIPH_BASE)
69 /* ---  MEMRMP Register ---*/
70 /* Alias word address of FB_MODE bit */
71 #define MEMRMP_OFFSET             SYSCFG_OFFSET
72 #define FB_MODE_BitNumber         8U
73 #define FB_MODE_BB                (PERIPH_BB_BASE + (MEMRMP_OFFSET * 32U) + (FB_MODE_BitNumber * 4U))
74 
75 /* --- SCSR Register ---*/
76 /* Alias word address of SRAM2ER bit */
77 #define SCSR_OFFSET               (SYSCFG_OFFSET + 0x18U)
78 #define BRER_BitNumber            0U
79 #define SCSR_SRAM2ER_BB           (PERIPH_BB_BASE + (SCSR_OFFSET * 32U) + (BRER_BitNumber * 4U))
80 
81 /* Private macro -------------------------------------------------------------*/
82 /* Private variables ---------------------------------------------------------*/
83 /* Private function prototypes -----------------------------------------------*/
84 
85 /* Exported variables --------------------------------------------------------*/
86 
87 /** @defgroup HAL_Exported_Variables HAL Exported Variables
88   * @{
89   */
90 __IO uint32_t uwTick;
91 uint32_t uwTickPrio = (1UL << __NVIC_PRIO_BITS); /* Invalid priority */
92 HAL_TickFreqTypeDef uwTickFreq = HAL_TICK_FREQ_DEFAULT;  /* 1KHz */
93 /**
94   * @}
95   */
96 
97 /* Exported functions --------------------------------------------------------*/
98 
99 /** @defgroup HAL_Exported_Functions HAL Exported Functions
100   * @{
101   */
102 
103 /** @defgroup HAL_Exported_Functions_Group1 Initialization and de-initialization Functions
104  *  @brief    Initialization and de-initialization functions
105  *
106 @verbatim
107  ===============================================================================
108               ##### Initialization and de-initialization functions #####
109  ===============================================================================
110     [..]  This section provides functions allowing to:
111       (+) Initialize the Flash interface, the NVIC allocation and initial time base
112           clock configuration.
113       (+) De-initialize common part of the HAL.
114       (+) Configure the time base source to have 1ms time base with a dedicated
115           Tick interrupt priority.
116         (++) SysTick timer is used by default as source of time base, but user
117              can eventually implement his proper time base source (a general purpose
118              timer for example or other time source), keeping in mind that Time base
119              duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and
120              handled in milliseconds basis.
121         (++) Time base configuration function (HAL_InitTick ()) is called automatically
122              at the beginning of the program after reset by HAL_Init() or at any time
123              when clock is configured, by HAL_RCC_ClockConfig().
124         (++) Source of time base is configured  to generate interrupts at regular
125              time intervals. Care must be taken if HAL_Delay() is called from a
126              peripheral ISR process, the Tick interrupt line must have higher priority
127             (numerically lower) than the peripheral interrupt. Otherwise the caller
128             ISR process will be blocked.
129        (++) functions affecting time base configurations are declared as __weak
130              to make  override possible  in case of other  implementations in user file.
131 @endverbatim
132   * @{
133   */
134 
135 /**
136   * @brief  Configure the Flash prefetch, the Instruction and Data caches,
137   *         the time base source, NVIC and any required global low level hardware
138   *         by calling the HAL_MspInit() callback function to be optionally defined in user file
139   *         stm32l4xx_hal_msp.c.
140   *
141   * @note   HAL_Init() function is called at the beginning of program after reset and before
142   *         the clock configuration.
143   *
144   * @note   In the default implementation the System Timer (Systick) is used as source of time base.
145   *         The Systick configuration is based on MSI clock, as MSI is the clock
146   *         used after a system Reset and the NVIC configuration is set to Priority group 4.
147   *         Once done, time base tick starts incrementing: the tick variable counter is incremented
148   *         each 1ms in the SysTick_Handler() interrupt handler.
149   *
150   * @retval HAL status
151   */
HAL_Init(void)152 HAL_StatusTypeDef HAL_Init(void)
153 {
154   HAL_StatusTypeDef  status = HAL_OK;
155 
156   /* Configure Flash prefetch, Instruction cache, Data cache */
157   /* Default configuration at reset is:                      */
158   /* - Prefetch disabled                                     */
159   /* - Instruction cache enabled                             */
160   /* - Data cache enabled                                    */
161 #if (INSTRUCTION_CACHE_ENABLE == 0)
162    __HAL_FLASH_INSTRUCTION_CACHE_DISABLE();
163 #endif /* INSTRUCTION_CACHE_ENABLE */
164 
165 #if (DATA_CACHE_ENABLE == 0)
166    __HAL_FLASH_DATA_CACHE_DISABLE();
167 #endif /* DATA_CACHE_ENABLE */
168 
169 #if (PREFETCH_ENABLE != 0)
170   __HAL_FLASH_PREFETCH_BUFFER_ENABLE();
171 #endif /* PREFETCH_ENABLE */
172 
173   /* Set Interrupt Group Priority */
174   HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4);
175 
176   /* Use SysTick as time base source and configure 1ms tick (default clock after Reset is MSI) */
177   if (HAL_InitTick(TICK_INT_PRIORITY) != HAL_OK)
178   {
179     status = HAL_ERROR;
180   }
181   else
182   {
183     /* Init the low level hardware */
184     HAL_MspInit();
185   }
186 
187   /* Return function status */
188   return status;
189 }
190 
191 /**
192   * @brief De-initialize common part of the HAL and stop the source of time base.
193   * @note This function is optional.
194   * @retval HAL status
195   */
HAL_DeInit(void)196 HAL_StatusTypeDef HAL_DeInit(void)
197 {
198   /* Reset of all peripherals */
199   __HAL_RCC_APB1_FORCE_RESET();
200   __HAL_RCC_APB1_RELEASE_RESET();
201 
202   __HAL_RCC_APB2_FORCE_RESET();
203   __HAL_RCC_APB2_RELEASE_RESET();
204 
205   __HAL_RCC_AHB1_FORCE_RESET();
206   __HAL_RCC_AHB1_RELEASE_RESET();
207 
208   __HAL_RCC_AHB2_FORCE_RESET();
209   __HAL_RCC_AHB2_RELEASE_RESET();
210 
211   __HAL_RCC_AHB3_FORCE_RESET();
212   __HAL_RCC_AHB3_RELEASE_RESET();
213 
214   /* De-Init the low level hardware */
215   HAL_MspDeInit();
216 
217   /* Return function status */
218   return HAL_OK;
219 }
220 
221 /**
222   * @brief  Initialize the MSP.
223   * @retval None
224   */
HAL_MspInit(void)225 __weak void HAL_MspInit(void)
226 {
227   /* NOTE : This function should not be modified, when the callback is needed,
228             the HAL_MspInit could be implemented in the user file
229    */
230 }
231 
232 /**
233   * @brief  DeInitialize the MSP.
234   * @retval None
235   */
HAL_MspDeInit(void)236 __weak void HAL_MspDeInit(void)
237 {
238   /* NOTE : This function should not be modified, when the callback is needed,
239             the HAL_MspDeInit could be implemented in the user file
240    */
241 }
242 
243 /**
244   * @brief This function configures the source of the time base:
245   *        The time source is configured to have 1ms time base with a dedicated
246   *        Tick interrupt priority.
247   * @note This function is called  automatically at the beginning of program after
248   *       reset by HAL_Init() or at any time when clock is reconfigured  by HAL_RCC_ClockConfig().
249   * @note In the default implementation, SysTick timer is the source of time base.
250   *       It is used to generate interrupts at regular time intervals.
251   *       Care must be taken if HAL_Delay() is called from a peripheral ISR process,
252   *       The SysTick interrupt must have higher priority (numerically lower)
253   *       than the peripheral interrupt. Otherwise the caller ISR process will be blocked.
254   *       The function is declared as __weak  to be overwritten  in case of other
255   *       implementation  in user file.
256   * @param TickPriority  Tick interrupt priority.
257   * @retval HAL status
258   */
HAL_InitTick(uint32_t TickPriority)259 __weak HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
260 {
261   HAL_StatusTypeDef  status = HAL_OK;
262 
263   /* Check uwTickFreq for MisraC 2012 (even if uwTickFreq is a enum type that doesn't take the value zero)*/
264   if ((uint32_t)uwTickFreq != 0U)
265   {
266     /*Configure the SysTick to have interrupt in 1ms time basis*/
267     if (HAL_SYSTICK_Config(SystemCoreClock / (1000U / (uint32_t)uwTickFreq)) == 0U)
268     {
269       /* Configure the SysTick IRQ priority */
270       if (TickPriority < (1UL << __NVIC_PRIO_BITS))
271       {
272         HAL_NVIC_SetPriority(SysTick_IRQn, TickPriority, 0U);
273         uwTickPrio = TickPriority;
274       }
275       else
276       {
277         status = HAL_ERROR;
278       }
279     }
280     else
281     {
282       status = HAL_ERROR;
283     }
284   }
285   else
286   {
287     status = HAL_ERROR;
288   }
289 
290   /* Return function status */
291   return status;
292 }
293 
294 /**
295   * @}
296   */
297 
298 /** @defgroup HAL_Exported_Functions_Group2 HAL Control functions
299  *  @brief    HAL Control functions
300  *
301 @verbatim
302  ===============================================================================
303                       ##### HAL Control functions #####
304  ===============================================================================
305     [..]  This section provides functions allowing to:
306       (+) Provide a tick value in millisecond
307       (+) Provide a blocking delay in millisecond
308       (+) Suspend the time base source interrupt
309       (+) Resume the time base source interrupt
310       (+) Get the HAL API driver version
311       (+) Get the device identifier
312       (+) Get the device revision identifier
313 
314 @endverbatim
315   * @{
316   */
317 
318 /**
319   * @brief This function is called to increment a global variable "uwTick"
320   *        used as application time base.
321   * @note In the default implementation, this variable is incremented each 1ms
322   *       in SysTick ISR.
323  * @note This function is declared as __weak to be overwritten in case of other
324   *      implementations in user file.
325   * @retval None
326   */
HAL_IncTick(void)327 __weak void HAL_IncTick(void)
328 {
329   uwTick += (uint32_t)uwTickFreq;
330 }
331 
332 /**
333   * @brief Provide a tick value in millisecond.
334   * @note This function is declared as __weak to be overwritten in case of other
335   *       implementations in user file.
336   * @retval tick value
337   */
HAL_GetTick(void)338 __weak uint32_t HAL_GetTick(void)
339 {
340   return uwTick;
341 }
342 
343 /**
344   * @brief This function returns a tick priority.
345   * @retval tick priority
346   */
HAL_GetTickPrio(void)347 uint32_t HAL_GetTickPrio(void)
348 {
349   return uwTickPrio;
350 }
351 
352 /**
353   * @brief Set new tick Freq.
354   * @param Freq tick frequency
355   * @retval HAL status
356   */
HAL_SetTickFreq(HAL_TickFreqTypeDef Freq)357 HAL_StatusTypeDef HAL_SetTickFreq(HAL_TickFreqTypeDef Freq)
358 {
359   HAL_StatusTypeDef status  = HAL_OK;
360   HAL_TickFreqTypeDef prevTickFreq;
361 
362   if (uwTickFreq != Freq)
363   {
364     /* Back up uwTickFreq frequency */
365     prevTickFreq = uwTickFreq;
366 
367     /* Update uwTickFreq global variable used by HAL_InitTick() */
368     uwTickFreq = Freq;
369 
370     /* Apply the new tick Freq  */
371     status = HAL_InitTick(uwTickPrio);
372     if (status != HAL_OK)
373     {
374       /* Restore previous tick frequency */
375       uwTickFreq = prevTickFreq;
376     }
377   }
378 
379   return status;
380 }
381 
382 /**
383   * @brief Return tick frequency.
384   * @retval Tick frequency.
385   *         Value of @ref HAL_TickFreqTypeDef.
386   */
HAL_GetTickFreq(void)387 HAL_TickFreqTypeDef HAL_GetTickFreq(void)
388 {
389   return uwTickFreq;
390 }
391 
392 /**
393   * @brief This function provides minimum delay (in milliseconds) based
394   *        on variable incremented.
395   * @note In the default implementation , SysTick timer is the source of time base.
396   *       It is used to generate interrupts at regular time intervals where uwTick
397   *       is incremented.
398   * @note This function is declared as __weak to be overwritten in case of other
399   *       implementations in user file.
400   * @param Delay  specifies the delay time length, in milliseconds.
401   * @retval None
402   */
HAL_Delay(uint32_t Delay)403 __weak void HAL_Delay(uint32_t Delay)
404 {
405   uint32_t tickstart = HAL_GetTick();
406   uint32_t wait = Delay;
407 
408   /* Add a period to guaranty minimum wait */
409   if (wait < HAL_MAX_DELAY)
410   {
411     wait += (uint32_t)uwTickFreq;
412   }
413 
414   while ((HAL_GetTick() - tickstart) < wait)
415   {
416   }
417 }
418 
419 /**
420   * @brief Suspend Tick increment.
421   * @note In the default implementation , SysTick timer is the source of time base. It is
422   *       used to generate interrupts at regular time intervals. Once HAL_SuspendTick()
423   *       is called, the SysTick interrupt will be disabled and so Tick increment
424   *       is suspended.
425   * @note This function is declared as __weak to be overwritten in case of other
426   *       implementations in user file.
427   * @retval None
428   */
HAL_SuspendTick(void)429 __weak void HAL_SuspendTick(void)
430 {
431   /* Disable SysTick Interrupt */
432   SysTick->CTRL &= ~SysTick_CTRL_TICKINT_Msk;
433 }
434 
435 /**
436   * @brief Resume Tick increment.
437   * @note In the default implementation , SysTick timer is the source of time base. It is
438   *       used to generate interrupts at regular time intervals. Once HAL_ResumeTick()
439   *       is called, the SysTick interrupt will be enabled and so Tick increment
440   *       is resumed.
441   * @note This function is declared as __weak to be overwritten in case of other
442   *       implementations in user file.
443   * @retval None
444   */
HAL_ResumeTick(void)445 __weak void HAL_ResumeTick(void)
446 {
447   /* Enable SysTick Interrupt */
448   SysTick->CTRL  |= SysTick_CTRL_TICKINT_Msk;
449 }
450 
451 /**
452   * @brief  Return the HAL revision.
453   * @retval version : 0xXYZR (8bits for each decimal, R for RC)
454   */
HAL_GetHalVersion(void)455 uint32_t HAL_GetHalVersion(void)
456 {
457   return STM32L4XX_HAL_VERSION;
458 }
459 
460 /**
461   * @brief  Return the device revision identifier.
462   * @retval Device revision identifier
463   */
HAL_GetREVID(void)464 uint32_t HAL_GetREVID(void)
465 {
466   return((DBGMCU->IDCODE & DBGMCU_IDCODE_REV_ID) >> 16);
467 }
468 
469 /**
470   * @brief  Return the device identifier.
471   * @retval Device identifier
472   */
HAL_GetDEVID(void)473 uint32_t HAL_GetDEVID(void)
474 {
475   return(DBGMCU->IDCODE & DBGMCU_IDCODE_DEV_ID);
476 }
477 
478 /**
479   * @brief  Return the first word of the unique device identifier (UID based on 96 bits)
480   * @retval Device identifier
481   */
HAL_GetUIDw0(void)482 uint32_t HAL_GetUIDw0(void)
483 {
484   return(READ_REG(*((uint32_t *)UID_BASE)));
485 }
486 
487 /**
488   * @brief  Return the second word of the unique device identifier (UID based on 96 bits)
489   * @retval Device identifier
490   */
HAL_GetUIDw1(void)491 uint32_t HAL_GetUIDw1(void)
492 {
493   return(READ_REG(*((uint32_t *)(UID_BASE + 4U))));
494 }
495 
496 /**
497   * @brief  Return the third word of the unique device identifier (UID based on 96 bits)
498   * @retval Device identifier
499   */
HAL_GetUIDw2(void)500 uint32_t HAL_GetUIDw2(void)
501 {
502   return(READ_REG(*((uint32_t *)(UID_BASE + 8U))));
503 }
504 
505 /**
506   * @}
507   */
508 
509 /** @defgroup HAL_Exported_Functions_Group3 HAL Debug functions
510  *  @brief    HAL Debug functions
511  *
512 @verbatim
513  ===============================================================================
514                       ##### HAL Debug functions #####
515  ===============================================================================
516     [..]  This section provides functions allowing to:
517       (+) Enable/Disable Debug module during SLEEP mode
518       (+) Enable/Disable Debug module during STOP0/STOP1/STOP2 modes
519       (+) Enable/Disable Debug module during STANDBY mode
520 
521 @endverbatim
522   * @{
523   */
524 
525 /**
526   * @brief  Enable the Debug Module during SLEEP mode.
527   * @retval None
528   */
HAL_DBGMCU_EnableDBGSleepMode(void)529 void HAL_DBGMCU_EnableDBGSleepMode(void)
530 {
531   SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_SLEEP);
532 }
533 
534 /**
535   * @brief  Disable the Debug Module during SLEEP mode.
536   * @retval None
537   */
HAL_DBGMCU_DisableDBGSleepMode(void)538 void HAL_DBGMCU_DisableDBGSleepMode(void)
539 {
540   CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_SLEEP);
541 }
542 
543 /**
544   * @brief  Enable the Debug Module during STOP0/STOP1/STOP2 modes.
545   * @retval None
546   */
HAL_DBGMCU_EnableDBGStopMode(void)547 void HAL_DBGMCU_EnableDBGStopMode(void)
548 {
549   SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
550 }
551 
552 /**
553   * @brief  Disable the Debug Module during STOP0/STOP1/STOP2 modes.
554   * @retval None
555   */
HAL_DBGMCU_DisableDBGStopMode(void)556 void HAL_DBGMCU_DisableDBGStopMode(void)
557 {
558   CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
559 }
560 
561 /**
562   * @brief  Enable the Debug Module during STANDBY mode.
563   * @retval None
564   */
HAL_DBGMCU_EnableDBGStandbyMode(void)565 void HAL_DBGMCU_EnableDBGStandbyMode(void)
566 {
567   SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
568 }
569 
570 /**
571   * @brief  Disable the Debug Module during STANDBY mode.
572   * @retval None
573   */
HAL_DBGMCU_DisableDBGStandbyMode(void)574 void HAL_DBGMCU_DisableDBGStandbyMode(void)
575 {
576   CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
577 }
578 
579 /**
580   * @}
581   */
582 
583 /** @defgroup HAL_Exported_Functions_Group4 HAL SYSCFG configuration functions
584  *  @brief    HAL SYSCFG configuration functions
585  *
586 @verbatim
587  ===============================================================================
588                       ##### HAL SYSCFG configuration functions #####
589  ===============================================================================
590     [..]  This section provides functions allowing to:
591       (+) Start a hardware SRAM2 erase operation
592       (+) Enable/Disable the Internal FLASH Bank Swapping
593       (+) Configure the Voltage reference buffer
594       (+) Enable/Disable the Voltage reference buffer
595       (+) Enable/Disable the I/O analog switch voltage booster
596 
597 @endverbatim
598   * @{
599   */
600 
601 /**
602   * @brief  Start a hardware SRAM2 erase operation.
603   * @note   As long as SRAM2 is not erased the SRAM2ER bit will be set.
604   *         This bit is automatically reset at the end of the SRAM2 erase operation.
605   * @retval None
606   */
HAL_SYSCFG_SRAM2Erase(void)607 void HAL_SYSCFG_SRAM2Erase(void)
608 {
609   /* unlock the write protection of the SRAM2ER bit */
610   SYSCFG->SKR = 0xCA;
611   SYSCFG->SKR = 0x53;
612   /* Starts a hardware SRAM2 erase operation*/
613   *(__IO uint32_t *) SCSR_SRAM2ER_BB = 0x00000001UL;
614 }
615 
616 /**
617   * @brief  Enable the Internal FLASH Bank Swapping.
618   *
619   * @note   This function can be used only for STM32L4xx devices.
620   *
621   * @note   Flash Bank2 mapped at 0x08000000 (and aliased @0x00000000)
622   *         and Flash Bank1 mapped at 0x08100000 (and aliased at 0x00100000)
623   *
624   * @retval None
625   */
HAL_SYSCFG_EnableMemorySwappingBank(void)626 void HAL_SYSCFG_EnableMemorySwappingBank(void)
627 {
628   *(__IO uint32_t *)FB_MODE_BB = 0x00000001UL;
629 }
630 
631 /**
632   * @brief  Disable the Internal FLASH Bank Swapping.
633   *
634   * @note   This function can be used only for STM32L4xx devices.
635   *
636   * @note   The default state : Flash Bank1 mapped at 0x08000000 (and aliased @0x0000 0000)
637   *         and Flash Bank2 mapped at 0x08100000 (and aliased at 0x00100000)
638   *
639   * @retval None
640   */
HAL_SYSCFG_DisableMemorySwappingBank(void)641 void HAL_SYSCFG_DisableMemorySwappingBank(void)
642 {
643 
644   *(__IO uint32_t *)FB_MODE_BB = 0x00000000UL;
645 }
646 
647 #if defined(VREFBUF)
648 /**
649   * @brief Configure the internal voltage reference buffer voltage scale.
650   * @param VoltageScaling  specifies the output voltage to achieve
651   *          This parameter can be one of the following values:
652   *            @arg SYSCFG_VREFBUF_VOLTAGE_SCALE0: VREF_OUT1 around 2.048 V.
653   *                                                This requires VDDA equal to or higher than 2.4 V.
654   *            @arg SYSCFG_VREFBUF_VOLTAGE_SCALE1: VREF_OUT2 around 2.5 V.
655   *                                                This requires VDDA equal to or higher than 2.8 V.
656   * @retval None
657   */
HAL_SYSCFG_VREFBUF_VoltageScalingConfig(uint32_t VoltageScaling)658 void HAL_SYSCFG_VREFBUF_VoltageScalingConfig(uint32_t VoltageScaling)
659 {
660   /* Check the parameters */
661   assert_param(IS_SYSCFG_VREFBUF_VOLTAGE_SCALE(VoltageScaling));
662 
663   MODIFY_REG(VREFBUF->CSR, VREFBUF_CSR_VRS, VoltageScaling);
664 }
665 
666 /**
667   * @brief Configure the internal voltage reference buffer high impedance mode.
668   * @param Mode  specifies the high impedance mode
669   *          This parameter can be one of the following values:
670   *            @arg SYSCFG_VREFBUF_HIGH_IMPEDANCE_DISABLE: VREF+ pin is internally connect to VREFINT output.
671   *            @arg SYSCFG_VREFBUF_HIGH_IMPEDANCE_ENABLE: VREF+ pin is high impedance.
672   * @retval None
673   */
HAL_SYSCFG_VREFBUF_HighImpedanceConfig(uint32_t Mode)674 void HAL_SYSCFG_VREFBUF_HighImpedanceConfig(uint32_t Mode)
675 {
676   /* Check the parameters */
677   assert_param(IS_SYSCFG_VREFBUF_HIGH_IMPEDANCE(Mode));
678 
679   MODIFY_REG(VREFBUF->CSR, VREFBUF_CSR_HIZ, Mode);
680 }
681 
682 /**
683   * @brief  Tune the Internal Voltage Reference buffer (VREFBUF).
684   * @retval None
685   */
HAL_SYSCFG_VREFBUF_TrimmingConfig(uint32_t TrimmingValue)686 void HAL_SYSCFG_VREFBUF_TrimmingConfig(uint32_t TrimmingValue)
687 {
688   /* Check the parameters */
689   assert_param(IS_SYSCFG_VREFBUF_TRIMMING(TrimmingValue));
690 
691   MODIFY_REG(VREFBUF->CCR, VREFBUF_CCR_TRIM, TrimmingValue);
692 }
693 
694 /**
695   * @brief  Enable the Internal Voltage Reference buffer (VREFBUF).
696   * @retval HAL_OK/HAL_TIMEOUT
697   */
HAL_SYSCFG_EnableVREFBUF(void)698 HAL_StatusTypeDef HAL_SYSCFG_EnableVREFBUF(void)
699 {
700   uint32_t  tickstart;
701 
702   SET_BIT(VREFBUF->CSR, VREFBUF_CSR_ENVR);
703 
704   /* Get Start Tick*/
705   tickstart = HAL_GetTick();
706 
707   /* Wait for VRR bit  */
708   while(READ_BIT(VREFBUF->CSR, VREFBUF_CSR_VRR) == 0U)
709   {
710     if((HAL_GetTick() - tickstart) > VREFBUF_TIMEOUT_VALUE)
711     {
712       return HAL_TIMEOUT;
713     }
714   }
715 
716   return HAL_OK;
717 }
718 
719 /**
720   * @brief  Disable the Internal Voltage Reference buffer (VREFBUF).
721   *
722   * @retval None
723   */
HAL_SYSCFG_DisableVREFBUF(void)724 void HAL_SYSCFG_DisableVREFBUF(void)
725 {
726   CLEAR_BIT(VREFBUF->CSR, VREFBUF_CSR_ENVR);
727 }
728 #endif /* VREFBUF */
729 
730 /**
731   * @brief  Enable the I/O analog switch voltage booster
732   *
733   * @retval None
734   */
HAL_SYSCFG_EnableIOAnalogSwitchBooster(void)735 void HAL_SYSCFG_EnableIOAnalogSwitchBooster(void)
736 {
737   SET_BIT(SYSCFG->CFGR1, SYSCFG_CFGR1_BOOSTEN);
738 }
739 
740 /**
741   * @brief  Disable the I/O analog switch voltage booster
742   *
743   * @retval None
744   */
HAL_SYSCFG_DisableIOAnalogSwitchBooster(void)745 void HAL_SYSCFG_DisableIOAnalogSwitchBooster(void)
746 {
747   CLEAR_BIT(SYSCFG->CFGR1, SYSCFG_CFGR1_BOOSTEN);
748 }
749 
750 /**
751   * @}
752   */
753 
754 /**
755   * @}
756   */
757 
758 #endif /* HAL_MODULE_ENABLED */
759 /**
760   * @}
761   */
762 
763 /**
764   * @}
765   */
766