1e41f4b71Sopenharmony_ci# DAC
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci## Overview
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ci### Function
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ciA digit-to-analog converter (DAC) is a device that converts a digital signal into an analog signal in electronics. DAC devices are used to:
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ci- Provide the output channel for the process control computer system and connect to the executor to implement automatic control of the production process.
10e41f4b71Sopenharmony_ci- Serve as an important module in the analog-to-digital converter using feedback technologies.
11e41f4b71Sopenharmony_ci
12e41f4b71Sopenharmony_ciThe DAC module provides a set of methods for DAC data transfer, including:
13e41f4b71Sopenharmony_ci- Opening or closing a DAC device
14e41f4b71Sopenharmony_ci- Setting the target digital-to-analog (DA) value
15e41f4b71Sopenharmony_ci
16e41f4b71Sopenharmony_ci### Basic Concepts
17e41f4b71Sopenharmony_ci
18e41f4b71Sopenharmony_ci- Resolution
19e41f4b71Sopenharmony_ci
20e41f4b71Sopenharmony_ci  The number of binary bits that can be converted by a DAC. A greater number of bits indicates a higher resolution.
21e41f4b71Sopenharmony_ci
22e41f4b71Sopenharmony_ci- Conversion precision
23e41f4b71Sopenharmony_ci
24e41f4b71Sopenharmony_ci  Difference between the actual output value of the DAC and the theoretical value when the maximum value is added to the input end. The conversion precision of a DAC converter varies depending on the structure of the chip integrated on the DAC and the interface circuit configuration. The ideal conversion precision value should be as small as possible. To achieve optimal DAC conversion precision, the DAC must have high resolution. In addition, errors in the devices or power supply of the interface circuits will affect the conversion precision. When the error exceeds a certain degree, a DAC conversion error will be caused.
25e41f4b71Sopenharmony_ci
26e41f4b71Sopenharmony_ci- Conversion speed
27e41f4b71Sopenharmony_ci
28e41f4b71Sopenharmony_ci  The conversion speed is determined by the setup time. The setup time is the period from the time the input suddenly changes from all 0s to all 1s to the time the output voltage remains within the FSR ± ½LSB (or FSR ± x%FSR). It is the maximum response time of the DAC, and hence used to measure the conversion speed.
29e41f4b71Sopenharmony_ci  
30e41f4b71Sopenharmony_ci  The full scale range (FSR) is the maximum range of the output signal amplitude of a DAC. Different DACs have different FSRs, which can be limited by positive and negative currents or voltages.
31e41f4b71Sopenharmony_ci  
32e41f4b71Sopenharmony_ci  The least significant byte (LSB) refers to bit 0 (the least significant bit) in a binary number.
33e41f4b71Sopenharmony_ci
34e41f4b71Sopenharmony_ci### Working Principles
35e41f4b71Sopenharmony_ci
36e41f4b71Sopenharmony_ciIn the Hardware Driver Foundation (HDF), the DAC module uses the unified service mode for API adaptation. In this mode, a service is used as the DAC 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, more device nodes need to be configured and memory resources will be consumed by services. The following figure illustrates the unified service mode of the DAC module.
37e41f4b71Sopenharmony_ci
38e41f4b71Sopenharmony_ciThe DAC module is divided into the following layers:
39e41f4b71Sopenharmony_ci
40e41f4b71Sopenharmony_ci- Interface layer: provides APIs for opening or closing a device and writing data.
41e41f4b71Sopenharmony_ci- Core layer: provides the capabilities of binding, initializing, and releasing devices.
42e41f4b71Sopenharmony_ci- Adaptation layer: implements driver-specific functions.
43e41f4b71Sopenharmony_ci
44e41f4b71Sopenharmony_ci![](../public_sys-resources/icon-note.gif)**NOTE**<br/>The core layer can call the functions of the interface layer and uses the hook to call functions of the adaptation layer. In this way, the adaptation layer can indirectly call the functions of the interface layer, but the interface layer cannot call the functions of the adaptation layer.
45e41f4b71Sopenharmony_ci
46e41f4b71Sopenharmony_ci**Figure 1** Unified service mode
47e41f4b71Sopenharmony_ci
48e41f4b71Sopenharmony_ci![](figures/unified-service-mode.png "DAC unified service mode")
49e41f4b71Sopenharmony_ci
50e41f4b71Sopenharmony_ci### Constraints
51e41f4b71Sopenharmony_ci
52e41f4b71Sopenharmony_ciThe DAC module supports only the kernel (LiteOS-A) for mini and small systems.
53e41f4b71Sopenharmony_ci
54e41f4b71Sopenharmony_ci## Usage Guidelines
55e41f4b71Sopenharmony_ci
56e41f4b71Sopenharmony_ci### When to Use
57e41f4b71Sopenharmony_ci
58e41f4b71Sopenharmony_ciThe DAC module converts digital signals into analog signals in the form of current, voltage, or charge. It is mainly used in audio devices. Audio players and headsets use the DAC module as the digital-to-analog conversion channels.
59e41f4b71Sopenharmony_ci
60e41f4b71Sopenharmony_ci### Available APIs
61e41f4b71Sopenharmony_ci
62e41f4b71Sopenharmony_ciThe following table describes the APIs of the DAC module. For more information about the APIs, see **//drivers/hdf_core/framework/include/platform/dac_if.h**.
63e41f4b71Sopenharmony_ci
64e41f4b71Sopenharmony_ci**Table 1** DAC driver APIs
65e41f4b71Sopenharmony_ci
66e41f4b71Sopenharmony_ci| API                                                            | Description    |
67e41f4b71Sopenharmony_ci| ------------------------------------------------------------------ | ------------ |
68e41f4b71Sopenharmony_ci| DevHandle DacOpen(uint32_t number)                                 | Opens a DAC device. |
69e41f4b71Sopenharmony_ci| void DacClose(DevHandle handle)                                    | Closes a DAC device. |
70e41f4b71Sopenharmony_ci| int32_t DacWrite(DevHandle handle, uint32_t channel, uint32_t val) | Sets a target DA value.|
71e41f4b71Sopenharmony_ci
72e41f4b71Sopenharmony_ci### How to Develop
73e41f4b71Sopenharmony_ci
74e41f4b71Sopenharmony_ciThe following figure illustrates how to use DAC APIs.
75e41f4b71Sopenharmony_ci
76e41f4b71Sopenharmony_ci**Figure 2** Process of using DAC APIs
77e41f4b71Sopenharmony_ci
78e41f4b71Sopenharmony_ci![Process of using a DAC](figures/using-DAC-process.png "Process of using a DAC")
79e41f4b71Sopenharmony_ci
80e41f4b71Sopenharmony_ci#### Opening a DAC Device
81e41f4b71Sopenharmony_ci
82e41f4b71Sopenharmony_ciCall **DacOpen()** to open a DAC device before performing the DA conversion.
83e41f4b71Sopenharmony_ci
84e41f4b71Sopenharmony_ci```c++
85e41f4b71Sopenharmony_ciDevHandle DacOpen(uint32_t number);
86e41f4b71Sopenharmony_ci```
87e41f4b71Sopenharmony_ci
88e41f4b71Sopenharmony_ci**Table 2** Description of DacOpen
89e41f4b71Sopenharmony_ci
90e41f4b71Sopenharmony_ci| Parameter      | Description         |
91e41f4b71Sopenharmony_ci| --------- | ---------------- |
92e41f4b71Sopenharmony_ci| number    | DAC device number.       |
93e41f4b71Sopenharmony_ci| **Return Value**| **Description**    |
94e41f4b71Sopenharmony_ci| NULL      | The operation fails. |
95e41f4b71Sopenharmony_ci| Device handle  | The operation is successful. The handle of the DAC device opened is returned.|
96e41f4b71Sopenharmony_ci
97e41f4b71Sopenharmony_ciOpen device 1 of the two DAC devices (numbered 0 and 1) in the system.
98e41f4b71Sopenharmony_ci
99e41f4b71Sopenharmony_ci```c++
100e41f4b71Sopenharmony_ciDevHandle dacHandle = NULL; // DAC device handle.
101e41f4b71Sopenharmony_ci
102e41f4b71Sopenharmony_ci/* Open DAC device 1. */
103e41f4b71Sopenharmony_cidacHandle = DacOpen(1);
104e41f4b71Sopenharmony_ciif (dacHandle == NULL) {
105e41f4b71Sopenharmony_ci    HDF_LOGE("DacOpen: failed\n");
106e41f4b71Sopenharmony_ci    return;
107e41f4b71Sopenharmony_ci}
108e41f4b71Sopenharmony_ci```
109e41f4b71Sopenharmony_ci
110e41f4b71Sopenharmony_ci#### Setting a DA Value
111e41f4b71Sopenharmony_ci
112e41f4b71Sopenharmony_ci```c++
113e41f4b71Sopenharmony_ciint32_t DacWrite(DevHandle handle, uint32_t channel, uint32_t val);
114e41f4b71Sopenharmony_ci```
115e41f4b71Sopenharmony_ci
116e41f4b71Sopenharmony_ci**Table 3** Description of DacWrite
117e41f4b71Sopenharmony_ci
118e41f4b71Sopenharmony_ci| Parameter      | Description     |
119e41f4b71Sopenharmony_ci| --------- | ------------ |
120e41f4b71Sopenharmony_ci| handle    | DAC device handle. |
121e41f4b71Sopenharmony_ci| channel   | DAC channel number.|
122e41f4b71Sopenharmony_ci| val       | DA value to set.  |
123e41f4b71Sopenharmony_ci| **Return Value**| **Description**|
124e41f4b71Sopenharmony_ci| 0         | The operation is successful.    |
125e41f4b71Sopenharmony_ci| Negative value      | The operation fails.    |
126e41f4b71Sopenharmony_ci
127e41f4b71Sopenharmony_ci```c++
128e41f4b71Sopenharmony_ci/* Write the target DA value through the DAC_CHANNEL_NUM channel. */
129e41f4b71Sopenharmony_ciret = DacWrite(dacHandle, DAC_CHANNEL_NUM, val);
130e41f4b71Sopenharmony_ciif (ret != HDF_SUCCESS) {
131e41f4b71Sopenharmony_ci    HDF_LOGE("%s: tp DAC write reg fail!:%d", __func__, ret);
132e41f4b71Sopenharmony_ci    DacClose(dacHandle);
133e41f4b71Sopenharmony_ci    return -1;
134e41f4b71Sopenharmony_ci}
135e41f4b71Sopenharmony_ci```
136e41f4b71Sopenharmony_ci
137e41f4b71Sopenharmony_ci#### Closing a DAC Device
138e41f4b71Sopenharmony_ci
139e41f4b71Sopenharmony_ciAfter the DAC communication is complete, call **DacClose()** to close the DAC device.
140e41f4b71Sopenharmony_ci```c++
141e41f4b71Sopenharmony_civoid DacClose(DevHandle handle);
142e41f4b71Sopenharmony_ci```
143e41f4b71Sopenharmony_ci
144e41f4b71Sopenharmony_ci**Table 4** Description of DacClose
145e41f4b71Sopenharmony_ci
146e41f4b71Sopenharmony_ci| Parameter      | Description     |
147e41f4b71Sopenharmony_ci| --------- | ------------ |
148e41f4b71Sopenharmony_ci| handle    | DAC device handle. |
149e41f4b71Sopenharmony_ci| **Return Value**| **Description**|
150e41f4b71Sopenharmony_ci| void      | -          |
151e41f4b71Sopenharmony_ci
152e41f4b71Sopenharmony_ciExample:
153e41f4b71Sopenharmony_ci
154e41f4b71Sopenharmony_ci```c++
155e41f4b71Sopenharmony_ciDacClose(dacHandle); /* Close the DAC device. */
156e41f4b71Sopenharmony_ci```
157e41f4b71Sopenharmony_ci
158e41f4b71Sopenharmony_ci## Example
159e41f4b71Sopenharmony_ci
160e41f4b71Sopenharmony_ciThe procedure is as follows:
161e41f4b71Sopenharmony_ci
162e41f4b71Sopenharmony_ci1. Open the DAC device based on the device number and obtain the device handle.
163e41f4b71Sopenharmony_ci2. Set the DA value. If the operation fails, close the device handle.
164e41f4b71Sopenharmony_ci3. Close the DAC device handle if the access to the DAC is complete.
165e41f4b71Sopenharmony_ci
166e41f4b71Sopenharmony_ciYou can obtain the operation result by printing the log information based on the **val**.
167e41f4b71Sopenharmony_ci
168e41f4b71Sopenharmony_ci```c++
169e41f4b71Sopenharmony_ci#include "dac_if.h"          /* Header file for DAC APIs. */
170e41f4b71Sopenharmony_ci#include "hdf_log.h"         /* Header file for log APIs. */
171e41f4b71Sopenharmony_ci
172e41f4b71Sopenharmony_ci/* Define device 0, channel 1. */
173e41f4b71Sopenharmony_ci#define DAC_DEVICE_NUM 0
174e41f4b71Sopenharmony_ci#define DAC_CHANNEL_NUM 1
175e41f4b71Sopenharmony_ci
176e41f4b71Sopenharmony_ci/* Main entry of DAC routines. */
177e41f4b71Sopenharmony_cistatic int32_t TestCaseDac(void)
178e41f4b71Sopenharmony_ci{
179e41f4b71Sopenharmony_ci    // Set the target DA value.
180e41f4b71Sopenharmony_ci    uint32_t val = 2;
181e41f4b71Sopenharmony_ci    int32_t ret;
182e41f4b71Sopenharmony_ci    DevHandle dacHandle;
183e41f4b71Sopenharmony_ci
184e41f4b71Sopenharmony_ci    /* Open the DAC device. */
185e41f4b71Sopenharmony_ci    dacHandle = DacOpen(DAC_DEVICE_NUM);
186e41f4b71Sopenharmony_ci    if (dacHandle == NULL) {
187e41f4b71Sopenharmony_ci        HDF_LOGE("%s: Open DAC%u fail!", __func__, DAC_DEVICE_NUM);
188e41f4b71Sopenharmony_ci        return -1;
189e41f4b71Sopenharmony_ci    }
190e41f4b71Sopenharmony_ci
191e41f4b71Sopenharmony_ci    /* Write data. */
192e41f4b71Sopenharmony_ci    ret = DacWrite(dacHandle, DAC_CHANNEL_NUM, val);
193e41f4b71Sopenharmony_ci    if (ret != HDF_SUCCESS) {
194e41f4b71Sopenharmony_ci        HDF_LOGE("%s: tp DAC write reg fail!:%d", __func__, ret);
195e41f4b71Sopenharmony_ci        DacClose(dacHandle);
196e41f4b71Sopenharmony_ci        return -1;
197e41f4b71Sopenharmony_ci    }
198e41f4b71Sopenharmony_ci
199e41f4b71Sopenharmony_ci    /* Close the DAC device. */
200e41f4b71Sopenharmony_ci    DacClose(dacHandle);
201e41f4b71Sopenharmony_ci
202e41f4b71Sopenharmony_ci    return 0;
203e41f4b71Sopenharmony_ci}
204e41f4b71Sopenharmony_ci```
205