ADC

The Riotee Module has two analog pins that can be sampled with the 12-bit successive approximation ADC on-board the nRF52. Additionally, the ADC can be used to sample the supply voltage and the capacitor voltage.

The API supports simple reading of single samples with a default configuration and more sophisticated periodic sampling with custom configuration.

Important

Always check the return code of riotee_adc_sample(...) to ensure that sampling has actually completed (RIOTEE_SUCCESS) before working with the data.

Example usage

 1
 2#include "riotee.h"
 3#include "riotee_timing.h"
 4#include "riotee_adc.h"
 5#include "printf.h"
 6
 7void lateinit(void) {
 8  riotee_adc_init();
 9}
10
11int main(void) {
12  for (;;) {
13    int16_t val = riotee_adc_read(RIOTEE_ADC_INPUT_A0);
14    printf("ADC value: %d\r\n", val);
15    riotee_sleep_ms(1000);
16  }
17}

API reference

enum riotee_adc_input_t

Values:

enumerator RIOTEE_ADC_INPUT_NC

Input not connected.

enumerator RIOTEE_ADC_INPUT_VCAP

Capacitor voltage.

enumerator RIOTEE_ADC_INPUT_A0

Analog input A0

enumerator RIOTEE_ADC_INPUT_A1

Analog input A1

enum riotee_adc_resolution_t

Values:

enumerator RIOTEE_ADC_RESOLUTION_8BIT

8 bit resolution

enumerator RIOTEE_ADC_RESOLUTION_10BIT

10 bit resolution

enumerator RIOTEE_ADC_RESOLUTION_12BIT

12 bit resolution

enumerator RIOTEE_ADC_RESOLUTION_14BIT

14 bit resolution

enum riotee_adc_oversample_t

Values:

enumerator RIOTEE_ADC_OVERSAMPLE_DISABLED

No oversampling.

enumerator RIOTEE_ADC_OVERSAMPLE_2X

Oversample 2x.

enumerator RIOTEE_ADC_OVERSAMPLE_4X

Oversample 4x.

enumerator RIOTEE_ADC_OVERSAMPLE_8X

Oversample 8x.

enumerator RIOTEE_ADC_OVERSAMPLE_16X

Oversample 16x.

enumerator RIOTEE_ADC_OVERSAMPLE_32X

Oversample 32x.

enumerator RIOTEE_ADC_OVERSAMPLE_64X

Oversample 64x.

enumerator RIOTEE_ADC_OVERSAMPLE_128X

Oversample 128x.

enumerator RIOTEE_ADC_OVERSAMPLE_256X

Oversample 256x.

enum riotee_adc_gain_t

Values:

enumerator RIOTEE_ADC_GAIN1_6

Gain factor 1/6.

enumerator RIOTEE_ADC_GAIN1_5

Gain factor 1/5.

enumerator RIOTEE_ADC_GAIN1_4

Gain factor 1/4.

enumerator RIOTEE_ADC_GAIN1_3

Gain factor 1/3.

enumerator RIOTEE_ADC_GAIN1_2

Gain factor 1/2.

enumerator RIOTEE_ADC_GAIN1

Gain factor 1.

enumerator RIOTEE_ADC_GAIN2

Gain factor 2.

enumerator RIOTEE_ADC_GAIN4

Gain factor 4.

enum riotee_adc_reference_t

Values:

enumerator RIOTEE_ADC_REFERENCE_INTERNAL

Internal reference (0.6 V).

enumerator RIOTEE_ADC_REFERENCE_VDD4

VDD/4 reference.

enum riotee_adc_acqtime_t

Values:

enumerator RIOTEE_ADC_ACQTIME_3US

3us acquisition time.

enumerator RIOTEE_ADC_ACQTIME_5US

5us acquisition time.

enumerator RIOTEE_ADC_ACQTIME_10US

10us acquisition time.

enumerator RIOTEE_ADC_ACQTIME_15US

15us acquisition time.

enumerator RIOTEE_ADC_ACQTIME_20US

20us acquisition time.

enumerator RIOTEE_ADC_ACQTIME_40US

40us acquisition time.

enum riotee_adc_res_t

Values:

enumerator RIOTEE_ADC_RES_NOPULL

No pullup on ADC input.

enumerator RIOTEE_ADC_RES_PULLDOWN

Pulldown on ADC input.

enumerator RIOTEE_ADC_RES_PULLUP

Pullup on ADC input.

enumerator RIOTEE_ADC_RES_VDD1_2

Pullup and pulldown (VDD/2) on ADC input.

void riotee_adc_init(void)

Initializes ADC. Must be called once after reset before ADC can be used.

riotee_rc_t riotee_adc_sample(int16_t *dst, riotee_adc_cfg_t *cfg)

Reads multiple samples from the ADC.

Periodically samples the ADC with a specified sampling interval storing the samples in the provided buffer. Blocks until all samples are taken or until the operation is aborted due to low energy.

Parameters:
  • dst – Buffer where samples are stored.

  • cfg – ADC and sampling configuration.

Return values:
  • RIOTEE_SUCCESS – Sampling completed.

  • RIOTEE_ERR_RESET – Reset occured while sampling.

  • RIOTEE_ERR_TEARDOWN – Teardown occured while sampling.

int16_t riotee_adc_read(riotee_adc_input_t in)

Reads a sample from the ADC.

Reads one sample from the ADC with a preconfigured configuration. Blocks until sample is taken. Gain and reference settings are chosen such that the full 12-bit input range equals the supply voltage.

Parameters:

in – Analog input.

Returns:

int16_t ADC sample as 12-bit value w.r.t. the supply voltage.

float riotee_adc_adc2vadc(int16_t adc, riotee_adc_cfg_t *cfg)

Converts a raw binary value sampled from the ADC to a voltage value.

Parameters:
  • adc – 12-bit ADC sample.

  • cfg – ADC configuration that was used for taking the sample.

Returns:

float Voltage value.

static inline float riotee_adc_vadc2vcap(float v_adc)

Converts ADC input voltage to capacitor voltage based on amplifier gain.

Parameters:

v_adc – ADC input voltage.

Returns:

float Capacitor voltage.

static inline riotee_rc_t riotee_adc_pin2input(riotee_adc_input_t *input, unsigned int pin)

Translates digital pin to analog input channel.

Parameters:
  • input – Pointer where ADC input gets stored.

  • pin – Pin number.

Return values:
  • RIOTEE_SUCCESS – Successfully converted.

  • RIOTEE_ERR_INVALIDARG – Pin is not an ADC input.

struct riotee_adc_cfg_t