1 /**
2   ******************************************************************************
3   * @file    stm32l5xx_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) 2019 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 "stm32l5xx_hal.h"
37 
38 /** @addtogroup STM32L5xx_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 STM32L5xx HAL Driver version number
53   */
54 #define STM32L5XX_HAL_VERSION_MAIN   (0x01U) /*!< [31:24] main version */
55 #define STM32L5XX_HAL_VERSION_SUB1   (0x00U) /*!< [23:16] sub1 version */
56 #define STM32L5XX_HAL_VERSION_SUB2   (0x05U) /*!< [15:8]  sub2 version */
57 #define STM32L5XX_HAL_VERSION_RC     (0x00U) /*!< [7:0]  release candidate */
58 #define STM32L5XX_HAL_VERSION        ((STM32L5XX_HAL_VERSION_MAIN  << 24U)\
59                                       |(STM32L5XX_HAL_VERSION_SUB1 << 16U)\
60                                       |(STM32L5XX_HAL_VERSION_SUB2 << 8U )\
61                                       |(STM32L5XX_HAL_VERSION_RC))
62 
63 #define VREFBUF_TIMEOUT_VALUE        10U   /*!<  10 ms (to be confirmed) */
64 #define VREFBUF_SC0_CAL_ADDR         ((uint8_t *) (0x0BFA0579UL)) /*!<  Address of VREFBUF trimming value for VRS=0,
65                                                                         VREF_SC0 in STM32L5 datasheet */
66 #define VREFBUF_SC1_CAL_ADDR         ((uint8_t *) (0x0BFA0530UL)) /*!<  Address of VREFBUF trimming value for VRS=1,
67                                                                         VREF_SC1 in STM32L5 datasheet */
68 
69 /* Private macro -------------------------------------------------------------*/
70 /* Private variables ---------------------------------------------------------*/
71 /* Private function prototypes -----------------------------------------------*/
72 
73 /* Exported variables --------------------------------------------------------*/
74 
75 /** @defgroup HAL_Exported_Variables HAL Exported Variables
76   * @{
77   */
78 __IO uint32_t uwTick;
79 uint32_t uwTickPrio = (1UL << __NVIC_PRIO_BITS); /* Invalid priority */
80 HAL_TickFreqTypeDef uwTickFreq = HAL_TICK_FREQ_DEFAULT;  /* 1KHz */
81 /**
82   * @}
83   */
84 
85 /* Exported functions --------------------------------------------------------*/
86 
87 /** @defgroup HAL_Exported_Functions HAL Exported Functions
88   * @{
89   */
90 
91 /** @defgroup HAL_Exported_Functions_Group1 Initialization and de-initialization Functions
92   *  @brief    Initialization and de-initialization functions
93   *
94 @verbatim
95  ===============================================================================
96               ##### Initialization and de-initialization functions #####
97  ===============================================================================
98     [..]  This section provides functions allowing to:
99       (+) Initialize the Flash interface the NVIC allocation and initial time base
100           clock configuration.
101       (+) De-initialize common part of the HAL.
102       (+) Configure the time base source to have 1ms time base with a dedicated
103           Tick interrupt priority.
104         (++) SysTick timer is used by default as source of time base, but user
105              can eventually implement his proper time base source (a general purpose
106              timer for example or other time source), keeping in mind that Time base
107              duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and
108              handled in milliseconds basis.
109         (++) Time base configuration function (HAL_InitTick ()) is called automatically
110              at the beginning of the program after reset by HAL_Init() or at any time
111              when clock is configured, by HAL_RCC_ClockConfig().
112         (++) Source of time base is configured  to generate interrupts at regular
113              time intervals. Care must be taken if HAL_Delay() is called from a
114              peripheral ISR process, the Tick interrupt line must have higher priority
115             (numerically lower) than the peripheral interrupt. Otherwise the caller
116             ISR process will be blocked.
117        (++) functions affecting time base configurations are declared as __weak
118              to make  override possible  in case of other  implementations in user file.
119 @endverbatim
120   * @{
121   */
122 
123 /**
124   * @brief  Configure the time base source, NVIC and any required global low level hardware
125   *         by calling the HAL_MspInit() callback function to be optionally defined in user file
126   *         stm32l5xx_hal_msp.c.
127   *
128   * @note   HAL_Init() function is called at the beginning of program after reset and before
129   *         the clock configuration.
130   *
131   * @note   In the default implementation the System Timer (Systick) is used as source of time base.
132   *         The Systick configuration is based on MSI clock, as MSI is the clock
133   *         used after a system Reset and the NVIC configuration is set to Priority group 4.
134   *         Once done, time base tick starts incrementing: the tick variable counter is incremented
135   *         each 1ms in the SysTick_Handler() interrupt handler.
136   *
137   * @retval HAL status
138   */
HAL_Init(void)139 HAL_StatusTypeDef HAL_Init(void)
140 {
141   HAL_StatusTypeDef  status = HAL_OK;
142 
143   /* Set Interrupt Group Priority */
144   HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_3);
145 
146   /* Insure time base clock coherency */
147   SystemCoreClockUpdate();
148 
149   /* Use SysTick as time base source and configure 1ms tick (default clock after Reset is MSI) */
150   if (HAL_InitTick(TICK_INT_PRIORITY) != HAL_OK)
151   {
152     status = HAL_ERROR;
153   }
154   else
155   {
156     /* Init the low level hardware */
157     HAL_MspInit();
158   }
159 
160   /* Return function status */
161   return status;
162 }
163 
164 /**
165   * @brief DeInitialize common part of the HAL and stop the source of time base.
166   * @note This function is optional.
167   * @retval HAL status
168   */
HAL_DeInit(void)169 HAL_StatusTypeDef HAL_DeInit(void)
170 {
171   /* Reset of all peripherals */
172   __HAL_RCC_APB1_FORCE_RESET();
173   __HAL_RCC_APB1_RELEASE_RESET();
174 
175   __HAL_RCC_APB2_FORCE_RESET();
176   __HAL_RCC_APB2_RELEASE_RESET();
177 
178   __HAL_RCC_AHB1_FORCE_RESET();
179   __HAL_RCC_AHB1_RELEASE_RESET();
180 
181   __HAL_RCC_AHB2_FORCE_RESET();
182   __HAL_RCC_AHB2_RELEASE_RESET();
183 
184   __HAL_RCC_AHB3_FORCE_RESET();
185   __HAL_RCC_AHB3_RELEASE_RESET();
186 
187   /* De-Init the low level hardware */
188   HAL_MspDeInit();
189 
190   /* Return function status */
191   return HAL_OK;
192 }
193 
194 /**
195   * @brief  Initialize the MSP.
196   * @retval None
197   */
HAL_MspInit(void)198 __weak void HAL_MspInit(void)
199 {
200   /* NOTE : This function should not be modified, when the callback is needed,
201             the HAL_MspInit could be implemented in the user file
202    */
203 }
204 
205 /**
206   * @brief  DeInitialize the MSP.
207   * @retval None
208   */
HAL_MspDeInit(void)209 __weak void HAL_MspDeInit(void)
210 {
211   /* NOTE : This function should not be modified, when the callback is needed,
212             the HAL_MspDeInit could be implemented in the user file
213    */
214 }
215 
216 /**
217   * @brief This function configures the source of the time base:
218   *        The time source is configured to have 1ms time base with a dedicated
219   *        Tick interrupt priority.
220   * @note This function is called  automatically at the beginning of program after
221   *       reset by HAL_Init() or at any time when clock is reconfigured  by HAL_RCC_ClockConfig().
222   * @note In the default implementation, SysTick timer is the source of time base.
223   *       It is used to generate interrupts at regular time intervals.
224   *       Care must be taken if HAL_Delay() is called from a peripheral ISR process,
225   *       The SysTick interrupt must have higher priority (numerically lower)
226   *       than the peripheral interrupt. Otherwise the caller ISR process will be blocked.
227   *       The function is declared as __weak  to be overwritten  in case of other
228   *       implementation  in user file.
229   * @param TickPriority  Tick interrupt priority.
230   * @retval HAL status
231   */
HAL_InitTick(uint32_t TickPriority)232 __weak HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
233 {
234   HAL_StatusTypeDef  status = HAL_OK;
235 
236   /* Check uwTickFreq for MisraC 2012 (even if uwTickFreq is a enum type that doesn't take the value zero)*/
237   if ((uint32_t)uwTickFreq != 0U)
238   {
239     /*Configure the SysTick to have interrupt in 1ms time basis*/
240     if (HAL_SYSTICK_Config(SystemCoreClock / (1000U / (uint32_t)uwTickFreq)) == 0U)
241     {
242       /* Configure the SysTick IRQ priority */
243       if (TickPriority < (1UL << __NVIC_PRIO_BITS))
244       {
245         HAL_NVIC_SetPriority(SysTick_IRQn, TickPriority, 0U);
246         uwTickPrio = TickPriority;
247       }
248       else
249       {
250         status = HAL_ERROR;
251       }
252     }
253     else
254     {
255       status = HAL_ERROR;
256     }
257   }
258   else
259   {
260     status = HAL_ERROR;
261   }
262 
263   /* Return function status */
264   return status;
265 }
266 
267 /**
268   * @}
269   */
270 
271 /** @defgroup HAL_Exported_Functions_Group2 HAL Control functions
272   *  @brief    HAL Control functions
273   *
274 @verbatim
275  ===============================================================================
276                       ##### HAL Control functions #####
277  ===============================================================================
278     [..]  This section provides functions allowing to:
279       (+) Provide a tick value in millisecond
280       (+) Provide a blocking delay in millisecond
281       (+) Suspend the time base source interrupt
282       (+) Resume the time base source interrupt
283       (+) Get the HAL API driver version
284       (+) Get the device identifier
285       (+) Get the device revision identifier
286 
287 @endverbatim
288   * @{
289   */
290 
291 /**
292   * @brief This function is called to increment a global variable "uwTick"
293   *        used as application time base.
294   * @note In the default implementation, this variable is incremented each 1ms
295   *       in SysTick ISR.
296   * @note This function is declared as __weak to be overwritten in case of other
297   *      implementations in user file.
298   * @retval None
299   */
HAL_IncTick(void)300 __weak void HAL_IncTick(void)
301 {
302   uwTick += (uint32_t)uwTickFreq;
303 }
304 
305 /**
306   * @brief Provide a tick value in millisecond.
307   * @note This function is declared as __weak to be overwritten in case of other
308   *       implementations in user file.
309   * @retval tick value
310   */
HAL_GetTick(void)311 __weak uint32_t HAL_GetTick(void)
312 {
313   return uwTick;
314 }
315 
316 /**
317   * @brief This function returns a tick priority.
318   * @retval tick priority
319   */
HAL_GetTickPrio(void)320 uint32_t HAL_GetTickPrio(void)
321 {
322   return uwTickPrio;
323 }
324 
325 /**
326   * @brief Set new tick Freq.
327   * @param Freq tick frequency
328   * @retval HAL status
329   */
HAL_SetTickFreq(HAL_TickFreqTypeDef Freq)330 HAL_StatusTypeDef HAL_SetTickFreq(HAL_TickFreqTypeDef Freq)
331 {
332   HAL_StatusTypeDef status  = HAL_OK;
333   HAL_TickFreqTypeDef prevTickFreq;
334 
335   if (uwTickFreq != Freq)
336   {
337     /* Back up uwTickFreq frequency */
338     prevTickFreq = uwTickFreq;
339 
340     /* Update uwTickFreq global variable used by HAL_InitTick() */
341     uwTickFreq = Freq;
342 
343     /* Apply the new tick Freq  */
344     status = HAL_InitTick(uwTickPrio);
345     if (status != HAL_OK)
346     {
347       /* Restore previous tick frequency */
348       uwTickFreq = prevTickFreq;
349     }
350   }
351 
352   return status;
353 }
354 
355 /**
356   * @brief Return tick frequency.
357   * @retval tick period in Hz
358   */
HAL_GetTickFreq(void)359 HAL_TickFreqTypeDef HAL_GetTickFreq(void)
360 {
361   return uwTickFreq;
362 }
363 
364 /**
365   * @brief This function provides minimum delay (in milliseconds) based
366   *        on variable incremented.
367   * @note In the default implementation , SysTick timer is the source of time base.
368   *       It is used to generate interrupts at regular time intervals where uwTick
369   *       is incremented.
370   * @note This function is declared as __weak to be overwritten in case of other
371   *       implementations in user file.
372   * @param Delay  specifies the delay time length, in milliseconds.
373   * @retval None
374   */
HAL_Delay(uint32_t Delay)375 __weak void HAL_Delay(uint32_t Delay)
376 {
377   uint32_t tickstart = HAL_GetTick();
378   uint32_t wait = Delay;
379 
380   /* Add a period to guaranty minimum wait */
381   if (wait < HAL_MAX_DELAY)
382   {
383     wait += (uint32_t)uwTickFreq;
384   }
385 
386   while ((HAL_GetTick() - tickstart) < wait)
387   {
388   }
389 }
390 
391 /**
392   * @brief Suspend Tick increment.
393   * @note In the default implementation , SysTick timer is the source of time base. It is
394   *       used to generate interrupts at regular time intervals. Once HAL_SuspendTick()
395   *       is called, the SysTick interrupt will be disabled and so Tick increment
396   *       is suspended.
397   * @note This function is declared as __weak to be overwritten in case of other
398   *       implementations in user file.
399   * @retval None
400   */
HAL_SuspendTick(void)401 __weak void HAL_SuspendTick(void)
402 {
403   /* Disable SysTick Interrupt */
404   SysTick->CTRL &= ~SysTick_CTRL_TICKINT_Msk;
405 }
406 
407 /**
408   * @brief Resume Tick increment.
409   * @note In the default implementation , SysTick timer is the source of time base. It is
410   *       used to generate interrupts at regular time intervals. Once HAL_ResumeTick()
411   *       is called, the SysTick interrupt will be enabled and so Tick increment
412   *       is resumed.
413   * @note This function is declared as __weak to be overwritten in case of other
414   *       implementations in user file.
415   * @retval None
416   */
HAL_ResumeTick(void)417 __weak void HAL_ResumeTick(void)
418 {
419   /* Enable SysTick Interrupt */
420   SysTick->CTRL  |= SysTick_CTRL_TICKINT_Msk;
421 }
422 
423 /**
424   * @brief  Return the HAL revision.
425   * @retval version : 0xXYZR (8bits for each decimal, R for RC)
426   */
HAL_GetHalVersion(void)427 uint32_t HAL_GetHalVersion(void)
428 {
429   return STM32L5XX_HAL_VERSION;
430 }
431 
432 /**
433   * @brief  Return the device revision identifier.
434   * @retval Device revision identifier
435   */
HAL_GetREVID(void)436 uint32_t HAL_GetREVID(void)
437 {
438   return ((DBGMCU->IDCODE & DBGMCU_IDCODE_REV_ID) >> DBGMCU_IDCODE_REV_ID_Pos);
439 }
440 
441 /**
442   * @brief  Return the device identifier.
443   * @retval Device identifier
444   */
HAL_GetDEVID(void)445 uint32_t HAL_GetDEVID(void)
446 {
447   return (DBGMCU->IDCODE & DBGMCU_IDCODE_DEV_ID);
448 }
449 
450 /**
451   * @brief  Return the first word of the unique device identifier (UID based on 96 bits)
452   * @retval Device identifier
453   */
HAL_GetUIDw0(void)454 uint32_t HAL_GetUIDw0(void)
455 {
456   return (READ_REG(*((uint32_t *)UID_BASE)));
457 }
458 
459 /**
460   * @brief  Return the second word of the unique device identifier (UID based on 96 bits)
461   * @retval Device identifier
462   */
HAL_GetUIDw1(void)463 uint32_t HAL_GetUIDw1(void)
464 {
465   return (READ_REG(*((uint32_t *)(UID_BASE + 4U))));
466 }
467 
468 /**
469   * @brief  Return the third word of the unique device identifier (UID based on 96 bits)
470   * @retval Device identifier
471   */
HAL_GetUIDw2(void)472 uint32_t HAL_GetUIDw2(void)
473 {
474   return (READ_REG(*((uint32_t *)(UID_BASE + 8U))));
475 }
476 
477 /**
478   * @}
479   */
480 
481 /** @defgroup HAL_Exported_Functions_Group3 HAL Debug functions
482   *  @brief    HAL Debug functions
483   *
484 @verbatim
485  ===============================================================================
486                       ##### HAL Debug functions #####
487  ===============================================================================
488     [..]  This section provides functions allowing to:
489       (+) Enable/Disable Debug module during STOP0/STOP1/STOP2 modes
490       (+) Enable/Disable Debug module during STANDBY mode
491 
492 @endverbatim
493   * @{
494   */
495 
496 /**
497   * @brief  Enable the Debug Module during STOP0/STOP1/STOP2 modes.
498   * @retval None
499   */
HAL_DBGMCU_EnableDBGStopMode(void)500 void HAL_DBGMCU_EnableDBGStopMode(void)
501 {
502   SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
503 }
504 
505 /**
506   * @brief  Disable the Debug Module during STOP0/STOP1/STOP2 modes.
507   * @retval None
508   */
HAL_DBGMCU_DisableDBGStopMode(void)509 void HAL_DBGMCU_DisableDBGStopMode(void)
510 {
511   CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
512 }
513 
514 /**
515   * @brief  Enable the Debug Module during STANDBY mode.
516   * @retval None
517   */
HAL_DBGMCU_EnableDBGStandbyMode(void)518 void HAL_DBGMCU_EnableDBGStandbyMode(void)
519 {
520   SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
521 }
522 
523 /**
524   * @brief  Disable the Debug Module during STANDBY mode.
525   * @retval None
526   */
HAL_DBGMCU_DisableDBGStandbyMode(void)527 void HAL_DBGMCU_DisableDBGStandbyMode(void)
528 {
529   CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
530 }
531 
532 /**
533   * @}
534   */
535 
536 /** @defgroup HAL_Exported_Functions_Group4 HAL SYSCFG configuration functions
537   *  @brief    HAL SYSCFG configuration functions
538   *
539 @verbatim
540  ===============================================================================
541                       ##### HAL SYSCFG configuration functions #####
542  ===============================================================================
543     [..]  This section provides functions allowing to:
544       (+) Start a hardware SRAM2 erase operation
545       (+) Configure the Voltage reference buffer
546       (+) Enable/Disable the Voltage reference buffer
547       (+) Enable/Disable the I/O analog switch voltage booster
548       (+) Enable/Disable the I/O analog switch supplied by VDD
549 
550 @endverbatim
551   * @{
552   */
553 
554 /**
555   * @brief  Start a hardware SRAM2 erase operation.
556   * @note   As long as SRAM2 is not erased the SRAM2ER bit will be set.
557   *         This bit is automatically reset at the end of the SRAM2 erase operation.
558   * @retval None
559   */
HAL_SYSCFG_SRAM2Erase(void)560 void HAL_SYSCFG_SRAM2Erase(void)
561 {
562   /* unlock the write protection of the SRAM2ER bit */
563   SYSCFG->SKR = 0xCA;
564   SYSCFG->SKR = 0x53;
565 
566   /* Starts a hardware SRAM2 erase operation*/
567   SET_BIT(SYSCFG->SCSR, SYSCFG_SCSR_SRAM2ER);
568 }
569 
570 /**
571   * @brief Configure the internal voltage reference buffer voltage scale.
572   * @param VoltageScaling  specifies the output voltage to achieve
573   *          This parameter can be one of the following values:
574   *            @arg SYSCFG_VREFBUF_VOLTAGE_SCALE0: VREF_OUT1 around 2.048 V.
575   *                                                This requires VDDA equal to or higher than 2.4 V.
576   *            @arg SYSCFG_VREFBUF_VOLTAGE_SCALE1: VREF_OUT1 around 2.5 V.
577   *                                                This requires VDDA equal to or higher than 2.8 V.
578   * @note   Retrieve the TrimmingValue from factory located at
579   *         VREFBUF_SC0_CAL_ADDR or VREFBUF_SC1_CAL_ADDR addresses.
580   * @retval None
581   */
HAL_SYSCFG_VREFBUF_VoltageScalingConfig(uint32_t VoltageScaling)582 void HAL_SYSCFG_VREFBUF_VoltageScalingConfig(uint32_t VoltageScaling)
583 {
584   uint32_t TrimmingValue;
585 
586   /* Check the parameters */
587   assert_param(IS_SYSCFG_VREFBUF_VOLTAGE_SCALE(VoltageScaling));
588 
589   MODIFY_REG(VREFBUF->CSR, VREFBUF_CSR_VRS, VoltageScaling);
590 
591   /* Restrieve Calibration data and store them into trimming field */
592   if (VoltageScaling == SYSCFG_VREFBUF_VOLTAGE_SCALE0)
593   {
594     TrimmingValue = ((uint32_t) *VREFBUF_SC0_CAL_ADDR) & 0x3FU;
595   }
596   else
597   {
598     TrimmingValue = ((uint32_t) *VREFBUF_SC1_CAL_ADDR) & 0x3FU;
599   }
600   assert_param(IS_SYSCFG_VREFBUF_TRIMMING(TrimmingValue));
601 
602   HAL_SYSCFG_VREFBUF_TrimmingConfig(TrimmingValue);
603 }
604 
605 /**
606   * @brief Configure the internal voltage reference buffer high impedance mode.
607   * @param Mode  specifies the high impedance mode
608   *          This parameter can be one of the following values:
609   *            @arg SYSCFG_VREFBUF_HIGH_IMPEDANCE_DISABLE: VREF+ pin is internally connect to VREFINT output.
610   *            @arg SYSCFG_VREFBUF_HIGH_IMPEDANCE_ENABLE: VREF+ pin is high impedance.
611   * @retval None
612   */
HAL_SYSCFG_VREFBUF_HighImpedanceConfig(uint32_t Mode)613 void HAL_SYSCFG_VREFBUF_HighImpedanceConfig(uint32_t Mode)
614 {
615   /* Check the parameters */
616   assert_param(IS_SYSCFG_VREFBUF_HIGH_IMPEDANCE(Mode));
617 
618   MODIFY_REG(VREFBUF->CSR, VREFBUF_CSR_HIZ, Mode);
619 }
620 
621 /**
622   * @brief  Tune the Internal Voltage Reference buffer (VREFBUF).
623   * @retval None
624   */
HAL_SYSCFG_VREFBUF_TrimmingConfig(uint32_t TrimmingValue)625 void HAL_SYSCFG_VREFBUF_TrimmingConfig(uint32_t TrimmingValue)
626 {
627   /* Check the parameters */
628   assert_param(IS_SYSCFG_VREFBUF_TRIMMING(TrimmingValue));
629 
630   MODIFY_REG(VREFBUF->CCR, VREFBUF_CCR_TRIM, TrimmingValue);
631 }
632 
633 /**
634   * @brief  Enable the Internal Voltage Reference buffer (VREFBUF).
635   * @retval HAL_OK/HAL_TIMEOUT
636   */
HAL_SYSCFG_EnableVREFBUF(void)637 HAL_StatusTypeDef HAL_SYSCFG_EnableVREFBUF(void)
638 {
639   uint32_t  tickstart;
640 
641   SET_BIT(VREFBUF->CSR, VREFBUF_CSR_ENVR);
642 
643   /* Get Start Tick*/
644   tickstart = HAL_GetTick();
645 
646   /* Wait for VRR bit  */
647   while (READ_BIT(VREFBUF->CSR, VREFBUF_CSR_VRR) == 0U)
648   {
649     if ((HAL_GetTick() - tickstart) > VREFBUF_TIMEOUT_VALUE)
650     {
651       return HAL_TIMEOUT;
652     }
653   }
654 
655   return HAL_OK;
656 }
657 
658 /**
659   * @brief  Disable the Internal Voltage Reference buffer (VREFBUF).
660   *
661   * @retval None
662   */
HAL_SYSCFG_DisableVREFBUF(void)663 void HAL_SYSCFG_DisableVREFBUF(void)
664 {
665   CLEAR_BIT(VREFBUF->CSR, VREFBUF_CSR_ENVR);
666 }
667 
668 /**
669   * @brief  Enable the I/O analog switch voltage booster
670   * @note   Insure low VDDA voltage operation with I/O analog switch control
671   * @retval None
672   */
HAL_SYSCFG_EnableIOAnalogBooster(void)673 void HAL_SYSCFG_EnableIOAnalogBooster(void)
674 {
675   MODIFY_REG(SYSCFG->CFGR1, (SYSCFG_CFGR1_BOOSTEN | SYSCFG_CFGR1_ANASWVDD), SYSCFG_CFGR1_BOOSTEN);
676 }
677 
678 /**
679   * @brief  Disable the I/O analog switch voltage booster
680   *
681   * @retval None
682   */
HAL_SYSCFG_DisableIOAnalogBooster(void)683 void HAL_SYSCFG_DisableIOAnalogBooster(void)
684 {
685   CLEAR_BIT(SYSCFG->CFGR1, SYSCFG_CFGR1_BOOSTEN);
686 }
687 
688 /**
689   * @brief  Enable the I/O analog switch supplied by VDD
690   * @note   To be used when I/O analog switch voltage booster is not enabled
691   * @retval None
692   */
HAL_SYSCFG_EnableIOAnalogSwitchVdd(void)693 void HAL_SYSCFG_EnableIOAnalogSwitchVdd(void)
694 {
695   MODIFY_REG(SYSCFG->CFGR1, (SYSCFG_CFGR1_BOOSTEN | SYSCFG_CFGR1_ANASWVDD), SYSCFG_CFGR1_ANASWVDD);
696 }
697 
698 /**
699   * @brief  Disable the I/O analog switch supplied by VDD
700   *
701   * @retval None
702   */
HAL_SYSCFG_DisableIOAnalogSwitchVdd(void)703 void HAL_SYSCFG_DisableIOAnalogSwitchVdd(void)
704 {
705   CLEAR_BIT(SYSCFG->CFGR1, SYSCFG_CFGR1_ANASWVDD);
706 }
707 
708 /**
709   * @}
710   */
711 
712 /** @defgroup HAL_Exported_Functions_Group5 HAL SYSCFG lock management functions
713   *  @brief SYSCFG lock management functions.
714   *
715 @verbatim
716  ===============================================================================
717                        ##### SYSCFG lock functions #####
718  ===============================================================================
719 
720 @endverbatim
721   * @{
722   */
723 
724 /**
725   * @brief  Lock the SYSCFG item(s).
726   * @note   Setting lock(s) depends on privilege mode in secure/non-secure code
727   *         Lock(s) cleared only at system reset
728   * @param  Item Item(s) to set lock on.
729   *         This parameter can be a combination of @ref SYSCFG_Lock_items
730   * @retval None
731   */
HAL_SYSCFG_Lock(uint32_t Item)732 void HAL_SYSCFG_Lock(uint32_t Item)
733 {
734   /* Check the parameters */
735   assert_param(IS_SYSCFG_LOCK_ITEMS(Item));
736 
737   /* Privilege secure/non-secure locks */
738   SYSCFG->CNSLCKR = (0xFFFFU & Item);  /* non-secure lock item in 16 lowest bits */
739 
740 #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
741   /* Privilege secure only locks */
742   SYSCFG->CSLCKR = ((0xFFFF0000U & Item) >> 16U);  /* Secure-only lock item in 16 highest bits */
743 #endif /* __ARM_FEATURE_CMSE */
744 }
745 
746 /**
747   * @brief  Get the lock state of SYSCFG item.
748   * @note   Getting lock(s) depends on privilege mode in secure/non-secure code
749   * @param  pItem pointer to return locked items
750   *         the return value can be a combination of @ref SYSCFG_Lock_items
751   * @retval HAL status
752   */
HAL_SYSCFG_GetLock(uint32_t * pItem)753 HAL_StatusTypeDef HAL_SYSCFG_GetLock(uint32_t *pItem)
754 {
755   uint32_t tmp_lock;
756 
757   /* Check null pointer */
758   if (pItem == NULL)
759   {
760     return HAL_ERROR;
761   }
762 
763   /* Get the non-secure lock state */
764   tmp_lock = SYSCFG->CNSLCKR;
765 
766   /* Get the secure lock state in secure code */
767 #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
768   tmp_lock |= (SYSCFG->CSLCKR << 16U);
769 #endif /* __ARM_FEATURE_CMSE */
770 
771   /* Return overall lock status */
772   *pItem = tmp_lock;
773 
774   return HAL_OK;
775 }
776 
777 /**
778   * @}
779   */
780 
781 #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
782 
783 
784 /** @defgroup HAL_Exported_Functions_Group6 HAL SYSCFG attributes management functions
785   *  @brief SYSCFG attributes management functions.
786   *
787 @verbatim
788  ===============================================================================
789                        ##### SYSCFG attributes functions #####
790  ===============================================================================
791 
792 @endverbatim
793   * @{
794   */
795 
796 /**
797   * @brief  Configure the SYSCFG item attribute(s).
798   * @note   Available attributes are to secure SYSCFG items, so this function is
799   *         only available in secure
800   * @param  Item Item(s) to set attributes on.
801   *         This parameter can be a one or a combination of @ref SYSCFG_Attributes_items
802   * @param  Attributes  specifies the secure/non-secure attributes.
803   * @retval None
804   */
HAL_SYSCFG_ConfigAttributes(uint32_t Item,uint32_t Attributes)805 void HAL_SYSCFG_ConfigAttributes(uint32_t Item, uint32_t Attributes)
806 {
807   uint32_t tmp;
808 
809   /* Check the parameters */
810   assert_param(IS_SYSCFG_ITEMS_ATTRIBUTES(Item));
811   assert_param(IS_SYSCFG_ATTRIBUTES(Attributes));
812 
813   tmp = SYSCFG_S->SECCFGR;
814 
815   /* Set or reset Item */
816   if ((Attributes & SYSCFG_SEC) != 0x00U)
817   {
818     tmp |= Item;
819   }
820   else
821   {
822     tmp &= ~Item;
823   }
824 
825   /* Set secure attributes */
826   SYSCFG_S->SECCFGR = tmp;
827 }
828 
829 /**
830   * @brief  Get the attribute of a SYSCFG item.
831   * @note   Available attributes are to secure SYSCFG items, so this function is
832   *         only available in secure
833   * @param  Item Single item to get secure/non-secure attribute from.
834   * @param  pAttributes pointer to return the attribute.
835   * @retval HAL status
836   */
HAL_SYSCFG_GetConfigAttributes(uint32_t Item,uint32_t * pAttributes)837 HAL_StatusTypeDef HAL_SYSCFG_GetConfigAttributes(uint32_t Item, uint32_t *pAttributes)
838 {
839   /* Check null pointer */
840   if (pAttributes == NULL)
841   {
842     return HAL_ERROR;
843   }
844 
845   /* Check the parameters */
846   assert_param(IS_SYSCFG_ITEMS_ATTRIBUTES(Item));
847 
848   /* Get the secure attribute state */
849   if ((SYSCFG_S->SECCFGR & Item) != 0U)
850   {
851     *pAttributes = SYSCFG_SEC;
852   }
853   else
854   {
855     *pAttributes = SYSCFG_NSEC;
856   }
857 
858   return HAL_OK;
859 }
860 
861 /**
862   * @}
863   */
864 
865 #endif /* __ARM_FEATURE_CMSE */
866 
867 /**
868   * @}
869   */
870 
871 #endif /* HAL_MODULE_ENABLED */
872 /**
873   * @}
874   */
875 
876 /**
877   * @}
878   */
879 
880