HAL CRS How to Use ¶
- group CRS_How_To_Use
-
How to use the HAL CRS driver ¶
The HAL CRS driver can be used as follows: ¶
Enable HSI144 in the system clock configuration.
Initialize the CRS handle with HAL_CRS_Init(). Enable the CRS clock by 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, the divider of the selected source, the reload value, the frequency error limit value, and whether to use automatic trimming with HAL_CRS_SetConfig(). This operation is optional. Keep the default configuration if it is suitable. Retrieve the default configuration by calling HAL_CRS_ResetConfig(). You can also use the HAL_CRS_CALCULATE_RELOAD macro to calculate the reload value directly from the target and synchronization frequencies. 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. Therefore, no synchronization signal is provided to the CRS to calibrate the HSI144 while running. To guarantee the required clock precision after waking up from Sleep mode, use the LSE or reference clock on the GPIOs as the synchronization signal.
Polling mode operation: ¶
Start the CRS driver with HAL_CRS_StartSync() to enable the frequency error counter.
Use HAL_CRS_PollForSync() to wait for complete synchronization. Based on the status, adjust the trimming or the synchronization source, or continue the application if synchronization is OK. Retrieve error codes with HAL_CRS_GetLastErrorCodes() and information related to synchronization with HAL_CRS_GetFrequencyErrorInfo().
To update error codes and synchronization information, call HAL_CRS_StopSync() before changing the synchronization configuration with HAL_CRS_SetConfig(), then call HAL_CRS_StartSync() again. Note: When the synchronization event is detected during the downcounting phase (before reaching the zero value), this means that the actual frequency is lower than the target, so the trimming value must be incremented. When it is detected during the upcounting phase, this means that the actual frequency is higher, so the trimming value must be decremented.
Interrupt mode operation: ¶
Override the weak definitions for the following callbacks:
Or register callbacks:
Start the CRS driver with HAL_CRS_StartSync_IT() to enable the interrupt sources and receive callbacks.
This triggers the overridden weak callbacks or the registered callbacks from HAL_CRS_IRQHandler().
Generate a software synchronization: ¶
To force a synchronization event, call HAL_CRS_GenerateSoftwareSync(). You can call this function before HAL_CRS_SetConfig() (for example, in the SysTick handler).
HAL CRS Driver State: ¶
Use HAL_CRS_GetState() to return the HAL CRS state.