RA Flexible Software Package Documentation  Release v5.6.0

 
Comparator, High-Speed (r_acmphs)

Functions

fsp_err_t R_ACMPHS_Open (comparator_ctrl_t *const p_ctrl, comparator_cfg_t const *const p_cfg)
 
fsp_err_t R_ACMPHS_InfoGet (comparator_ctrl_t *const p_ctrl, comparator_info_t *const p_info)
 
fsp_err_t R_ACMPHS_OutputEnable (comparator_ctrl_t *const p_ctrl)
 
fsp_err_t R_ACMPHS_StatusGet (comparator_ctrl_t *const p_ctrl, comparator_status_t *const p_status)
 
fsp_err_t R_ACMPHS_Close (comparator_ctrl_t *p_ctrl)
 

Detailed Description

Driver for the ACMPHS peripheral on RA MCUs. This module implements the Comparator Interface.

Overview

Features

The ACMPHS HAL module supports the following features:

Note
1. This output pin is not available on all MCUs.

Configuration

Build Time Configurations for r_acmphs

The following build time configurations are defined in fsp_cfg/r_acmphs_cfg.h:

ConfigurationOptionsDefaultDescription
Parameter Checking
  • Default (BSP)
  • Enabled
  • Disabled
Default (BSP) If selected code for parameter checking is included in the build.

Configurations for Analog > Comparator, High-Speed (r_acmphs)

This module can be added to the Stacks tab via New Stack > Analog > Comparator, High-Speed (r_acmphs).

ConfigurationOptionsDefaultDescription
NameName must be a valid C symbolg_comparator0 Module name.
ChannelValue must be a non-negative integer0 Select the hardware channel.
Trigger Edge Selector
  • Rising
  • Falling
  • Both Edge
Both Edge The trigger specifies when a comparator callback event should occur. Unused if the interrupt priority is disabled or the callback is NULL.
Noise Filter
  • No Filter
  • 8
  • 16
  • 32
No Filter Select the PCLK divisor for the hardware digital debounce filter. Larger divisors provide a longer debounce and take longer for the output to update.
Maximum status retries (CMPMON)Must be a valid non-negative integer between 2 and 32-bit maximum value1024 Maximum number of status retries.
Output Polarity
  • Not Inverted
  • Inverted
Not Inverted When enabled comparator output is inverted. This affects the output read from R_ACMPHS_StatusGet(), the pin output level, and the edge trigger.
Pin Output
  • Disabled
  • Enabled
Disabled Turn this on to enable the CMPOUTn signal for this channel. The CMPOUTn signal for each channel is OR'd together and the result is output to VCOUT. More pin output options are available on select MCUs.
CallbackName must be a valid C symbolNULL Define this function in the application. It is called when the Trigger event occurs.
Comparator Interrupt PriorityMCU Specific OptionsSelect the interrupt priority for the comparator interrupt.
Analog Input Voltage Source (IVCMP)MCU Specific OptionsSelect the Analog input voltage source. Channel mentioned in the options represents channel in ACMPHS
Reference Voltage Input Source (IVREF)MCU Specific OptionsSelect the Analog reference voltage source. Channel mentioned in the options represents channel in ACMPHS

Clock Configuration

The ACMPHS peripheral is clocked from PCLKB. You can set the PCLKB frequency using the Clocks tab of the RA Configuration editor or by using the CGC Interface at run-time.

Pin Configuration

Comparator output can be enabled or disabled on each channel individually. The VCOUT pin is a logical OR of all comparator outputs.

The IVCMPn pins are used as comparator inputs. The IVREFn pins are used as comparator reference values.

Usage Notes

Noise Filter

When the noise filter is enabled, the ACMPHP0/ACMPHP1 signal is sampled three times based on the sampling clock selected. The filter clock frequency is determined by PCLKB and the comparator_filter_t setting.

Output Polarity

If output polarity is configured as "Inverted" then the VCOUT signal will be inverted and the R_ACMPHS_StatusGet() will return an inverted status.

Limitations

Examples

Basic Example

The following is a basic example of using the ACMPHS to detect when the analog voltage input to IVCMP rises above the analog voltage input to IVREF. A GPIO output acts as the comparator input and is externally connected to the IVCMP input of the ACMPHS. An analog voltage input should also be supplied to the IVREF input pin.

