• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# DAC
2
3## Overview
4
5### Function
6
7A digit-to-analog converter (DAC) is a device that converts a digital signal into an analog signal in electronics. DAC devices are used to:
8
9- Provide the output channel for the process control computer system and connect to the executor to implement automatic control of the production process.
10- Serve as an important module in the analog-to-digital converter using feedback technologies.
11
12The DAC module provides a set of methods for DAC data transfer, including:
13- Opening or closing a DAC device
14- Setting the target digital-to-analog (DA) value
15
16### Basic Concepts
17
18- Resolution
19
20  The number of binary bits that can be converted by a DAC. A greater number of bits indicates a higher resolution.
21
22- Conversion precision
23
24  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.
25
26- Conversion speed
27
28  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.
29
30  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.
31
32  The least significant byte (LSB) refers to bit 0 (the least significant bit) in a binary number.
33
34### Working Principles
35
36In 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.
37
38The DAC module is divided into the following layers:
39
40- Interface layer: provides APIs for opening or closing a device and writing data.
41- Core layer: provides the capabilities of binding, initializing, and releasing devices.
42- Adaptation layer: implements driver-specific functions.
43
44![](../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.
45
46**Figure 1** Unified service mode
47
48![](figures/unified-service-mode.png "DAC unified service mode")
49
50### Constraints
51
52The DAC module supports only the kernel (LiteOS-A) for mini and small systems.
53
54## Usage Guidelines
55
56### When to Use
57
58The 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.
59
60### Available APIs
61
62The 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**.
63
64**Table 1** DAC driver APIs
65
66| API                                                            | Description    |
67| ------------------------------------------------------------------ | ------------ |
68| DevHandle DacOpen(uint32_t number)                                 | Opens a DAC device. |
69| void DacClose(DevHandle handle)                                    | Closes a DAC device. |
70| int32_t DacWrite(DevHandle handle, uint32_t channel, uint32_t val) | Sets a target DA value.|
71
72### How to Develop
73
74The following figure illustrates how to use DAC APIs.
75
76**Figure 2** Process of using DAC APIs
77
78![Process of using a DAC](figures/using-DAC-process.png "Process of using a DAC")
79
80#### Opening a DAC Device
81
82Call **DacOpen()** to open a DAC device before performing the DA conversion.
83
84```c++
85DevHandle DacOpen(uint32_t number);
86```
87
88**Table 2** Description of DacOpen
89
90| Parameter      | Description         |
91| --------- | ---------------- |
92| number    | DAC device number.       |
93| **Return Value**| **Description**    |
94| NULL      | The operation fails. |
95| Device handle  | The operation is successful. The handle of the DAC device opened is returned.|
96
97Open device 1 of the two DAC devices (numbered 0 and 1) in the system.
98
99```c++
100DevHandle dacHandle = NULL; // DAC device handle.
101
102/* Open DAC device 1. */
103dacHandle = DacOpen(1);
104if (dacHandle == NULL) {
105    HDF_LOGE("DacOpen: failed\n");
106    return;
107}
108```
109
110#### Setting a DA Value
111
112```c++
113int32_t DacWrite(DevHandle handle, uint32_t channel, uint32_t val);
114```
115
116**Table 3** Description of DacWrite
117
118| Parameter      | Description     |
119| --------- | ------------ |
120| handle    | DAC device handle. |
121| channel   | DAC channel number.|
122| val       | DA value to set.  |
123| **Return Value**| **Description**|
124| 0         | The operation is successful.    |
125| Negative value      | The operation fails.    |
126
127```c++
128/* Write the target DA value through the DAC_CHANNEL_NUM channel. */
129ret = DacWrite(dacHandle, DAC_CHANNEL_NUM, val);
130if (ret != HDF_SUCCESS) {
131    HDF_LOGE("%s: tp DAC write reg fail!:%d", __func__, ret);
132    DacClose(dacHandle);
133    return -1;
134}
135```
136
137#### Closing a DAC Device
138
139After the DAC communication is complete, call **DacClose()** to close the DAC device.
140```c++
141void DacClose(DevHandle handle);
142```
143
144**Table 4** Description of DacClose
145
146| Parameter      | Description     |
147| --------- | ------------ |
148| handle    | DAC device handle. |
149| **Return Value**| **Description**|
150| void      | -          |
151
152Example:
153
154```c++
155DacClose(dacHandle); /* Close the DAC device. */
156```
157
158## Example
159
160The procedure is as follows:
161
1621. Open the DAC device based on the device number and obtain the device handle.
1632. Set the DA value. If the operation fails, close the device handle.
1643. Close the DAC device handle if the access to the DAC is complete.
165
166You can obtain the operation result by printing the log information based on the **val**.
167
168```c++
169#include "dac_if.h"          /* Header file for DAC APIs. */
170#include "hdf_log.h"         /* Header file for log APIs. */
171
172/* Define device 0, channel 1. */
173#define DAC_DEVICE_NUM 0
174#define DAC_CHANNEL_NUM 1
175
176/* Main entry of DAC routines. */
177static int32_t TestCaseDac(void)
178{
179    // Set the target DA value.
180    uint32_t val = 2;
181    int32_t ret;
182    DevHandle dacHandle;
183
184    /* Open the DAC device. */
185    dacHandle = DacOpen(DAC_DEVICE_NUM);
186    if (dacHandle == NULL) {
187        HDF_LOGE("%s: Open DAC%u fail!", __func__, DAC_DEVICE_NUM);
188        return -1;
189    }
190
191    /* Write data. */
192    ret = DacWrite(dacHandle, DAC_CHANNEL_NUM, val);
193    if (ret != HDF_SUCCESS) {
194        HDF_LOGE("%s: tp DAC write reg fail!:%d", __func__, ret);
195        DacClose(dacHandle);
196        return -1;
197    }
198
199    /* Close the DAC device. */
200    DacClose(dacHandle);
201
202    return 0;
203}
204```
205