1e41f4b71Sopenharmony_ci# ADC
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci## Overview
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ci### Function
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ciAn analog-to-digital converter (ADC) converts analog signals into digital signals for storage and computing. In addition to the power cable and ground cable, the ADC requires only one cable to connect to the target device. The following figure shows the physical connection of the ADC.
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ci**Figure 1** ADC physical connection
10e41f4b71Sopenharmony_ci
11e41f4b71Sopenharmony_ci![](figures/ADC_physical_connection.png "ADC_physical_connection")
12e41f4b71Sopenharmony_ci
13e41f4b71Sopenharmony_ciThe ADC module provides a set of APIs to complete AD conversion, including:
14e41f4b71Sopenharmony_ci
15e41f4b71Sopenharmony_ci-  Opening or closing an ADC device
16e41f4b71Sopenharmony_ci-  Obtaining the analog-to-digital (AD) conversion result
17e41f4b71Sopenharmony_ci
18e41f4b71Sopenharmony_ci### Basic Concepts
19e41f4b71Sopenharmony_ci
20e41f4b71Sopenharmony_ci- Resolution
21e41f4b71Sopenharmony_ci
22e41f4b71Sopenharmony_ci  The number of binary bits that can be converted by an ADC. A greater number of bits indicates a higher resolution.
23e41f4b71Sopenharmony_ci
24e41f4b71Sopenharmony_ci- Conversion error
25e41f4b71Sopenharmony_ci
26e41f4b71Sopenharmony_ci  Difference between the actual and theoretical digital values output by an ADC. It is expressed by a multiple of the least significant bit. Generally, the maximum output error is used.
27e41f4b71Sopenharmony_ci
28e41f4b71Sopenharmony_ci- Transition time
29e41f4b71Sopenharmony_ci
30e41f4b71Sopenharmony_ci  Time required by an ADC to perform a complete conversion.
31e41f4b71Sopenharmony_ci
32e41f4b71Sopenharmony_ci### Working Principles
33e41f4b71Sopenharmony_ci
34e41f4b71Sopenharmony_ciIn the Hardware Driver Foundation (HDF), the ADC module uses the unified service mode for API adaptation. In this mode, a service is used as the ADC manager to handle external access requests in a unified manner. The unified service mode applies when the system has multiple device objects of the same type. If the independent service mode is used in this case, more device nodes need to be configured and more memory resources will be consumed.
35e41f4b71Sopenharmony_ci
36e41f4b71Sopenharmony_ci### Constraints
37e41f4b71Sopenharmony_ci
38e41f4b71Sopenharmony_ciThe ADC module can read data only in polling mode.
39e41f4b71Sopenharmony_ci
40e41f4b71Sopenharmony_ci## Usage Guidelines
41e41f4b71Sopenharmony_ci
42e41f4b71Sopenharmony_ci### When to Use
43e41f4b71Sopenharmony_ci
44e41f4b71Sopenharmony_ciADC devices are used to convert analog voltage or current into digital parameters. For example, an ADC can be used with an NTC resistor to measure temperature, or can be used to convert the output of an analog sensor into a digital parameter.
45e41f4b71Sopenharmony_ci
46e41f4b71Sopenharmony_ci### Available APIs
47e41f4b71Sopenharmony_ci
48e41f4b71Sopenharmony_ciThe following table describes the APIs of the ADC module. For more information, see **//drivers/hdf_core/framework/include/platform/adc_if.h**.
49e41f4b71Sopenharmony_ci
50e41f4b71Sopenharmony_ci**Table 1** APIs of the ADC driver
51e41f4b71Sopenharmony_ci
52e41f4b71Sopenharmony_ci| API  | Description        |
53e41f4b71Sopenharmony_ci| -------- | ---------------- |
54e41f4b71Sopenharmony_ci| DevHandle AdcOpen(uint32_t number)  | Opens an ADC device.     |
55e41f4b71Sopenharmony_ci| void AdcClose(DevHandle handle) | Closes an ADC device.     |
56e41f4b71Sopenharmony_ci| int32_t AdcRead(DevHandle handle, uint32_t channel, uint32_t \*val)  | Obtains the AD conversion result.|
57e41f4b71Sopenharmony_ci
58e41f4b71Sopenharmony_ci### How to Develop
59e41f4b71Sopenharmony_ci
60e41f4b71Sopenharmony_ciThe following figure illustrates how to use ADC APIs.
61e41f4b71Sopenharmony_ci
62e41f4b71Sopenharmony_ci **Figure 2** Process of using ADC APIs
63e41f4b71Sopenharmony_ci
64e41f4b71Sopenharmony_ci![](figures/using-ADC-process.png)
65e41f4b71Sopenharmony_ci
66e41f4b71Sopenharmony_ci
67e41f4b71Sopenharmony_ci#### Opening an ADC Device
68e41f4b71Sopenharmony_ci
69e41f4b71Sopenharmony_ciCall **AdcOpen()** to open an ADC device.
70e41f4b71Sopenharmony_ci
71e41f4b71Sopenharmony_ci```c
72e41f4b71Sopenharmony_ciDevHandle AdcOpen(int16_t number);
73e41f4b71Sopenharmony_ci```
74e41f4b71Sopenharmony_ci
75e41f4b71Sopenharmony_ci**Table 2** Description of AdcOpen
76e41f4b71Sopenharmony_ci
77e41f4b71Sopenharmony_ci| Parameter      | Description         |
78e41f4b71Sopenharmony_ci| ---------- | ----------------- |
79e41f4b71Sopenharmony_ci| number     | ADC device number.        |
80e41f4b71Sopenharmony_ci| **Return Value**| **Description**   |
81e41f4b71Sopenharmony_ci| NULL       | The operation fails.  |
82e41f4b71Sopenharmony_ci| Device handle  | The operation is successful. The handle of the ADC device opened is returned.|
83e41f4b71Sopenharmony_ci
84e41f4b71Sopenharmony_ciExample: Open device 1 of the two ADCs (numbered 0 and 1) in the system.
85e41f4b71Sopenharmony_ci
86e41f4b71Sopenharmony_ci```c
87e41f4b71Sopenharmony_ciDevHandle adcHandle = NULL; /* ADC device handle /
88e41f4b71Sopenharmony_ci
89e41f4b71Sopenharmony_ci/* Open ADC device 1. */
90e41f4b71Sopenharmony_ciadcHandle = AdcOpen(1);
91e41f4b71Sopenharmony_ciif (adcHandle == NULL) {
92e41f4b71Sopenharmony_ci    HDF_LOGE("AdcOpen: fail\n");
93e41f4b71Sopenharmony_ci    return;
94e41f4b71Sopenharmony_ci}
95e41f4b71Sopenharmony_ci```
96e41f4b71Sopenharmony_ci
97e41f4b71Sopenharmony_ci#### Obtaining the AD Conversion Result
98e41f4b71Sopenharmony_ci
99e41f4b71Sopenharmony_ci```c
100e41f4b71Sopenharmony_ciint32_t AdcRead(DevHandle handle, uint32_t channel, uint32_t *val);
101e41f4b71Sopenharmony_ci```
102e41f4b71Sopenharmony_ci
103e41f4b71Sopenharmony_ci**Table 3** Description of AdcRead
104e41f4b71Sopenharmony_ci
105e41f4b71Sopenharmony_ci| Parameter      | Description      |
106e41f4b71Sopenharmony_ci| ---------- | -------------- |
107e41f4b71Sopenharmony_ci| handle     | ADC device handle.   |
108e41f4b71Sopenharmony_ci| channel    | ADC device channel number. |
109e41f4b71Sopenharmony_ci| val        | Pointer to the AD conversion result.    |
110e41f4b71Sopenharmony_ci| **Return Value**| **Description**|
111e41f4b71Sopenharmony_ci| 0          | The operation is successful.      |
112e41f4b71Sopenharmony_ci| Negative value      | The operation fails.      |
113e41f4b71Sopenharmony_ci
114e41f4b71Sopenharmony_ciExample: Obtain the AD conversion result of channel 1.
115e41f4b71Sopenharmony_ci
116e41f4b71Sopenharmony_ci```c
117e41f4b71Sopenharmony_ciuint32_t value;
118e41f4b71Sopenharmony_ciint32_t ret;
119e41f4b71Sopenharmony_ci
120e41f4b71Sopenharmony_ciret = AdcRead(adcHandle, 1, &value);
121e41f4b71Sopenharmony_ciif (ret != 0) {
122e41f4b71Sopenharmony_ci    HDF_LOGE("ADC read fail!\n");
123e41f4b71Sopenharmony_ci    return;
124e41f4b71Sopenharmony_ci}
125e41f4b71Sopenharmony_ci```
126e41f4b71Sopenharmony_ci
127e41f4b71Sopenharmony_ci#### Closing an ADC Device
128e41f4b71Sopenharmony_ci
129e41f4b71Sopenharmony_ciCall **AdcClose()** to close the ADC device after the ADC communication is complete.
130e41f4b71Sopenharmony_ci```c
131e41f4b71Sopenharmony_civoid AdcClose(DevHandle handle); 
132e41f4b71Sopenharmony_ci```
133e41f4b71Sopenharmony_ci**Table 4** Description of AdcClose
134e41f4b71Sopenharmony_ci
135e41f4b71Sopenharmony_ci| Parameter  | Description   |
136e41f4b71Sopenharmony_ci| ------ | ----------- |
137e41f4b71Sopenharmony_ci| handle | ADC device handle.|
138e41f4b71Sopenharmony_ci| **Return Value**| **Description** |
139e41f4b71Sopenharmony_ci| N/A    | N/A         |
140e41f4b71Sopenharmony_ci
141e41f4b71Sopenharmony_ciExample:
142e41f4b71Sopenharmony_ci
143e41f4b71Sopenharmony_ci```c
144e41f4b71Sopenharmony_ciAdcClose(adcHandle); /* Close the ADC device. */
145e41f4b71Sopenharmony_ci```
146e41f4b71Sopenharmony_ci
147e41f4b71Sopenharmony_ci### Example
148e41f4b71Sopenharmony_ci
149e41f4b71Sopenharmony_ciThe following example shows how to use ADC APIs to manage an ADC device on a Hi3516D V300 board.
150e41f4b71Sopenharmony_ci
151e41f4b71Sopenharmony_ciThe hardware information is as follows:
152e41f4b71Sopenharmony_ci
153e41f4b71Sopenharmony_ci-   SoC: hi3516dv300
154e41f4b71Sopenharmony_ci
155e41f4b71Sopenharmony_ci-   The potentiometer is connected to channel 1 of ADC 0.
156e41f4b71Sopenharmony_ci
157e41f4b71Sopenharmony_ciPerform continuous read operations on the ADC device to check whether the ADC is functioning.
158e41f4b71Sopenharmony_ci
159e41f4b71Sopenharmony_ciExample:
160e41f4b71Sopenharmony_ci
161e41f4b71Sopenharmony_ci```c
162e41f4b71Sopenharmony_ci#include "adc_if.h"          /* Header file for ADC APIs */
163e41f4b71Sopenharmony_ci#include "hdf_log.h"         /* Header file for log APIs */
164e41f4b71Sopenharmony_ci
165e41f4b71Sopenharmony_ci/* Define device 0, channel 1. */
166e41f4b71Sopenharmony_ci#define ADC_DEVICE_NUM  0
167e41f4b71Sopenharmony_ci#define ADC_CHANNEL_NUM 1
168e41f4b71Sopenharmony_ci#define ADC_TEST_NUM    30
169e41f4b71Sopenharmony_ci
170e41f4b71Sopenharmony_ci/* Main entry of ADC routines. */
171e41f4b71Sopenharmony_cistatic int32_t TestCaseAdc(void)
172e41f4b71Sopenharmony_ci{
173e41f4b71Sopenharmony_ci    int32_t i;
174e41f4b71Sopenharmony_ci    int32_t ret;
175e41f4b71Sopenharmony_ci    DevHandle adcHandle = NULL;
176e41f4b71Sopenharmony_ci    uint32_t readBuf[ADC_TEST_NUM] = {0};
177e41f4b71Sopenharmony_ci
178e41f4b71Sopenharmony_ci    /* Open the ADC device. */
179e41f4b71Sopenharmony_ci    adcHandle = AdcOpen(ADC_DEVICE_NUM);
180e41f4b71Sopenharmony_ci    if (adcHandle == NULL) {
181e41f4b71Sopenharmony_ci        HDF_LOGE("%s: Open ADC%u fail!", __func__, ADC_DEVICE_NUM);
182e41f4b71Sopenharmony_ci        return -1;
183e41f4b71Sopenharmony_ci    }
184e41f4b71Sopenharmony_ci
185e41f4b71Sopenharmony_ci    /* Perform 30 times of AD conversions continuously and read the conversion results. */
186e41f4b71Sopenharmony_ci    for (i = 0; i < ADC_TEST_NUM; i++) {
187e41f4b71Sopenharmony_ci        ret = AdcRead(adcHandle, ADC_CHANNEL_NUM, &readBuf[i]);
188e41f4b71Sopenharmony_ci        if (ret != HDF_SUCCESS) {
189e41f4b71Sopenharmony_ci            HDF_LOGE("%s: ADC read fail!:%d", __func__, ret);
190e41f4b71Sopenharmony_ci            AdcClose(adcHandle);
191e41f4b71Sopenharmony_ci            return -1;
192e41f4b71Sopenharmony_ci        }
193e41f4b71Sopenharmony_ci    }
194e41f4b71Sopenharmony_ci    HDF_LOGI("%s: ADC read successful!", __func__);
195e41f4b71Sopenharmony_ci
196e41f4b71Sopenharmony_ci    /* Close the ADC device. */
197e41f4b71Sopenharmony_ci    AdcClose(adcHandle);
198e41f4b71Sopenharmony_ci
199e41f4b71Sopenharmony_ci    return 0;
200e41f4b71Sopenharmony_ci}
201e41f4b71Sopenharmony_ci```
202