STM32L4xx_HAL_Driver  1.14.0
Extended Clock Recovery System Control functions

Extended Clock Recovery System Control functions. More...

Functions

void HAL_RCCEx_CRSConfig (RCC_CRSInitTypeDef *pInit)
 Start automatic synchronization for polling mode. More...
 
void HAL_RCCEx_CRSSoftwareSynchronizationGenerate (void)
 Generate the software synchronization event. More...
 
void HAL_RCCEx_CRSGetSynchronizationInfo (RCC_CRSSynchroInfoTypeDef *pSynchroInfo)
 Return synchronization info. More...
 
uint32_t HAL_RCCEx_CRSWaitSynchronization (uint32_t Timeout)
 Wait for CRS Synchronization status. More...
 
void HAL_RCCEx_CRS_IRQHandler (void)
 Handle the Clock Recovery System interrupt request. More...
 
void HAL_RCCEx_CRS_SyncOkCallback (void)
 RCCEx Clock Recovery System SYNCOK interrupt callback. More...
 
void HAL_RCCEx_CRS_SyncWarnCallback (void)
 RCCEx Clock Recovery System SYNCWARN interrupt callback. More...
 
void HAL_RCCEx_CRS_ExpectedSyncCallback (void)
 RCCEx Clock Recovery System Expected SYNC interrupt callback. More...
 
void HAL_RCCEx_CRS_ErrorCallback (uint32_t Error)
 RCCEx Clock Recovery System Error interrupt callback. More...
 

Detailed Description

