<alp/adc.h> — Analog-to-Digital Conversion
Single-shot reads, streaming captures, and optional on-chain filtering / spectrum analysis.
Header
#include <alp/adc.h>
#include <alp/e1m_pinout.h>
Single-shot
alp_adc_t *adc = alp_adc_open(&(alp_adc_config_t){
.channel_id = ALP_E1M_ADC0,
.resolution_bits = 12,
.reference = ALP_ADC_REF_INTERNAL,
});
int32_t uv = 0;
alp_adc_read_uv(adc, &uv); // converted by the backend, in microvolts
alp_adc_close(adc);
Prefer alp_adc_read_uv(): the backend converts against the SoM's actual reference. If you scale raw codes yourself, use the full-scale voltage of the module, not 3.3 V. On E1M-AEN the analog inputs are referenced to 1.8 V (DS-AEN-001), so a 12-bit code converts as mV = raw * 1800 / 4095; scaling by 3300 reads about 1.83 times too high.
Config struct
| Field | Type | Notes |
|---|---|---|
channel_id | uint32_t | ALP_E1M_ADC0..ALP_E1M_ADC7. Must be < ALP_E1M_ADC_COUNT. |
resolution_bits | uint8_t | 8 / 10 / 12 / 14 / 16. 0 = devicetree default. Capped by <alp/soc_caps.h>. |
acquisition_us | uint16_t | Sample-and-hold time, microseconds. |
reference | alp_adc_ref_t | Reference source: ALP_ADC_REF_INTERNAL / _EXTERNAL_0 / _EXTERNAL_1 / _VDD. |
gain_num | uint8_t | Gain numerator (e.g. 1 for 1/6 gain). |
gain_den | uint8_t | Gain denominator (e.g. 6 for 1/6 gain). |
oversampling_ratio | uint16_t | HW oversampling ratio: 0 (backend default) or a power of two, 1..256. Since v0.17.0 a ratio that is not a power of two is refused with ALP_ERR_NOSUPPORT at alp_adc_open() instead of being silently rounded. |
sample_cycles | uint16_t | Extra sample-and-hold cycles at the ADC clock. Backend-defined count (e.g. GD32 RSMP cycles, clamped to 2..638), with no rounding to discrete taps. 0 = backend default. |
Streaming rate (sample_rate_hz) is not part of this struct — it lives in alp_adc_stream_config_t (see below).
Units
Every read entry point carries its unit as a name suffix, so the split is visible at the call site. The one-shot path favours precision (signed microvolts); the streaming / DSP path favours wire and ring-buffer density (16-bit millivolts — what the 12-bit hardware actually delivers at streaming rates).
| Function | Sample type | Unit |
|---|---|---|
alp_adc_read_raw | int32_t | Native ADC code (no unit). |
alp_adc_read_raw_n | int32_t[] | Native ADC codes, n back-to-back samples. |
alp_adc_read_uv | int32_t | Signed microvolts (µV). |
alp_adc_stream_read_mv | uint16_t | Unsigned millivolts (mV). |
alp_adc_filter_read_mv | int16_t | Signed millivolts (mV) — DSP chains need signed headroom. |
alp_adc_spectrum_read_bins | float | FFT bins (input scale mV). |
Conversion: 1 mV = 1000 µV — a stream sample s sits at s * 1000 on the alp_adc_read_uv scale. The sample widths are wire formats and will not change.
Burst reads
int32_t burst[8];
alp_status_t st = alp_adc_read_raw_n(adc, burst, 8);
alp_adc_read_raw_n(adc, raw_out, n) (v0.17.0) takes n >= 1 back-to-back raw conversions on one channel, in the same value domain as alp_adc_read_raw(). Backends behind a command bridge (the V2N GD32 supervisor) fetch up to 8 samples per bus round trip instead of one, which is what makes short bursts affordable; backends without a native burst fall back to n calls of alp_adc_read_raw(). The samples are not averaged and not paced — use alp_adc_stream_open() for a rate-controlled capture. Returns ALP_OK, ALP_ERR_NOT_READY, ALP_ERR_INVAL (NULL, n == 0), ALP_ERR_IO or ALP_ERR_NOSUPPORT.
alp_adc_read_raw() and alp_adc_read_uv() can now also return ALP_ERR_NOSUPPORT.
Streaming + filtering (v0.5)
// Plain streaming: unsigned uint16 mV samples straight from the ring.
alp_adc_stream_t *stream = alp_adc_stream_open(&(alp_adc_stream_config_t){
.channel_id = ALP_E1M_ADC0,
.sample_rate_hz = 16000,
});
uint16_t samples[256]; // stream samples are uint16 mV
size_t got;
alp_adc_stream_read_mv(stream, samples, sizeof(samples)/sizeof(samples[0]), &got);
// Filtered streaming: compose the same source with a FIR/IIR chain
// from <alp/dsp.h> under one handle; reads come back as signed int16 mV.
alp_adc_filter_t *filter = alp_adc_filter_open(&(alp_adc_filter_config_t){
.channel_id = ALP_E1M_ADC0,
.sample_rate_hz = 16000,
.stages = my_filter_stages, // FIR / IIR chain (no FFT stage)
.n_stages = n_stages,
});
int16_t filtered[256]; // filter samples are int16 mV (signed DSP headroom)
alp_adc_filter_read_mv(filter, filtered, 256, &got);
alp_adc_spectrum_t composes the same stream with the FFT chain in <alp/dsp.h> for spectrum capture.
Close semantics
alp_adc_stream_close(), alp_adc_filter_close() and alp_adc_spectrum_close() each block until any in-flight read on the same handle returns before tearing it down, and are idempotent (a second close is a no-op).
alp_adc_stream_close() always releases the handle, but recycles the backend stream slot only once the backend confirms the stream-end. If the backend stays busy or unreachable through a bounded retry, the slot stays reserved — a later alp_adc_stream_open() reports ALP_ERR_BUSY — so a still-running backend stream is never reused. Close is void, so it cannot report that case itself.
V2N: GD32-side ADC bank
On V2N family, eight ADC channels are exposed by the GD32G553 supervisor. The portable alp_adc_* calls above route there automatically; for direct access, <alp/chips/gd32g553.h> offers gd32g553_adc_read(), which returns samples consecutive millivolt readings:
uint16_t mv[4];
gd32g553_adc_read(&bridge, /* channel */ 2, /* samples */ 4, mv);
Pin-as-GPIO fallback
ADC pads that aren't currently sampling can be repurposed as plain GPIOs. Open the pin via <alp/peripheral.h>::alp_gpio_open with the matching ALP_E1M_GPIO_<pad> ID; the SDK detaches the ADC input and switches the pin to GPIO mode automatically. The same applies to DAC pads. Useful when an application uses only a subset of the analog channels and wants the remaining pads as digital I/O without paying for two driver instances.
Analog output lives in its own header since v0.7 — see <alp/dac.h>. The two share the analog pads (and the pin-as-GPIO fallback above) but are separate registries.
Capability validation
alp_adc_t *adc = alp_adc_open(&(alp_adc_config_t){
.resolution_bits = 16, // ← fails on a 12-bit SoC
});
// adc == NULL; alp_last_error() == ALP_ERR_OUT_OF_RANGE
Instance capabilities
alp_adc_capabilities(adc) returns the handle's alp_capabilities_t (see Capabilities). Since v0.17.0 the ADC-specific facts are class bits carried in class_flags, and only meaningful once ALP_INSTANCE_CAP_REPORTED is set in flags:
| Bit | Value | Meaning |
|---|---|---|
ALP_ADC_CAP_HW_OVERSAMPLE | 1u << 0 | Hardware oversampling / averaging. |
ALP_ADC_CAP_HW_TRIGGER | 1u << 1 | Hardware-triggered (timer / PWM) sampling. |
ALP_ADC_CAP_DIFFERENTIAL | 1u << 2 | Differential channel pairs supported. |
They replace the removed ALP_INSTANCE_CAP_HW_OVERSAMPLE / _HW_TRIGGER / _DIFFERENTIAL flags.
Default-initialiser macros
v0.10 adds an ALP_*_CONFIG_DEFAULT(id) macro for each of the four ADC config structs. Each takes the channel id and fills every other field with its canonical default, so callers set only what they change:
alp_adc_config_t cfg = ALP_ADC_CONFIG_DEFAULT(ALP_E1M_ADC0);
cfg.resolution_bits = 12;
alp_adc_t *ch = alp_adc_open(&cfg);
| Macro | Defaults |
|---|---|
ALP_ADC_CONFIG_DEFAULT(id) | resolution_bits = 0 and acquisition_us = 0 (both "use the devicetree default"), reference = ALP_ADC_REF_INTERNAL, gain_num = 1, gain_den = 1 (unity gain), oversampling_ratio = 0, sample_cycles = 0. |
ALP_ADC_STREAM_CONFIG_DEFAULT(id) | sample_rate_hz = 0. |
ALP_ADC_FILTER_CONFIG_DEFAULT(id) | sample_rate_hz = 0, stages = NULL, n_stages = 0. |
ALP_ADC_SPECTRUM_CONFIG_DEFAULT(id) | sample_rate_hz = 0, stages = NULL, n_stages = 0. |
See the shared macro contract for the C++ compound-literal caveat that applies to every ALP_*_CONFIG_DEFAULT.
See also
- Examples: adc-voltmeter
<alp/dsp.h>— FIR/IIR/FFT chains<alp/peripheral.h>