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