Skip to content

ADC

Analog to digital converters, typically used for measuring voltages for sensors, battery voltage, etc.

Defines

MIRA_ADC_PIN_VDD

#define MIRA_ADC_PIN_VDD

MIRA_ADC_PIN_DISABLED

#define MIRA_ADC_PIN_DISABLED

Types

mira_adc_value_t

typedef int16_t mira_adc_value_t;

Enums

mira_adc_reference_t

Name Description
MIRA_ADC_REF_VDD
MIRA_ADC_REF_INT_3_6V

Structs

mira_adc_context_t

Struct containing the context of an ADC channel.

Do not change this struct directly, use the modifier methods mira_adc_set_*().

Name Type Description
gpio_pin_p uint16_t
gpio_pin_n uint16_t
reference mira_adc_reference_t
is_measuring mira_bool_t
value mira_adc_value_t
measuring_process struct process *

Functions

mira_adc_init

mira_status_t mira_adc_init(mira_adc_context_t *context);

Initialize the ADC module and an ADC context with default values.

Use this to set up the context block with default parameters and initialize the ADC module.

Parameters

Name Description
context Configuration block for the ADC.

Return value

Name Description
MIRA_SUCCESS The initialization was successful.

mira_adc_uninit

mira_status_t mira_adc_uninit(mira_adc_context_t *context);

Uninitialize the ADC module.

Uninitializes the ADC module and aborts on-going measurement.

Parameters

Name Description
context Configuration block for the ADC.

Return value

Name Description
MIRA_SUCCESS The initialization was successful.

mira_adc_set_source_supply

mira_status_t mira_adc_set_source_supply(mira_adc_context_t *context);

Set the source of the ADC to the supply voltage.

Parameters

Name Description
context Configuration block for the ADC.

Return value

Name Description
MIRA_SUCCESS The operation was successful.

mira_adc_set_source_single

mira_status_t mira_adc_set_source_single(mira_adc_context_t *context, mira_gpio_pin_t pin);

Set the source of the ADC to a single ended measurement.

Parameters

Name Description
context Configuration block for the ADC.
pin GPIO pin for the source (not analog pin number).

Return value

Name Description
MIRA_SUCCESS The operation was successful. No error on wrong pin selection, mira_adc_measurement_start() detects that instead.

mira_adc_set_source_diff

mira_status_t mira_adc_set_source_diff(mira_adc_context_t *context, mira_gpio_pin_t pin_p, mira_gpio_pin_t pin_n);

Set the source of the ADC to a differential measurement.

If CPU doesn't support differential measurements, this will be emulated by two consecutive measurements.

Parameters

Name Description
context Configuration block for the ADC.
pin_p GPIO pin for the positive source (not analog pin number).
pin_n GPIO pin for the negative soruce (not analog pin number).

Return value

Name Description
MIRA_SUCCESS The operation was successful. No error on wrong pin selection, mira_adc_measurement_start() detects that instead.

mira_adc_set_reference

mira_status_t mira_adc_set_reference(mira_adc_context_t *context, mira_adc_reference_t reference);

Set the reference voltage of the analog conversion.

Sets the voltage that maps to the maximum voltage of the conversion.

Parameters

Name Description
context Configuration block for the ADC.
reference The reference for the conversion.

Return value

Name Description
MIRA_SUCCESS The operation was successful.
MIRA_ERROR_NOT_SUPPORTED The reference voltage is not supported by the device.

mira_adc_measurement_start

mira_status_t mira_adc_measurement_start(mira_adc_context_t *context);

Start a measurement.

mira_adc_measurement_in_progress() returns MIRA_FALSE when the measurement is finished.

Correct procedure for measurement:

mira_status_t status;
mira_value_t value;
status = mira_adc_measurement_start(&context);
if(status == MIRA_SUCCESS) {
    PROCESS_WAIT_UNTIL(!mira_adc_measurement_in_progress(&context));
    status = mira_adc_measurement_finish(&context, &value);
    ... measurement result and status ...
} else {
    ... handle error ...
}

Call mira_adc_measurement_finish() for every successful mira_adc_measurement_start() call.

Parameters

Name Description
context Configuration block for the ADC.

Return value

Name Description
MIRA_SUCCESS The operation was successful.
MIRA_ERROR_NOT_INITIALIZED The ADC module is not initialized.
MIRA_ADC_ERROR_INVALID_PIN A pin is invalid.
MIRA_ADC_ERROR_CHANNEL_BUSY A measurement is already in progress.

mira_adc_measurement_in_progress

mira_bool_t mira_adc_measurement_in_progress(mira_adc_context_t *context);

Check if the measurement is in progress.

Parameters

Name Description
context Configuration block for the ADC.

Return value

Name Description
MIRA_TRUE A measurement is in progress.
MIRA_FALSE No measurement is in progress.

mira_adc_measurement_finish

mira_status_t mira_adc_measurement_finish(mira_adc_context_t *context, mira_adc_value_t *value);

Finish the conversion and get the result.

Call this for each successful mira_adc_measurement_start() call, in order to free the ADC for other measurements.

Parameters

Name Description
context Configuration block for the ADC.
value Storage of the measurement value. Only set if successful.

Return value

Name Description
MIRA_SUCCESS The operation was successful.
MIRA_ADC_ERROR_NO_MEASUREMENT Measurement failed to start.