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