Extended Clock Recovery System Control functions.

 ===============================================================================
                ##### Extended Clock Recovery System Control functions  #####
 ===============================================================================
    [..]
      For devices with Clock Recovery System feature (CRS), RCC Extention HAL driver can be used as follows:

      (#) In System clock config, HSI48 needs to be enabled

      (#) Enable CRS clock in IP MSP init which will use CRS functions

      (#) Call CRS functions as follows:
          (##) Prepare synchronization configuration necessary for HSI48 calibration
              (+++) Default values can be set for frequency Error Measurement (reload and error limit)
                        and also HSI48 oscillator smooth trimming.
              (+++) Macro __HAL_RCC_CRS_RELOADVALUE_CALCULATE can be also used to calculate
                        directly reload value with target and sychronization frequencies values
          (##) Call function HAL_RCCEx_CRSConfig which
              (+++) Resets CRS registers to their default values.
              (+++) Configures CRS registers with synchronization configuration
              (+++) Enables automatic calibration and frequency error counter feature
           Note: When using USB LPM (Link Power Management) and the device is in Sleep mode, the
           periodic USB SOF will not be generated by the host. No SYNC signal will therefore be
           provided to the CRS to calibrate the HSI48 on the run. To guarantee the required clock
           precision after waking up from Sleep mode, the LSE or reference clock on the GPIOs
           should be used as SYNC signal.

          (##) A polling function is provided to wait for complete synchronization
              (+++) Call function HAL_RCCEx_CRSWaitSynchronization()
              (+++) According to CRS status, user can decide to adjust again the calibration or continue
                        application if synchronization is OK

      (#) User can retrieve information related to synchronization in calling function
            HAL_RCCEx_CRSGetSynchronizationInfo()

      (#) Regarding synchronization status and synchronization information, user can try a new calibration
           in changing synchronization configuration and call again HAL_RCCEx_CRSConfig.
           Note: When the SYNC event is detected during the downcounting phase (before reaching the zero value),
           it means that the actual frequency is lower than the target (and so, that the TRIM value should be
           incremented), while when it is detected during the upcounting phase it means that the actual frequency
           is higher (and that the TRIM value should be decremented).

      (#) In interrupt mode, user can resort to the available macros (__HAL_RCC_CRS_XXX_IT). Interrupts will go
          through CRS Handler (CRS_IRQn/CRS_IRQHandler)
              (++) Call function HAL_RCCEx_CRSConfig()
              (++) Enable CRS_IRQn (thanks to NVIC functions)
              (++) Enable CRS interrupt (__HAL_RCC_CRS_ENABLE_IT)
              (++) Implement CRS status management in the following user callbacks called from
                   HAL_RCCEx_CRS_IRQHandler():
                   (+++) HAL_RCCEx_CRS_SyncOkCallback()
                   (+++) HAL_RCCEx_CRS_SyncWarnCallback()
                   (+++) HAL_RCCEx_CRS_ExpectedSyncCallback()
                   (+++) HAL_RCCEx_CRS_ErrorCallback()

      (#) To force a SYNC EVENT, user can use the function HAL_RCCEx_CRSSoftwareSynchronizationGenerate().
          This function can be called before calling HAL_RCCEx_CRSConfig (for instance in Systick handler)

Function Documentation

◆ HAL_RCCEx_CRS_ErrorCallback()

__weak void HAL_RCCEx_CRS_ErrorCallback ( uint32_t  Error)

RCCEx Clock Recovery System Error interrupt callback.

Parameters
ErrorCombination of Error status. This parameter can be a combination of the following values:
  • RCC_CRS_SYNCERR
  • RCC_CRS_SYNCMISS
  • RCC_CRS_TRIMOVF
Return values
none

Definition at line 2852 of file stm32l4xx_hal_rcc_ex.c.

2853 {
2854  /* Prevent unused argument(s) compilation warning */
2855  UNUSED(Error);
2856 
2857  /* NOTE : This function should not be modified, when the callback is needed,
2858  the @ref HAL_RCCEx_CRS_ErrorCallback should be implemented in the user file
2859  */
2860 }

◆ HAL_RCCEx_CRS_ExpectedSyncCallback()

__weak void HAL_RCCEx_CRS_ExpectedSyncCallback ( void  )

RCCEx Clock Recovery System Expected SYNC interrupt callback.

Return values
none

Definition at line 2836 of file stm32l4xx_hal_rcc_ex.c.

2837 {
2838  /* NOTE : This function should not be modified, when the callback is needed,
2839  the @ref HAL_RCCEx_CRS_ExpectedSyncCallback should be implemented in the user file
2840  */
2841 }

◆ HAL_RCCEx_CRS_IRQHandler()

void HAL_RCCEx_CRS_IRQHandler ( void  )

Handle the Clock Recovery System interrupt request.

Return values
None

Definition at line 2749 of file stm32l4xx_hal_rcc_ex.c.

2750 {
2751  uint32_t crserror = RCC_CRS_NONE;
2752  /* Get current IT flags and IT sources values */
2753  uint32_t itflags = READ_REG(CRS->ISR);
2754  uint32_t itsources = READ_REG(CRS->CR);
2755 
2756  /* Check CRS SYNCOK flag */
2757  if(((itflags & RCC_CRS_FLAG_SYNCOK) != 0U) && ((itsources & RCC_CRS_IT_SYNCOK) != 0U))
2758  {
2759  /* Clear CRS SYNC event OK flag */
2760  WRITE_REG(CRS->ICR, CRS_ICR_SYNCOKC);
2761 
2762  /* user callback */
2764  }
2765  /* Check CRS SYNCWARN flag */
2766  else if(((itflags & RCC_CRS_FLAG_SYNCWARN) != 0U) && ((itsources & RCC_CRS_IT_SYNCWARN) != 0U))
2767  {
2768  /* Clear CRS SYNCWARN flag */
2769  WRITE_REG(CRS->ICR, CRS_ICR_SYNCWARNC);
2770 
2771  /* user callback */
2773  }
2774  /* Check CRS Expected SYNC flag */
2775  else if(((itflags & RCC_CRS_FLAG_ESYNC) != 0U) && ((itsources & RCC_CRS_IT_ESYNC) != 0U))
2776  {
2777  /* frequency error counter reached a zero value */
2778  WRITE_REG(CRS->ICR, CRS_ICR_ESYNCC);
2779 
2780  /* user callback */
2782  }
2783  /* Check CRS Error flags */
2784  else
2785  {
2786  if(((itflags & RCC_CRS_FLAG_ERR) != 0U) && ((itsources & RCC_CRS_IT_ERR) != 0U))
2787  {
2788  if((itflags & RCC_CRS_FLAG_SYNCERR) != 0U)
2789  {
2790  crserror |= RCC_CRS_SYNCERR;
2791  }
2792  if((itflags & RCC_CRS_FLAG_SYNCMISS) != 0U)
2793  {
2794  crserror |= RCC_CRS_SYNCMISS;
2795  }
2796  if((itflags & RCC_CRS_FLAG_TRIMOVF) != 0U)
2797  {
2798  crserror |= RCC_CRS_TRIMOVF;
2799  }
2800 
2801  /* Clear CRS Error flags */
2802  WRITE_REG(CRS->ICR, CRS_ICR_ERRC);
2803 
2804  /* user error callback */
2805  HAL_RCCEx_CRS_ErrorCallback(crserror);
2806  }
2807  }
2808 }
void HAL_RCCEx_CRS_ExpectedSyncCallback(void)
RCCEx Clock Recovery System Expected SYNC interrupt callback.
void HAL_RCCEx_CRS_SyncOkCallback(void)
RCCEx Clock Recovery System SYNCOK interrupt callback.
void HAL_RCCEx_CRS_ErrorCallback(uint32_t Error)
RCCEx Clock Recovery System Error interrupt callback.
void HAL_RCCEx_CRS_SyncWarnCallback(void)
RCCEx Clock Recovery System SYNCWARN interrupt callback.

◆ HAL_RCCEx_CRS_SyncOkCallback()

__weak void HAL_RCCEx_CRS_SyncOkCallback ( void  )

RCCEx Clock Recovery System SYNCOK interrupt callback.

Return values
none

Definition at line 2814 of file stm32l4xx_hal_rcc_ex.c.

2815 {
2816  /* NOTE : This function should not be modified, when the callback is needed,
2817  the @ref HAL_RCCEx_CRS_SyncOkCallback should be implemented in the user file
2818  */
2819 }

◆ HAL_RCCEx_CRS_SyncWarnCallback()

__weak void HAL_RCCEx_CRS_SyncWarnCallback ( void  )

RCCEx Clock Recovery System SYNCWARN interrupt callback.

Return values
none

Definition at line 2825 of file stm32l4xx_hal_rcc_ex.c.

2826 {
2827  /* NOTE : This function should not be modified, when the callback is needed,
2828  the @ref HAL_RCCEx_CRS_SyncWarnCallback should be implemented in the user file
2829  */
2830 }

◆ HAL_RCCEx_CRSConfig()

void HAL_RCCEx_CRSConfig ( RCC_CRSInitTypeDef pInit)

Start automatic synchronization for polling mode.

Parameters
pInitPointer on RCC_CRSInitTypeDef structure
Return values
None

Definition at line 2580 of file stm32l4xx_hal_rcc_ex.c.

2581 {
2582  uint32_t value; /* no init needed */
2583 
2584  /* Check the parameters */
2585  assert_param(IS_RCC_CRS_SYNC_DIV(pInit->Prescaler));
2586  assert_param(IS_RCC_CRS_SYNC_SOURCE(pInit->Source));
2587  assert_param(IS_RCC_CRS_SYNC_POLARITY(pInit->Polarity));
2588  assert_param(IS_RCC_CRS_RELOADVALUE(pInit->ReloadValue));
2589  assert_param(IS_RCC_CRS_ERRORLIMIT(pInit->ErrorLimitValue));
2590  assert_param(IS_RCC_CRS_HSI48CALIBRATION(pInit->HSI48CalibrationValue));
2591 
2592  /* CONFIGURATION */
2593 
2594  /* Before configuration, reset CRS registers to their default values*/
2595  __HAL_RCC_CRS_FORCE_RESET();
2596  __HAL_RCC_CRS_RELEASE_RESET();
2597 
2598  /* Set the SYNCDIV[2:0] bits according to Prescaler value */
2599  /* Set the SYNCSRC[1:0] bits according to Source value */
2600  /* Set the SYNCSPOL bit according to Polarity value */
2601  value = (pInit->Prescaler | pInit->Source | pInit->Polarity);
2602  /* Set the RELOAD[15:0] bits according to ReloadValue value */
2603  value |= pInit->ReloadValue;
2604  /* Set the FELIM[7:0] bits according to ErrorLimitValue value */
2605  value |= (pInit->ErrorLimitValue << CRS_CFGR_FELIM_Pos);
2606  WRITE_REG(CRS->CFGR, value);
2607 
2608  /* Adjust HSI48 oscillator smooth trimming */
2609  /* Set the TRIM[6:0] bits for STM32L412xx/L422xx or TRIM[5:0] bits otherwise
2610  according to RCC_CRS_HSI48CalibrationValue value */
2611  MODIFY_REG(CRS->CR, CRS_CR_TRIM, (pInit->HSI48CalibrationValue << CRS_CR_TRIM_Pos));
2612 
2613  /* START AUTOMATIC SYNCHRONIZATION*/
2614 
2615  /* Enable Automatic trimming & Frequency error counter */
2616  SET_BIT(CRS->CR, CRS_CR_AUTOTRIMEN | CRS_CR_CEN);
2617 }
MODIFY_REG(hrtc->Instance->CR, RTC_CR_WUCKSEL,(uint32_t) WakeUpClock)
assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock))

◆ HAL_RCCEx_CRSGetSynchronizationInfo()

void HAL_RCCEx_CRSGetSynchronizationInfo ( RCC_CRSSynchroInfoTypeDef pSynchroInfo)

Return synchronization info.

Parameters
pSynchroInfoPointer on RCC_CRSSynchroInfoTypeDef structure
Return values
None

Definition at line 2633 of file stm32l4xx_hal_rcc_ex.c.

2634 {
2635  /* Check the parameter */
2636  assert_param(pSynchroInfo != (void *)NULL);
2637 
2638  /* Get the reload value */
2639  pSynchroInfo->ReloadValue = (READ_BIT(CRS->CFGR, CRS_CFGR_RELOAD));
2640 
2641  /* Get HSI48 oscillator smooth trimming */
2642  pSynchroInfo->HSI48CalibrationValue = (READ_BIT(CRS->CR, CRS_CR_TRIM) >> CRS_CR_TRIM_Pos);
2643 
2644  /* Get Frequency error capture */
2645  pSynchroInfo->FreqErrorCapture = (READ_BIT(CRS->ISR, CRS_ISR_FECAP) >> CRS_ISR_FECAP_Pos);
2646 
2647  /* Get Frequency error direction */
2648  pSynchroInfo->FreqErrorDirection = (READ_BIT(CRS->ISR, CRS_ISR_FEDIR));
2649 }
assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock))

◆ HAL_RCCEx_CRSSoftwareSynchronizationGenerate()

void HAL_RCCEx_CRSSoftwareSynchronizationGenerate ( void  )

Generate the software synchronization event.

Return values
None

Definition at line 2623 of file stm32l4xx_hal_rcc_ex.c.

2624 {
2625  SET_BIT(CRS->CR, CRS_CR_SWSYNC);
2626 }

◆ HAL_RCCEx_CRSWaitSynchronization()

uint32_t HAL_RCCEx_CRSWaitSynchronization ( uint32_t  Timeout)

Wait for CRS Synchronization status.

Parameters
TimeoutDuration of the timeout
Note
Timeout is based on the maximum time to receive a SYNC event based on synchronization frequency.
If Timeout set to HAL_MAX_DELAY, HAL_TIMEOUT will be never returned.
Return values
Combinationof Synchronization status This parameter can be a combination of the following values:
  • RCC_CRS_TIMEOUT
  • RCC_CRS_SYNCOK
  • RCC_CRS_SYNCWARN
  • RCC_CRS_SYNCERR
  • RCC_CRS_SYNCMISS
  • RCC_CRS_TRIMOVF

Definition at line 2666 of file stm32l4xx_hal_rcc_ex.c.

2667 {
2668  uint32_t crsstatus = RCC_CRS_NONE;
2669  uint32_t tickstart;
2670 
2671  /* Get timeout */
2672  tickstart = HAL_GetTick();
2673 
2674  /* Wait for CRS flag or timeout detection */
2675  do
2676  {
2677  if(Timeout != HAL_MAX_DELAY)
2678  {
2679  if(((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
2680  {
2681  crsstatus = RCC_CRS_TIMEOUT;
2682  }
2683  }
2684  /* Check CRS SYNCOK flag */
2685  if(__HAL_RCC_CRS_GET_FLAG(RCC_CRS_FLAG_SYNCOK))
2686  {
2687  /* CRS SYNC event OK */
2688  crsstatus |= RCC_CRS_SYNCOK;
2689 
2690  /* Clear CRS SYNC event OK bit */
2691  __HAL_RCC_CRS_CLEAR_FLAG(RCC_CRS_FLAG_SYNCOK);
2692  }
2693 
2694  /* Check CRS SYNCWARN flag */
2695  if(__HAL_RCC_CRS_GET_FLAG(RCC_CRS_FLAG_SYNCWARN))
2696  {
2697  /* CRS SYNC warning */
2698  crsstatus |= RCC_CRS_SYNCWARN;
2699 
2700  /* Clear CRS SYNCWARN bit */
2701  __HAL_RCC_CRS_CLEAR_FLAG(RCC_CRS_FLAG_SYNCWARN);
2702  }
2703 
2704  /* Check CRS TRIM overflow flag */
2705  if(__HAL_RCC_CRS_GET_FLAG(RCC_CRS_FLAG_TRIMOVF))
2706  {
2707  /* CRS SYNC Error */
2708  crsstatus |= RCC_CRS_TRIMOVF;
2709 
2710  /* Clear CRS Error bit */
2711  __HAL_RCC_CRS_CLEAR_FLAG(RCC_CRS_FLAG_TRIMOVF);
2712  }
2713 
2714  /* Check CRS Error flag */
2715  if(__HAL_RCC_CRS_GET_FLAG(RCC_CRS_FLAG_SYNCERR))
2716  {
2717  /* CRS SYNC Error */
2718  crsstatus |= RCC_CRS_SYNCERR;
2719 
2720  /* Clear CRS Error bit */
2721  __HAL_RCC_CRS_CLEAR_FLAG(RCC_CRS_FLAG_SYNCERR);
2722  }
2723 
2724  /* Check CRS SYNC Missed flag */
2725  if(__HAL_RCC_CRS_GET_FLAG(RCC_CRS_FLAG_SYNCMISS))
2726  {
2727  /* CRS SYNC Missed */
2728  crsstatus |= RCC_CRS_SYNCMISS;
2729 
2730  /* Clear CRS SYNC Missed bit */
2731  __HAL_RCC_CRS_CLEAR_FLAG(RCC_CRS_FLAG_SYNCMISS);
2732  }
2733 
2734  /* Check CRS Expected SYNC flag */
2735  if(__HAL_RCC_CRS_GET_FLAG(RCC_CRS_FLAG_ESYNC))
2736  {
2737  /* frequency error counter reached a zero value */
2738  __HAL_RCC_CRS_CLEAR_FLAG(RCC_CRS_FLAG_ESYNC);
2739  }
2740  } while(RCC_CRS_NONE == crsstatus);
2741 
2742  return crsstatus;
2743 }
uint32_t HAL_GetTick(void)
Provide a tick value in millisecond.