Driver for the OPAMP peripheral on RA MCUs. This module implements the OPAMP Interface.
Overview
The OPAMP HAL module provides a high level API for signal amplification applications and supports the OPAMP peripheral available on RA MCUs.
Features
- Low power or high-speed mode
- Start by software or AGT compare match
- Stop by software or ADC conversion end (stop by ADC conversion end only supported on op-amp channels configured to start by AGT compare match)
- Trimming available on some MCUs (see hardware manual)
Supported Devices
| Device Group | Devices |
| RA2 | RA2A1 |
| RA4 | RA4M1, RA4W1 |
Configuration
Build Time Configurations for r_opamp
The following build time configurations are defined in fsp_cfg/r_opamp_cfg.h:
| Configuration | Options | Default | Description |
| Parameter Checking |
-
Default (BSP)
-
Enabled
-
Disabled
| Default (BSP) | If selected code for parameter checking is included in the build. |
Configurations for Analog > Operational Amplifier (r_opamp)
This module can be added to the Stacks tab via New Stack > Analog > Operational Amplifier (r_opamp).
| Configuration | Options | Default | Description |
| Name | Name must be a valid C symbol | g_opamp0 | Module name. |
| AGT Start Trigger Configuration (N/A unless AGT Start Trigger is Selected for the Channel) |
-
AGT1 Compare Match Starts OPAMPs 0 and 2 if configured for AGT Start, AGT0 Compare Match Starts OPAMPs 1 and 3 if configured for AGT Start
-
AGT1 Compare Match Starts OPAMPs 0 and 1 if configured for AGT Start, AGT0 Compare Match Starts OPAMPs 2 and 3 if configured for AGT Start
-
AGT1 Compare Match Starts all OPAMPs configured for AGT Start
| AGT1 Compare Match Starts all OPAMPs configured for AGT Start | Configure which AGT channel event triggers which op-amp channel. The AGT compare match event only starts the op-amp channel if the AGT Start trigger is selected in the Trigger configuration for the channel. |
| Power Mode | MCU Specific Options | | Configure the op-amp based on power or speed requirements. This setting affects the minimum required stabilization time. Middle speed is not available for all MCUs. |
| Trigger Channel 0 | MCU Specific Options | | Select the event triggers to start or stop op-amp channel 0. If the event trigger is selected for start, the start() API enables the event trigger for this channel. If the event trigger is selected for stop, the stop() API disables the event trigger for this channel. |
| Trigger Channel 1 | MCU Specific Options | | Select the event triggers to start or stop op-amp channel 1. If the event trigger is selected for start, the start() API enables the event trigger for this channel. If the event trigger is selected for stop, the stop() API disables the event trigger for this channel. |
| Trigger Channel 2 |
-
Software Start Software Stop
-
AGT Start Software Stop
-
AGT Start ADC Stop
| Software Start Software Stop | Select the event triggers to start or stop op-amp channel 2. If the event trigger is selected for start, the start() API enables the event trigger for this channel. If the event trigger is selected for stop, the stop() API disables the event trigger for this channel. |
| Trigger Channel 3 | MCU Specific Options | | Select the event triggers to start or stop op-amp channel 3. If the event trigger is selected for start, the start() API enables the event trigger for this channel. If the event trigger is selected for stop, the stop() API disables the event trigger for this channel. |
| OPAMP AMP0OS | MCU Specific Options | | Select output to connect to AMP0O pin |
| OPAMP AMP0PS | MCU Specific Options | | Select input to connect to AMP0+ pin |
| OPAMP AMP0MS | MCU Specific Options | | Select input to connect to AMP0- pin |
| OPAMP AMP1PS | MCU Specific Options | | Select input to connect to AMP1+ pin |
| OPAMP AMP1MS | MCU Specific Options | | Select input to connect to AMP1- pin |
| OPAMP AMP2PS | MCU Specific Options | | Select input to connect to AMP2+ pin |
| OPAMP AMP2MS | MCU Specific Options | | Select input to connect to AMP2- pin |
Clock Configuration
The OPAMP runs on PCLKB.
Pin Configuration
To use the OPAMP HAL module, the port pins for the channels receiving the analog input must be set as inputs on the Pins tab of the RA Configuration editor.
Refer to the most recent FSP Release Notes for any additional operational limitations for this module.
Usage Notes
Trimming the OPAMP
- On MCUs that support trimming, the op-amp trim register is set to the factory default after the Open API is called.
- This function allows the application to trim the operational amplifier to a user setting, which overwrites the factory default trim values.
- Supported on selected MCUs. See hardware manual for details.
- Not supported if configured for low power mode (OPAMP_MODE_LOW_POWER).
- This function is not reentrant. Only one side of one op-amp can be trimmed at a time. Complete the procedure for one side of one channel before calling the trim API with the command OPAMP_TRIM_CMD_START again.
- The trim procedure works as follows:
- Call trim() for the Pch (+) side input with command OPAMP_TRIM_CMD_START.
- Connect a fixed voltage to the Pch (+) input.
- Connect the Nch (-) input to the op-amp output to create a voltage follower.
- Ensure the op-amp is operating and stabilized.
- Call trim() for the Pch (+) side input with command OPAMP_TRIM_CMD_START.
- Measure the fixed voltage connected to the Pch (+) input using the SAR ADC and save the value (referred to as A later in this procedure).
- Iterate over the following loop 5 times:
- Call trim() for the Pch (+) side input with command OPAMP_TRIM_CMD_NEXT_STEP.
- Measure the op-amp output using the SAR ADC (referred to as B in the next step).
- If A <= B, call trim() for the Pch (+) side input with command OPAMP_TRIM_CMD_CLEAR_BIT.
- Call trim() for the Nch (-) side input with command OPAMP_TRIM_CMD_START.
- Measure the fixed voltage connected to the Pch (+) input using the SAR ADC and save the value (referred to as A later in this procedure).
- Iterate over the following loop 5 times:
- Call trim() for the Nch (-) side input with command OPAMP_TRIM_CMD_NEXT_STEP.
- Measure the op-amp output using the SAR ADC (referred to as B in the next step).
- If A <= B, call trim() for the Nch (-) side input with command OPAMP_TRIM_CMD_CLEAR_BIT.
Examples
Basic Example
This is a basic example of minimal use of the R_OPAMP in an application. The example demonstrates configuring OPAMP channel 0 for high speed mode, starting the OPAMP and reading the status of the OPAMP channel running. It also verifies that the stabilization wait time is the expected time for selected power mode
#define OPAMP_EXAMPLE_CHANNEL (0U)
void basic_example (void)
{
assert(FSP_SUCCESS == err);
assert(FSP_SUCCESS == err);
assert(FSP_SUCCESS == err);
}
Trim Example
This example demonstrates the typical trimming procedure for opamp channel 0 using R_OPAMP_Trim() API.
#ifndef OPAMP_EXAMPLE_CHANNEL
#define OPAMP_EXAMPLE_CHANNEL (0U)
#endif
#ifndef OPAMP_EXAMPLE_ADC_CHANNEL
#define OPAMP_EXAMPLE_ADC_CHANNEL (ADC_CHANNEL_2)
#endif
#define ADC_SCAN_END_DELAY (100U)
#define OPAMP_TRIM_LOOP_COUNT (5)
#define ADC_SCAN_END_MAX_TIMEOUT (0xFFFF)
uint32_t g_callback_event_counter = 0;
{
};
{
g_callback_event_counter++;
}
void trimming_example (void)
{
assert(FSP_SUCCESS == err);
assert(FSP_SUCCESS == err);
assert(FSP_SUCCESS == err);
trim_procedure(&trim_args_ch);
assert(FSP_SUCCESS == err);
trim_procedure(&trim_args_ch);
}
{
assert(FSP_SUCCESS == err);
g_callback_event_counter = 0;
assert(FSP_SUCCESS == err);
uint32_t timeout = ADC_SCAN_END_MAX_TIMEOUT;
while (g_callback_event_counter == 0 && timeout != 0)
{
timeout--;
}
if (0 == timeout)
{
assert(FSP_SUCCESS == err);
}
uint16_t result_a;
err =
R_ADC_Read(&g_adc_ctrl, OPAMP_EXAMPLE_ADC_CHANNEL, &result_a);
assert(FSP_SUCCESS == err);
uint8_t count = OPAMP_TRIM_LOOP_COUNT;
while (count > 0)
{
count--;
assert(FSP_SUCCESS == err);
g_callback_event_counter = 0;
assert(FSP_SUCCESS == err);
timeout = ADC_SCAN_END_MAX_TIMEOUT;
while (g_callback_event_counter == 0 && timeout != 0)
{
timeout--;
}
if (0 == timeout)
{
assert(FSP_SUCCESS == err);
}
uint16_t result_b;
err =
R_ADC_Read(&g_adc_ctrl, OPAMP_EXAMPLE_ADC_CHANNEL, &result_b);
assert(FSP_SUCCESS == err);
if (result_a <= result_b)
{
assert(FSP_SUCCESS == err);
}
}
}
◆ opamp_extended_cfg_t
| struct opamp_extended_cfg_t |
◆ opamp_instance_ctrl_t
| struct opamp_instance_ctrl_t |
◆ OPAMP_MASK_CHANNEL_0
| #define OPAMP_MASK_CHANNEL_0 |
Version of code that implements the API defined in this file
◆ opamp_trigger_t
Start and stop trigger for the op-amp.
| Enumerator |
|---|
| OPAMP_TRIGGER_SOFTWARE_START_SOFTWARE_STOP | Start and stop with APIs.
|
| OPAMP_TRIGGER_AGT_START_SOFTWARE_STOP | Start by AGT compare match and stop with API.
|
| OPAMP_TRIGGER_AGT_START_ADC_STOP | Start by AGT compare match and stop after ADC conversion.
|
◆ opamp_agt_link_t
Which AGT timer starts the op-amp. Only applies to channels if OPAMP_TRIGGER_AGT_START_SOFTWARE_STOP or OPAMP_TRIGGER_AGT_START_ADC_STOP is selected for the channel. If OPAMP_TRIGGER_SOFTWARE_START_SOFTWARE_STOP is selected for a channel, then no AGT compare match event will start that op-amp channel.
| Enumerator |
|---|
| OPAMP_AGT_LINK_AGT1_OPAMP_0_2_AGT0_OPAMP_1_3 | OPAMP channel 0 and 2 are started by AGT1 compare match. OPAMP channel 1 and 3 are started by AGT0 compare match.
|
| OPAMP_AGT_LINK_AGT1_OPAMP_0_1_AGT0_OPAMP_2_3 | OPAMP channel 0 and 1 are started by AGT1 compare match. OPAMP channel 2 and 3 are started by AGT0 compare match.
|
| OPAMP_AGT_LINK_AGT1_OPAMP_0_1_2_3 | All OPAMP channels are started by AGT1 compare match.
|
◆ opamp_mode_t
Op-amp mode.
| Enumerator |
|---|
| OPAMP_MODE_LOW_POWER | Low power mode.
|
| OPAMP_MODE_MIDDLE_SPEED | Middle speed mode (not supported on all MCUs)
|
| OPAMP_MODE_HIGH_SPEED | High speed mode.
|
◆ opamp_plus_input_t
Options to connect AMPnPS pins.
| Enumerator |
|---|
| OPAMP_PLUS_INPUT_NONE | No Connection.
|
| OPAMP_PLUS_INPUT_AMPPS0 | Set AMPPS0. See hardware manual for channel specific options.
|
| OPAMP_PLUS_INPUT_AMPPS1 | Set AMPPS1. See hardware manual for channel specific options.
|
| OPAMP_PLUS_INPUT_AMPPS2 | Set AMPPS2. See hardware manual for channel specific options.
|
| OPAMP_PLUS_INPUT_AMPPS3 | Set AMPPS3. See hardware manual for channel specific options.
|
| OPAMP_PLUS_INPUT_AMPPS7 | Set AMPPS7. See hardware manual for channel specific options.
|
◆ opamp_minus_input_t
Options to connect AMPnMS pins.
| Enumerator |
|---|
| OPAMP_MINUS_INPUT_NONE | No Connection.
|
| OPAMP_MINUS_INPUT_AMPMS0 | Set AMPMS0. See hardware manual for channel specific options.
|
| OPAMP_MINUS_INPUT_AMPMS1 | Set AMPMS1. See hardware manual for channel specific options.
|
| OPAMP_MINUS_INPUT_AMPMS2 | Set AMPMS2. See hardware manual for channel specific options.
|
| OPAMP_MINUS_INPUT_AMPMS3 | Set AMPMS3. See hardware manual for channel specific options.
|
| OPAMP_MINUS_INPUT_AMPMS4 | Set AMPMS4. See hardware manual for channel specific options.
|
| OPAMP_MINUS_INPUT_AMPMS7 | Set AMPMS7. See hardware manual for channel specific options.
|
◆ opamp_output_t
Options to connect AMP0OS pin.
| Enumerator |
|---|
| OPAMP_OUTPUT_NONE | No Connection.
|
| OPAMP_OUTPUT_AMPOS0 | Set AMPOS0. See hardware manual for channel specific options.
|
| OPAMP_OUTPUT_AMPOS1 | Set AMPOS1. See hardware manual for channel specific options.
|
| OPAMP_OUTPUT_AMPOS2 | Set AMPOS2. See hardware manual for channel specific options.
|
| OPAMP_OUTPUT_AMPOS3 | Set AMPOS3. See hardware manual for channel specific options.
|
◆ R_OPAMP_Open()
Applies power to the OPAMP and initializes the hardware based on the user configuration. Implements opamp_api_t::open.
The op-amp is not operational until the opamp_api_t::start is called. If the op-amp is configured to start after AGT compare match, the op-amp is not operational until opamp_api_t::start and the associated AGT compare match event occurs.
Some MCUs have switches that must be set before starting the op-amp. These switches must be set in the application code after opamp_api_t::open and before opamp_api_t::start.
Example:
- Return values
-
| FSP_SUCCESS | Configuration successful. |
| FSP_ERR_ASSERTION | An input pointer is NULL. |
| FSP_ERR_ALREADY_OPEN | Control block is already opened. |
| FSP_ERR_INVALID_ARGUMENT | An attempt to configure OPAMP in middle speed mode on MCU that does not support middle speed mode. |
◆ R_OPAMP_InfoGet()
Provides the minimum stabilization wait time in microseconds. Implements opamp_api_t::infoGet.
- Example:
assert(FSP_SUCCESS == err);
- Return values
-
| FSP_SUCCESS | information on opamp_power_mode stored in p_info. |
| FSP_ERR_ASSERTION | An input pointer was NULL. |
| FSP_ERR_NOT_OPEN | Instance control block is not open. |
◆ R_OPAMP_Start()
If the OPAMP is configured for hardware triggers, enables hardware triggers. Otherwise, starts the op-amp. Implements opamp_api_t::start.
Some MCUs have switches that must be set before starting the op-amp. These switches must be set in the application code after opamp_api_t::open and before opamp_api_t::start.
Example:
assert(FSP_SUCCESS == err);
- Return values
-
| FSP_SUCCESS | Op-amp started or hardware triggers enabled successfully. |
| FSP_ERR_ASSERTION | An input pointer was NULL. |
| FSP_ERR_NOT_OPEN | Instance control block is not open. |
| FSP_ERR_INVALID_ARGUMENT | channel_mask includes a channel that does not exist on this MCU. |
◆ R_OPAMP_Stop()
Stops the op-amp. If the OPAMP is configured for hardware triggers, disables hardware triggers. Implements opamp_api_t::stop.
- Return values
-
| FSP_SUCCESS | Op-amp stopped or hardware triggers disabled successfully. |
| FSP_ERR_ASSERTION | An input pointer was NULL. |
| FSP_ERR_NOT_OPEN | Instance control block is not open. |
| FSP_ERR_INVALID_ARGUMENT | channel_mask includes a channel that does not exist on this MCU. |
◆ R_OPAMP_StatusGet()
Provides the operating status for each op-amp in a bitmask. This bit is set when operation begins, before the stabilization wait time has elapsed. Implements opamp_api_t::statusGet.
- Return values
-
| FSP_SUCCESS | Operating status of each op-amp provided in p_status. |
| FSP_ERR_ASSERTION | An input pointer was NULL. |
| FSP_ERR_NOT_OPEN | Instance control block is not open. |
◆ R_OPAMP_Trim()
On MCUs that support trimming, the op-amp trim register is set to the factory default after open(). This function allows the application to trim the operational amplifier to a user setting, which overwrites the factory default factory trim values.
Not supported on all MCUs. See hardware manual for details. Not supported if configured for low power mode (OPAMP_MODE_LOW_POWER).
This function is not reentrant. Only one side of one op-amp can be trimmed at a time. Complete the procedure for one side of one channel before calling trim() with command OPAMP_TRIM_CMD_START again.
Implements opamp_api_t::trim.
See "User Offset Trimming" in the OPAMP section of the relevant hardware manual. The trim procedure works as follows:
- Call trim() for the Pch (+) side input with command OPAMP_TRIM_CMD_START.
- Connect a fixed voltage to the Pch (+) input.
- Connect the Nch (-) input to the op-amp output to create a voltage follower.
- Ensure the op-amp is operating and stabilized.
- Call trim() for the Pch (+) side input with command OPAMP_TRIM_CMD_START.
- Measure the fixed voltage connected to the Pch (+) input using the SAR ADC and save the value (referred to as A later in this procedure).
- Iterate over the following loop 5 times:
- Call trim() for the Pch (+) side input with command OPAMP_TRIM_CMD_NEXT_STEP.
- Measure the op-amp output using the SAR ADC (referred to as B in the next step).
- If A <= B, call trim() for the Pch (+) side input with command OPAMP_TRIM_CMD_CLEAR_BIT.
- Call trim() for the Nch (-) side input with command OPAMP_TRIM_CMD_START.
- Measure the fixed voltage connected to the Pch (+) input using the SAR ADC and save the value (referred to as A later in this procedure).
- Iterate over the following loop 5 times:
- Call trim() for the Nch (-) side input with command OPAMP_TRIM_CMD_NEXT_STEP.
- Measure the op-amp output using the SAR ADC (referred to as B in the next step).
- If A <= B, call trim() for the Nch (-) side input with command OPAMP_TRIM_CMD_CLEAR_BIT.
- Return values
-
| FSP_SUCCESS | Conversion result in p_data. |
| FSP_ERR_UNSUPPORTED | Trimming is not supported on this MCU. |
| FSP_ERR_INVALID_STATE | The command is not valid in the current state of the trim state machine. |
| FSP_ERR_INVALID_ARGUMENT | The requested channel is not operating or the trim procedure is not in progress for this channel/input combination. |
| FSP_ERR_INVALID_MODE | Trim is not allowed in low power mode. |
| FSP_ERR_ASSERTION | An input pointer was NULL. |
| FSP_ERR_NOT_OPEN | Instance control block is not open. |
◆ R_OPAMP_Close()
Stops the op-amps. Implements opamp_api_t::close.
- Return values
-
| FSP_SUCCESS | Instance control block closed successfully. |
| FSP_ERR_ASSERTION | An input pointer was NULL. |
| FSP_ERR_NOT_OPEN | Instance control block is not open. |
◆ g_opamp_on_opamp
OPAMP Implementation of OPAMP interface.