1 /**
2   **********************************************************************************************************************
3   * @file    stm32h5xx_hal.c
4   * @author  MCD Application Team
5   * @brief   HAL module driver.
6   *          This is the common part of the HAL initialization
7   *
8   @verbatim
9   ======================================================================================================================
10                      ##### How to use this driver #####
11   ======================================================================================================================
12     [..]
13     The common HAL driver contains a set of generic and common APIs that can be
14     used by the PPP peripheral drivers and the user to start using the HAL.
15     [..]
16     The HAL contains two APIs' categories:
17          (+) Common HAL APIs
18          (+) Services HAL APIs
19 
20   @endverbatim
21   **********************************************************************************************************************
22   * @attention
23   *
24   * Copyright (c) 2022 STMicroelectronics.
25   * All rights reserved.
26   *
27   * This software is licensed under terms that can be found in the LICENSE file
28   * in the root directory of this software component.
29   * If no LICENSE file comes with this software, it is provided AS-IS.
30   *
31   **********************************************************************************************************************
32   */
33 
34 /* Includes ----------------------------------------------------------------------------------------------------------*/
35 #include "stm32h5xx_hal.h"
36 
37 /** @addtogroup STM32H5xx_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 STM32H5xx HAL Driver version number 0.5.0
52    */
53 #define __STM32H5XX_HAL_VERSION_MAIN   (0x00U) /*!< [31:24] main version */
54 #define __STM32H5XX_HAL_VERSION_SUB1   (0x05U) /*!< [23:16] sub1 version */
55 #define __STM32H5XX_HAL_VERSION_SUB2   (0x00U) /*!< [15:8]  sub2 version */
56 #define __STM32H5XX_HAL_VERSION_RC     (0x00U) /*!< [7:0]  release candidate */
57 #define __STM32H5XX_HAL_VERSION         ((__STM32H5XX_HAL_VERSION_MAIN << 24U)\
58                                          |(__STM32H5XX_HAL_VERSION_SUB1 << 16U)\
59                                          |(__STM32H5XX_HAL_VERSION_SUB2 << 8U )\
60                                          |(__STM32H5XX_HAL_VERSION_RC))
61 
62 #define VREFBUF_TIMEOUT_VALUE           10U   /* 10 ms (to be confirmed) */
63 
64 /* Private macro -----------------------------------------------------------------------------------------------------*/
65 /* Private variables -------------------------------------------------------------------------------------------------*/
66 /* Exported variables ------------------------------------------------------------------------------------------------*/
67 
68 /** @defgroup HAL_Exported_Variables HAL Exported Variables
69   * @{
70   */
71 __IO uint32_t uwTick;
72 uint32_t uwTickPrio   = (1UL << __NVIC_PRIO_BITS); /* Invalid PRIO */
73 HAL_TickFreqTypeDef uwTickFreq = HAL_TICK_FREQ_DEFAULT;  /* 1KHz */
74 /**
75   * @}
76   */
77 
78 /* Private function prototypes ---------------------------------------------------------------------------------------*/
79 /* Exported functions ------------------------------------------------------------------------------------------------*/
80 
81 /** @defgroup HAL_Exported_Functions HAL Exported Functions
82   * @{
83   */
84 
85 /** @defgroup HAL_Exported_Functions_Group1 Initialization and de-initialization Functions
86   *  @brief    Initialization and de-initialization functions
87   *
88 @verbatim
89  =======================================================================================================================
90               ##### Initialization and de-initialization functions #####
91  =======================================================================================================================
92     [..]  This section provides functions allowing to:
93       (+) Initializes the Flash interface the NVIC allocation and initial clock
94           configuration. It initializes the systick also when timeout is needed
95           and the backup domain when enabled.
96       (+) De-Initializes common part of the HAL.
97       (+) Configure The time base source to have 1ms time base with a dedicated
98           Tick interrupt priority.
99         (++) SysTick timer is used by default as source of time base, but user
100              can eventually implement his proper time base source (a general purpose
101              timer for example or other time source), keeping in mind that Time base
102              duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and
103              handled in milliseconds basis.
104         (++) Time base configuration function (HAL_InitTick ()) is called automatically
105              at the beginning of the program after reset by HAL_Init() or at any time
106              when clock is configured, by HAL_RCC_ClockConfig().
107         (++) Source of time base is configured  to generate interrupts at regular
108              time intervals. Care must be taken if HAL_Delay() is called from a
109              peripheral ISR process, the Tick interrupt line must have higher priority
110             (numerically lower) than the peripheral interrupt. Otherwise the caller
111             ISR process will be blocked.
112        (++) functions affecting time base configurations are declared as __weak
113              to make  override possible  in case of other  implementations in user file.
114 @endverbatim
115   * @{
116   */
117 
118 /**
119   * @brief  Configure the Flash prefetch, the time base source, NVIC and any required global low
120   *         level hardware by calling the HAL_MspInit() callback function to be optionally defined
121   *         in user file stm32h5xx_hal_msp.c.
122   *
123   * @note   HAL_Init() function is called at the beginning of program after reset and before
124   *         the clock configuration.
125   *
126   * @note   In the default implementation the System Timer (Systick) is used as source of time base.
127   *         The Systick configuration is based on HSI clock, as HSI is the clock
128   *         used after a system Reset and the NVIC configuration is set to Priority group 4.
129   *         Once done, time base tick starts incrementing: the tick variable counter is incremented
130   *         each 1ms in the SysTick_Handler() interrupt handler.
131   *
132   * @retval HAL status
133   */
HAL_Init(void)134 HAL_StatusTypeDef HAL_Init(void)
135 {
136   /* Configure Flash prefetch */
137 #if (PREFETCH_ENABLE != 0U)
138   __HAL_FLASH_PREFETCH_BUFFER_ENABLE();
139 #endif /* PREFETCH_ENABLE */
140 
141   /* Set Interrupt Group Priority */
142   HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4);
143 
144   /* Update the SystemCoreClock global variable */
145   SystemCoreClock = HAL_RCC_GetSysClockFreq() >> AHBPrescTable[(RCC->CFGR2 & RCC_CFGR2_HPRE) >> RCC_CFGR2_HPRE_Pos];
146 
147   /* Use systick as time base source and configure 1ms tick (default clock after Reset is HSI) */
148   if (HAL_InitTick(TICK_INT_PRIORITY) != HAL_OK)
149   {
150     return HAL_ERROR;
151   }
152 
153   /* Init the low level hardware */
154   HAL_MspInit();
155 
156   /* Return function status */
157   return HAL_OK;
158 }
159 
160 /**
161   * @brief  This function de-Initializes common part of the HAL and stops the systick.
162   *         This function is optional.
163   * @retval HAL status
164   */
HAL_DeInit(void)165 HAL_StatusTypeDef HAL_DeInit(void)
166 {
167   /* Reset of all peripherals */
168   __HAL_RCC_APB1_FORCE_RESET();
169   __HAL_RCC_APB1_RELEASE_RESET();
170 
171   __HAL_RCC_APB2_FORCE_RESET();
172   __HAL_RCC_APB2_RELEASE_RESET();
173 
174   __HAL_RCC_APB3_FORCE_RESET();
175   __HAL_RCC_APB3_RELEASE_RESET();
176 
177   __HAL_RCC_AHB1_FORCE_RESET();
178   __HAL_RCC_AHB1_RELEASE_RESET();
179 
180   __HAL_RCC_AHB2_FORCE_RESET();
181   __HAL_RCC_AHB2_RELEASE_RESET();
182 
183 #if defined(AHB4PERIPH_BASE)
184   __HAL_RCC_AHB4_FORCE_RESET();
185   __HAL_RCC_AHB4_RELEASE_RESET();
186 #endif /* AHB4PERIPH_BASE */
187 
188   /* De-Init the low level hardware */
189   HAL_MspDeInit();
190 
191   /* Return function status */
192   return HAL_OK;
193 }
194 
195 /**
196   * @brief  Initializes the MSP.
197   * @retval None
198   */
HAL_MspInit(void)199 __weak void HAL_MspInit(void)
200 {
201   /* NOTE : This function Should not be modified, when the callback is needed,
202             the HAL_MspInit could be implemented in the user file
203    */
204 }
205 
206 /**
207   * @brief  DeInitializes the MSP.
208   * @retval None
209   */
HAL_MspDeInit(void)210 __weak void HAL_MspDeInit(void)
211 {
212   /* NOTE : This function Should not be modified, when the callback is needed,
213             the HAL_MspDeInit could be implemented in the user file
214    */
215 }
216 
217 /**
218   * @brief This function configures the source of the time base.
219   *        The time source is configured to have 1ms time base with a dedicated
220   *        Tick interrupt priority.
221   * @note This function is called  automatically at the beginning of program after
222   *       reset by HAL_Init() or at any time when clock is reconfigured  by HAL_RCC_ClockConfig().
223   * @note In the default implementation, SysTick timer is the source of time base.
224   *       It is used to generate interrupts at regular time intervals.
225   *       Care must be taken if HAL_Delay() is called from a peripheral ISR process,
226   *       The SysTick interrupt must have higher priority (numerically lower)
227   *       than the peripheral interrupt. Otherwise the caller ISR process will be blocked.
228   *       The function is declared as __weak  to be overwritten  in case of other
229   *       implementation  in user file.
230   * @param TickPriority: Tick interrupt priority.
231   * @retval HAL status
232   */
HAL_InitTick(uint32_t TickPriority)233 __weak HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
234 {
235   /* Check uwTickFreq for MisraC 2012 (even if uwTickFreq is a enum type that don't take the value zero)*/
236   if ((uint32_t)uwTickFreq == 0UL)
237   {
238     return HAL_ERROR;
239   }
240 
241   /* Configure the SysTick to have interrupt in 1ms time basis*/
242   if (HAL_SYSTICK_Config(SystemCoreClock / (1000UL / (uint32_t)uwTickFreq)) > 0U)
243   {
244     return HAL_ERROR;
245   }
246 
247   /* Configure the SysTick IRQ priority */
248   if (TickPriority < (1UL << __NVIC_PRIO_BITS))
249   {
250     HAL_NVIC_SetPriority(SysTick_IRQn, TickPriority, 0U);
251     uwTickPrio = TickPriority;
252   }
253   else
254   {
255     return HAL_ERROR;
256   }
257 
258   /* Return function status */
259   return HAL_OK;
260 }
261 
262 /**
263   * @}
264   */
265 
266 /** @defgroup HAL_Group2 HAL Control functions
267   *  @brief    HAL Control functions
268   *
269 @verbatim
270  =======================================================================================================================
271                                        ##### HAL Control functions #####
272  =======================================================================================================================
273     [..]  This section provides functions allowing to:
274       (+) Provide a tick value in millisecond
275       (+) Provide a blocking delay in millisecond
276       (+) Suspend the time base source interrupt
277       (+) Resume the time base source interrupt
278       (+) Get the HAL API driver version
279       (+) Get the device identifier
280       (+) Get the device revision identifier
281 
282 @endverbatim
283   * @{
284   */
285 
286 /**
287   * @brief This function is called to increment a global variable "uwTick"
288   *        used as application time base.
289   * @note In the default implementation, this variable is incremented each 1ms
290   *       in Systick ISR.
291   * @note This function is declared as __weak to be overwritten in case of other
292   *      implementations in user file.
293   * @retval None
294   */
HAL_IncTick(void)295 __weak void HAL_IncTick(void)
296 {
297   uwTick += (uint32_t)uwTickFreq;
298 }
299 
300 /**
301   * @brief Provides a tick value in millisecond.
302   * @note This function is declared as __weak to be overwritten in case of other
303   *       implementations in user file.
304   * @retval tick value
305   */
HAL_GetTick(void)306 __weak uint32_t HAL_GetTick(void)
307 {
308   return uwTick;
309 }
310 
311 /**
312   * @brief This function returns a tick priority.
313   * @retval tick priority
314   */
HAL_GetTickPrio(void)315 uint32_t HAL_GetTickPrio(void)
316 {
317   return uwTickPrio;
318 }
319 
320 /**
321   * @brief Set new tick Freq.
322   * @retval HAL status
323   */
HAL_SetTickFreq(HAL_TickFreqTypeDef Freq)324 HAL_StatusTypeDef HAL_SetTickFreq(HAL_TickFreqTypeDef Freq)
325 {
326   HAL_StatusTypeDef status  = HAL_OK;
327   HAL_TickFreqTypeDef prevTickFreq;
328 
329   assert_param(IS_TICKFREQ(Freq));
330 
331   if (uwTickFreq != Freq)
332   {
333 
334     /* Back up uwTickFreq frequency */
335     prevTickFreq = uwTickFreq;
336 
337     /* Update uwTickFreq global variable used by HAL_InitTick() */
338     uwTickFreq = Freq;
339 
340     /* Apply the new tick Freq  */
341     status = HAL_InitTick(uwTickPrio);
342     if (status != HAL_OK)
343     {
344       /* Restore previous tick frequency */
345       uwTickFreq = prevTickFreq;
346     }
347   }
348 
349   return status;
350 }
351 
352 /**
353   * @brief Return tick frequency.
354   * @retval tick period in Hz
355   */
HAL_GetTickFreq(void)356 HAL_TickFreqTypeDef HAL_GetTickFreq(void)
357 {
358   return uwTickFreq;
359 }
360 
361 /**
362   * @brief This function provides minimum delay (in milliseconds) based
363   *        on variable incremented.
364   * @note In the default implementation , SysTick timer is the source of time base.
365   *       It is used to generate interrupts at regular time intervals where uwTick
366   *       is incremented.
367   * @note This function is declared as __weak to be overwritten in case of other
368   *       implementations in user file.
369   * @param Delay  specifies the delay time length, in milliseconds.
370   * @retval None
371   */
HAL_Delay(uint32_t Delay)372 __weak void HAL_Delay(uint32_t Delay)
373 {
374   uint32_t tickstart = HAL_GetTick();
375   uint32_t wait = Delay;
376 
377   /* Add a freq to guarantee minimum wait */
378   if (wait < HAL_MAX_DELAY)
379   {
380     wait += (uint32_t)(uwTickFreq);
381   }
382 
383   while ((HAL_GetTick() - tickstart) < wait)
384   {
385   }
386 }
387 
388 /**
389   * @brief Suspend Tick increment.
390   * @note In the default implementation , SysTick timer is the source of time base. It is
391   *       used to generate interrupts at regular time intervals. Once HAL_SuspendTick()
392   *       is called, the SysTick interrupt will be disabled and so Tick increment
393   *       is suspended.
394   * @note This function is declared as __weak to be overwritten in case of other
395   *       implementations in user file.
396   * @retval None
397   */
HAL_SuspendTick(void)398 __weak void HAL_SuspendTick(void)
399 {
400   /* Disable SysTick Interrupt */
401   SysTick->CTRL &= ~SysTick_CTRL_TICKINT_Msk;
402 }
403 
404 /**
405   * @brief Resume Tick increment.
406   * @note In the default implementation , SysTick timer is the source of time base. It is
407   *       used to generate interrupts at regular time intervals. Once HAL_ResumeTick()
408   *       is called, the SysTick interrupt will be enabled and so Tick increment
409   *       is resumed.
410   * @note This function is declared as __weak to be overwritten in case of other
411   *       implementations in user file.
412   * @retval None
413   */
HAL_ResumeTick(void)414 __weak void HAL_ResumeTick(void)
415 {
416   /* Enable SysTick Interrupt */
417   SysTick->CTRL  |= SysTick_CTRL_TICKINT_Msk;
418 }
419 
420 /**
421   * @brief  Returns the HAL revision
422   * @retval version : 0xXYZR (8bits for each decimal, R for RC)
423   */
HAL_GetHalVersion(void)424 uint32_t HAL_GetHalVersion(void)
425 {
426   return __STM32H5XX_HAL_VERSION;
427 }
428 
429 /**
430   * @brief  Returns the device revision identifier.
431   * @retval Device revision identifier
432   */
HAL_GetREVID(void)433 uint32_t HAL_GetREVID(void)
434 {
435   return ((DBGMCU->IDCODE & DBGMCU_IDCODE_REV_ID) >> 16);
436 }
437 
438 /**
439   * @brief  Returns the device identifier.
440   * @retval Device identifier
441   */
HAL_GetDEVID(void)442 uint32_t HAL_GetDEVID(void)
443 {
444   return (DBGMCU->IDCODE & DBGMCU_IDCODE_DEV_ID);
445 }
446 
447 /**
448   * @brief  Return the first word of the unique device identifier (UID based on 96 bits)
449   * @retval Device identifier
450   */
HAL_GetUIDw0(void)451 uint32_t HAL_GetUIDw0(void)
452 {
453   return(READ_REG(*((uint32_t *)UID_BASE)));
454 }
455 
456 /**
457   * @brief  Return the second word of the unique device identifier (UID based on 96 bits)
458   * @retval Device identifier
459   */
HAL_GetUIDw1(void)460 uint32_t HAL_GetUIDw1(void)
461 {
462   return(READ_REG(*((uint32_t *)(UID_BASE + 4U))));
463 }
464 
465 /**
466   * @brief  Return the third word of the unique device identifier (UID based on 96 bits)
467   * @retval Device identifier
468   */
HAL_GetUIDw2(void)469 uint32_t HAL_GetUIDw2(void)
470 {
471   return(READ_REG(*((uint32_t *)(UID_BASE + 8U))));
472 }
473 
474 /**
475   * @}
476   */
477 
478 
479 /** @defgroup HAL_Exported_Functions_Group3 HAL Debug functions
480   *  @brief    HAL Debug functions
481   *
482 @verbatim
483  =======================================================================================================================
484                                        ##### HAL Debug functions #####
485  =======================================================================================================================
486     [..]  This section provides functions allowing to:
487       (+) Enable/Disable Debug module during STOP mode
488       (+) Enable/Disable Debug module during STANDBY mode
489 
490 @endverbatim
491   * @{
492   */
493 
494 /**
495   * @brief  Enable the Debug Module during STOP mode.
496   * @retval None
497   */
HAL_DBGMCU_EnableDBGStopMode(void)498 void HAL_DBGMCU_EnableDBGStopMode(void)
499 {
500   SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
501 }
502 
503 /**
504   * @brief  Disable the Debug Module during STOP mode.
505   * @retval None
506   */
HAL_DBGMCU_DisableDBGStopMode(void)507 void HAL_DBGMCU_DisableDBGStopMode(void)
508 {
509   CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
510 }
511 
512 /**
513   * @brief  Enable the Debug Module during STANDBY mode.
514   * @retval None
515   */
HAL_DBGMCU_EnableDBGStandbyMode(void)516 void HAL_DBGMCU_EnableDBGStandbyMode(void)
517 {
518   SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
519 }
520 
521 /**
522   * @brief  Disable the Debug Module during STANDBY mode.
523   * @retval None
524   */
HAL_DBGMCU_DisableDBGStandbyMode(void)525 void HAL_DBGMCU_DisableDBGStandbyMode(void)
526 {
527   CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
528 }
529 
530 /**
531   * @}
532   */
533 
534 /** @defgroup HAL_Exported_Functions_Group4 HAL SBS configuration functions
535   *  @brief    HAL SBS configuration functions
536   *
537 @verbatim
538  =======================================================================================================================
539                                        ##### HAL SBS configuration functions #####
540  =======================================================================================================================
541     [..]  This section provides functions allowing to:
542       (+) Configure the Voltage reference buffer
543       (+) Enable/Disable the Voltage reference buffer
544       (+) Enable/Disable the I/O analog switch voltage booster
545 
546 @endverbatim
547   * @{
548   */
549 
550 /**
551   * @brief Configure the internal voltage reference buffer voltage scale.
552   * @param  VoltageScaling: specifies the output voltage to achieve
553   *          This parameter can be one of the following values:
554   *            @arg VREFBUF_VOLTAGE_SCALE0: VREF_OUT1 around 2.5 V.
555   *                                                This requires VDDA equal to or higher than 2.8 V.
556   *            @arg VREFBUF_VOLTAGE_SCALE1: VREF_OUT2 around 2.048 V.
557   *                                                This requires VDDA equal to or higher than 2.4 V.
558   *            @arg VREFBUF_VOLTAGE_SCALE2: VREF_OUT3 around 1.8 V.
559   *                                                This requires VDDA equal to or higher than 2.1 V.
560   *            @arg VREFBUF_VOLTAGE_SCALE3: VREF_OUT4 around 1.5 V.
561   *                                                This requires VDDA equal to or higher than 1.8 V.
562   * @retval None
563   */
HAL_VREFBUF_VoltageScalingConfig(uint32_t VoltageScaling)564 void HAL_VREFBUF_VoltageScalingConfig(uint32_t VoltageScaling)
565 {
566   /* Check the parameters */
567   assert_param(IS_VREFBUF_VOLTAGE_SCALE(VoltageScaling));
568 
569   MODIFY_REG(VREFBUF->CSR, VREFBUF_CSR_VRS, VoltageScaling);
570 }
571 
572 /**
573   * @brief Configure the internal voltage reference buffer high impedance mode.
574   * @param  Mode: specifies the high impedance mode
575   *          This parameter can be one of the following values:
576   *            @arg VREFBUF_HIGH_IMPEDANCE_DISABLE: VREF+ pin is internally connect to VREFINT output.
577   *            @arg VREFBUF_HIGH_IMPEDANCE_ENABLE: VREF+ pin is high impedance.
578   * @retval None
579   */
HAL_VREFBUF_HighImpedanceConfig(uint32_t Mode)580 void HAL_VREFBUF_HighImpedanceConfig(uint32_t Mode)
581 {
582   /* Check the parameters */
583   assert_param(IS_VREFBUF_HIGH_IMPEDANCE(Mode));
584 
585   MODIFY_REG(VREFBUF->CSR, VREFBUF_CSR_HIZ, Mode);
586 }
587 
588 /**
589   * @brief  Tune the Internal Voltage Reference buffer (VREFBUF).
590   * @retval None
591   */
HAL_VREFBUF_TrimmingConfig(uint32_t TrimmingValue)592 void HAL_VREFBUF_TrimmingConfig(uint32_t TrimmingValue)
593 {
594   /* Check the parameters */
595   assert_param(IS_VREFBUF_TRIMMING(TrimmingValue));
596 
597   MODIFY_REG(VREFBUF->CCR, VREFBUF_CCR_TRIM, TrimmingValue);
598 }
599 
600 /**
601   * @brief  Enable the Internal Voltage Reference buffer (VREFBUF).
602   * @retval HAL_OK/HAL_TIMEOUT
603   */
HAL_EnableVREFBUF(void)604 HAL_StatusTypeDef HAL_EnableVREFBUF(void)
605 {
606   uint32_t  tickstart;
607 
608   SET_BIT(VREFBUF->CSR, VREFBUF_CSR_ENVR);
609 
610   /* Get Start Tick*/
611   tickstart = HAL_GetTick();
612 
613   /* Wait for VRR bit  */
614   while (READ_BIT(VREFBUF->CSR, VREFBUF_CSR_VRR) == 0UL)
615   {
616     if ((HAL_GetTick() - tickstart) > VREFBUF_TIMEOUT_VALUE)
617     {
618       return HAL_TIMEOUT;
619     }
620   }
621 
622   return HAL_OK;
623 }
624 
625 /**
626   * @brief  Disable the Internal Voltage Reference buffer (VREFBUF).
627   *
628   * @retval None
629   */
HAL_DisableVREFBUF(void)630 void HAL_DisableVREFBUF(void)
631 {
632   CLEAR_BIT(VREFBUF->CSR, VREFBUF_CSR_ENVR);
633 }
634 
635 #if defined(SBS_PMCR_ETH_SEL_PHY)
636 /**
637   * @brief  Ethernet PHY Interface Selection either MII or RMII
638   * @param  SBS_ETHInterface: Selects the Ethernet PHY interface
639   *   This parameter can be one of the following values:
640   *   @arg SBS_ETH_MII : Select the Media Independent Interface
641   *   @arg SBS_ETH_RMII: Select the Reduced Media Independent Interface
642   * @retval None
643   */
HAL_SBS_ETHInterfaceSelect(uint32_t SBS_ETHInterface)644 void HAL_SBS_ETHInterfaceSelect(uint32_t SBS_ETHInterface)
645 {
646   /* Check the parameter */
647   assert_param(IS_SBS_ETHERNET_CONFIG(SBS_ETHInterface));
648 
649   MODIFY_REG(SBS->PMCR, SBS_PMCR_ETH_SEL_PHY, (uint32_t)(SBS_ETHInterface));
650 }
651 #endif /* SBS_PMCR_ETH_SEL_PHY */
652 
653 /**
654   * @brief  Enable the I/O analog switch voltage booster
655   *
656   * @retval None
657   */
HAL_SBS_EnableIOAnalogSwitchBooster(void)658 void HAL_SBS_EnableIOAnalogSwitchBooster(void)
659 {
660   SET_BIT(SBS->PMCR, SBS_PMCR_BOOSTEN);
661 }
662 
663 /**
664   * @brief  Disable the I/O analog switch voltage booster
665   *
666   * @retval None
667   */
HAL_SBS_DisableIOAnalogSwitchBooster(void)668 void HAL_SBS_DisableIOAnalogSwitchBooster(void)
669 {
670   CLEAR_BIT(SBS->PMCR, SBS_PMCR_BOOSTEN);
671 }
672 
673 /**
674   * @brief  Analog switch supply voltage selection (VDD/VDDA/booster)
675   * @param  SBS_BOOSTVDDSEL: Selects the Analog switch supply voltage (VDD/VDDA/booster)
676   *         This parameter can be one of the following values:
677   *           @arg SBS_BOOSTVDDSEL_VDDA : Select the VDDA as analog switch supply voltage (when BOOSTEN bit is cleared).
678   *           @arg SBS_BOOSTVDDSEL_VDD: Select the VDD as analog switch supply voltage.
679   * @retval None
680   */
HAL_SBS_AnalogSwitchSupplyVoltageSelection(uint32_t SBS_BOOSTVDDSEL)681 void HAL_SBS_AnalogSwitchSupplyVoltageSelection(uint32_t SBS_BOOSTVDDSEL)
682 {
683   /* Check the parameter */
684   assert_param(IS_SBS_BOOSTVDD_SELECTION(SBS_BOOSTVDDSEL));
685 
686   MODIFY_REG(SBS->PMCR, SBS_PMCR_BOOSTVDDSEL, (uint32_t)(SBS_BOOSTVDDSEL));
687 }
688 
689 /**
690   * @brief  Enables the VDD I/Os Compensation Cell.
691   * @note   The I/O compensation cell can be used only when the device supply
692   *         voltage ranges from 2.4 to 3.6 V.
693   * @retval None
694   */
HAL_SBS_EnableVddIO1CompensationCell(void)695 void HAL_SBS_EnableVddIO1CompensationCell(void)
696 {
697   SET_BIT(SBS->CCCSR, SBS_CCCSR_EN1) ;
698 }
699 
700 /**
701   * @brief  Power-down the VDD I/Os Compensation Cell.
702   * @note   The I/O compensation cell can be used only when the device supply
703   *         voltage ranges from 2.4 to 3.6 V.
704   * @retval None
705   */
HAL_SBS_DisableVddIO1CompensationCell(void)706 void HAL_SBS_DisableVddIO1CompensationCell(void)
707 {
708   CLEAR_BIT(SBS->CCCSR, SBS_CCCSR_EN1);
709 }
710 
711 /**
712   * @brief  Enables the VDDIO2 I/Os Compensation Cell.
713   * @note   The I/O compensation cell can be used only when the device supply
714   *         voltage ranges from 2.4 to 3.6 V.
715   * @retval None
716   */
HAL_SBS_EnableVddIO2CompensationCell(void)717 void HAL_SBS_EnableVddIO2CompensationCell(void)
718 {
719   SET_BIT(SBS->CCCSR, SBS_CCCSR_EN2) ;
720 }
721 
722 /**
723   * @brief  Power-down the VDDIO2 I/Os Compensation Cell.
724   * @note   The I/O compensation cell can be used only when the device supply
725   *         voltage ranges from 2.4 to 3.6 V.
726   * @retval None
727   */
HAL_SBS_DisableVddIO2CompensationCell(void)728 void HAL_SBS_DisableVddIO2CompensationCell(void)
729 {
730   CLEAR_BIT(SBS->CCCSR, SBS_CCCSR_EN2);
731 }
732 
733 /**
734   * @brief  Code selection for the VDD I/O Compensation cell
735   * @param  SBS_CompCode: Selects the code to be applied for the I/O compensation cell
736   *         This parameter can be one of the following values:
737   *           @arg SBS_VDD_CELL_CODE : Select Code from the cell (available in the SBS_CCVALR)
738   *           @arg SBS_VDD_REGISTER_CODE: Select Code from the SBS compensation cell code register (SBS_CCSWCR)
739   * @retval None
740   */
HAL_SBS_VDDCompensationCodeSelect(uint32_t SBS_CompCode)741 void HAL_SBS_VDDCompensationCodeSelect(uint32_t SBS_CompCode)
742 {
743   /* Check the parameter */
744   assert_param(IS_SBS_VDD_CODE_SELECT(SBS_CompCode));
745   MODIFY_REG(SBS->CCCSR, SBS_CCCSR_CS1, (uint32_t)(SBS_CompCode));
746 }
747 
748 /**
749   * @brief  Code selection for the VDDIO I/O Compensation cell
750   * @param  SBS_CompCode: Selects the code to be applied for the I/O compensation cell
751   *         This parameter can be one of the following values:
752   *           @arg SBS_VDDIO_CELL_CODE : Select Code from the cell (available in the SBS_CCVALR)
753   *           @arg SBS_VDDIO_REGISTER_CODE: Select Code from the SBS compensation cell code register (SBS_CCSWCR)
754   * @retval None
755   */
HAL_SBS_VDDIOCompensationCodeSelect(uint32_t SBS_CompCode)756 void HAL_SBS_VDDIOCompensationCodeSelect(uint32_t SBS_CompCode)
757 {
758   /* Check the parameter */
759   assert_param(IS_SBS_VDDIO_CODE_SELECT(SBS_CompCode));
760   MODIFY_REG(SBS->CCCSR, SBS_CCCSR_CS2, (uint32_t)(SBS_CompCode));
761 }
762 
763 /**
764   * @brief  VDDIO1 I/O Compensation cell get ready flag status
765   * @param  None
766   * @retval State of bit (1 or 0).
767   */
HAL_SBS_GetVddIO1CompensationCellReadyFlag(void)768 uint32_t HAL_SBS_GetVddIO1CompensationCellReadyFlag(void)
769 {
770   return ((READ_BIT(SBS->CCCSR, SBS_CCCSR_RDY1) == SBS_CCCSR_RDY1) ? 1UL : 0UL);
771 }
772 
773 /**
774   * @brief  VDDIO2 I/O Compensation cell get ready flag status
775   * @param  None
776   * @retval State of bit (1 or 0).
777   */
HAL_SBS_GetVddIO2CompensationCellReadyFlag(void)778 uint32_t HAL_SBS_GetVddIO2CompensationCellReadyFlag(void)
779 {
780   return ((READ_BIT(SBS->CCCSR, SBS_CCCSR_RDY2) == SBS_CCCSR_RDY2) ? 1UL : 0UL);
781 }
782 
783 /**
784   * @brief  Code selection for the VDD I/O Compensation cell
785   * @param  SBS_PMOSCode: PMOS compensation code
786   *         This code is applied to the VDD I/O compensation cell when the CS1 bit of the
787   *          SBS_CCSR is set
788   * @param  SBS_NMOSCode: NMOS compensation code
789   *         This code is applied to the VDD I/O compensation cell when the CS1 bit of the
790   *          SBS_CCSR is set
791   * @retval None
792   */
HAL_SBS_VDDCompensationCodeConfig(uint32_t SBS_PMOSCode,uint32_t SBS_NMOSCode)793 void HAL_SBS_VDDCompensationCodeConfig(uint32_t SBS_PMOSCode, uint32_t SBS_NMOSCode)
794 {
795   /* Check the parameter */
796   assert_param(IS_SBS_CODE_CONFIG(SBS_PMOSCode));
797   assert_param(IS_SBS_CODE_CONFIG(SBS_NMOSCode));
798   MODIFY_REG(SBS->CCSWCR, SBS_CCSWCR_SW_ANSRC1 | SBS_CCSWCR_SW_APSRC1, (((uint32_t)(SBS_PMOSCode) << 4) | \
799                                                                         (uint32_t)(SBS_NMOSCode)));
800 }
801 
802 /**
803   * @brief  Code selection for the VDDIO I/O Compensation cell
804   * @param  SBS_PMOSCode: PMOS compensation code
805   *         This code is applied to the VDDIO I/O compensation cell when the CS2 bit of the
806   *          SBS_CCSR is set
807   * @param  SBS_NMOSCode: NMOS compensation code
808   *         This code is applied to the VDDIO I/O compensation cell when the CS2 bit of the
809   *          SBS_CCSR is set
810   * @retval None
811   */
HAL_SBS_VDDIOCompensationCodeConfig(uint32_t SBS_PMOSCode,uint32_t SBS_NMOSCode)812 void HAL_SBS_VDDIOCompensationCodeConfig(uint32_t SBS_PMOSCode, uint32_t SBS_NMOSCode)
813 {
814   /* Check the parameter */
815   assert_param(IS_SBS_CODE_CONFIG(SBS_PMOSCode));
816   assert_param(IS_SBS_CODE_CONFIG(SBS_NMOSCode));
817   MODIFY_REG(SBS->CCSWCR, SBS_CCSWCR_SW_ANSRC2 | SBS_CCSWCR_SW_APSRC2, (((uint32_t)(SBS_PMOSCode) << 12) | \
818                                                                         ((uint32_t)(SBS_NMOSCode) << 8)));
819 }
820 
821 /**
822   * @brief  Get NMOS compensation value of the I/Os supplied by VDD
823   * @param  None
824   * @retval None
825   */
HAL_SBS_GetNMOSVddCompensationValue(void)826 uint32_t HAL_SBS_GetNMOSVddCompensationValue(void)
827 {
828   return (uint32_t)(READ_BIT(SBS->CCVALR, SBS_CCVALR_ANSRC1));
829 }
830 
831 /**
832   * @brief  Get PMOS compensation value of the I/Os supplied by VDD
833   * @param  None
834   * @retval None
835   */
HAL_SBS_GetPMOSVddCompensationValue(void)836 uint32_t HAL_SBS_GetPMOSVddCompensationValue(void)
837 {
838   return (uint32_t)(READ_BIT(SBS->CCVALR, SBS_CCVALR_APSRC1) >>  SBS_CCVALR_APSRC1_Pos);
839 }
840 
841 /**
842   * @brief  Get NMOS compensation value of the I/Os supplied by VDDIO2
843   * @param  None
844   * @retval None
845   */
HAL_SBS_GetNMOSVddIO2CompensationValue(void)846 uint32_t HAL_SBS_GetNMOSVddIO2CompensationValue(void)
847 {
848   return (uint32_t)(READ_BIT(SBS->CCVALR, SBS_CCVALR_ANSRC2) >>  SBS_CCVALR_ANSRC2_Pos);
849 }
850 
851 
852 /**
853   * @brief  Get PMOS compensation value of the I/Os supplied by VDDIO2
854   * @param  None
855   * @retval None
856   */
HAL_SBS_GetPMOSVddIO2CompensationValue(void)857 uint32_t HAL_SBS_GetPMOSVddIO2CompensationValue(void)
858 {
859   return (uint32_t)(READ_BIT(SBS->CCVALR, SBS_CCVALR_APSRC2) >>  SBS_CCVALR_APSRC2_Pos);
860 }
861 
862 #if defined(SBS_EPOCHSELCR_EPOCH_SEL)
863 /**
864   * @brief  Select EPOCH security sent to SAES IP to encrypt/decrypt keys
865   * @param  Epoch_Selection: Select EPOCH security
866   *         This parameter can be one of the following values:
867   *           @arg SBS_EPOCH_SEL_SECURE    : EPOCH secure selected.
868   *           @arg SBS_EPOCH_SEL_NONSECURE : EPOCH non secure selected.
869   *           @arg SBS_EPOCH_SEL_PUFCHECK  : EPOCH all zeros for PUF integrity check.
870   * @retval None
871   */
HAL_SBS_EPOCHSelection(uint32_t Epoch_Selection)872 void HAL_SBS_EPOCHSelection(uint32_t Epoch_Selection)
873 {
874   /* Check the parameter */
875   assert_param(IS_SBS_EPOCH_SELECTION(Epoch_Selection));
876 
877   MODIFY_REG(SBS->EPOCHSELCR, SBS_EPOCHSELCR_EPOCH_SEL, (uint32_t)(Epoch_Selection));
878 }
879 
880 /**
881   * @brief  Get EPOCH security selection
882   * @param  none
883   * @retval Returned value can be one of the following values:
884   *           @arg SBS_EPOCH_SEL_SECURE    : EPOCH secure selected.
885   *           @arg SBS_EPOCH_SEL_NONSECURE : EPOCH non secure selected.
886   *           @arg SBS_EPOCH_SEL_PUFCHECK  : EPOCH all zeros for PUF integrity check.
887   */
HAL_SBS_GetEPOCHSelection(void)888 uint32_t HAL_SBS_GetEPOCHSelection(void)
889 {
890   return (uint32_t)(READ_BIT(SBS->EPOCHSELCR, SBS_EPOCHSELCR_EPOCH_SEL));
891 }
892 #endif /* SBS_EPOCHSELCR_EPOCH_SEL */
893 
894 /**
895   * @brief  Increment by 1 the HDPL value
896   * @param  None
897   * @retval None
898   */
HAL_SBS_IncrementHDPLValue(void)899 void HAL_SBS_IncrementHDPLValue(void)
900 {
901   MODIFY_REG(SBS->HDPLCR, SBS_HDPLCR_INCR_HDPL, 0x00000006AU);
902 }
903 
904 /**
905   * @brief  Get the HDPL Value.
906   *
907   * @retval  Returns the HDPL value
908   *          This return value can be one of the following values:
909   *            @arg SBS_HDPL_VALUE_0: HDPL0
910   *            @arg SBS_HDPL_VALUE_1: HDPL1
911   *            @arg SBS_HDPL_VALUE_2: HDPL2
912   *            @arg SBS_HDPL_VALUE_3: HDPL3
913   */
HAL_SBS_GetHDPLValue(void)914 uint32_t HAL_SBS_GetHDPLValue(void)
915 {
916   return (uint32_t)(READ_BIT(SBS->HDPLSR, SBS_HDPLSR_HDPL));
917 }
918 
919 #if defined(SBS_NEXTHDPLCR_NEXTHDPL)
920 /**
921   * @brief  Set the OBK-HDPL Value.
922   * @param  Set the increment to add to HDPL value to generate the OBK-HDPL.
923   *         This parameter can be one of the following values:
924   *           @arg SBS_OBKHDPL_INCR_0 : HDPL
925   *           @arg SBS_OBKHDPL_INCR_1 : HDPL + 1
926   *           @arg SBS_OBKHDPL_INCR_2 : HDPL + 2
927   *           @arg SBS_OBKHDPL_INCR_3 : HDPL + 3
928   * @retval None
929   */
HAL_SBS_SetOBKHDPL(uint32_t OBKHDPL_Value)930 void HAL_SBS_SetOBKHDPL(uint32_t OBKHDPL_Value)
931 {
932   /* Check the parameter */
933   assert_param(IS_SBS_OBKHDPL_SELECTION(OBKHDPL_Value));
934 
935   MODIFY_REG(SBS->NEXTHDPLCR, SBS_NEXTHDPLCR_NEXTHDPL, (uint32_t)(OBKHDPL_Value));
936 }
937 
938 /**
939   * @brief  Get the OBK-HDPL Value.
940   * @retval  Returns the incremement to add to HDPL value to generate OBK-HDPL
941   *          This return value can be one of the following values:
942   *            @arg SBS_OBKHDPL_INCR_0: HDPL
943   *            @arg SBS_OBKHDPL_INCR_1: HDPL + 1
944   *            @arg SBS_OBKHDPL_INCR_2: HDPL + 2
945   *            @arg SBS_OBKHDPL_INCR_3: HDPL + 3
946   */
HAL_SBS_GetOBKHDPL(void)947 uint32_t HAL_SBS_GetOBKHDPL(void)
948 {
949   return (uint32_t)(READ_BIT(SBS->NEXTHDPLCR, SBS_NEXTHDPLCR_NEXTHDPL));
950 }
951 #endif /* SBS_NEXTHDPLCR_NEXTHDPL */
952 
953 /**
954   * @brief  Disable the NMI in case of double ECC error in FLASH Interface.
955   *
956   * @retval None
957   */
HAL_SBS_FLASH_DisableECCNMI(void)958 void HAL_SBS_FLASH_DisableECCNMI(void)
959 {
960   SET_BIT(SBS->ECCNMIR, SBS_ECCNMIR_ECCNMI_MASK_EN);
961 }
962 
963 /**
964   * @brief  Enable the NMI in case of double ECC error in FLASH Interface.
965   *
966   * @retval None
967   */
HAL_SBS_FLASH_EnableECCNMI(void)968 void HAL_SBS_FLASH_EnableECCNMI(void)
969 {
970   CLEAR_BIT(SBS->ECCNMIR, SBS_ECCNMIR_ECCNMI_MASK_EN);
971 }
972 
973 /**
974   * @brief  Check if the NMI is Enabled in case of double ECC error in FLASH Interface.
975   *
976   * @retval State of bit (1 or 0).
977   */
HAL_SBS_FLASH_ECCNMI_IsDisabled(void)978 uint32_t HAL_SBS_FLASH_ECCNMI_IsDisabled(void)
979 {
980   return ((READ_BIT(SBS->ECCNMIR, SBS_ECCNMIR_ECCNMI_MASK_EN) == SBS_ECCNMIR_ECCNMI_MASK_EN) ? 1UL : 0UL);
981 }
982 
983 
984 /**
985   * @}
986   */
987 
988 /** @defgroup HAL_Exported_Functions_Group5 HAL SBS lock management functions
989   *  @brief SBS lock management functions.
990   *
991 @verbatim
992  =======================================================================================================================
993                                         ##### SBS lock functions #####
994  =======================================================================================================================
995 
996 @endverbatim
997   * @{
998   */
999 
1000 /**
1001   * @brief  Lock the SBS item(s).
1002   * @note   Setting lock(s) depends on privilege mode in secure/non-secure code
1003   *         Lock(s) cleared only at system reset
1004   * @param  Item Item(s) to set lock on.
1005   *         This parameter can be a combination of @ref SBS_Lock_items
1006   * @retval None
1007   */
HAL_SBS_Lock(uint32_t Item)1008 void HAL_SBS_Lock(uint32_t Item)
1009 {
1010   /* Check the parameters */
1011   assert_param(IS_SBS_LOCK_ITEMS(Item));
1012 
1013   /* Privilege secure/non-secure locks */
1014   SBS->CNSLCKR = (0xFFFFU & Item);  /* non-secure lock item in 16 lowest bits */
1015 
1016 #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
1017   /* Privilege secure only locks */
1018   SBS->CSLCKR = ((0xFFFF0000U & Item) >> 16U);  /* Secure-only lock item in 16 highest bits */
1019 #endif /* __ARM_FEATURE_CMSE */
1020 }
1021 
1022 /**
1023   * @brief  Get the lock state of SBS items.
1024   * @note   Getting lock(s) depends on privilege mode in secure/non-secure code
1025   * @param  pItem pointer to return locked items
1026   *         the return value can be a combination of @ref SBS_Lock_items
1027   * @retval HAL status
1028   */
HAL_SBS_GetLock(uint32_t * pItem)1029 HAL_StatusTypeDef HAL_SBS_GetLock(uint32_t *pItem)
1030 {
1031   uint32_t tmp_lock;
1032 
1033   /* Check null pointer */
1034   if (pItem == NULL)
1035   {
1036     return HAL_ERROR;
1037   }
1038 
1039   /* Get the non-secure lock state */
1040   tmp_lock = SBS->CNSLCKR;
1041 
1042   /* Get the secure lock state in secure code */
1043 #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
1044   tmp_lock |= (SBS->CSLCKR << 16U);
1045 #endif /* __ARM_FEATURE_CMSE */
1046 
1047   /* Return overall lock status */
1048   *pItem = tmp_lock;
1049 
1050   return HAL_OK;
1051 }
1052 
1053 /**
1054   * @}
1055   */
1056 
1057 /** @defgroup HAL_Exported_Functions_Group6 HAL SBS attributes management functions
1058   *  @brief SBS attributes management functions.
1059   *
1060 @verbatim
1061  =======================================================================================================================
1062                                         ##### SBS attributes functions #####
1063  =======================================================================================================================
1064 
1065 @endverbatim
1066   * @{
1067   */
1068 
1069 #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
1070 /**
1071   * @brief  Configure the SBS item attribute(s).
1072   * @note   Available attributes are to secure SBS items, so this function is
1073   *         only available in secure.
1074   *         SBS_FPU item attribute is only configurable through PRIVILEGE transaction.
1075   * @param  Item Item(s) to set attributes on.
1076   *         This parameter can be a one or a combination of @ref SBS_Attributes_items
1077   * @param  Attributes  specifies the secure/non-secure attributes.
1078   * @retval None
1079   */
HAL_SBS_ConfigAttributes(uint32_t Item,uint32_t Attributes)1080 void HAL_SBS_ConfigAttributes(uint32_t Item, uint32_t Attributes)
1081 {
1082   uint32_t tmp;
1083 
1084   /* Check the parameters */
1085   assert_param(IS_SBS_ITEMS_ATTRIBUTES(Item));
1086   assert_param(IS_SBS_ATTRIBUTES(Attributes));
1087 
1088   tmp = SBS->SECCFGR;
1089 
1090   /* Set or reset Item */
1091   if ((Attributes & SBS_SEC) != 0x00U)
1092   {
1093     tmp |= Item;
1094   }
1095   else
1096   {
1097     tmp &= ~Item;
1098   }
1099 
1100   /* Set secure attributes */
1101   SBS->SECCFGR = tmp;
1102 }
1103 
1104 
1105 /**
1106   * @brief  Get the attribute of a SBS items.
1107   * @note   Available attributes have read restrictions, so this function is
1108   *         only available in secure
1109   * @param  Item Single item to get secure/non-secure attribute from.
1110   * @param  pAttributes pointer to return the attribute.
1111   * @retval HAL status
1112   */
HAL_SBS_GetConfigAttributes(uint32_t Item,uint32_t * pAttributes)1113 HAL_StatusTypeDef HAL_SBS_GetConfigAttributes(uint32_t Item, uint32_t *pAttributes)
1114 {
1115   /* Check null pointer */
1116   if (pAttributes == NULL)
1117   {
1118     return HAL_ERROR;
1119   }
1120 
1121   /* Check the parameters */
1122   assert_param(IS_SBS_ITEMS_ATTRIBUTES(Item));
1123 
1124   /* Get the secure attribute state */
1125   if ((SBS->SECCFGR & Item) != 0U)
1126   {
1127     *pAttributes = SBS_SEC;
1128   }
1129   else
1130   {
1131     *pAttributes = SBS_NSEC;
1132   }
1133 
1134   return HAL_OK;
1135 }
1136 #endif /* __ARM_FEATURE_CMSE */
1137 
1138 /**
1139   * @}
1140   */
1141 
1142 /**
1143   * @}
1144   */
1145 
1146 #endif /* HAL_MODULE_ENABLED */
1147 /**
1148   * @}
1149   */
1150 
1151 /**
1152   * @}
1153   */
1154 
1155