• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# ADC<a name="1"></a>
2
3## Overview<a name="section1"></a>
4
5An analog-to-digital converter (ADC) is a device that converts analog signals into digital signals.
6
7The ADC APIs provide a set of common functions for ADC data transfer, including:
8- Opening or closing an ADC device
9
10-   Obtaining the analog-to-digital (AD) conversion result
11
12    **Figure 1** ADC physical connection<br/>
13
14    ![](figures/ADC_physical_connection.png "ADC_physical_connection")
15
16## Available APIs<a name="section2"></a>
17
18**Table 1** APIs of the ADC driver
19
20<a name="table1"></a>
21
22<table><thead align="left"><tr><th class="cellrowborder" valign="top" width="18.63%"><p>Category</p>
23</th>
24<th class="cellrowborder" valign="top" width="28.03%"><p>API</p>
25</th>
26<th class="cellrowborder" valign="top" width="53.339999999999996%"><p>Description</p>
27</th>
28</tr>
29</thead>
30<tbody><tr><td class="cellrowborder" bgcolor="#ffffff" rowspan="2" valign="top" width="18.63%"><p>Managing ADC devices</p>
31</td>
32<td class="cellrowborder" valign="top" width="28.03%"><p>AdcOpen</p>
33</td>
34<td class="cellrowborder" valign="top" width="53.339999999999996%">Opens an ADC device.</p>
35</td>
36</tr>
37<tr><td class="cellrowborder" valign="top"><p>AdcClose</p>
38</td>
39<td valign="top"><p>Closes an ADC device.</p>
40</td>
41</tr>
42<tr><td class="cellrowborder" bgcolor="#ffffff" valign="top" width="18.63%"><p>Obtaining the conversion result</p>
43</td>
44<td class="cellrowborder" valign="top" width="28.03%"><p>AdcRead</p>
45</td>
46<td class="cellrowborder" valign="top" width="53.339999999999996%"><p>Reads the AD conversion result.</p>
47</td>
48</tr>
49</table>
50
51## Usage Guidelines<a name="section3"></a>
52
53### How to Use<a name="section4"></a>
54
55The figure below illustrates how to use the APIs.
56
57 **Figure 2** Using ADC driver APIs<br/>
58
59![](figures/using-ADC-process.png "using-ADC-process.png")
60
61### Opening an ADC Device<a name="section5"></a>
62
63Call **AdcOpen** to open an ADC device.
64
65```c
66DevHandle AdcOpen(int16_t number);
67```
68
69**Table 2** Description of AdcOpen
70
71<a name="table2"></a>
72
73<table><thead align="left"><tr><th class="cellrowborder" valign="top" width="20.66%"><p> Parameter</strong></p>
74</th>
75<th class="cellrowborder" valign="top" width="79.34%"><p><strong>Description</strong></p>
76</th>
77</tr>
78</thead>
79<tbody><tr><td class="cellrowborder" valign="top" width="20.66%"><p>number</p>
80</td>
81<td class="cellrowborder" valign="top" width="79.34%"><p>ADC device number.</p>
82</td>
83</tr>
84<tr><td class="cellrowborder" valign="top" width="20.66%"><p><strong>Return Value</strong></p>
85</td>
86<td class="cellrowborder" valign="top" width="79.34%"><p><strong>Description</strong></p>
87</td>
88</tr>
89<tr><td class="cellrowborder" valign="top" width="20.66%"><p>NULL</p>
90</td>
91<td class="cellrowborder" valign="top" width="79.34%"><p>Failed to open the ADC device.</p>
92</td>
93</tr>
94<tr><td class="cellrowborder" valign="top" width="20.66%"><p>Device handle</p>
95</td>
96<td class="cellrowborder" valign="top" width="79.34%"><p>Handle of the ADC device opened.</p>
97</td>
98</tr>
99</tbody>
100</table>
101
102For example, open device 1 of the two ADCs (numbered 0 and 1) in the system.
103
104```c
105DevHandle adcHandle = NULL; /* ADC device handle /
106
107/* Open the ADC device. */
108adcHandle = AdcOpen(1);
109if (adcHandle == NULL) {
110    HDF_LOGE("AdcOpen: failed\n");
111    return;
112}
113```
114
115### Obtaining the AD Conversion Result<a name="section6"></a>
116
117```c
118int32_t AdcRead(DevHandle handle, uint32_t channel, uint32_t *val);
119```
120
121**Table 3** Description of AdcRead
122
123<a name="table3"></a>
124
125<table><thead align="left"><tr><th class="cellrowborder" valign="top" width="50%"><p><strong> Parameter</strong></p>
126</th>
127<th class="cellrowborder" valign="top" width="50%"><p><strong>Description</strong></p>
128</th>
129</tr>
130</thead>
131<tbody><tr><td class="cellrowborder" valign="top" width="50%"><p>handle</p>
132</td>
133<td class="cellrowborder" valign="top" width="50%"><p>ADC device handle.</p>
134</td>
135</tr>
136<tr><td class="cellrowborder" valign="top" width="50%"><p>channel</p>
137</td>
138<td class="cellrowborder"valign="top" width="50%"><p>ADC device channel number.</p>
139</td>
140</tr>
141<tr><td class="cellrowborder" valign="top" width="50%"><p>val</p>
142</td>
143<td class="cellrowborder" valign="top" width="50%"><p>AD conversion result.</p>
144</td>
145</tr>
146<tr><td class="cellrowborder" valign="top" width="50%"><p><strong>Return Value</strong></p>
147</td>
148<td class="cellrowborder" valign="top" width="50%"><p><strong>Description</strong></p>
149</td>
150</tr>
151<tr><td class="cellrowborder" valign="top" width="50%"><p>0</p>
152</td>
153<td class="cellrowborder" valign="top" width="50%"><p>The operation is successful.</p>
154</td>
155</tr>
156<tr><td class="cellrowborder" valign="top" width="50%"><p>Negative number</p>
157</td>
158<td class="cellrowborder" valign="top" width="50%"><p>Failed to obtain the AC conversion result.</p>
159</td>
160</tr>
161</tbody>
162</table>
163
164### Closing an ADC Device<a name="section7"></a>
165
166Call **AdcClose** to close the ADC device after the ADC communication is complete.
167```c
168void AdcClose(DevHandle handle);
169```
170**Table 4** Description of AdcClose
171
172<a name="table4"></a>
173
174<table><thead align="left"><tr><th class="cellrowborder" valign="top" width="50%"><p> Parameter</p>
175</th>
176<th class="cellrowborder" valign="top" width="50%"><p>Description</p>
177</th>
178</tr>
179</thead>
180<tbody><tr><td class="cellrowborder" valign="top" width="50%"><p>handle</p>
181</td>
182<td class="cellrowborder" valign="top" width="50%"><p>ADC device handle.</p>
183</td>
184</tr>
185<tr><td class="cellrowborder" valign="top" width="50%"><p><strong>Return Value</strong></p>
186</td>
187<td class="cellrowborder" valign="top" width="50%"><p><strong>Description</strong></p>
188</td>
189</tr>
190<tr><td class="cellrowborder" valign="top" width="50%"><p>None</p>
191</td>
192<td class="cellrowborder" valign="top" width="50%"><p>No value is returned if the ADC device is closed.</p>
193</td>
194</tr>
195</tbody>
196</table>
197
198Example:
199
200```c
201AdcClose(adcHandle); /* Close the ADC device. */
202```
203
204## Example<a name="section8"></a>
205
206This following example shows how to use ADC APIs to manage an ADC device on a Hi3516D V300 development board.
207
208The basic hardware information is as follows:
209
210-   SoC: hi3516dv300
211
212-   The potentiometer is connected to channel 1 of ADC device 0.
213
214Perform continuous read operations on the ADC device to check whether the ADC is functioning.
215
216Example:
217
218```c
219#include "adc_if.h"          /* Header file for ADC APIs */
220#include "hdf_log.h"         /* Header file for log APIs */
221
222/* Define device 0, channel 1. */
223#define ADC_DEVICE_NUM 0
224#define ADC_CHANNEL_NUM 1
225
226/* Main entry of ADC routines. */
227static int32_t TestCaseAdc(void)
228{
229    int32_t i;
230    int32_t ret;
231    DevHandle adcHandle;
232    uint32_t Readbuf[30] = {0};
233
234    /* Open the ADC device. */
235    adcHandle = AdcOpen(ADC_DEVICE_NUM);
236    if (adcHandle == NULL) {
237        HDF_LOGE("%s: Open ADC%u fail!", __func__, ADC_DEVICE_NUM);
238        return -1;
239    }
240
241    /* Perform 30 times of AD conversions continuously and read the conversion results. */
242    for (i = 0; i < 30; i++) {
243        ret = AdcRead(adcHandle, ADC_CHANNEL_NUM, &Readbuf[i]);
244        if (ret != HDF_SUCCESS) {
245            HDF_LOGE("%s: tp ADC write reg fail!:%d", __func__, ret);
246            AdcClose(adcHandle);
247            return -1;
248        }
249    }
250    HDF_LOGI("%s: ADC read successful!", __func__);
251
252    /* Close the ADC device. */
253    AdcClose(adcHandle);
254
255    return 0;
256}
257```
258