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