ADC
Overview
An analog-to-digital converter (ADC) is a device that converts analog signals into digital signals.
The ADC APIs provide a set of common functions for ADC data transfer, including:
-
Opening or closing an ADC device
-
Obtaining the analog-to-digital (AD) conversion result
Figure 1 ADC physical connection
Available APIs
Table 1 APIs of the ADC driver
Category |
API |
Description |
---|---|---|
Managing ADC devices |
AdcOpen |
Opens an ADC device. |
AdcClose |
Closes an ADC device. |
|
Obtaining the conversion result |
AdcRead |
Reads the AD conversion result. |
Usage Guidelines
How to Use
The figure below illustrates how to use the APIs.
Figure 2 Using ADC driver APIs
Opening an ADC Device
Call AdcOpen to open an ADC device.
DevHandle AdcOpen(int16_t number);
Table 2 Description of AdcOpen
Parameter |
Description |
---|---|
number |
ADC device number. |
Return Value |
Description |
NULL |
Failed to open the ADC device. |
Device handle |
Handle of the ADC device opened. |
For example, open device 1 of the two ADCs (numbered 0 and 1) in the system.
DevHandle adcHandle = NULL; /* ADC device handle /
/* Open the ADC device. */
adcHandle = AdcOpen(1);
if (adcHandle == NULL) {
HDF_LOGE("AdcOpen: failed\n");
return;
}
Obtaining the AD Conversion Result
int32_t AdcRead(DevHandle handle, uint32_t channel, uint32_t *val);
Table 3 Description of AdcRead
Parameter |
Description |
---|---|
handle |
ADC device handle. |
channel |
ADC device channel number. |
val |
AD conversion result. |
Return Value |
Description |
0 |
The operation is successful. |
Negative number |
Failed to obtain the AC conversion result. |
Closing an ADC Device
Call AdcClose to close the ADC device after the ADC communication is complete.
void AdcClose(DevHandle handle);
Table 4 Description of AdcClose
Parameter |
Description |
---|---|
handle |
ADC device handle. |
Return Value |
Description |
None |
No value is returned if the ADC device is closed. |
Example:
AdcClose(adcHandle); /* Close the ADC device. */
Example
This following example shows how to use ADC APIs to manage an ADC device on a Hi3516D V300 development board.
The basic hardware information is as follows:
-
SoC: hi3516dv300
-
The potentiometer is connected to channel 1 of ADC device 0.
Perform continuous read operations on the ADC device to check whether the ADC is functioning.
Example:
#include "adc_if.h" /* Header file for ADC APIs */
#include "hdf_log.h" /* Header file for log APIs */
/* Define device 0, channel 1. */
#define ADC_DEVICE_NUM 0
#define ADC_CHANNEL_NUM 1
/* Main entry of ADC routines. */
static int32_t TestCaseAdc(void)
{
int32_t i;
int32_t ret;
DevHandle adcHandle;
uint32_t Readbuf[30] = {0};
/* Open the ADC device. */
adcHandle = AdcOpen(ADC_DEVICE_NUM);
if (adcHandle == NULL) {
HDF_LOGE("%s: Open ADC%u fail!", __func__, ADC_DEVICE_NUM);
return -1;
}
/* Perform 30 times of AD conversions continuously and read the conversion results. */
for (i = 0; i < 30; i++) {
ret = AdcRead(adcHandle, ADC_CHANNEL_NUM, &Readbuf[i]);
if (ret != HDF_SUCCESS) {
HDF_LOGE("%s: tp ADC write reg fail!:%d", __func__, ret);
AdcClose(adcHandle);
return -1;
}
}
HDF_LOGI("%s: ADC read successful!", __func__);
/* Close the ADC device. */
AdcClose(adcHandle);
return 0;
}