HAL COMP How to Use ¶
- group COMP_How_To_Use
-
How to use the COMP (analog comparator) HAL module driver ¶
HAL COMP driver usage ¶
COMP configuration :
System configuration (out of HAL COMP driver).
RCC to provide COMP kernel clock.
GPIO to connect comparator inputs and output to device pins.
CPU Cortex NVIC to configure interrupts lines (if COMP usage with interrupt).
COMP peripheral configuration.
COMP peripheral is structured in subblocks with each a specific scope. HAL COMP follows this structure with a configuration structure and associated function for each subblock.
COMP instance subblock:
basic configuration (prefix HAL_COMP).
output blanking from signal of other peripheral (typically: timer) (optional).
COMP multi-instances subblocks:
window mode (prefix HAL_COMP_WINDOW): combine multiple COMP instances for voltage comparison to 2 thresholds.
COMP instances can belong to a COMP common instance, in this case they can share features (window mode, other shared features, …). HAL COMP driver provides a mechanism to link HAL COMP handles and manage shared features.
HAL COMP configuration steps:
Configure system.
Initialize HAL COMP handle using HAL_COMP_Init().
Case of multiple COMP instances used: Link HAL COMP handles using HAL_COMP_WINDOW_SetHandle(). (for more details, refer to function description).
Configure comparator using functions HAL_COMP_[WINDOW]_SetConfig{Features}() and optional features with unitary functions HAL_COMP_[WINDOW]_Set{Features}()
COMP operation
Activation and deactivation :
COMP peripheral requires a specific procedure for activation (internal analog circuitry control, delay for stabilization time). Note: From activation step, COMP internal analog hardware is enabled, inducing current consumption. Therefore, after COMP usage, COMP must be deactivated for power optimization.
COMP analog comparison management :
Comparisons can be performed using two programming models:
Background operation (for system wake up, asynchronous read of comparator output, comparator output on GPIO: using HAL_COMP_[WINDOW]_Start()
Interrupt mode: using HAL_COMP_[WINDOW]_StartConv_IT(), HAL_COMP_IRQHandler() and callback functions.
HAL COMP operation steps:
Activate and start COMP comparison using functions HAL_COMP_[WINDOW]_Start…(). Optionally, lock comparator using function HAL_COMP_[WINDOW]_Lock(): for safety purpose, comparator configuration frozen until system reset.
Process comparison using HAL_COMP_[WINDOW]_GetOutputLevel(), IRQ handler and callback functions
Deactivate and stop COMP comparison using functions HAL_COMP_[WINDOW]_Stop…() (if not locked).
Callback registration ¶
When the compilation flag USE_HAL_COMP_REGISTER_CALLBACKS is set to 1, functions HAL_COMP_Register…Callback() allow registering following callbacks:
HAL_COMP_OutputTriggerCallback() : COMP output trigger callback
When the compilation flag USE_HAL_COMP_REGISTER_CALLBACKS is set to 0 or not defined, the callback registration feature is not available and all callbacks are set to the corresponding weak functions.