3.1.1. adc — Analog to digital convertion

Source code: src/drivers/basic/adc.h, src/drivers/basic/adc.c

Test code: tst/drivers/hardware/basic/adc/main.c


Defines

ADC_REFERENCE_VCC

Functions

int adc_module_init(void)

Initialize the ADC driver module. This function must be called before calling any other function in this module.

The module will only be initialized once even if this function is called multiple times.

Return
zero(0) or negative error code.

int adc_init(struct adc_driver_t *self_p, struct adc_device_t *dev_p, struct pin_device_t *pin_dev_p, int reference, long sampling_rate)

Initialize given driver object from given configuration.

Return
zero(0) or negative error code.
Parameters
  • self_p: Driver object to be initialized.
  • dev_p: ADC device to use.
  • pin_dev_p: Pin device to use.
  • reference: Voltage reference. Only ADC_REFERENCE_VCC is supported.
  • sampling_rate: Sampling rate in Hz. The lowest allowed value is one and the highest value depends on the architechture. The sampling rate is not used in single sample convertions, ie. calls to adc_async_convert() and adc_convert() with length one; or calls to adc_convert_isr().

int adc_async_convert(struct adc_driver_t *self_p, uint16_t *samples_p, size_t length)

Start an asynchronous convertion of analog signal to digital samples. Call adc_async_wait() to wait for the convertion to complete.

Return
zero(0) or negative error code.
Parameters
  • self_p: Driver object.
  • samples_p: Converted samples.
  • length: Length of samples array.

int adc_async_wait(struct adc_driver_t *self_p)

Wait for an asynchronous convertion to complete.

Return
zero(0) or negative error code.
Parameters
  • self_p: Driver object.

int adc_convert(struct adc_driver_t *self_p, uint16_t *samples_p, size_t length)

Start a synchronous convertion of an analog signal to digital samples. This is equivalent to adc_async_convert() + adc_async_wait(), but in a single function call.

Return
zero(0) or negative error code.
Parameters
  • self_p: Driver object.
  • samples_p: Converted samples.
  • length: Length of samples array.

int adc_convert_isr(struct adc_driver_t *self_p, uint16_t *sample_p)

Start a synchronous convertion of analog signal to digital samples from isr or with the system lock taken. This function will poll the ADC hardware until the sample has been coverted.

Return
zero(0) or negative error code.
Parameters
  • self_p: Driver object.
  • sample_p: Converted sample.

int adc_is_valid_device(struct adc_device_t *dev_p)

Check if given ADC device is valid.

Return
true(1) if the pin device is valid, otherwise false(0).
Parameters
  • dev_p: ADC device to validate.

Variables

struct adc_device_t adc_device[ADC_DEVICE_MAX]