HAL CRS How to Use ¶
- group CRS_How_To_Use
-
The clock recovery system (CRS) is an advanced digital controller acting on the internal fine-granularity trimmable RC oscillator HSI48. The CRS provides powerful means for oscillator output frequency evaluation, based on comparison with a selectable synchronization signal. The oscillator trimming can be automatically adjusted based on the measured frequency error value, while still allowing for manual trimming to be performed if required. The CRS is ideally suited to provide a precise clock to the USB peripheral. In such a case, the synchronization signal can be derived from the start-of-frame (SOF) packet signalization on the USB bus, which is sent by a USB host at 1 ms intervals. The synchronization signal can also be derived from the LSE oscillator output or it can be generated by user software.
Main features ¶
The main features of CRS are described below:
-
Selectable synchronization source with programmable prescaler and polarity:
-
LSE oscillator output
-
USB SOF packet reception
-
-
Possibility to generate synchronization pulses by software
-
Automatic oscillator trimming capability with no need of CPU action
-
Manual control option for faster start-up convergence
-
16-bit frequency error counter with automatic error value capture and reload
-
Programmable limit for automatic frequency error value evaluation and status reporting
-
Maskable interrupts/events:
-
Expected synchronization (ESYNC)
-
Synchronization OK (SYNCOK)
-
Synchronization warning (SYNCWARN)
-
Synchronization or trimming error (ERR)
-
How to use the HAL CRS driver ¶
The HAL CRS driver can be used as follows: ¶
-
In System clock config, HSI48 needs to be enabled.
-
Initialize the CRS according to the associated handle with HAL_CRS_Init() . CRS clock is disabled by default but can be enabled in setting USE_HAL_CRS_CLK_ENABLE_MODEL to HAL_CLK_ENABLE_PERIPH_ONLY.
-
Set the configuration of the CRS to choose the source, the input polarity and the divider of the selected source, the reload value, the frequency error limit value and the use of automatic trimming or not with HAL_CRS_SetConfig() . This operation is optional, the user can keep the default configuration. The user can retrieve the default configuration by calling HAL_CRS_ResetConfig() . Macro HAL_CRS_CALCULATE_RELOAD can be also used to calculate directly reload value with target and synchronization frequencies values. Note: When using USB LPM (Link Power Management) and the device is in Sleep mode, the periodic USB SOF is not generated by the host. No synchronization signal is therefore 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 must be used as synchronization signal.
Polling mode operation: ¶
-
Start the CRS driver to enable the frequency error counter with HAL_CRS_StartSync() .
-
A polling function is provided to wait for complete synchronization ( HAL_CRS_PollForSync() ). According to the status, user can decide to adjust again the trimming or the synchronization source or continue application if synchronization is OK. User can retrieve error codes with HAL_CRS_GetLastErrorCodes() and information related to synchronization with HAL_CRS_GetFrequencyErrorInfo() .
-
Regarding error codes and synchronization information, user need to call HAL_CRS_StopSync() before trying a new calibration in changing synchronization configuration with HAL_CRS_SetConfig() and call again HAL_CRS_StartSync() . Note: When the synchronization 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 trimming value must be incremented), while when it is detected during the upcounting phase it means that the actual frequency is higher (and that the trimming value must be decremented).
Interrupt mode operation: ¶
-
Override weak definition for following callbacks:
-
Or use register callbacks:
-
Start the CRS driver with HAL_CRS_StartSync_IT() to enable the interrupt sources and allow to receive callbacks.
-
It either triggers the associated overridden weak callbacks or the register ones launched in HAL_CRS_IRQHandler() .
Generate a software synchronization: ¶
To force a synchronization event, user can use the function HAL_CRS_GenerateSoftwareSync() . This function can be called before calling HAL_CRS_SetConfig() (for instance in SysTick handler).
HAL CRS Driver State: ¶
-
Use HAL_CRS_GetState() function to return the HAL CRS state.
-