RT-Thread RTOS
An open source embedded real-time operating system
adc_v2.h File Reference
#include <rtconfig.h>
#include "misc.h"
#include <rtatomic.h>
#include <rtdevice.h>
#include <rtthread.h>
+ Include dependency graph for adc_v2.h:

Data Structures

struct  rt_adc_core_ops
 
struct  rt_adc_sequence_ops
 
struct  rt_adc_ops
 
struct  rt_adc_sequence_cfg
 
struct  rt_adc_session_ctrl
 
struct  rt_adc_device
 

Macros

#define RT_ADC_CHANNEL_MASK(_channel)   (1UL << (rt_uint32_t)(_channel))
 

Enumerations

enum  rt_adc_control_cmd {
  RT_ADC_CMD_GET_RESOLUTION = RT_DEVICE_CTRL_BASE(ADC) + 1 , RT_ADC_CMD_CALIBRATE , RT_ADC_CMD_SET_VREF , RT_ADC_CMD_CALC_VREF ,
  RT_ADC_CMD_GET_VREF_CHANNEL , RT_ADC_CMD_SET_SESSION , RT_ADC_CMD_SET_CONFIG , RT_ADC_CMD_GET_CONFIG ,
  RT_ADC_CMD_CLEAR_CHANNEL_CONFIG
}
 
enum  rt_adc_input_mode { RT_ADC_INPUT_MODE_SINGLE_ENDED = 0x01U , RT_ADC_INPUT_MODE_DIFFERENTIAL = 0x02U }
 
enum  rt_adc_state { RT_ADC_STATE_IDLE = 0 , RT_ADC_STATE_LOCKED , RT_ADC_STATE_STREAM }
 

Functions

rt_inline rt_size_t rt_adc_channel_mask_count (rt_uint32_t channels)
 
rt_inline rt_uint8_t rt_adc_channel_mask_take_lsb (rt_uint32_t *channels)
 
rt_err_t rt_hw_adc_register (rt_adc_device_t device, const char *name, const struct rt_adc_ops *ops, const void *user_data)
 
rt_err_t rt_adc_session_channel_index (rt_uint32_t session_channels, rt_uint32_t channel, rt_size_t *index)
 
rt_err_t rt_adc_read_sequence (rt_adc_device_t device, rt_uint32_t channels, const struct rt_adc_sequence_cfg *cfg, rt_size_t *read_count)
 
rt_err_t rt_adc_raw_to_voltage_mv (rt_uint32_t raw_value, rt_uint32_t vref_mv, rt_uint8_t resolution_bits, rt_uint32_t *voltage_mv)
 
rt_err_t rt_adc_raw_to_scaled_voltage_mv (rt_uint32_t raw_value, rt_uint32_t vref_mv, rt_uint8_t resolution_bits, rt_uint32_t scale_num, rt_uint32_t scale_den, rt_uint32_t *voltage_mv)
 
rt_err_t rt_adc_voltage (rt_adc_device_t device, rt_uint32_t *voltages_mv, rt_size_t size, rt_int32_t timeout_ms)
 

Detailed Description

Standalone ADC V2 framework declarations.

Macro Definition Documentation

◆ RT_ADC_CHANNEL_MASK

#define RT_ADC_CHANNEL_MASK (   _channel)    (1UL << (rt_uint32_t)(_channel))

Build an ADC channel mask from one logical channel index.

Parameters
_channelLogical ADC channel index.

Enumeration Type Documentation

◆ rt_adc_control_cmd

ADC control command identifiers.

Enumerator
RT_ADC_CMD_GET_RESOLUTION 

Get resolution in bits.

RT_ADC_CMD_CALIBRATE 

Run ADC calibration.

RT_ADC_CMD_SET_VREF 

Set fallback VREF from rt_uint32_t mV.

RT_ADC_CMD_CALC_VREF 

Calculate VDDA from a raw VREF sample.

RT_ADC_CMD_GET_VREF_CHANNEL 

Get the backend VREF sample channel as rt_uint8_t.

RT_ADC_CMD_SET_SESSION 

Configure and cache conversion channels from rt_uint32_t mask.

RT_ADC_CMD_SET_CONFIG 

Set backend private device configuration.

RT_ADC_CMD_GET_CONFIG 

Get backend private device configuration.

RT_ADC_CMD_CLEAR_CHANNEL_CONFIG 

Clear backend channel/rank configuration explicitly.

◆ rt_adc_input_mode

ADC input mode request flags.

Enumerator
RT_ADC_INPUT_MODE_SINGLE_ENDED 

Single-ended ADC input mode.

RT_ADC_INPUT_MODE_DIFFERENTIAL 

Differential ADC input mode.

◆ rt_adc_state

ADC framework runtime state.

