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   (0x06U) /*!< [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 frequency.
358   *         Value of @ref HAL_TickFreqTypeDef.
359   */
HAL_GetTickFreq(void)360 HAL_TickFreqTypeDef HAL_GetTickFreq(void)
361 {
362   return uwTickFreq;
363 }
364 
365 /**
366   * @brief This function provides minimum delay (in milliseconds) based
367   *        on variable incremented.
368   * @note In the default implementation , SysTick timer is the source of time base.
369   *       It is used to generate interrupts at regular time intervals where uwTick
370   *       is incremented.
371   * @note This function is declared as __weak to be overwritten in case of other
372   *       implementations in user file.
373   * @param Delay  specifies the delay time length, in milliseconds.
374   * @retval None
375   */
HAL_Delay(uint32_t Delay)376 __weak void HAL_Delay(uint32_t Delay)
377 {
378   uint32_t tickstart = HAL_GetTick();
379   uint32_t wait = Delay;
380 
381   /* Add a period to guaranty minimum wait */
382   if (wait < HAL_MAX_DELAY)
383   {
384     wait += (uint32_t)uwTickFreq;
385   }
386 
387   while ((HAL_GetTick() - tickstart) < wait)
388   {
389   }
390 }
391 
392 /**
393   * @brief Suspend Tick increment.
394   * @note In the default implementation , SysTick timer is the source of time base. It is
395   *       used to generate interrupts at regular time intervals. Once HAL_SuspendTick()
396   *       is called, the SysTick interrupt will be disabled and so Tick increment
397   *       is suspended.
398   * @note This function is declared as __weak to be overwritten in case of other
399   *       implementations in user file.
400   * @retval None
401   */
HAL_SuspendTick(void)402 __weak void HAL_SuspendTick(void)
403 {
404   /* Disable SysTick Interrupt */
405   SysTick->CTRL &= ~SysTick_CTRL_TICKINT_Msk;
406 }
407 
408 /**
409   * @brief Resume Tick increment.
410   * @note In the default implementation , SysTick timer is the source of time base. It is
411   *       used to generate interrupts at regular time intervals. Once HAL_ResumeTick()
412   *       is called, the SysTick interrupt will be enabled and so Tick increment
413   *       is resumed.
414   * @note This function is declared as __weak to be overwritten in case of other
415   *       implementations in user file.
416   * @retval None
417   */
HAL_ResumeTick(void)418 __weak void HAL_ResumeTick(void)
419 {
420   /* Enable SysTick Interrupt */
421   SysTick->CTRL  |= SysTick_CTRL_TICKINT_Msk;
422 }
423 
424 /**
425   * @brief  Return the HAL revision.
426   * @retval version : 0xXYZR (8bits for each decimal, R for RC)
427   */
HAL_GetHalVersion(void)428 uint32_t HAL_GetHalVersion(void)
429 {
430   return STM32L5XX_HAL_VERSION;
431 }
432 
433 /**
434   * @brief  Return the device revision identifier.
435   * @retval Device revision identifier
436   */
HAL_GetREVID(void)437 uint32_t HAL_GetREVID(void)
438 {
439   return ((DBGMCU->IDCODE & DBGMCU_IDCODE_REV_ID) >> DBGMCU_IDCODE_REV_ID_Pos);
440 }
441 
442 /**
443   * @brief  Return the device identifier.
444   * @retval Device identifier
445   */
HAL_GetDEVID(void)446 uint32_t HAL_GetDEVID(void)
447 {
448   return (DBGMCU->IDCODE & DBGMCU_IDCODE_DEV_ID);
449 }
450 
451 /**
452   * @brief  Return the first word of the unique device identifier (UID based on 96 bits)
453   * @retval Device identifier
454   */
HAL_GetUIDw0(void)455 uint32_t HAL_GetUIDw0(void)
456 {
457   return (READ_REG(*((uint32_t *)UID_BASE)));
458 }
459 
460 /**
461   * @brief  Return the second word of the unique device identifier (UID based on 96 bits)
462   * @retval Device identifier
463   */
HAL_GetUIDw1(void)464 uint32_t HAL_GetUIDw1(void)
465 {
466   return (READ_REG(*((uint32_t *)(UID_BASE + 4U))));
467 }
468 
469 /**
470   * @brief  Return the third word of the unique device identifier (UID based on 96 bits)
471   * @retval Device identifier
472   */
HAL_GetUIDw2(void)473 uint32_t HAL_GetUIDw2(void)
474 {
475   return (READ_REG(*((uint32_t *)(UID_BASE + 8U))));
476 }
477 
478 /**
479   * @}
480   */
481 
482 /** @defgroup HAL_Exported_Functions_Group3 HAL Debug functions
483   *  @brief    HAL Debug functions
484   *
485 @verbatim
486  ===============================================================================
487                       ##### HAL Debug functions #####
488  ===============================================================================
489     [..]  This section provides functions allowing to:
490       (+) Enable/Disable Debug module during STOP0/STOP1/STOP2 modes
491       (+) Enable/Disable Debug module during STANDBY mode
492 
493 @endverbatim
494   * @{
495   */
496 
497 /**
498   * @brief  Enable the Debug Module during STOP0/STOP1/STOP2 modes.
499   * @retval None
500   */
HAL_DBGMCU_EnableDBGStopMode(void)501 void HAL_DBGMCU_EnableDBGStopMode(void)
502 {
503   SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
504 }
505 
506 /**
507   * @brief  Disable the Debug Module during STOP0/STOP1/STOP2 modes.
508   * @retval None
509   */
HAL_DBGMCU_DisableDBGStopMode(void)510 void HAL_DBGMCU_DisableDBGStopMode(void)
511 {
512   CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
513 }
514 
515 /**
516   * @brief  Enable the Debug Module during STANDBY mode.
517   * @retval None
518   */
HAL_DBGMCU_EnableDBGStandbyMode(void)519 void HAL_DBGMCU_EnableDBGStandbyMode(void)
520 {
521   SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
522 }
523 
524 /**
525   * @brief  Disable the Debug Module during STANDBY mode.
526   * @retval None
527   */
HAL_DBGMCU_DisableDBGStandbyMode(void)528 void HAL_DBGMCU_DisableDBGStandbyMode(void)
529 {
530   CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
531 }
532 
533 /**
534   * @}
535   */
536 
537 /** @defgroup HAL_Exported_Functions_Group4 HAL SYSCFG configuration functions
538   *  @brief    HAL SYSCFG configuration functions
539   *
540 @verbatim
541  ===============================================================================
542                       ##### HAL SYSCFG configuration functions #####
543  ===============================================================================
544     [..]  This section provides functions allowing to:
545       (+) Start a hardware SRAM2 erase operation
546       (+) Configure the Voltage reference buffer
547       (+) Enable/Disable the Voltage reference buffer
548       (+) Enable/Disable the I/O analog switch voltage booster
549       (+) Enable/Disable the I/O analog switch supplied by VDD
550 
551 @endverbatim
552   * @{
553   */
554 
555 /**
556   * @brief  Start a hardware SRAM2 erase operation.
557   * @note   As long as SRAM2 is not erased the SRAM2ER bit will be set.
558   *         This bit is automatically reset at the end of the SRAM2 erase operation.
559   * @retval None
560   */
HAL_SYSCFG_SRAM2Erase(void)561 void HAL_SYSCFG_SRAM2Erase(void)
562 {
563   /* unlock the write protection of the SRAM2ER bit */
564   SYSCFG->SKR = 0xCA;
565   SYSCFG->SKR = 0x53;
566 
567   /* Starts a hardware SRAM2 erase operation*/
568   SET_BIT(SYSCFG->SCSR, SYSCFG_SCSR_SRAM2ER);
569 }
570 
571 /**
572   * @brief Configure the internal voltage reference buffer voltage scale.
573   * @param VoltageScaling  specifies the output voltage to achieve
574   *          This parameter can be one of the following values:
575   *            @arg SYSCFG_VREFBUF_VOLTAGE_SCALE0: VREF_OUT1 around 2.048 V.
576   *                                                This requires VDDA equal to or higher than 2.4 V.
577   *            @arg SYSCFG_VREFBUF_VOLTAGE_SCALE1: VREF_OUT1 around 2.5 V.
578   *                                                This requires VDDA equal to or higher than 2.8 V.
579   * @note   Retrieve the TrimmingValue from factory located at
580   *         VREFBUF_SC0_CAL_ADDR or VREFBUF_SC1_CAL_ADDR addresses.
581   * @retval None
582   */
HAL_SYSCFG_VREFBUF_VoltageScalingConfig(uint32_t VoltageScaling)583 void HAL_SYSCFG_VREFBUF_VoltageScalingConfig(uint32_t VoltageScaling)
584 {
585   uint32_t TrimmingValue;
586 
587   /* Check the parameters */
588   assert_param(IS_SYSCFG_VREFBUF_VOLTAGE_SCALE(VoltageScaling));
589 
590   MODIFY_REG(VREFBUF->CSR, VREFBUF_CSR_VRS, VoltageScaling);
591 
592   /* Restrieve Calibration data and store them into trimming field */
593   if (VoltageScaling == SYSCFG_VREFBUF_VOLTAGE_SCALE0)
594   {
595     TrimmingValue = ((uint32_t) *VREFBUF_SC0_CAL_ADDR) & 0x3FU;
596   }
597   else
598   {
599     TrimmingValue = ((uint32_t) *VREFBUF_SC1_CAL_ADDR) & 0x3FU;
600   }
601   assert_param(IS_SYSCFG_VREFBUF_TRIMMING(TrimmingValue));
602 
603   HAL_SYSCFG_VREFBUF_TrimmingConfig(TrimmingValue);
604 }
605 
606 /**
607   * @brief Configure the internal voltage reference buffer high impedance mode.
608   * @param Mode  specifies the high impedance mode
609   *          This parameter can be one of the following values:
610   *            @arg SYSCFG_VREFBUF_HIGH_IMPEDANCE_DISABLE: VREF+ pin is internally connect to VREFINT output.
611   *            @arg SYSCFG_VREFBUF_HIGH_IMPEDANCE_ENABLE: VREF+ pin is high impedance.
612   * @retval None
613   */
HAL_SYSCFG_VREFBUF_HighImpedanceConfig(uint32_t Mode)614 void HAL_SYSCFG_VREFBUF_HighImpedanceConfig(uint32_t Mode)
615 {
616   /* Check the parameters */
617   assert_param(IS_SYSCFG_VREFBUF_HIGH_IMPEDANCE(Mode));
618 
619   MODIFY_REG(VREFBUF->CSR, VREFBUF_CSR_HIZ, Mode);
620 }
621 
622 /**
623   * @brief  Tune the Internal Voltage Reference buffer (VREFBUF).
624   * @retval None
625   */
HAL_SYSCFG_VREFBUF_TrimmingConfig(uint32_t TrimmingValue)626 void HAL_SYSCFG_VREFBUF_TrimmingConfig(uint32_t TrimmingValue)
627 {
628   /* Check the parameters */
629   assert_param(IS_SYSCFG_VREFBUF_TRIMMING(TrimmingValue));
630 
631   MODIFY_REG(VREFBUF->CCR, VREFBUF_CCR_TRIM, TrimmingValue);
632 }
633 
634 /**
635   * @brief  Enable the Internal Voltage Reference buffer (VREFBUF).
636   * @retval HAL_OK/HAL_TIMEOUT
637   */
HAL_SYSCFG_EnableVREFBUF(void)638 HAL_StatusTypeDef HAL_SYSCFG_EnableVREFBUF(void)
639 {
640   uint32_t  tickstart;
641 
642   SET_BIT(VREFBUF->CSR, VREFBUF_CSR_ENVR);
643 
644   /* Get Start Tick*/
645   tickstart = HAL_GetTick();
646 
647   /* Wait for VRR bit  */
648   while (READ_BIT(VREFBUF->CSR, VREFBUF_CSR_VRR) == 0U)
649   {
650     if ((HAL_GetTick() - tickstart) > VREFBUF_TIMEOUT_VALUE)
651     {
652       return HAL_TIMEOUT;
653     }
654   }
655 
656   return HAL_OK;
657 }
658 
659 /**
660   * @brief  Disable the Internal Voltage Reference buffer (VREFBUF).
661   *
662   * @retval None
663   */
HAL_SYSCFG_DisableVREFBUF(void)664 void HAL_SYSCFG_DisableVREFBUF(void)
665 {
666   CLEAR_BIT(VREFBUF->CSR, VREFBUF_CSR_ENVR);
667 }
668 
669 /**
670   * @brief  Enable the I/O analog switch voltage booster
671   * @note   Insure low VDDA voltage operation with I/O analog switch control
672   * @retval None
673   */
HAL_SYSCFG_EnableIOAnalogBooster(void)674 void HAL_SYSCFG_EnableIOAnalogBooster(void)
675 {
676   MODIFY_REG(SYSCFG->CFGR1, (SYSCFG_CFGR1_BOOSTEN | SYSCFG_CFGR1_ANASWVDD), SYSCFG_CFGR1_BOOSTEN);
677 }
678 
679 /**
680   * @brief  Disable the I/O analog switch voltage booster
681   *
682   * @retval None
683   */
HAL_SYSCFG_DisableIOAnalogBooster(void)684 void HAL_SYSCFG_DisableIOAnalogBooster(void)
685 {
686   CLEAR_BIT(SYSCFG->CFGR1, SYSCFG_CFGR1_BOOSTEN);
687 }
688 
689 /**
690   * @brief  Enable the I/O analog switch supplied by VDD
691   * @note   To be used when I/O analog switch voltage booster is not enabled
692   * @retval None
693   */
HAL_SYSCFG_EnableIOAnalogSwitchVdd(void)694 void HAL_SYSCFG_EnableIOAnalogSwitchVdd(void)
695 {
696   MODIFY_REG(SYSCFG->CFGR1, (SYSCFG_CFGR1_BOOSTEN | SYSCFG_CFGR1_ANASWVDD), SYSCFG_CFGR1_ANASWVDD);
697 }
698 
699 /**
700   * @brief  Disable the I/O analog switch supplied by VDD
701   *
702   * @retval None
703   */
HAL_SYSCFG_DisableIOAnalogSwitchVdd(void)704 void HAL_SYSCFG_DisableIOAnalogSwitchVdd(void)
705 {
706   CLEAR_BIT(SYSCFG->CFGR1, SYSCFG_CFGR1_ANASWVDD);
707 }
708 
709 /**
710   * @}
711   */
712 
713 /** @defgroup HAL_Exported_Functions_Group5 HAL SYSCFG lock management functions
714   *  @brief SYSCFG lock management functions.
715   *
716 @verbatim
717  ===============================================================================
718                        ##### SYSCFG lock functions #####
719  ===============================================================================
720 
721 @endverbatim
722   * @{
723   */
724 
725 /**
726   * @brief  Lock the SYSCFG item(s).
727   * @note   Setting lock(s) depends on privilege mode in secure/non-secure code
728   *         Lock(s) cleared only at system reset
729   * @param  Item Item(s) to set lock on.
730   *         This parameter can be a combination of @ref SYSCFG_Lock_items
731   * @retval None
732   */
HAL_SYSCFG_Lock(uint32_t Item)733 void HAL_SYSCFG_Lock(uint32_t Item)
734 {
735   /* Check the parameters */
736   assert_param(IS_SYSCFG_LOCK_ITEMS(Item));
737 
738   /* Privilege secure/non-secure locks */
739   SYSCFG->CNSLCKR = (0xFFFFU & Item);  /* non-secure lock item in 16 lowest bits */
740 
741 #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
742   /* Privilege secure only locks */
743   SYSCFG->CSLCKR = ((0xFFFF0000U & Item) >> 16U);  /* Secure-only lock item in 16 highest bits */
744 #endif /* __ARM_FEATURE_CMSE */
745 }
746 
747 /**
748   * @brief  Get the lock state of SYSCFG item.
749   * @note   Getting lock(s) depends on privilege mode in secure/non-secure code
750   * @param  pItem pointer to return locked items
751   *         the return value can be a combination of @ref SYSCFG_Lock_items
752   * @retval HAL status
753   */
HAL_SYSCFG_GetLock(uint32_t * pItem)754 HAL_StatusTypeDef HAL_SYSCFG_GetLock(uint32_t *pItem)
755 {
756   uint32_t tmp_lock;
757 
758   /* Check null pointer */
759   if (pItem == NULL)
760   {
761     return HAL_ERROR;
762   }
763 
764   /* Get the non-secure lock state */
765   tmp_lock = SYSCFG->CNSLCKR;
766 
767   /* Get the secure lock state in secure code */
768 #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
769   tmp_lock |= (SYSCFG->CSLCKR << 16U);
770 #endif /* __ARM_FEATURE_CMSE */
771 
772   /* Return overall lock status */
773   *pItem = tmp_lock;
774 
775   return HAL_OK;
776 }
777 
778 /**
779   * @}
780   */
781 
782 #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
783 
784 
785 /** @defgroup HAL_Exported_Functions_Group6 HAL SYSCFG attributes management functions
786   *  @brief SYSCFG attributes management functions.
787   *
788 @verbatim
789  ===============================================================================
790                        ##### SYSCFG attributes functions #####
791  ===============================================================================
792 
793 @endverbatim
794   * @{
795   */
796 
797 /**
798   * @brief  Configure the SYSCFG item attribute(s).
799   * @note   Available attributes are to secure SYSCFG items, so this function is
800   *         only available in secure
801   * @param  Item Item(s) to set attributes on.
802   *         This parameter can be a one or a combination of @ref SYSCFG_Attributes_items
803   * @param  Attributes  specifies the secure/non-secure attributes.
804   * @retval None
805   */
HAL_SYSCFG_ConfigAttributes(uint32_t Item,uint32_t Attributes)806 void HAL_SYSCFG_ConfigAttributes(uint32_t Item, uint32_t Attributes)
807 {
808   uint32_t tmp;
809 
810   /* Check the parameters */
811   assert_param(IS_SYSCFG_ITEMS_ATTRIBUTES(Item));
812   assert_param(IS_SYSCFG_ATTRIBUTES(Attributes));
813 
814   tmp = SYSCFG_S->SECCFGR;
815 
816   /* Set or reset Item */
817   if ((Attributes & SYSCFG_SEC) != 0x00U)
818   {
819     tmp |= Item;
820   }
821   else
822   {
823     tmp &= ~Item;
824   }
825 
826   /* Set secure attributes */
827   SYSCFG_S->SECCFGR = tmp;
828 }
829 
830 /**
831   * @brief  Get the attribute of a SYSCFG item.
832   * @note   Available attributes are to secure SYSCFG items, so this function is
833   *         only available in secure
834   * @param  Item Single item to get secure/non-secure attribute from.
835   * @param  pAttributes pointer to return the attribute.
836   * @retval HAL status
837   */
HAL_SYSCFG_GetConfigAttributes(uint32_t Item,uint32_t * pAttributes)838 HAL_StatusTypeDef HAL_SYSCFG_GetConfigAttributes(uint32_t Item, uint32_t *pAttributes)
839 {
840   /* Check null pointer */
841   if (pAttributes == NULL)
842   {
843     return HAL_ERROR;
844   }
845 
846   /* Check the parameters */
847   assert_param(IS_SYSCFG_ITEMS_ATTRIBUTES(Item));
848 
849   /* Get the secure attribute state */
850   if ((SYSCFG_S->SECCFGR & Item) != 0U)
851   {
852     *pAttributes = SYSCFG_SEC;
853   }
854   else
855   {
856     *pAttributes = SYSCFG_NSEC;
857   }
858 
859   return HAL_OK;
860 }
861 
862 /**
863   * @}
864   */
865 
866 #endif /* __ARM_FEATURE_CMSE */
867 
868 /**
869   * @}
870   */
871 
872 #endif /* HAL_MODULE_ENABLED */
873 /**
874   * @}
875   */
876 
877 /**
878   * @}
879   */
880 
881