1 /**
2   ******************************************************************************
3   * @file    stm32wbaxx_hal.c
4   * @author  MCD Application Team
5   * @brief   HAL module driver.
6   *          This is the common part of the HAL initialization
7   *
8   ******************************************************************************
9   * @attention
10   *
11   * Copyright (c) 2022 STMicroelectronics.
12   * All rights reserved.
13   *
14   * This software is licensed under terms that can be found in the LICENSE file
15   * in the root directory of this software component.
16   * If no LICENSE file comes with this software, it is provided AS-IS.
17   *
18   ******************************************************************************
19   @verbatim
20   ==============================================================================
21                      ##### How to use this driver #####
22   ==============================================================================
23     [..]
24     The common HAL driver contains a set of generic and common APIs that can be
25     used by the PPP peripheral drivers and the user to start using the HAL.
26     [..]
27     The HAL contains two APIs' categories:
28          (+) Common HAL APIs (Version, Init, Tick)
29          (+) Services HAL APIs (DBGMCU, SYSCFG)
30 
31   @endverbatim
32   ******************************************************************************
33   */
34 
35 /* Includes ------------------------------------------------------------------*/
36 #include "stm32wbaxx_hal.h"
37 
38 /** @addtogroup STM32WBAxx_HAL_Driver
39   * @{
40   */
41 
42 /** @defgroup HAL HAL
43   * @brief HAL module driver
44   * @{
45   */
46 
47 #ifdef HAL_MODULE_ENABLED
48 
49 /* Private typedef -----------------------------------------------------------*/
50 /* Private define ------------------------------------------------------------*/
51 
52 /* Private macros ------------------------------------------------------------*/
53 /* Private variables ---------------------------------------------------------*/
54 /* Private function prototypes -----------------------------------------------*/
55 
56 /* Exported variables --------------------------------------------------------*/
57 
58 /** @defgroup HAL_Exported_Variables HAL Exported Variables
59   * @{
60   */
61 __IO uint32_t uwTick;
62 uint32_t uwTickPrio            = (1UL << __NVIC_PRIO_BITS); /* Invalid priority */
63 HAL_TickFreqTypeDef uwTickFreq = HAL_TICK_FREQ_DEFAULT;  /* 1KHz */
64 /**
65   * @}
66   */
67 
68 /* Exported functions --------------------------------------------------------*/
69 
70 /** @defgroup HAL_Exported_Functions HAL Exported Functions
71   * @{
72   */
73 
74 /** @defgroup HAL_Exported_Functions_Group1 Initialization and de-initialization Functions
75   *  @brief    Initialization and de-initialization functions
76   *
77 @verbatim
78  ===============================================================================
79               ##### Initialization and de-initialization functions #####
80  ===============================================================================
81     [..]  This section provides functions allowing to:
82       (+) Initialize the Flash interface the NVIC allocation and initial time base
83           clock configuration.
84       (+) De-initialize common part of the HAL.
85       (+) Configure the time base source to have 1ms time base with a dedicated
86           Tick interrupt priority.
87         (++) SysTick timer is used by default as source of time base, but user
88              can eventually implement his proper time base source (a general purpose
89              timer for example or other time source), keeping in mind that Time base
90              duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and
91              handled in milliseconds basis.
92         (++) Time base configuration function (HAL_InitTick ()) is called automatically
93              at the beginning of the program after reset by HAL_Init() or at any time
94              when clock is configured, by HAL_RCC_ClockConfig().
95         (++) Source of time base is configured  to generate interrupts at regular
96              time intervals. Care must be taken if HAL_Delay() is called from a
97              peripheral ISR process, the Tick interrupt line must have higher priority
98             (numerically lower) than the peripheral interrupt. Otherwise the caller
99             ISR process will be blocked.
100        (++) functions affecting time base configurations are declared as __weak
101              to make  override possible  in case of other  implementations in user file.
102 @endverbatim
103   * @{
104   */
105 
106 /**
107   * @brief  Configure the Flash prefetch, the time base source, NVIC and any required global low
108   *         level hardware by calling the HAL_MspInit() callback function to be optionally defined
109   *         in user file stm32wbaxx_hal_msp.c.
110   *
111   * @note   HAL_Init() function is called at the beginning of program after reset and before
112   *         the clock configuration.
113   *
114   * @note   In the default implementation the System Timer (SysTick) is used as source of time base.
115   *         The SysTick configuration is based on HSI clock, as HSI is the clock
116   *         used after a system Reset and the NVIC configuration is set to Priority group 4.
117   *         Once done, time base tick starts incrementing: the tick variable counter is incremented
118   *         each 1ms in the SysTick_Handler() interrupt handler.
119   *
120   * @retval HAL status
121   */
HAL_Init(void)122 HAL_StatusTypeDef HAL_Init(void)
123 {
124   /* Configure Flash prefetch */
125 #if (PREFETCH_ENABLE != 0U)
126   __HAL_FLASH_PREFETCH_BUFFER_ENABLE();
127 #endif /* PREFETCH_ENABLE */
128 
129   /* Set Interrupt Group Priority */
130   HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4);
131 
132   /* Ensure time base clock coherency */
133   SystemCoreClockUpdate();
134 
135   /* Select HCLK as SysTick clock source */
136   HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
137 
138   /* Initialize 1ms tick time base (default SysTick based on HSI clock after Reset) */
139   if (HAL_InitTick(TICK_INT_PRIORITY) != HAL_OK)
140   {
141     return HAL_ERROR;
142   }
143 
144   /* Init the low level hardware */
145   HAL_MspInit();
146 
147   /* Return function status */
148   return HAL_OK;
149 }
150 
151 /**
152   * @brief De-initialize common part of the HAL and stop the source of time base.
153   * @note This function is optional.
154   * @retval HAL status
155   */
HAL_DeInit(void)156 HAL_StatusTypeDef HAL_DeInit(void)
157 {
158   /* Reset of all peripherals */
159   __HAL_RCC_APB1_FORCE_RESET();
160   __HAL_RCC_APB1_RELEASE_RESET();
161 
162   __HAL_RCC_APB2_FORCE_RESET();
163   __HAL_RCC_APB2_RELEASE_RESET();
164 
165   __HAL_RCC_APB7_FORCE_RESET();
166   __HAL_RCC_APB7_RELEASE_RESET();
167 
168   __HAL_RCC_AHB1_FORCE_RESET();
169   __HAL_RCC_AHB1_RELEASE_RESET();
170 
171   __HAL_RCC_AHB2_FORCE_RESET();
172   __HAL_RCC_AHB2_RELEASE_RESET();
173 
174   __HAL_RCC_AHB4_FORCE_RESET();
175   __HAL_RCC_AHB4_RELEASE_RESET();
176 
177   __HAL_RCC_AHB5_FORCE_RESET();
178   __HAL_RCC_AHB5_RELEASE_RESET();
179 
180   /* De-Init the low level hardware */
181   HAL_MspDeInit();
182 
183   /* Return function status */
184   return HAL_OK;
185 }
186 
187 /**
188   * @brief  Initialize the MSP.
189   * @retval None
190   */
HAL_MspInit(void)191 __weak void HAL_MspInit(void)
192 {
193   /* NOTE : This function should not be modified, when the callback is needed,
194             the HAL_MspInit could be implemented in the user file
195    */
196 }
197 
198 /**
199   * @brief  DeInitialize the MSP.
200   * @retval None
201   */
HAL_MspDeInit(void)202 __weak void HAL_MspDeInit(void)
203 {
204   /* NOTE : This function should not be modified, when the callback is needed,
205             the HAL_MspDeInit could be implemented in the user file
206    */
207 }
208 
209 /**
210   * @brief This function configures the source of the time base:
211   *        The time source is configured to have 1ms time base with a dedicated
212   *        Tick interrupt priority.
213   * @note This function is called  automatically at the beginning of program after
214   *       reset by HAL_Init() or at any time when clock is reconfigured  by HAL_RCC_ClockConfig().
215   * @note In the default implementation, SysTick timer is the source of time base.
216   *       It is used to generate interrupts at regular time intervals.
217   *       Care must be taken if HAL_Delay() is called from a peripheral ISR process,
218   *       The SysTick interrupt must have higher priority (numerically lower)
219   *       than the peripheral interrupt. Otherwise the caller ISR process will be blocked.
220   *       The function is declared as __weak  to be overwritten  in case of other
221   *       implementation  in user file.
222   * @param TickPriority  Tick interrupt priority.
223   * @retval HAL status
224   */
HAL_InitTick(uint32_t TickPriority)225 __weak HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
226 {
227   uint32_t ticknumber = 0U;
228   uint32_t systicksel;
229 
230   /* Check uwTickFreq for MisraC 2012 (even if uwTickFreq is a enum type that don't take the value zero)*/
231   if ((uint32_t)uwTickFreq == 0UL)
232   {
233     return HAL_ERROR;
234   }
235 
236   /* Check Clock source to calculate the tickNumber */
237   if(READ_BIT(SysTick->CTRL, SysTick_CTRL_CLKSOURCE_Msk) == SysTick_CTRL_CLKSOURCE_Msk)
238   {
239     /* HCLK selected as SysTick clock source */
240     ticknumber = SystemCoreClock / (1000UL / (uint32_t)uwTickFreq);
241   }
242   else
243   {
244     systicksel = __HAL_RCC_GET_SYSTICK_SOURCE();
245     switch (systicksel)
246     {
247       /* HCLK_DIV8 selected as SysTick clock source */
248       case RCC_SYSTICKCLKSOURCE_HCLK_DIV8:
249         /* Calculate tick value */
250         ticknumber = (SystemCoreClock / (8000UL / (uint32_t)uwTickFreq));
251         break;
252 
253       /* LSI selected as SysTick clock source */
254       case RCC_SYSTICKCLKSOURCE_LSI:
255         /* Calculate tick value */
256         ticknumber = (LSI_VALUE / (1000UL / (uint32_t)uwTickFreq));
257         break;
258 
259       /* LSE selected as SysTick clock source */
260       case RCC_SYSTICKCLKSOURCE_LSE:
261         /* Calculate tick value */
262         ticknumber = (LSE_VALUE / (1000UL / (uint32_t)uwTickFreq));
263         break;
264 
265       default:
266         /* Nothing to do */
267         break;
268     }
269   }
270 
271   /* Configure the SysTick */
272   if (HAL_SYSTICK_Config(ticknumber) > 0U)
273   {
274     return HAL_ERROR;
275   }
276 
277   /* Configure the SysTick IRQ priority */
278   HAL_NVIC_SetPriority(SysTick_IRQn, TickPriority, 0U);
279   uwTickPrio = TickPriority;
280 
281   /* Return function status */
282   return HAL_OK;
283 }
284 
285 /**
286   * @}
287   */
288 
289 /** @defgroup HAL_Exported_Functions_Group2 HAL Control functions
290   *  @brief    HAL Control functions
291   *
292 @verbatim
293  ===============================================================================
294                       ##### HAL Control functions #####
295  ===============================================================================
296     [..]  This section provides functions allowing to:
297       (+) Provide a tick value in millisecond
298       (+) Provide a blocking delay in millisecond
299       (+) Suspend the time base source interrupt
300       (+) Resume the time base source interrupt
301       (+) Get the HAL API driver version
302       (+) Get the device identifier
303       (+) Get the device revision identifier
304 
305 @endverbatim
306   * @{
307   */
308 
309 /**
310   * @brief This function is called to increment a global variable "uwTick"
311   *        used as application time base.
312   * @note In the default implementation, this variable is incremented each 1ms
313   *       in SysTick ISR.
314   * @note This function is declared as __weak to be overwritten in case of other
315   *      implementations in user file.
316   * @retval None
317   */
HAL_IncTick(void)318 __weak void HAL_IncTick(void)
319 {
320   uwTick += (uint32_t)uwTickFreq;
321 }
322 
323 /**
324   * @brief Provide a tick value in millisecond.
325   * @note This function is declared as __weak to be overwritten in case of other
326   *       implementations in user file.
327   * @retval tick value
328   */
HAL_GetTick(void)329 __weak uint32_t HAL_GetTick(void)
330 {
331   return uwTick;
332 }
333 
334 /**
335   * @brief This function returns a tick priority.
336   * @retval tick priority
337   */
HAL_GetTickPrio(void)338 uint32_t HAL_GetTickPrio(void)
339 {
340   return uwTickPrio;
341 }
342 
343 /**
344   * @brief Set new tick frequency.
345   * @param Freq tick frequency
346   * @retval HAL status
347   */
HAL_SetTickFreq(HAL_TickFreqTypeDef Freq)348 HAL_StatusTypeDef HAL_SetTickFreq(HAL_TickFreqTypeDef Freq)
349 {
350   HAL_StatusTypeDef status  = HAL_OK;
351   HAL_TickFreqTypeDef prevTickFreq;
352 
353   if (uwTickFreq != Freq)
354   {
355     /* Back up uwTickFreq frequency */
356     prevTickFreq = uwTickFreq;
357 
358     /* Update uwTickFreq global variable used by HAL_InitTick() */
359     uwTickFreq = Freq;
360 
361     /* Apply the new tick Freq  */
362     status = HAL_InitTick(uwTickPrio);
363     if (status != HAL_OK)
364     {
365       /* Restore previous tick frequency */
366       uwTickFreq = prevTickFreq;
367     }
368   }
369 
370   return status;
371 }
372 
373 /**
374   * @brief Return tick frequency.
375   * @retval Tick frequency.
376   *         Value of @ref HAL_TickFreqTypeDef.
377   */
HAL_GetTickFreq(void)378 HAL_TickFreqTypeDef HAL_GetTickFreq(void)
379 {
380   return uwTickFreq;
381 }
382 
383 /**
384   * @brief This function provides minimum delay (in milliseconds) based
385   *        on variable incremented.
386   * @note In the default implementation , SysTick timer is the source of time base.
387   *       It is used to generate interrupts at regular time intervals where uwTick
388   *       is incremented.
389   * @note This function is declared as __weak to be overwritten in case of other
390   *       implementations in user file.
391   * @param Delay  specifies the delay time length, in milliseconds.
392   * @retval None
393   */
HAL_Delay(uint32_t Delay)394 __weak void HAL_Delay(uint32_t Delay)
395 {
396   uint32_t tickstart = HAL_GetTick();
397   uint32_t wait = Delay;
398 
399   /* Add a freq to guarantee minimum wait */
400   if (wait < HAL_MAX_DELAY)
401   {
402     wait += (uint32_t)(uwTickFreq);
403   }
404 
405   while ((HAL_GetTick() - tickstart) < wait)
406   {
407   }
408 }
409 
410 /**
411   * @brief Suspend Tick increment.
412   * @note In the default implementation , SysTick timer is the source of time base. It is
413   *       used to generate interrupts at regular time intervals. Once HAL_SuspendTick()
414   *       is called, the SysTick interrupt will be disabled and so Tick increment
415   *       is suspended.
416   * @note This function is declared as __weak to be overwritten in case of other
417   *       implementations in user file.
418   * @retval None
419   */
HAL_SuspendTick(void)420 __weak void HAL_SuspendTick(void)
421 {
422   /* Disable SysTick Interrupt */
423   SysTick->CTRL &= ~SysTick_CTRL_TICKINT_Msk;
424 }
425 
426 /**
427   * @brief Resume Tick increment.
428   * @note In the default implementation , SysTick timer is the source of time base. It is
429   *       used to generate interrupts at regular time intervals. Once HAL_ResumeTick()
430   *       is called, the SysTick interrupt will be enabled and so Tick increment
431   *       is resumed.
432   * @note This function is declared as __weak to be overwritten in case of other
433   *       implementations in user file.
434   * @retval None
435   */
HAL_ResumeTick(void)436 __weak void HAL_ResumeTick(void)
437 {
438   /* Enable SysTick Interrupt */
439   SysTick->CTRL  |= SysTick_CTRL_TICKINT_Msk;
440 }
441 
442 /**
443   * @brief  Return the HAL revision.
444   * @retval version : 0xXYZR (8bits for each decimal, R for RC)
445   */
HAL_GetHalVersion(void)446 uint32_t HAL_GetHalVersion(void)
447 {
448   return __STM32WBAxx_HAL_VERSION;
449 }
450 
451 /**
452   * @brief  Return the device revision identifier.
453   * @retval Device revision identifier
454   */
HAL_GetREVID(void)455 uint32_t HAL_GetREVID(void)
456 {
457   return ((DBGMCU->IDCODE & DBGMCU_IDCODE_REV_ID) >> DBGMCU_IDCODE_REV_ID_Pos);
458 }
459 
460 /**
461   * @brief  Return the device identifier.
462   * @retval Device identifier
463   */
HAL_GetDEVID(void)464 uint32_t HAL_GetDEVID(void)
465 {
466   return (DBGMCU->IDCODE & DBGMCU_IDCODE_DEV_ID);
467 }
468 
469 /**
470   * @brief  Return the first word of the unique device identifier (UID based on 96 bits)
471   * @retval Device identifier
472   */
HAL_GetUIDw0(void)473 uint32_t HAL_GetUIDw0(void)
474 {
475   return (READ_REG(*((uint32_t *)UID_BASE)));
476 }
477 
478 /**
479   * @brief  Return the second word of the unique device identifier (UID based on 96 bits)
480   * @retval Device identifier
481   */
HAL_GetUIDw1(void)482 uint32_t HAL_GetUIDw1(void)
483 {
484   return (READ_REG(*((uint32_t *)(UID_BASE + 4U))));
485 }
486 
487 /**
488   * @brief  Return the third word of the unique device identifier (UID based on 96 bits)
489   * @retval Device identifier
490   */
HAL_GetUIDw2(void)491 uint32_t HAL_GetUIDw2(void)
492 {
493   return (READ_REG(*((uint32_t *)(UID_BASE + 8U))));
494 }
495 /**
496   * @}
497   */
498 
499 /** @defgroup HAL_Exported_Functions_Group3 HAL Debug functions
500   *  @brief    HAL Debug functions
501   *
502 @verbatim
503  ===============================================================================
504                       ##### HAL Debug functions #####
505  ===============================================================================
506     [..]  This section provides functions allowing to:
507       (+) Enable/Disable Debug module during STOP0/STOP1/STOP2 modes
508       (+) Enable/Disable Debug module during STANDBY mode
509 
510 @endverbatim
511   * @{
512   */
513 
514 /**
515   * @brief  Enable the Debug Module during STOP0/STOP1/STOP2 modes.
516   * @retval None
517   */
HAL_DBGMCU_EnableDBGStopMode(void)518 void HAL_DBGMCU_EnableDBGStopMode(void)
519 {
520   SET_BIT(DBGMCU->SCR, DBGMCU_SCR_DBG_STOP);
521 }
522 
523 /**
524   * @brief  Disable the Debug Module during STOP0/STOP1/STOP2 modes.
525   * @retval None
526   */
HAL_DBGMCU_DisableDBGStopMode(void)527 void HAL_DBGMCU_DisableDBGStopMode(void)
528 {
529   CLEAR_BIT(DBGMCU->SCR, DBGMCU_SCR_DBG_STOP);
530 }
531 
532 /**
533   * @brief  Enable the Debug Module during STANDBY mode.
534   * @retval None
535   */
HAL_DBGMCU_EnableDBGStandbyMode(void)536 void HAL_DBGMCU_EnableDBGStandbyMode(void)
537 {
538   SET_BIT(DBGMCU->SCR, DBGMCU_SCR_DBG_STANDBY);
539 }
540 
541 /**
542   * @brief  Disable the Debug Module during STANDBY mode.
543   * @retval None
544   */
HAL_DBGMCU_DisableDBGStandbyMode(void)545 void HAL_DBGMCU_DisableDBGStandbyMode(void)
546 {
547   CLEAR_BIT(DBGMCU->SCR, DBGMCU_SCR_DBG_STANDBY);
548 }
549 
550 /**
551   * @}
552   */
553 
554 /** @defgroup HAL_Exported_Functions_Group4 HAL SYSCFG configuration functions
555   *  @brief    HAL SYSCFG configuration functions
556   *
557 @verbatim
558  ===============================================================================
559                       ##### HAL SYSCFG configuration functions #####
560  ===============================================================================
561     [..]  This section provides functions allowing to:
562       (+) Enable/Disable the I/O analog switch voltage booster
563       (+) Configure the Voltage reference buffer
564       (+) Enable/Disable the Voltage reference buffer
565       (+) Enable/Disbale the OTG PHY
566       (+) Configure the OTG PHY power down
567       (+) Select the OTG PHY reference clock
568       (+) Configure the OTG PHY disconnect/squelch threshold
569       (+) Configure the OTG PHY transmitter pre-emphasis current
570       (+) Enable/Disable the compensation cell
571       (+) Get the compensation cell ready status
572       (+) Configure/Get the code selection for the compensation cell
573 
574 @endverbatim
575   * @{
576   */
577 
578 /**
579   * @brief  Enable the I/O analog switch voltage booster
580   *
581   * @retval None
582   */
HAL_SYSCFG_EnableIOAnalogSwitchBooster(void)583 void HAL_SYSCFG_EnableIOAnalogSwitchBooster(void)
584 {
585   MODIFY_REG(SYSCFG->CFGR1, (SYSCFG_CFGR1_BOOSTEN | SYSCFG_CFGR1_ANASWVDD), SYSCFG_CFGR1_BOOSTEN);
586 }
587 
588 /**
589   * @brief  Disable the I/O analog switch voltage booster
590   *
591   * @retval None
592   */
HAL_SYSCFG_DisableIOAnalogSwitchBooster(void)593 void HAL_SYSCFG_DisableIOAnalogSwitchBooster(void)
594 {
595   CLEAR_BIT(SYSCFG->CFGR1, SYSCFG_CFGR1_BOOSTEN);
596 }
597 
598 /**
599   * @brief  Enable the I/O analog switch supplied by VDD
600   * @note   To be used when I/O analog switch voltage booster is not enabled
601   * @retval None
602   */
HAL_SYSCFG_EnableIOAnalogSwitchVdd(void)603 void HAL_SYSCFG_EnableIOAnalogSwitchVdd(void)
604 {
605   MODIFY_REG(SYSCFG->CFGR1, (SYSCFG_CFGR1_BOOSTEN | SYSCFG_CFGR1_ANASWVDD), SYSCFG_CFGR1_ANASWVDD);
606 }
607 
608 /**
609   * @brief  Disable the I/O analog switch supplied by VDD
610   * @retval None
611   */
HAL_SYSCFG_DisableIOAnalogSwitchVdd(void)612 void HAL_SYSCFG_DisableIOAnalogSwitchVdd(void)
613 {
614   CLEAR_BIT(SYSCFG->CFGR1, SYSCFG_CFGR1_ANASWVDD);
615 }
616 
617 
618 #ifdef SYSCFG_OTGHSPHYCR_EN
619 /**
620   * @brief  Enable the OTG PHY .
621   * @param  OTGPHYConfig Defines the OTG PHY configuration.
622             This parameter can be one of @ref SYSCFG_OTG_PHY_Enable
623   * @retval None
624   */
HAL_SYSCFG_EnableOTGPHY(uint32_t OTGPHYConfig)625 void HAL_SYSCFG_EnableOTGPHY(uint32_t OTGPHYConfig)
626 {
627   /* Check the parameter */
628   assert_param(IS_SYSCFG_OTGPHY_CONFIG(OTGPHYConfig));
629 
630   MODIFY_REG(SYSCFG->OTGHSPHYCR, SYSCFG_OTGHSPHYCR_EN, OTGPHYConfig);
631 }
632 
633 /**
634   * @brief  Set the OTG PHY Power Down config.
635   * @param  PowerDownConfig Defines the OTG PHY Power down configuration.
636             This parameter can be one of @ref SYSCFG_OTG_PHY_PowerDown
637   * @retval None
638   */
HAL_SYSCFG_SetOTGPHYPowerDownConfig(uint32_t PowerDownConfig)639 void HAL_SYSCFG_SetOTGPHYPowerDownConfig(uint32_t PowerDownConfig)
640 {
641   /* Check the parameter */
642   assert_param(IS_SYSCFG_OTGPHY_POWERDOWN_CONFIG(PowerDownConfig));
643 
644   MODIFY_REG(SYSCFG->OTGHSPHYCR, SYSCFG_OTGHSPHYCR_PDCTRL, PowerDownConfig);
645 }
646 
647 /**
648   * @brief  Set the OTG PHY reference clock selection.
649   * @param  RefClkSelection Defines the OTG PHY reference clock selection.
650             This parameter can be one of the @ref SYSCFG_OTG_PHY_RefenceClockSelection
651   * @retval None
652   */
HAL_SYSCFG_SetOTGPHYReferenceClockSelection(uint32_t RefClkSelection)653 void HAL_SYSCFG_SetOTGPHYReferenceClockSelection(uint32_t RefClkSelection)
654 {
655   /* Check the parameter */
656   assert_param(IS_SYSCFG_OTGPHY_REFERENCE_CLOCK(RefClkSelection));
657 
658   MODIFY_REG(SYSCFG->OTGHSPHYCR, SYSCFG_OTGHSPHYCR_CLKSEL, RefClkSelection);
659 }
660 
661 /**
662   * @brief  Set the OTG PHY Disconnect Threshold.
663   * @param  DisconnectThreshold Defines the voltage level for the threshold used to detect a disconnect event.
664             This parameter can be one of the @ref SYSCFG_OTG_PHYTUNER_DisconnectThreshold
665   * @retval None
666   */
HAL_SYSCFG_SetOTGPHYDisconnectThreshold(uint32_t DisconnectThreshold)667 void HAL_SYSCFG_SetOTGPHYDisconnectThreshold(uint32_t DisconnectThreshold)
668 {
669   /* Check the parameter */
670   assert_param(IS_SYSCFG_OTGPHY_DISCONNECT(DisconnectThreshold));
671 
672   MODIFY_REG(SYSCFG->OTGHSPHYTUNER2, SYSCFG_OTGHSPHYTUNER2_COMPDISTUNE, DisconnectThreshold);
673 }
674 
675 /**
676   * @brief  Set the OTG PHY Squelch Threshold.
677   * @param  SquelchThreshold Defines the voltage level.
678             This parameter can be onez of the @ref SYSCFG_OTG_PHYTUNER_SquelchThreshold
679 
680   * @retval None
681   */
HAL_SYSCFG_SetOTGPHYSquelchThreshold(uint32_t SquelchThreshold)682 void HAL_SYSCFG_SetOTGPHYSquelchThreshold(uint32_t SquelchThreshold)
683 {
684   /* Check the parameter */
685   assert_param(IS_SYSCFG_OTGPHY_SQUELCH(SquelchThreshold));
686 
687   MODIFY_REG(SYSCFG->OTGHSPHYTUNER2, SYSCFG_OTGHSPHYTUNER2_SQRXTUNE, SquelchThreshold);
688 }
689 
690 /**
691   * @brief  Set the OTG PHY transmitter pre-emphasis current.
692   * @param  PreemphasisCurrent Defines the current configuration.
693             This parameter can be one of the @ref SYSCFG_OTG_PHYTUNER_PreemphasisCurrent
694 
695   * @retval None
696   */
HAL_SYSCFG_SetOTGPHYPreemphasisCurrent(uint32_t PreemphasisCurrent)697 void HAL_SYSCFG_SetOTGPHYPreemphasisCurrent(uint32_t PreemphasisCurrent)
698 {
699   /* Check the parameter */
700   assert_param(IS_SYSCFG_OTGPHY_PREEMPHASIS(PreemphasisCurrent));
701 
702   MODIFY_REG(SYSCFG->OTGHSPHYTUNER2, SYSCFG_OTGHSPHYTUNER2_TXPREEMPAMPTUNE, PreemphasisCurrent);
703 }
704 #endif /* SYSCFG_OTGHSPHYCR_EN */
705 
706 /**
707   * @brief  Enable the compensation cell
708   * @param  Selection specifies the concerned compensation cell
709   *         This parameter can the combination of the following values:
710   *            @arg SYSCFG_IO_CELL Compensation cell for the VDD I/O power rail
711   *            @arg SYSCFG_IO2_CELL Compensation cell for the VDDIO2 I/O power rail
712   * @retval None
713   */
HAL_SYSCFG_EnableCompensationCell(uint32_t Selection)714 void HAL_SYSCFG_EnableCompensationCell(uint32_t Selection)
715 {
716   /* Check the parameter */
717   assert_param(IS_SYSCFG_COMPENSATION_CELL(Selection));
718 
719   SET_BIT(SYSCFG->CCCSR, Selection);
720 }
721 
722 /**
723   * @brief  Disable the compensation cell
724   * @param  Selection specifies the concerned compensation cell
725   *         This parameter can the combination of the following values:
726   *            @arg SYSCFG_IO_CELL Compensation cell for the VDD I/O power rail
727   *            @arg SYSCFG_IO2_CELL Compensation cell for the VDDIO2 I/O power rail
728   * @retval None
729   */
HAL_SYSCFG_DisableCompensationCell(uint32_t Selection)730 void HAL_SYSCFG_DisableCompensationCell(uint32_t Selection)
731 {
732   /* Check the parameter */
733   assert_param(IS_SYSCFG_COMPENSATION_CELL(Selection));
734 
735   MODIFY_REG(SYSCFG->CCCSR, Selection, 0U);
736 }
737 
738 /**
739   * @brief  Get the compensation cell ready status
740   * @param  Selection specifies the concerned compensation cell
741   *         This parameter can one of the following values:
742   *            @arg SYSCFG_IO_CELL_READY Compensation cell for the VDD I/O power rail
743   *            @arg SYSCFG_IO2_CELL_READY Compensation cell for the VDDIO2 I/O power rail
744   * @retval Ready status (1 or 0)
745   */
HAL_SYSCFG_GetCompensationCellReadyStatus(uint32_t Selection)746 uint32_t HAL_SYSCFG_GetCompensationCellReadyStatus(uint32_t Selection)
747 {
748   /* Check the parameter */
749   assert_param(IS_SYSCFG_COMPENSATION_CELL_READY(Selection));
750 
751   return (((SYSCFG->CCCSR & Selection) == 0U) ? 0UL : 1UL);
752 }
753 
754 /**
755   * @brief  Configure the code selection for the compensation cell
756   * @param  Selection specifies the concerned compensation cell
757   *         This parameter can one of the following values:
758   *            @arg SYSCFG_IO_CELL Compensation cell for the VDD I/O power rail
759   *            @arg SYSCFG_IO2_CELL Compensation cell for the VDDIO2 I/O power rail
760   * @param  Code code selection to be applied for the I/O compensation cell
761   *         This parameter can be one of the following values:
762   *            @arg SYSCFG_IO_CELL_CODE  Code from the cell (available in the SYSCFG_CCVR)
763   *            @arg SYSCFG_IO_REGISTER_CODE Code from the compensation cell code register (SYSCFG_CCCR)
764   * @param  NmosValue In case SYSCFG_IO_REGISTER_CODE is selected, it  provides the Nmos value
765   *                   to apply in range 0 to 15 else this parameter is not used
766   * @param  PmosValue In case SYSCFG_IO_REGISTER_CODE is selected, it  provides the Pmos value
767   *                   to apply in range 0 to 15 else this parameter is not used
768   * @retval None
769   */
HAL_SYSCFG_ConfigCompensationCell(uint32_t Selection,uint32_t Code,uint32_t NmosValue,uint32_t PmosValue)770 void HAL_SYSCFG_ConfigCompensationCell(uint32_t Selection, uint32_t Code, uint32_t NmosValue, uint32_t PmosValue)
771 {
772   uint32_t offset;
773 
774   /* Check the parameters */
775   assert_param(IS_SYSCFG_COMPENSATION_CELL(Selection));
776   assert_param(IS_SYSCFG_COMPENSATION_CELL_CODE(Code));
777 
778   if (Code == SYSCFG_IO_REGISTER_CODE)
779   {
780     /* Check the parameters */
781     assert_param(IS_SYSCFG_COMPENSATION_CELL_NMOS_VALUE(NmosValue));
782     assert_param(IS_SYSCFG_COMPENSATION_CELL_PMOS_VALUE(PmosValue));
783 
784     offset = ((Selection == SYSCFG_IO_CELL) ? 0U : 8U);
785 
786     MODIFY_REG(SYSCFG->CCCR, (0xFFU << offset), ((NmosValue << offset) | (PmosValue << (offset + 4U))));
787   }
788 
789   MODIFY_REG(SYSCFG->CCCSR, (Selection << 1U), (Code << (POSITION_VAL(Selection) + 1U)));
790 }
791 
792 /**
793   * @brief  Get the code selection for the compensation cell
794   * @param  Selection specifies the concerned compensation cell
795   *         This parameter can one of the following values:
796   *            @arg SYSCFG_IO_CELL Compensation cell for the VDD I/O power rail
797   *            @arg SYSCFG_IO2_CELL Compensation cell for the VDDIO2 I/O power rail
798   * @param  pCode pointer code selection
799   *         This parameter can be one of the following values:
800   *            @arg SYSCFG_IO_CELL_CODE  Code from the cell (available in the SYSCFG_CCVR)
801   *            @arg SYSCFG_IO_REGISTER_CODE Code from the compensation cell code register (SYSCFG_CCCR)
802   * @param  pNmosValue pointer to the Nmos value in range 0 to 15
803   * @param  pPmosValue pointer to the Pmos value in range 0 to 15
804   * @retval  HAL_OK (all values available) or HAL_ERROR (check parameters)
805   */
HAL_SYSCFG_GetCompensationCell(uint32_t Selection,uint32_t * pCode,uint32_t * pNmosValue,uint32_t * pPmosValue)806 HAL_StatusTypeDef HAL_SYSCFG_GetCompensationCell(uint32_t Selection, uint32_t *pCode, uint32_t *pNmosValue,
807                                               uint32_t *pPmosValue)
808 {
809   uint32_t reg;
810   uint32_t offset;
811   HAL_StatusTypeDef status = HAL_ERROR;
812 
813   /* Check parameters */
814   if ((pCode != NULL) && (pNmosValue != NULL) && (pPmosValue != NULL))
815   {
816     *pCode = ((SYSCFG->CCCSR & (Selection << 1U)) == 0U) ? SYSCFG_IO_CELL_CODE : SYSCFG_IO_REGISTER_CODE;
817 
818     reg = (*pCode == SYSCFG_IO_CELL_CODE) ? (SYSCFG->CCVR) : (SYSCFG->CCCR);
819     offset = ((Selection == SYSCFG_IO_CELL) ? 0U : 8U);
820 
821     *pNmosValue = ((reg >> offset) & 0xFU);
822     *pPmosValue = ((reg >> (offset + 4U)) & 0xFU);
823 
824     status = HAL_OK;
825   }
826   return status;
827 }
828 
829 /**
830   * @}
831   */
832 
833 /** @defgroup HAL_Exported_Functions_Group5 HAL SYSCFG lock management functions
834   *  @brief SYSCFG lock management functions.
835   *
836 @verbatim
837  ===============================================================================
838                        ##### SYSCFG lock functions #####
839  ===============================================================================
840 
841 @endverbatim
842   * @{
843   */
844 
845 /**
846   * @brief  Lock the SYSCFG item(s).
847   * @note   Setting lock(s) depends on privilege mode in secure/non-secure code
848   *         Lock(s) cleared only at system reset
849   * @param  Item Item(s) to set lock on.
850   *         This parameter can be a combination of @ref SYSCFG_Lock_items
851   * @retval None
852   */
HAL_SYSCFG_Lock(uint32_t Item)853 void HAL_SYSCFG_Lock(uint32_t Item)
854 {
855   /* Check the parameters */
856   assert_param(IS_SYSCFG_LOCK_ITEMS(Item));
857 
858   /* Privilege secure/non-secure locks */
859   SYSCFG->CNSLCKR = (0xFFFFU & Item);  /* non-secure lock item in 16 lowest bits */
860 
861 #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
862   /* Privilege secure only locks */
863   SYSCFG->CSLCKR = ((0xFFFF0000U & Item) >> 16U);  /* Secure-only lock item in 16 highest bits */
864 #endif /* __ARM_FEATURE_CMSE */
865 }
866 
867 /**
868   * @brief  Get the lock state of SYSCFG item.
869   * @note   Getting lock(s) depends on privilege mode in secure/non-secure code
870   * @param  pItem pointer to return locked items
871   *         the return value can be a combination of @ref SYSCFG_Lock_items
872   * @retval HAL status
873   */
HAL_SYSCFG_GetLock(uint32_t * pItem)874 HAL_StatusTypeDef HAL_SYSCFG_GetLock(uint32_t *pItem)
875 {
876   uint32_t tmp_lock;
877 
878   /* Check null pointer */
879   if (pItem == NULL)
880   {
881     return HAL_ERROR;
882   }
883 
884   /* Get the non-secure lock state */
885   tmp_lock = SYSCFG->CNSLCKR;
886 
887   /* Get the secure lock state in secure code */
888 #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
889   tmp_lock |= (SYSCFG->CSLCKR << 16U);
890 #endif /* __ARM_FEATURE_CMSE */
891 
892   /* Return overall lock status */
893   *pItem = tmp_lock;
894 
895   return HAL_OK;
896 }
897 
898 /**
899   * @}
900   */
901 
902 
903 #if defined(SYSCFG_SECCFGR_SYSCFGSEC)
904 /** @defgroup HAL_Exported_Functions_Group6 HAL SYSCFG attributes management functions
905   *  @brief SYSCFG attributes management functions.
906   *
907 @verbatim
908  ===============================================================================
909                        ##### SYSCFG attributes functions #####
910  ===============================================================================
911 
912 @endverbatim
913   * @{
914   */
915 
916 #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
917 /**
918   * @brief  Configure the SYSCFG item attribute(s).
919   * @note   Available attributes are to secure SYSCFG items, so this function is
920   *         only available in secure
921   * @param  Item Item(s) to set attributes on.
922   *         This parameter can be a one or a combination of @ref SYSCFG_Attributes_items
923   * @param  Attributes  specifies the secure/non-secure attributes.
924   * @retval None
925   */
HAL_SYSCFG_ConfigAttributes(uint32_t Item,uint32_t Attributes)926 void HAL_SYSCFG_ConfigAttributes(uint32_t Item, uint32_t Attributes)
927 {
928   uint32_t tmp;
929 
930   /* Check the parameters */
931   assert_param(IS_SYSCFG_ITEMS_ATTRIBUTES(Item));
932   assert_param(IS_SYSCFG_ATTRIBUTES(Attributes));
933 
934   tmp = SYSCFG_S->SECCFGR;
935 
936   /* Set or reset Item */
937   if ((Attributes & SYSCFG_SEC) != 0x00U)
938   {
939     tmp |= Item;
940   }
941   else
942   {
943     tmp &= ~Item;
944   }
945 
946   /* Set secure attributes */
947   SYSCFG_S->SECCFGR = tmp;
948 }
949 
950 #endif /* __ARM_FEATURE_CMSE */
951 
952 /**
953   * @brief  Get the attribute of a SYSCFG item.
954   * @note   Available attributes are to secure SYSCFG items, so this function is
955   *         only available in secure
956   * @param  Item Single item to get secure/non-secure attribute from.
957   * @param  pAttributes pointer to return the attribute.
958   * @retval HAL status
959   */
HAL_SYSCFG_GetConfigAttributes(uint32_t Item,uint32_t * pAttributes)960 HAL_StatusTypeDef HAL_SYSCFG_GetConfigAttributes(uint32_t Item, uint32_t *pAttributes)
961 {
962   /* Check null pointer */
963   if (pAttributes == NULL)
964   {
965     return HAL_ERROR;
966   }
967 
968   /* Check the parameters */
969   assert_param(IS_SYSCFG_SINGLE_ITEMS_ATTRIBUTES(Item));
970 
971   /* Get the secure attribute state */
972   if ((SYSCFG->SECCFGR & Item) != 0U)
973   {
974     *pAttributes = SYSCFG_SEC;
975   }
976   else
977   {
978     *pAttributes = SYSCFG_NSEC;
979   }
980 
981   return HAL_OK;
982 }
983 
984 /**
985   * @}
986   */
987 
988 #endif /* SYSCFG_SECCFGR_SYSCFGSEC */
989 
990 /**
991   * @}
992   */
993 
994 #endif /* HAL_MODULE_ENABLED */
995 
996 /**
997   * @}
998   */
999 
1000 /**
1001   * @}
1002   */
1003