Skip to main content

<alp/adc.h> — Analog-to-Digital Conversion

Single-shot reads, streaming captures, and optional on-chain filtering / spectrum analysis.

#include <alp/adc.h>
#include <alp/e1m_pinout.h>

Single-shot

alp_adc_t *adc = alp_adc_open(&(alp_adc_config_t){
.channel_id = E1M_ADC0_CH0,
.resolution_bits = 12,
.reference_mv = 3300,
});

uint32_t raw;
alp_adc_read(adc, &raw);

uint32_t millivolts = (raw * 3300u) / ((1u << 12) - 1u);
alp_adc_close(adc);

Config struct

FieldTypeNotes
channel_idalp_adc_id_tE1M_ADC<X>_CH<Y>.
resolution_bitsuint8_t8 / 10 / 12 / 14 / 16. Capped by <alp/soc_caps.h>.
reference_mvuint32_tReference voltage in millivolts.
sample_rate_hzuint32_tStreaming rate (when using alp_adc_stream_*).

Streaming + filtering (v0.5)

alp_adc_filter_t filter = {
.chain = my_filter_chain, // FIR / IIR / FFT chain from <alp/dsp.h>
};

alp_adc_stream_t *stream = alp_adc_stream_open(&(alp_adc_stream_config_t){
.channel_id = E1M_ADC0_CH0,
.sample_rate_hz = 16000,
.filter = &filter,
});

uint32_t samples[256];
size_t got;
alp_adc_stream_read(stream, samples, sizeof(samples)/sizeof(samples[0]), &got);

alp_adc_spectrum_t composes the same stream with the FFT chain in <alp/dsp.h> for spectrum capture.

V2N: GD32-side ADC bank

On V2N family, eight ADC channels are exposed by the GD32G553 supervisor. Route through <alp/chips/gd32g553.h>:

uint16_t mv;
gd32g553_adc_read_mv(&bridge, /* channel */ 2, &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 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.

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

See also

Questions about this page? Discuss in Community Forum