Enumerator
RT_ADC_STATE_IDLE 

The ADC device is idle.

RT_ADC_STATE_LOCKED 

The ADC device is temporarily locked by a framework operation.

RT_ADC_STATE_STREAM 

The ADC device is owned by an active stream session.

Function Documentation

◆ rt_adc_channel_mask_count()

rt_inline rt_size_t rt_adc_channel_mask_count ( rt_uint32_t  channels)

Count selected ADC channels in one channel mask.

Parameters
channelsADC channel selection mask.
Returns
Number of selected channels.

◆ rt_adc_channel_mask_take_lsb()

rt_inline rt_uint8_t rt_adc_channel_mask_take_lsb ( rt_uint32_t *  channels)

Take the lowest selected ADC channel from one channel mask.

Parameters
channelsPointer to the ADC channel selection mask.
Returns
Selected channel index, or 0xff if the mask is empty.

◆ rt_hw_adc_register()

rt_err_t rt_hw_adc_register ( rt_adc_device_t  device,
const char *  name,
const struct rt_adc_ops ops,
const void *  user_data 
)

Register an ADC device with the RT-Thread device framework.

Parameters
devicePointer to the ADC device object.
namePointer to the ADC device name string.
opsPointer to the ADC driver operation table.
user_dataPointer to driver private data.
Returns
Operation status.

◆ rt_adc_session_channel_index()

rt_err_t rt_adc_session_channel_index ( rt_uint32_t  session_channels,
rt_uint32_t  channel,
rt_size_t *  index 
)

Get the raw sample index of one channel in an ADC session mask.

Parameters
session_channelsConfigured ADC session channel mask.
channelSingle-channel mask to locate.
indexPointer to the output raw sample index.
Returns
Operation status.

◆ rt_adc_read_sequence()

rt_err_t rt_adc_read_sequence ( rt_adc_device_t  device,
rt_uint32_t  channels,
const struct rt_adc_sequence_cfg cfg,
rt_size_t *  read_count 
)

Read one ADC conversion sequence frame.

Parameters
devicePointer to the ADC device object.
channelsADC channel selection mask.
cfgPointer to the ADC sequence request object.
read_countPointer to the stored sample count.
Returns
Operation status.

◆ rt_adc_raw_to_voltage_mv()

rt_err_t rt_adc_raw_to_voltage_mv ( rt_uint32_t  raw_value,
rt_uint32_t  vref_mv,
rt_uint8_t  resolution_bits,
rt_uint32_t *  voltage_mv 
)

Convert one raw ADC sample to millivolts.

Parameters
raw_valueRaw ADC sample value.
vref_mvADC reference voltage in millivolts.
resolution_bitsADC resolution in bits.
voltage_mvPointer to the output voltage in millivolts.
Returns
Operation status.

◆ rt_adc_raw_to_scaled_voltage_mv()

rt_err_t rt_adc_raw_to_scaled_voltage_mv ( rt_uint32_t  raw_value,
rt_uint32_t  vref_mv,
rt_uint8_t  resolution_bits,
rt_uint32_t  scale_num,
rt_uint32_t  scale_den,
rt_uint32_t *  voltage_mv 
)

Convert one ADC raw sample to a scaled voltage in millivolts.

Parameters
raw_valueRaw ADC sample code.
vref_mvADC reference voltage in millivolts.
resolution_bitsADC resolution in bits.
scale_numVoltage scaling numerator.
scale_denVoltage scaling denominator.
voltage_mvPointer to the output scaled voltage in millivolts.
Returns
Operation status.

The scaled voltage is calculated as:

raw_to_voltage_mv(raw_value) * scale_num / scale_den
Note
scale_num and scale_den may describe any positive rational scaling ratio that does not overflow the internal calculation. The final scaled millivolt value must fit in rt_uint32_t.
Return values
RT_EOKThe conversion completed successfully.
-RT_EINVALA pointer is null, a scale argument is zero, the ADC conversion arguments are invalid, or the scaled result overflows rt_uint32_t.

◆ rt_adc_voltage()

rt_err_t rt_adc_voltage ( rt_adc_device_t  device,
rt_uint32_t *  voltages_mv,
rt_size_t  size,
rt_int32_t  timeout_ms 
)

Read ADC samples and convert them to millivolts.

Parameters
devicePointer to the ADC device object.
voltages_mvPointer to the output voltage buffer in millivolts.
sizeOutput voltage buffer length.
timeout_msPer-sample timeout in milliseconds; negative means wait forever.
Returns
Operation status.
Note
This API reads the currently configured ADC session and does not modify the active channel mask. If default_vref_mv is zero, callers that need backend-calculated VDDA must include the backend VREF channel in the configured session. The VREF sample is used only as the reference for conversion and is not auto-inserted by this API.