Skip to content

ADC

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

Defines

Name Value Description
MIRA_ADC_PIN_VDD 0xFF00
MIRA_ADC_PIN_DISABLED 0xFF01

Types

Name Type Description
mira_adc_value_t int16_t

Enums

mira_adc_reference_t

Value Description
MIRA_ADC_REF_VDD
MIRA_ADC_REF_INT_3_6V

Structs

mira_adc_context_t

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

Functions

mira_adc_init

mira_status_t mira_adc_init(
    mira_adc_context_t*  context);

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

Parameters

Parameter Description
context Configuration block for the ADC.

Return

Status of the operation.

Value Description
MIRA_SUCCESS If initialization was successful.

mira_adc_uninit

mira_status_t mira_adc_uninit(
    mira_adc_context_t*  context);

Use this to uninitialize the ADC module and abort current measuring.

Parameters

Parameter Description
context Configuration block for the ADC.

Return

Status of the operation.

Value Description
MIRA_SUCCESS If initialization was successful.

mira_adc_set_source_supply

mira_status_t mira_adc_set_source_supply(
    mira_adc_context_t*  context);

Parameters

Parameter Description
context Configuration block for the ADC.

Return

Status of the operation.

Value Description
MIRA_SUCCESS If operation was successful.
MIRA_ADC_ERROR_INVALID_PIN If supply voltage can't be measured.

mira_adc_set_source_single

mira_status_t mira_adc_set_source_single(
    mira_adc_context_t*  context,
    mira_gpio_pin_t      pin);

Parameters

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

Return

Status of the operation.

Value Description
MIRA_SUCCESS If operation was successful.
MIRA_ADC_ERROR_INVALID_PIN If the pin does not allow analog input.

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);

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

Parameters

Parameter 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

Status of the operation.

Value Description
MIRA_SUCCESS If operation was successful.
MIRA_ADC_ERROR_INVALID_PIN If any of the pins does not allow analog input.

mira_adc_set_reference

mira_status_t mira_adc_set_reference(
    mira_adc_context_t*   context,
    mira_adc_reference_t  reference);

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

Parameters

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

Return

Status of the operation.

Value Description
MIRA_SUCCESS If operation was successful.
MIRA_ERROR_NOT_SUPPORTED If reference voltage is not supported by the device.

mira_adc_measurement_start

mira_status_t mira_adc_measurement_start(
    mira_adc_context_t*  context);

The measurement will be started. mira_adc_measurement_in_progress() returns MIRA_FALSE when measurement is finished. To do a proper measurement, do the following:

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 ...
}

Make sure the mira_adc_measurement_finish() is called for every successful mira_adc_measurement_start() call.

Parameters

Parameter Description
context Configuration block for the ADC.

Return

Status of the operation.

Value Description
MIRA_SUCCESS If operation was successful.
MIRA_ERROR_NOT_INITIALIZED If ADC module is not initialized.
MIRA_ADC_ERROR_CHANNEL_BUSY If a measurement is already in progress.
MIRA_ADC_ERROR_INVALID_REFERENCE If the reference is invalid.
MIRA_ADC_ERROR_INVALID_PIN If the pin is invalid.

mira_adc_measurement_in_progress

mira_bool_t mira_adc_measurement_in_progress(
    mira_adc_context_t*  context);

Use to poll if the measurement is in progress or not.

Parameters

Parameter Description
context Configuration block for the ADC.

Return

Status of measurement.

Value Description
MIRA_TRUE If measurement is in progress.
MIRA_FALSE If measurement is not in progress.

mira_adc_measurement_finish

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

Use to finish up a conversion. This must be executed for each successful mira_adc_measurement_start() call to free the ADC for other measurements.

Parameters

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

Return

Status of the operation.

Value Description
MIRA_SUCCESS If operation was successful.
MIRA_ADC_ERROR_NO_MEASUREMENT If no measurement was started.