1 /**
2 ******************************************************************************
3 * @file stm32mp1xx_hal.c
4 * @author MCD Application Team
5 * @brief HAL module driver.
6 * This is the common part of the HAL initialization
7 *
8 ******************************************************************************
9 * @attention
10 *
11 * Copyright (c) 2019 STMicroelectronics.
12 * All rights reserved.
13 *
14 * This software is licensed under terms that can be found in the LICENSE file
15 * in the root directory of this software component.
16 * If no LICENSE file comes with this software, it is provided AS-IS.
17 *
18 ******************************************************************************
19 @verbatim
20 ==============================================================================
21 ##### How to use this driver #####
22 ==============================================================================
23 [..]
24 The common HAL driver contains a set of generic and common APIs that can be
25 used by the PPP peripheral drivers and the user to start using the HAL.
26 [..]
27 The HAL contains two APIs' categories:
28 (+) Common HAL APIs
29 (+) Services HAL APIs
30
31 @endverbatim
32 ******************************************************************************
33 */
34
35 /* Includes ------------------------------------------------------------------*/
36 #include "stm32mp1xx_hal.h"
37
38 /** @addtogroup STM32MP1xx_HAL_Driver
39 * @{
40 */
41
42 /** @defgroup HAL HAL
43 * @brief HAL module driver.
44 * @{
45 */
46
47 /* Private typedef -----------------------------------------------------------*/
48 /* Private define ------------------------------------------------------------*/
49 /** @defgroup HAL_Private_Defines HAL Private Defines
50 * @{
51 */
52
53 /**
54 * @brief STM32MP1xx HAL Driver version number
55 */
56 #define __STM32MP1xx_HAL_VERSION_MAIN (0x01U) /*!< [31:24] main version */
57 #define __STM32MP1xx_HAL_VERSION_SUB1 (0x06U) /*!< [23:16] sub1 version */
58 #define __STM32MP1xx_HAL_VERSION_SUB2 (0x00U) /*!< [15:8] sub2 version */
59 #define __STM32MP1xx_HAL_VERSION_RC (0x00U) /*!< [7:0] release candidate */
60 #define __STM32MP1xx_HAL_VERSION ((__STM32MP1xx_HAL_VERSION_MAIN << 24)\
61 |(__STM32MP1xx_HAL_VERSION_SUB1 << 16)\
62 |(__STM32MP1xx_HAL_VERSION_SUB2 << 8 )\
63 |(__STM32MP1xx_HAL_VERSION_RC))
64
65 #define IDCODE_DEVID_MASK ((uint32_t)0x00000FFF)
66 #define VREFBUF_TIMEOUT_VALUE (uint32_t)10 /* 10 ms */
67 /**
68 * @}
69 */
70
71 /** @defgroup HAL_Private_Constants HAL Private Constants
72 * @{
73 */
74 #define SYSCFG_DEFAULT_TIMEOUT 100U
75 /**
76 * @}
77 */
78
79 /* Private macro -------------------------------------------------------------*/
80 /* Private variables ---------------------------------------------------------*/
81 /* Exported variables --------------------------------------------------------*/
82
83 /** @defgroup HAL_Exported_Variables HAL Exported Variables
84 * @{
85 */
86 __IO uint32_t uwTick;
87 #if defined(CORE_CM4)
88 uint32_t uwTickPrio = (1UL << __NVIC_PRIO_BITS); /* Invalid PRIO */
89 #else /* CA7 */
90 uint32_t uwTickPrio = (1UL << 4); /* Invalid PRIO */
91 #endif
92 HAL_TickFreqTypeDef uwTickFreq = HAL_TICK_FREQ_DEFAULT; /* 1KHz */
93 /**
94 * @}
95 */
96
97 /* Private function prototypes -----------------------------------------------*/
98 /* Private functions ---------------------------------------------------------*/
99
100 /** @defgroup HAL_Private_Functions HAL Private Functions
101 * @{
102 */
103
104 /** @defgroup HAL_Group1 Initialization and de-initialization Functions
105 * @brief Initialization and de-initialization functions
106 *
107 @verbatim
108 ===============================================================================
109 ##### Initialization and de-initialization functions #####
110 ===============================================================================
111 [..] This section provides functions allowing to:
112 (+) Initializes the Flash interface the NVIC allocation and initial clock
113 configuration. It initializes the systick also when timeout is needed
114 and the backup domain when enabled.
115 (+) De-Initializes common part of the HAL.
116 (+) Configure The time base source to have 1ms time base with a dedicated
117 Tick interrupt priority.
118 (++) SysTick timer is used by default as source of time base, but user
119 can eventually implement his proper time base source (a general purpose
120 timer for example or other time source), keeping in mind that Time base
121 duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and
122 handled in milliseconds basis.
123 (++) Time base configuration function (HAL_InitTick ()) is called automatically
124 at the beginning of the program after reset by HAL_Init() or at any time
125 when clock is configured, by HAL_RCC_ClockConfig().
126 (++) Source of time base is configured to generate interrupts at regular
127 time intervals. Care must be taken if HAL_Delay() is called from a
128 peripheral ISR process, the Tick interrupt line must have higher priority
129 (numerically lower) than the peripheral interrupt. Otherwise the caller
130 ISR process will be blocked.
131 (++) functions affecting time base configurations are declared as __weak
132 to make override possible in case of other implementations in user file.
133 @endverbatim
134 * @{
135 */
136
137 /**
138 * @brief This function is used to initialize the HAL Library; it must be the first
139 * instruction to be executed in the main program (before to call any other
140 * HAL function), it performs the following:
141 * Configures the SysTick to generate an interrupt each 1 millisecond,
142 * which is clocked by the HSI (at this stage, the clock is not yet
143 * configured and thus the system is running from the internal HSI at 64 MHz).
144 * Set NVIC Group Priority to 4.
145 * Calls the HAL_MspInit() callback function defined in user file
146 * "stm32mp1xx_hal_msp.c" to do the global low level hardware initialization
147 *
148 * @note SysTick is used as time base for the HAL_Delay() function, the application
149 * need to ensure that the SysTick time base is always set to 1 millisecond
150 * to have correct HAL operation.
151 * @retval HAL status
152 */
HAL_Init(void)153 HAL_StatusTypeDef HAL_Init(void)
154 {
155 /* Set Interrupt Group Priority */
156 #if defined (CORE_CM4)
157 HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4);
158 #endif
159
160 /* Update the SystemCoreClock global variable */
161 SystemCoreClock = HAL_RCC_GetSystemCoreClockFreq();
162
163 /* Use systick as time base source and configure 1ms tick (default clock after Reset is HSI) */
164 if(HAL_InitTick(TICK_INT_PRIORITY) != HAL_OK)
165 {
166 return HAL_ERROR;
167 }
168
169 /* Init the low level hardware */
170 HAL_MspInit();
171
172 /* Return function status */
173 return HAL_OK;
174 }
175
176 /**
177 * @brief This function de-Initializes common part of the HAL and stops the systick.
178 * This function is optional.
179 * @retval HAL status
180 */
HAL_DeInit(void)181 HAL_StatusTypeDef HAL_DeInit(void)
182 {
183 /* Reset of all peripherals */
184
185
186 /* De-Init the low level hardware */
187 HAL_MspDeInit();
188
189 /* Return function status */
190 return HAL_OK;
191 }
192
193 /**
194 * @brief Initializes the MSP.
195 * @retval None
196 */
HAL_MspInit(void)197 __weak void HAL_MspInit(void)
198 {
199 /* NOTE : This function Should not be modified, when the callback is needed,
200 the HAL_MspInit could be implemented in the user file
201 */
202 }
203
204 /**
205 * @brief DeInitializes the MSP.
206 * @retval None
207 */
HAL_MspDeInit(void)208 __weak void HAL_MspDeInit(void)
209 {
210 /* NOTE : This function Should not be modified, when the callback is needed,
211 the HAL_MspDeInit could be implemented in the user file
212 */
213 }
214
215 /**
216 * @brief This function configures the source of the time base.
217 * The time source is configured to have 1ms time base with a dedicated
218 * Tick interrupt priority.
219 * @note This function is called automatically at the beginning of program after
220 * reset by HAL_Init() or at any time when clock is reconfigured by HAL_RCC_ClockConfig().
221 * @note In the default implementation, SysTick timer is the source of time base.
222 * It is used to generate interrupts at regular time intervals.
223 * Care must be taken if HAL_Delay() is called from a peripheral ISR process,
224 * The the SysTick interrupt must have higher priority (numerically lower)
225 * than the peripheral interrupt. Otherwise the caller ISR process will be blocked.
226 * The function is declared as __weak to be overwritten in case of other
227 * implementation in user file.
228 * @param TickPriority: Tick interrupt priority.
229 * @retval HAL status
230 */
HAL_InitTick(uint32_t TickPriority)231 __weak HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
232 {
233 /*Configure the Tick to have interrupt in 1ms time basis*/
234 #if defined (CORE_CA7)
235
236 #if defined(USE_ST_CASIS)
237 HAL_SYSTICK_Config(SystemCoreClock/1000);
238 #elif defined (USE_PL1_SecurePhysicalTimer_IRQ)
239 /* Stop Timer */
240 PL1_SetControl(0x0);
241
242 PL1_SetCounterFrequency(HSI_VALUE);
243
244 /* Initialize Counter */
245 PL1_SetLoadValue(HSI_VALUE/1000);
246
247 /* Disable corresponding IRQ */
248 IRQ_Disable(SecurePhysicalTimer_IRQn);
249 IRQ_ClearPending(SecurePhysicalTimer_IRQn);
250
251 /* Set timer priority to lowest (Only bit 7:3 are implemented in MP1 CA7 GIC) */
252 /* TickPriority is based on 16 level priority (from MCUs) so set it in 7:4 and leave bit 3=0 */
253 if (TickPriority < (1UL << 4))
254 {
255 IRQ_SetPriority(SecurePhysicalTimer_IRQn, TickPriority << 4);
256 uwTickPrio = TickPriority;
257 }
258 else
259 {
260 return HAL_ERROR;
261 }
262
263 /* Set edge-triggered IRQ */
264 IRQ_SetMode(SecurePhysicalTimer_IRQn, IRQ_MODE_TRIG_EDGE);
265
266 /* Enable corresponding interrupt */
267 IRQ_Enable(SecurePhysicalTimer_IRQn);
268
269 /* Kick start Timer */
270 PL1_SetControl(0x1);
271 #else
272 /*Set Counter Frequency */
273 PL1_SetCounterFrequency(HSI_VALUE);
274 // __set_CNTFRQ(HSI_VALUE);
275 /* Initialize Counter */
276 PL1_SetLoadValue(0x1);
277 // __set_CNTP_TVAL(0x1);
278 #endif
279
280 #endif /* CORE_CA7 */
281
282
283 #if defined (CORE_CM4)
284 if ((uint32_t)uwTickFreq == 0U)
285 {
286 return HAL_ERROR;
287 }
288
289 /* Configure the SysTick to have interrupt in 1ms time basis*/
290 if (HAL_SYSTICK_Config(SystemCoreClock /(1000U / uwTickFreq)) > 0U)
291 {
292 return HAL_ERROR;
293 }
294 /* Configure the SysTick IRQ priority */
295 if (TickPriority < (1UL << __NVIC_PRIO_BITS))
296 {
297 HAL_NVIC_SetPriority(SysTick_IRQn, TickPriority, 0U);
298 uwTickPrio = TickPriority;
299 }
300 else
301 {
302 return HAL_ERROR;
303 }
304 #endif /* CORE_CM4 */
305
306
307
308 /* Return function status */
309 return HAL_OK;
310 }
311
312 /**
313 * @}
314 */
315
316 /** @defgroup HAL_Group2 HAL Control functions
317 * @brief HAL Control functions
318 *
319 @verbatim
320 ===============================================================================
321 ##### HAL Control functions #####
322 ===============================================================================
323 [..] This section provides functions allowing to:
324 (+) Provide a tick value in millisecond
325 (+) Provide a blocking delay in millisecond
326 (+) Suspend the time base source interrupt
327 (+) Resume the time base source interrupt
328 (+) Get the HAL API driver version
329 (+) Get the device identifier
330 (+) Get the device revision identifier
331 (+) Enable/Disable Debug module during SLEEP mode
332 (+) Enable/Disable Debug module during STOP mode
333 (+) Enable/Disable Debug module during STANDBY mode
334
335 @endverbatim
336 * @{
337 */
338
339 /**
340 * @brief This function is called to increment a global variable "uwTick"
341 * used as application time base.
342 * @note In the default implementation, this variable is incremented each 1ms
343 * in Systick ISR.
344 * @note This function is declared as __weak to be overwritten in case of other
345 * implementations in user file.
346 * @retval None
347 */
HAL_IncTick(void)348 __weak void HAL_IncTick(void)
349 {
350 uwTick += (uint32_t)uwTickFreq;
351 }
352
353 /**
354 * @brief Provides a tick value in millisecond.
355 * @note This function is declared as __weak to be overwritten in case of other
356 * implementations in user file.
357 * @retval tick value
358 */
HAL_GetTick(void)359 __weak uint32_t HAL_GetTick(void)
360 {
361 #if defined (CORE_CA7)
362
363 #if defined (USE_ST_CASIS)
364 return ( Gen_Timer_Get_PhysicalCount() / (HSI_VALUE/1000));
365 #elif defined (USE_PL1_SecurePhysicalTimer_IRQ)
366 /* tick is incremented in SecurePhysicalTimer_IRQ handler */
367 return uwTick;
368 #else
369 /* tick value directly got from 64bits CA7 register*/
370 return ( PL1_GetCurrentPhysicalValue() / (HSI_VALUE/1000));
371 #endif
372
373 #endif /* CORE_CA7 */
374
375
376 #if defined (CORE_CM4)
377 /* tick is incremented in systick handler */
378 return uwTick;
379 #endif /* CORE_CM4 */
380
381 }
382
383 #if defined (CORE_CM4)
384 /**
385 * @brief This function returns a tick priority.
386 * @retval tick priority
387 */
HAL_GetTickPrio(void)388 uint32_t HAL_GetTickPrio(void)
389 {
390 return uwTickPrio;
391 }
392
393 /**
394 * @brief Set new tick Freq.
395 * @retval Status
396 */
HAL_SetTickFreq(HAL_TickFreqTypeDef Freq)397 HAL_StatusTypeDef HAL_SetTickFreq(HAL_TickFreqTypeDef Freq)
398 {
399 HAL_StatusTypeDef status = HAL_OK;
400 HAL_TickFreqTypeDef prevTickFreq;
401 assert_param(IS_TICKFREQ(Freq));
402
403 if (uwTickFreq != Freq)
404 {
405 /* Back up uwTickFreq frequency */
406 prevTickFreq = uwTickFreq;
407
408 /* Update uwTickFreq global variable used by HAL_InitTick() */
409 uwTickFreq = Freq;
410
411 /* Apply the new tick Freq */
412 status = HAL_InitTick(uwTickPrio);
413
414 if (status != HAL_OK)
415 {
416 /* Restore previous tick frequency */
417 uwTickFreq = prevTickFreq;
418 }
419 }
420
421 return status;
422 }
423
424 /**
425 * @brief Return tick frequency.
426 * @retval tick period in Hz
427 */
HAL_GetTickFreq(void)428 HAL_TickFreqTypeDef HAL_GetTickFreq(void)
429 {
430 return uwTickFreq;
431 }
432 #endif
433
434 /**
435 * @brief This function provides accurate delay (in milliseconds) based
436 * on variable incremented.
437 * @note In the default implementation , SysTick timer is the source of time base.
438 * It is used to generate interrupts at regular time intervals where uwTick
439 * is incremented.
440 * @note ThiS function is declared as __weak to be overwritten in case of other
441 * implementations in user file.
442 * @param Delay: specifies the delay time length, in milliseconds.
443 * @retval None
444 */
HAL_Delay(uint32_t Delay)445 __weak void HAL_Delay(uint32_t Delay)
446 {
447 uint32_t tickstart = HAL_GetTick();
448 uint32_t wait = Delay;
449
450 /* Add a freq to guarantee minimum wait */
451 if (wait < HAL_MAX_DELAY)
452 {
453 wait += (uint32_t)(uwTickFreq);
454 }
455
456 while ((HAL_GetTick() - tickstart) < wait)
457 {
458 }
459 }
460
461 /**
462 * @brief Suspend Tick increment.
463 * @note In the default implementation , SysTick timer is the source of time base. It is
464 * used to generate interrupts at regular time intervals. Once HAL_SuspendTick()
465 * is called, the the SysTick interrupt will be disabled and so Tick increment
466 * is suspended.
467 * @note This function is declared as __weak to be overwritten in case of other
468 * implementations in user file.
469 * @retval None
470 */
HAL_SuspendTick(void)471 __weak void HAL_SuspendTick(void)
472 {
473 #if defined (CORE_CA7)
474 #elif defined (CORE_CM4)
475 /* Disable SysTick Interrupt */
476 SysTick->CTRL &= ~SysTick_CTRL_TICKINT_Msk;
477 #endif
478 }
479
480 /**
481 * @brief Resume Tick increment.
482 * @note In the default implementation , SysTick timer is the source of time base. It is
483 * used to generate interrupts at regular time intervals. Once HAL_ResumeTick()
484 * is called, the the SysTick interrupt will be enabled and so Tick increment
485 * is resumed.
486 * @note This function is declared as __weak to be overwritten in case of other
487 * implementations in user file.
488 * @retval None
489 */
HAL_ResumeTick(void)490 __weak void HAL_ResumeTick(void)
491 {
492 #if defined (CORE_CA7)
493 #elif defined (CORE_CM4)
494 /* Enable SysTick Interrupt */
495 SysTick->CTRL |= SysTick_CTRL_TICKINT_Msk;
496 #endif
497 }
498
499 /**
500 * @brief Returns the HAL revision
501 * @retval version : 0xXYZR (8bits for each decimal, R for RC)
502 */
HAL_GetHalVersion(void)503 uint32_t HAL_GetHalVersion(void)
504 {
505 return __STM32MP1xx_HAL_VERSION;
506 }
507
508 /**
509 * @brief Returns the device revision identifier.
510 * @retval Device revision identifier
511 */
HAL_GetREVID(void)512 uint32_t HAL_GetREVID(void)
513 {
514 return((DBGMCU->IDCODE) >> 16);
515 }
516
517 /**
518 * @brief Returns the device identifier.
519 * @retval Device identifier
520 */
HAL_GetDEVID(void)521 uint32_t HAL_GetDEVID(void)
522 {
523 return((DBGMCU->IDCODE) & IDCODE_DEVID_MASK);
524 }
525
526 /**
527 * @brief Return the first word of the unique device identifier (UID based on 96 bits)
528 * @retval Device identifier
529 */
HAL_GetUIDw0(void)530 uint32_t HAL_GetUIDw0(void)
531 {
532 return(READ_REG(*((uint32_t *)UID_BASE)));
533 }
534
535 /**
536 * @brief Return the second word of the unique device identifier (UID based on 96 bits)
537 * @retval Device identifier
538 */
HAL_GetUIDw1(void)539 uint32_t HAL_GetUIDw1(void)
540 {
541 return(READ_REG(*((uint32_t *)(UID_BASE + 4U))));
542 }
543
544 /**
545 * @brief Return the third word of the unique device identifier (UID based on 96 bits)
546 * @retval Device identifier
547 */
HAL_GetUIDw2(void)548 uint32_t HAL_GetUIDw2(void)
549 {
550 return(READ_REG(*((uint32_t *)(UID_BASE + 8U))));
551 }
552
553 /**
554 * @brief Enable DBG wake up on AIEC
555 * @retval None
556 */
HAL_EnableDBGWakeUp(void)557 void HAL_EnableDBGWakeUp(void)
558 {
559 #if defined (CORE_CA7)
560 SET_BIT(EXTI_C1->IMR3, EXTI_IMR3_IM75);
561 #elif defined (CORE_CM4)
562 SET_BIT(EXTI_C2->IMR3, EXTI_IMR3_IM75);
563 #endif
564 }
565
566 /**
567 * @brief Disable DBG wake up on AIEC
568 * @retval None
569 */
HAL_DisableDBGWakeUp(void)570 void HAL_DisableDBGWakeUp(void)
571 {
572 #if defined (CORE_CA7)
573 CLEAR_BIT(EXTI_C1->IMR3, EXTI_IMR3_IM75);
574 #elif defined (CORE_CM4)
575 CLEAR_BIT(EXTI_C2->IMR3, EXTI_IMR3_IM75);
576 #endif
577 }
578
579 /**
580 * @brief Enable the Debug Module during Domain1 SLEEP mode
581 * @retval None
582 */
HAL_EnableDBGSleepMode(void)583 void HAL_EnableDBGSleepMode(void)
584 {
585 SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_SLEEP);
586 }
587
588 /**
589 * @brief Disable the Debug Module during Domain1 SLEEP mode
590 * @retval None
591 */
HAL_DisableDBGSleepMode(void)592 void HAL_DisableDBGSleepMode(void)
593 {
594 CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_SLEEP);
595 }
596
597 /**
598 * @brief Enable the Debug Module during Domain1 STOP mode
599 * @retval None
600 */
HAL_EnableDBGStopMode(void)601 void HAL_EnableDBGStopMode(void)
602 {
603 SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
604 }
605
606 /**
607 * @brief Disable the Debug Module during Domain1 STOP mode
608 * @retval None
609 */
HAL_DisableDBGStopMode(void)610 void HAL_DisableDBGStopMode(void)
611 {
612 CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STOP);
613 }
614
615 /**
616 * @brief Enable the Debug Module during Domain1 STANDBY mode
617 * @retval None
618 */
HAL_EnableDBGStandbyMode(void)619 void HAL_EnableDBGStandbyMode(void)
620 {
621 SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
622 }
623
624 /**
625 * @brief Disable the Debug Module during Domain1 STANDBY mode
626 * @retval None
627 */
HAL_DisableDBGStandbyMode(void)628 void HAL_DisableDBGStandbyMode(void)
629 {
630 CLEAR_BIT(DBGMCU->CR, DBGMCU_CR_DBG_STANDBY);
631 }
632
633
634 /**
635 * @brief Configure the internal voltage reference buffer voltage scale.
636 * @param VoltageScaling specifies the output voltage to achieve
637 * This parameter can be one of the following values:
638 * @arg SYSCFG_VREFBUF_VOLTAGE_SCALE0: VREF_OUT1 around 2.048 V.
639 * This requires VDDA equal to or higher than 2.4 V.
640 * @arg SYSCFG_VREFBUF_VOLTAGE_SCALE1: VREF_OUT2 around 2.5 V.
641 * This requires VDDA equal to or higher than 2.8 V.
642 * @arg SYSCFG_VREFBUF_VOLTAGE_SCALE2: VREF_OUT3 around 1.5 V.
643 * This requires VDDA equal to or higher than 1.8 V.
644 * @arg SYSCFG_VREFBUF_VOLTAGE_SCALE3: VREF_OUT4 around 1.8 V.
645 * This requires VDDA equal to or higher than 2.1 V.
646 * @retval None
647 */
HAL_SYSCFG_VREFBUF_VoltageScalingConfig(uint32_t VoltageScaling)648 void HAL_SYSCFG_VREFBUF_VoltageScalingConfig(uint32_t VoltageScaling)
649 {
650 /* Check the parameters */
651 assert_param(IS_SYSCFG_VREFBUF_VOLTAGE_SCALE(VoltageScaling));
652
653 MODIFY_REG(VREFBUF->CSR, VREFBUF_CSR_VRS, VoltageScaling);
654 }
655
656 /**
657 * @brief Configure the internal voltage reference buffer high impedance mode.
658 * @param Mode specifies the high impedance mode
659 * This parameter can be one of the following values:
660 * @arg SYSCFG_VREFBUF_HIGH_IMPEDANCE_DISABLE: VREF+ pin is internally connect to VREFINT output.
661 * @arg SYSCFG_VREFBUF_HIGH_IMPEDANCE_ENABLE: VREF+ pin is high impedance.
662 * @retval None
663 */
HAL_SYSCFG_VREFBUF_HighImpedanceConfig(uint32_t Mode)664 void HAL_SYSCFG_VREFBUF_HighImpedanceConfig(uint32_t Mode)
665 {
666 /* Check the parameters */
667 assert_param(IS_SYSCFG_VREFBUF_HIGH_IMPEDANCE(Mode));
668
669 MODIFY_REG(VREFBUF->CSR, VREFBUF_CSR_HIZ, Mode);
670 }
671
672 /**
673 * @brief Tune the Internal Voltage Reference buffer (VREFBUF).
674 * @retval None
675 */
HAL_SYSCFG_VREFBUF_TrimmingConfig(uint32_t TrimmingValue)676 void HAL_SYSCFG_VREFBUF_TrimmingConfig(uint32_t TrimmingValue)
677 {
678 /* Check the parameters */
679 assert_param(IS_SYSCFG_VREFBUF_TRIMMING(TrimmingValue));
680
681 MODIFY_REG(VREFBUF->CCR, VREFBUF_CCR_TRIM, TrimmingValue);
682 }
683
684 /**
685 * @brief Enable the Internal Voltage Reference buffer (VREFBUF).
686 * @retval HAL_OK/HAL_TIMEOUT
687 */
HAL_SYSCFG_EnableVREFBUF(void)688 HAL_StatusTypeDef HAL_SYSCFG_EnableVREFBUF(void)
689 {
690 uint32_t tickstart = 0;
691
692 SET_BIT(VREFBUF->CSR, VREFBUF_CSR_ENVR);
693
694 /* Get Start Tick*/
695 tickstart = HAL_GetTick();
696
697 /* Wait for VRR bit */
698 while(READ_BIT(VREFBUF->CSR, VREFBUF_CSR_VRR) == RESET)
699 {
700 if((HAL_GetTick() - tickstart) > VREFBUF_TIMEOUT_VALUE)
701 {
702 return HAL_TIMEOUT;
703 }
704 }
705
706 return HAL_OK;
707 }
708
709 /**
710 * @brief Disable the Internal Voltage Reference buffer (VREFBUF).
711 *
712 * @retval None
713 */
HAL_SYSCFG_DisableVREFBUF(void)714 void HAL_SYSCFG_DisableVREFBUF(void)
715 {
716 CLEAR_BIT(VREFBUF->CSR, VREFBUF_CSR_ENVR);
717 }
718
719 /**
720 * @brief Ethernet PHY Interface Selection either MII or RMII
721 * @param SYSCFG_ETHInterface: Selects the Ethernet PHY interface
722 * This parameter can be one of the following values:
723 * @arg SYSCFG_ETH_MII : Select the Media Independent Interface
724 * @arg SYSCFG_ETH_GMII : Select the Gigabit Media Independent Interface
725 * @arg SYSCFG_ETH_RGMII: Select the Gigabit Reduced Media Independent Interface
726 * @arg SYSCFG_ETH_RMII : Select the Reduced Media Independent Interface
727 * @retval None
728 */
HAL_SYSCFG_ETHInterfaceSelect(uint32_t SYSCFG_ETHInterface)729 void HAL_SYSCFG_ETHInterfaceSelect(uint32_t SYSCFG_ETHInterface)
730 {
731 /* Check the parameter */
732 assert_param(IS_SYSCFG_ETHERNET_CONFIG(SYSCFG_ETHInterface));
733 SYSCFG->PMCCLRR = SYSCFG_PMCSETR_ETH_SEL|SYSCFG_PMCSETR_ETH_SELMII_SEL;
734 SYSCFG->PMCSETR = (uint32_t)(SYSCFG_ETHInterface);
735 }
736
737 /**
738 * @brief Analog Switch control for dual analog pads.
739 * @param SYSCFG_AnalogSwitch: Selects the analog pad
740 * This parameter can be one or a combination of the following values:
741 * @arg SYSCFG_SWITCH_PA0 : Select PA0 analog switch
742 * @arg SYSCFG_SWITCH_PA1: Select PA1 analog switch
743 * @param SYSCFG_SwitchState: Open or Close the analog switch between dual pads (
744 * This parameter can be one or a combination of the following values:
745 * @arg SYSCFG_SWITCH_PA0_OPEN
746 * @arg SYSCFG_SWITCH_PA0_CLOSE
747 * @arg SYSCFG_SWITCH_PA1_OPEN
748 * @arg SYSCFG_SWITCH_PA1_CLOSE
749 * @retval None
750 */
751
HAL_SYSCFG_AnalogSwitchConfig(uint32_t SYSCFG_AnalogSwitch,uint32_t SYSCFG_SwitchState)752 void HAL_SYSCFG_AnalogSwitchConfig(uint32_t SYSCFG_AnalogSwitch , uint32_t SYSCFG_SwitchState )
753 {
754 /* Check the parameter */
755 assert_param(IS_SYSCFG_ANALOG_SWITCH(SYSCFG_AnalogSwitch));
756 assert_param(IS_SYSCFG_SWITCH_STATE(SYSCFG_SwitchState));
757 SYSCFG->PMCCLRR = SYSCFG_AnalogSwitch;
758 SYSCFG->PMCSETR = (uint32_t)(SYSCFG_SwitchState);
759
760 }
761
762
763 /**
764 * @brief Enables the booster to reduce the total harmonic distortion of the analog
765 * switch when the supply voltage is lower than 2.7 V.
766 * @note Activating the booster allows to guaranty the analog switch AC performance
767 * when the supply voltage is below 2.7 V: in this case, the analog switch
768 * performance is the same on the full voltage range
769 * @retval None
770 */
HAL_SYSCFG_EnableBOOST(void)771 void HAL_SYSCFG_EnableBOOST(void)
772 {
773 SYSCFG->PMCSETR = SYSCFG_PMCSETR_EN_BOOSTER;
774
775 }
776
777 /**
778 * @brief Disables the booster
779 * @note Activating the booster allows to guaranty the analog switch AC performance
780 * when the supply voltage is below 2.7 V: in this case, the analog switch
781 * performance is the same on the full voltage range
782 * @retval None
783 */
HAL_SYSCFG_DisableBOOST(void)784 void HAL_SYSCFG_DisableBOOST(void)
785 {
786 SYSCFG->PMCCLRR = SYSCFG_PMCCLRR_EN_BOOSTER;
787 }
788
789
790 /**
791 * @brief Enables the I/O Compensation Cell.
792 * @note The I/O compensation cell can be used only when the device supply
793 * voltage ranges from 2.4 to 3.6 V.
794 * @retval None
795 */
HAL_EnableCompensationCell(void)796 void HAL_EnableCompensationCell(void)
797 {
798 #if defined(CORE_CM4)
799 SYSCFG->CMPENSETR = SYSCFG_CMPENSETR_MCU_EN;
800 #elif defined(CORE_CA7)
801 SYSCFG->CMPENSETR = SYSCFG_CMPENSETR_MPU_EN;
802 #endif
803 }
804
805 /**
806 * @brief Power-down the I/O Compensation Cell.
807 * @note The I/O compensation cell can be used only when the device supply
808 * voltage ranges from 2.4 to 3.6 V.
809 * @retval None
810 */
HAL_DisableCompensationCell(void)811 void HAL_DisableCompensationCell(void)
812 {
813 #if defined(CORE_CM4)
814 SYSCFG->CMPENCLRR = SYSCFG_CMPENCLRR_MCU_EN;
815 #elif defined(CORE_CA7)
816 SYSCFG->CMPENCLRR = SYSCFG_CMPENCLRR_MPU_EN;
817 #endif
818 }
819
820
821 /**
822 * @brief To Enable optimize the I/O speed when the product voltage is low.
823 * @note This bit is active only if PRODUCT_BELOW_25V user option bit is set. It must be
824 * used only if the product supply voltage is below 2.5 V. Setting this bit when VDD is
825 * higher than 2.5 V might be destructive.
826 * @param SYSCFG_HighSpeedSignal: Signal selection (TRACE, QUADSPI...)
827 * This parameter can be one or a combination of the following values:
828 * @arg SYSCFG_HIGHSPEED_TRACE_SIGNAL
829 * @arg SYSCFG_HIGHSPEED_QUADSPI_SIGNAL
830 * @arg SYSCFG_HIGHSPEED_ETH_SIGNAL
831 * @arg SYSCFG_HIGHSPEED_SDMMC_SIGNAL
832 * @arg SYSCFG_HIGHSPEED_SPI_SIGNAL
833 * @retval None
834 */
HAL_SYSCFG_EnableIOSpeedOptimize(uint32_t SYSCFG_HighSpeedSignal)835 void HAL_SYSCFG_EnableIOSpeedOptimize(uint32_t SYSCFG_HighSpeedSignal )
836 {
837 SYSCFG->IOCTRLSETR = SYSCFG_HighSpeedSignal;
838 }
839
840 /**
841 * @brief To Disable optimize the I/O speed when the product voltage is low.
842 * @note This bit is active only if PRODUCT_BELOW_25V user option bit is set. It must be
843 * used only if the product supply voltage is below 2.5 V. Setting this bit when VDD is
844 * higher than 2.5 V might be destructive.
845 * @param SYSCFG_HighSpeedSignal: Signal selection (TRACE, QUADSPI...)
846 * This parameter can be one or a combination of the following values:
847 * @arg SYSCFG_HIGHSPEED_TRACE_SIGNAL
848 * @arg SYSCFG_HIGHSPEED_QUADSPI_SIGNAL
849 * @arg SYSCFG_HIGHSPEED_ETH_SIGNAL
850 * @arg SYSCFG_HIGHSPEED_SDMMC_SIGNAL
851 * @arg SYSCFG_HIGHSPEED_SPI_SIGNAL
852 * @retval None
853 */
HAL_SYSCFG_DisableIOSpeedOptimize(uint32_t SYSCFG_HighSpeedSignal)854 void HAL_SYSCFG_DisableIOSpeedOptimize(uint32_t SYSCFG_HighSpeedSignal )
855 {
856 SYSCFG->IOCTRLCLRR = SYSCFG_HighSpeedSignal;
857 }
858
859 /**
860 * @brief Code selection for the I/O Compensation cell
861 * @param SYSCFG_CompCode: Selects the code to be applied for the I/O compensation cell
862 * This parameter can be one of the following values:
863 * @arg SYSCFG_CELL_CODE : Select Code from the cell (available in the SYSCFG_CCVR)
864 * @arg SYSCFG_REGISTER_CODE: Select Code from the SYSCFG compensation cell code register (SYSCFG_CCCR)
865 * @retval None
866 */
HAL_SYSCFG_CompensationCodeSelect(uint32_t SYSCFG_CompCode)867 void HAL_SYSCFG_CompensationCodeSelect(uint32_t SYSCFG_CompCode)
868 {
869 /* Check the parameter */
870 assert_param(IS_SYSCFG_CODE_SELECT(SYSCFG_CompCode));
871 MODIFY_REG(SYSCFG->CMPCR, SYSCFG_CMPCR_SW_CTRL, (uint32_t)(SYSCFG_CompCode));
872 }
873
874 /**
875 * @brief Code selection for the I/O Compensation cell
876 * @param SYSCFG_PMOSCode: PMOS compensation code
877 * This code is applied to the I/O compensation cell when the CS bit of the
878 * SYSCFG_CMPCR is set
879 * @param SYSCFG_NMOSCode: NMOS compensation code
880 * This code is applied to the I/O compensation cell when the CS bit of the
881 * SYSCFG_CMPCR is set
882 * @retval None
883 */
HAL_SYSCFG_CompensationCodeConfig(uint32_t SYSCFG_PMOSCode,uint32_t SYSCFG_NMOSCode)884 void HAL_SYSCFG_CompensationCodeConfig(uint32_t SYSCFG_PMOSCode, uint32_t SYSCFG_NMOSCode )
885 {
886 /* Check the parameter */
887 assert_param(IS_SYSCFG_CODE_CONFIG(SYSCFG_PMOSCode));
888 assert_param(IS_SYSCFG_CODE_CONFIG(SYSCFG_NMOSCode));
889 MODIFY_REG(SYSCFG->CMPCR, SYSCFG_CMPCR_RANSRC|SYSCFG_CMPCR_RAPSRC, (((uint32_t)(SYSCFG_PMOSCode)<< 4)|(uint32_t)(SYSCFG_NMOSCode)) );
890 }
891
892 /**
893 * @brief Disable IO compensation mechanism
894 * E.g. before going into STOP
895 * @retval None
896 */
HAL_SYSCFG_DisableIOCompensation(void)897 void HAL_SYSCFG_DisableIOCompensation(void)
898 {
899 uint32_t pmos_val = 0;
900 uint32_t nmos_val = 0;
901
902 /* Get I/O compensation cell values for PMOS and NMOS transistors */
903 pmos_val = (__HAL_SYSCFG_GET_PMOS_CMP() >> 28);
904 nmos_val = (__HAL_SYSCFG_GET_NMOS_CMP() >> 24);
905
906 /* Copy actual value of SYSCFG_CMPCR.APSRC[3:0]/ANSRC[3:0] in
907 * SYSCFG_CMPCR.RAPSRC[3:0]/RANSRC[3:0]
908 */
909 HAL_SYSCFG_CompensationCodeConfig(pmos_val, nmos_val);
910
911 /* Set SYSCFG_CMPCR.SW_CTRL = 1 */
912 HAL_SYSCFG_CompensationCodeSelect(SYSCFG_REGISTER_CODE);
913
914 /* Disable the Compensation Cell */
915 HAL_DisableCompensationCell();
916 }
917
918 /**
919 * @brief Enable IO compensation mechanism
920 * By default the I/O compensation cell is not used. However when the
921 * I/O output buffer speed is configured in 50 MHz mode and above, it
922 * is recommended to use the compensation cell for a slew rate control
923 * on I/O tf(IO)out/tr(IO)out commutation to reduce the I/O noise on
924 * the power supply.
925 * @note Use polling mode for timeout as code could be used on critical
926 * section (IRQs disabled)
927 * @retval HAL_StatusTypeDef value
928 */
HAL_SYSCFG_EnableIOCompensation(void)929 HAL_StatusTypeDef HAL_SYSCFG_EnableIOCompensation(void)
930 {
931 HAL_StatusTypeDef status = HAL_OK;
932 __IO uint32_t count = SYSCFG_DEFAULT_TIMEOUT * (SystemCoreClock / 20U / 1000U);
933
934 /* Set SYSCFG_CMPENSETR.MCU_EN */
935 HAL_EnableCompensationCell();
936
937 /* Wait SYSCFG_CMPCR.READY = 1 */
938 do
939 {
940 if (count-- == 0U)
941 {
942 return HAL_TIMEOUT;
943 }
944 }
945 while (__HAL_SYSCFG_CMP_CELL_GET_FLAG() == 0U);
946
947 /* Set SYSCFG_CMPCR.SW_CTRL = 0 */
948 HAL_SYSCFG_CompensationCodeSelect(SYSCFG_CELL_CODE);
949
950 return status;
951 }
952
953 /**
954 * @}
955 */
956
957 /**
958 * @}
959 */
960
961 /**
962 * @}
963 */
964
965 /**
966 * @}
967 */
968