#define ADC_PGA_BYPASS_VALUE (0x9999)
/* Connect this control pin to the IVCMP input of the comparator. This can be any GPIO pin
* that is not input only. */
#define ACMPHS_EXAMPLE_CONTROL_PIN (BSP_IO_PORT_05_PIN_03)
volatile uint32_t g_comparator_events = 0U;
/* This callback is called when a comparator event occurs. */
void acmphs_example_callback (comparator_callback_args_t * p_args)
{
g_comparator_events++;
}
void acmphs_example ()
{
fsp_err_t err = FSP_SUCCESS;
/* Disable pin register write protection, if enabled */
/*
* Start with the IVCMP pin low. This example assumes the comparator is configured to trigger
* when the voltage of the analog input to IVCMP rises above voltage of the analog input to
* IVREF.
*/
(void) R_BSP_PinWrite(ACMPHS_EXAMPLE_CONTROL_PIN, BSP_IO_LEVEL_LOW);
/* Initialize the ACMPHS module */
err = R_ACMPHS_Open(&g_comparator_ctrl, &g_comparator_cfg);
assert(FSP_SUCCESS == err);
/*
* If an ADC PGA exists for the analog input pin, then the PGA must be manually configured in order for the pin to be used as
* an IVCMP input. This procedure is slightly different depending on the MCU (See below).
*/
#if BSP_MCU_GROUP_RA6M3
/* The following applies for MCUs with the ADC peripheral:
*
* Bypass the PGA on ADC unit 0.
* (See Table 50.2 "Input source configuration of the ACMPHS" in the RA6M3 User's Manual (R01UH0886EJ0100)) */
R_ADC0->ADPGACR = ADC_PGA_BYPASS_VALUE;
R_ADC0->ADPGADCR0 = 0;
#elif BSP_MCU_GROUP_RA6T2
/* The following applies for MCUs with the ADC_B peripheral:
*
* Configure PGA on ADC unit 0.
* (See Table 36.11 "PGA Settings and Available Related Functions" in the RA6T2 User's Manual (R01UH0951EJ0100)) */
R_ADC_B->ADPGACR[0] = R_ADC_B0_ADPGACR_PGAGEN_Msk;
#endif
/* Wait for the minimum stabilization wait time before enabling output. */
R_ACMPHS_InfoGet(&g_comparator_ctrl, &info);
/* Enable the comparator output */
(void) R_ACMPHS_OutputEnable(&g_comparator_ctrl);
/* Set the IVCMP pin high. */
(void) R_BSP_PinWrite(ACMPHS_EXAMPLE_CONTROL_PIN, BSP_IO_LEVEL_HIGH);
while (0 == g_comparator_events)
{
/* Wait for interrupt. */
}
/* Check status of comparator, Status will be COMPARATOR_STATE_OUTPUT_HIGH */
(void) R_ACMPHS_StatusGet(&g_comparator_ctrl, &status);
}

Function Documentation

◆ R_ACMPHS_Open()

fsp_err_t R_ACMPHS_Open ( comparator_ctrl_t *const  p_ctrl,
comparator_cfg_t const *const  p_cfg 
)

Configures the comparator and starts operation. Callbacks and pin output are not active until outputEnable() is called. comparator_api_t::outputEnable() should be called after the output has stabilized. Implements comparator_api_t::open().

Comparator inputs must be configured in the application code prior to calling this function.

Return values
FSP_SUCCESSOpen successful.
FSP_ERR_ASSERTIONAn input pointer is NULL
FSP_ERR_INVALID_ARGUMENTAn argument is invalid. Window mode (COMPARATOR_MODE_WINDOW) and filter of 1 (COMPARATOR_FILTER_1) are not supported in this implementation.
FSP_ERR_ALREADY_OPENThe control block is already open or the hardware lock is taken.

◆ R_ACMPHS_InfoGet()

fsp_err_t R_ACMPHS_InfoGet ( comparator_ctrl_t *const  p_ctrl,
comparator_info_t *const  p_info 
)

Provides the minimum stabilization wait time in microseconds. Implements comparator_api_t::infoGet().

Return values
FSP_SUCCESSInformation stored in p_info.
FSP_ERR_ASSERTIONAn input pointer was NULL.
FSP_ERR_NOT_OPENInstance control block is not open.

◆ R_ACMPHS_OutputEnable()

fsp_err_t R_ACMPHS_OutputEnable ( comparator_ctrl_t *const  p_ctrl)

Enables the comparator output, which can be polled using comparator_api_t::statusGet(). Also enables pin output and interrupts as configured during comparator_api_t::open(). Implements comparator_api_t::outputEnable().

Return values
FSP_SUCCESSComparator output is enabled.
FSP_ERR_ASSERTIONAn input pointer was NULL.
FSP_ERR_NOT_OPENInstance control block is not open.

◆ R_ACMPHS_StatusGet()

fsp_err_t R_ACMPHS_StatusGet ( comparator_ctrl_t *const  p_ctrl,
comparator_status_t *const  p_status 
)

Provides the operating status of the comparator. Implements comparator_api_t::statusGet().

Return values
FSP_SUCCESSOperating status of the comparator is provided in p_status.
FSP_ERR_ASSERTIONAn input pointer was NULL.
FSP_ERR_NOT_OPENInstance control block is not open.
FSP_ERR_TIMEOUTThe debounce filter is off and 2 consecutive matching values were not read within 1024 attempts.

◆ R_ACMPHS_Close()

fsp_err_t R_ACMPHS_Close ( comparator_ctrl_t p_ctrl)

Stops the comparator. Implements comparator_api_t::close().

Return values
FSP_SUCCESSInstance control block closed successfully.
FSP_ERR_ASSERTIONAn input pointer was NULL.
FSP_ERR_NOT_OPENInstance control block is not open.