未验证 提交 ba23c1a7 编写于 作者: O openharmony_ci 提交者: Gitee

!5849 [翻译完成】#I5A3P4

Merge pull request !5849 from Annie_wang/PR4571
# ADC<a name="EN-US_TOPIC_0000001153677754"></a>
# ADC
## Overview<a name="section268031773165048"></a>
The analog-to-digital converter \(ADC\) is a device that converts analog signals into digital signals. In the Hardware Driver Foundation \(HDF\), the ADC module uses the unified service mode for API adaptation. In this mode, a device service is used as the ADC manager to handle external access requests in a unified manner, which is reflected in the configuration file. The unified service mode applies to the scenario where there are many device objects of the same type, for example, when the ADC has more than 10 controllers. If the independent service mode is used, more device nodes need to be configured and memory resources will be consumed by services.
## Overview
**Figure 1** Unified service mode<a name="fig14423182615525"></a>
![](figures/unified-service-mode.png "ADC-unified-service-mode")
An analog-to-digital converter (ADC) is a device that converts analog signals into digital signals. In the Hardware Driver Foundation (HDF), the ADC module uses the unified service mode for API adaptation. In this mode, a device service is used as the ADC manager to handle external access requests in a unified manner, which is reflected in the configuration file. The unified service mode applies to the scenario where there are many device objects of the same type, for example, when the ADC has more than 10 controllers. If the independent service mode is used, more device nodes need to be configured and memory resources will be consumed by services.
## Available APIs<a name="section752964871810"></a>
**Figure 1** Unified service mode
![](figures/unified-service-mode.png "ADC Unified Service Mode")
## Available APIs
**AdcMethod**:
### AdcMethod<a name="section1618135285210"></a>
```
struct AdcMethod {
......@@ -19,93 +23,49 @@ struct AdcMethod {
};
```
**Table 1** Callbacks for the members in the AdcMethod structure
<a name="table1943202316536"></a>
<table><thead align="left"><tr id="row2451223135315"><th class="cellrowborder" valign="top" width="20%" id="mcps1.2.6.1.1"><p id="p845123185313"><a name="p845123185313"></a><a name="p845123185313"></a>Callback</p>
</th>
<th class="cellrowborder" valign="top" width="20%" id="mcps1.2.6.1.2"><p id="p345192315311"><a name="p345192315311"></a><a name="p345192315311"></a>Input Parameter</p>
</th>
<th class="cellrowborder" valign="top" width="20%" id="mcps1.2.6.1.3"><p id="p74512313536"><a name="p74512313536"></a><a name="p74512313536"></a>Output Parameter</p>
</th>
<th class="cellrowborder" valign="top" width="20%" id="mcps1.2.6.1.4"><p id="p174542313531"><a name="p174542313531"></a><a name="p174542313531"></a>Return Value</p>
</th>
<th class="cellrowborder" valign="top" width="20%" id="mcps1.2.6.1.5"><p id="p445112315535"><a name="p445112315535"></a><a name="p445112315535"></a>Description</p>
</th>
</tr>
</thead>
<tbody><tr id="row545102316536"><td class="cellrowborder" valign="top" width="20%" headers="mcps1.2.6.1.1 "><p id="p14562345312"><a name="p14562345312"></a><a name="p14562345312"></a>read</p>
</td>
<td class="cellrowborder" valign="top" width="20%" headers="mcps1.2.6.1.2 "><p id="p474720389561"><a name="p474720389561"></a><a name="p474720389561"></a><strong id="b167472038115611"><a name="b167472038115611"></a><a name="b167472038115611"></a>device</strong>: structure pointer to the ADC controller at the core layer.</p>
<p id="p14459238536"><a name="p14459238536"></a><a name="p14459238536"></a><strong id="b179891252205514"><a name="b179891252205514"></a><a name="b179891252205514"></a>channel</strong>: channel ID of the uint32_t type.</p>
</td>
<td class="cellrowborder" valign="top" width="20%" headers="mcps1.2.6.1.3 "><p id="p746423195320"><a name="p746423195320"></a><a name="p746423195320"></a><strong id="b626111125816"><a name="b626111125816"></a><a name="b626111125816"></a>val</strong>: pointer to the signal data to be transmitted. It is of the uint32_t type.</p>
</td>
<td class="cellrowborder" valign="top" width="20%" headers="mcps1.2.6.1.4 "><p id="p1746192315311"><a name="p1746192315311"></a><a name="p1746192315311"></a>HDF_STATUS</p>
</td>
<td class="cellrowborder" valign="top" width="20%" headers="mcps1.2.6.1.5 "><p id="p146152311537"><a name="p146152311537"></a><a name="p146152311537"></a>Reads the signal data sampled by the ADC.</p>
</td>
</tr>
<tr id="row6461236531"><td class="cellrowborder" valign="top" width="20%" headers="mcps1.2.6.1.1 "><p id="p446182316538"><a name="p446182316538"></a><a name="p446182316538"></a>stop</p>
</td>
<td class="cellrowborder" valign="top" width="20%" headers="mcps1.2.6.1.2 "><p id="p1446112318536"><a name="p1446112318536"></a><a name="p1446112318536"></a><strong id="b345886135912"><a name="b345886135912"></a><a name="b345886135912"></a>device</strong>: structure pointer to the ADC controller at the core layer.</p>
</td>
<td class="cellrowborder" valign="top" width="20%" headers="mcps1.2.6.1.3 "><p id="p4461823125317"><a name="p4461823125317"></a><a name="p4461823125317"></a></p>
</td>
<td class="cellrowborder" valign="top" width="20%" headers="mcps1.2.6.1.4 "><p id="p1346122314530"><a name="p1346122314530"></a><a name="p1346122314530"></a>HDF_STATUS</p>
</td>
<td class="cellrowborder" valign="top" width="20%" headers="mcps1.2.6.1.5 "><p id="p1346223185318"><a name="p1346223185318"></a><a name="p1346223185318"></a>Stops the ADC device.</p>
</td>
</tr>
<tr id="row194622375313"><td class="cellrowborder" valign="top" width="20%" headers="mcps1.2.6.1.1 "><p id="p144642305313"><a name="p144642305313"></a><a name="p144642305313"></a>start</p>
</td>
<td class="cellrowborder" valign="top" width="20%" headers="mcps1.2.6.1.2 "><p id="p17461323185314"><a name="p17461323185314"></a><a name="p17461323185314"></a><strong id="b1988212105110"><a name="b1988212105110"></a><a name="b1988212105110"></a>device</strong>: structure pointer to the ADC controller at the core layer.</p>
</td>
<td class="cellrowborder" valign="top" width="20%" headers="mcps1.2.6.1.3 "><p id="p1146623105317"><a name="p1146623105317"></a><a name="p1146623105317"></a></p>
</td>
<td class="cellrowborder" valign="top" width="20%" headers="mcps1.2.6.1.4 "><p id="p546132311535"><a name="p546132311535"></a><a name="p546132311535"></a>HDF_STATUS</p>
</td>
<td class="cellrowborder" valign="top" width="20%" headers="mcps1.2.6.1.5 "><p id="p1471623195311"><a name="p1471623195311"></a><a name="p1471623195311"></a>Starts the ADC device.</p>
</td>
</tr>
</tbody>
</table>
## How to Develop<a name="section100579767165048"></a>
**Table 1** Description of the callback functions in AdcMethod
| Function| Input Parameter| Output Parameter| Return Value| Description|
| -------- | -------- | -------- | -------- | -------- |
| read | **device**: structure pointer to the ADC controller at the core layer.<br>**channel**: channel ID, which is of the uint32_t type.| **val**: pointer to the signal data to be transmitted. It is of the uint32_t type.| HDF_STATUS| Reads the signal data sampled by the ADC.|
| stop | **device**: structure pointer to the ADC controller at the core layer.| –| HDF_STATUS| Stops an ADC device.|
| start | **device**: structure pointer to the ADC controller at the core layer.| –| HDF_STATUS| Starts an ADC device.|
## How to Develop
The ADC module adaptation involves the following steps:
1. Instantiate the driver entry.
- Instantiate the **HdfDriverEntry** structure.
- Call **HDF\_INIT** to register the **HdfDriverEntry** instance with the HDF.
- Call **HDF_INIT** to register the **HdfDriverEntry** instance with the HDF.
2. Configure attribute files.
- Add the **deviceNode** information to the **device\_info.hcs** file.
- \(Optional\) Add the **adc\_config.hcs** file.
- Add the **deviceNode** information to the **device_info.hcs** file.
- (Optional) Add the **adc_config.hcs** file.
3. Instantiate the ADC controller object.
- Initialize **AdcDevice**.
- Instantiate **AdcMethod** in the **AdcDevice** object.
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**<br>
> For details about the functions in **AdcMethod**, see [Available APIs](#available-apis).
For details, see [Available APIs](#available-apis).
4. \(Optional\) Debug the driver.
For new drivers, verify basic functions, for example, verify the information returned after the connect operation and whether the signal collection is successful.
4. Debug the driver.
(Optional) For new drivers, verify basic functions, for example, check the information returned after the driver is attached and check whether signals are successfully collected.
## Development Example<a name="section1745221471165048"></a>
## Development Example
The following uses **adc\_hi35xx.c** as an example to present the contents that need to be provided by the vendor to implement device functions.
The following uses **adc_hi35xx.c** as an example to present the information required for implementing device functions.
1. Instantiate the driver entry.<br/>The driver entry must be a global variable of the **HdfDriverEntry** type (defined in **hdf_device_desc.h**), and the value of **moduleName** must be the same as that in **device_info.hcs**. In the HDF, the start address of each **HdfDriverEntry** object of all loaded drivers is collected to form a segment address space similar to an array for the upper layer to invoke.
Generally, the HDF calls the **Bind** function and then the **Init** function to load a driver. If **Init** fails to be called, the HDF calls **Release** to release driver resources and exit.
1. Instantiate the driver entry. The driver entry must be a global variable of the **HdfDriverEntry** type \(defined in **hdf\_device\_desc.h**\), and the value of **moduleName** must be the same as that in **device\_info.hcs**. In the HDF, the start address of each **HdfDriverEntry** object of all loaded drivers is collected to form a segment address space similar to an array for the upper layer to invoke.
ADC driver entry example:
Generally, HDF calls the **Bind** function and then the **Init** function to load a driver. If **Init** fails to be called, HDF calls **Release** to release driver resources and exits.
Many devices may connect to the ADC. In the HDF, a manager object needs to be created for the ADC. When a device needs to be started, the manager object locates the target device based on the specified parameters.
- ADC driver entry reference
The driver of the ADC manager is implemented by the core layer. The **Init** function of the driver layer calls **AdcDeviceAdd** of the core layer to add devices to the ADC manager. Therefore, vendors do not need to implement this part.
Many devices may connect to the ADC. In the HDF, a manager object needs to be created for the ADC. When a device needs to be started, the manager object locates the device based on the specified parameters.
The driver of the ADC manager is implemented by the core layer. Vendors do not need to pay attention to the implementation of this part. However, when they implement the **Init** function, the **AdcDeviceAdd** function of the core layer must be called to implement the corresponding features.
```
static struct HdfDriverEntry g_hi35xxAdcDriverEntry = {
......@@ -121,48 +81,24 @@ The following uses **adc\_hi35xx.c** as an example to present the contents that
.moduleVersion = 1,
.Init = AdcManagerInit,
.Release = AdcManagerRelease,
.moduleName = "HDF_PLATFORM_ADC_MANAGER",// This parameter corresponds to device0 in the device_info file.
.moduleName = "HDF_PLATFORM_ADC_MANAGER",// Correspond to device0 in the device_info file.
};
HDF_INIT(g_adcManagerEntry);
```
2. Add the **deviceNode** information to the **device\_info.hcs** file and configure the device attributes in the **adc\_config.hcs** file. The **deviceNode** information is related to registration of the driver entry. The device attribute values are closely related to the driver implementation and the default values or value ranges of the **AdcDevice** members at the core layer.
In the unified service mode, the first device node in the **device\_info** file must be the ADC manager, and the parameters must be set as follows:
<a name="table1344068233165048"></a>
<table><thead align="left"><tr id="row1551612465165048"><th class="cellrowborder" valign="top" width="50%" id="mcps1.1.3.1.1"><p id="entry1856185125165048p0"><a name="entry1856185125165048p0"></a><a name="entry1856185125165048p0"></a>Member</p>
</th>
<th class="cellrowborder" valign="top" width="50%" id="mcps1.1.3.1.2"><p id="entry720672143165048p0"><a name="entry720672143165048p0"></a><a name="entry720672143165048p0"></a>Value</p>
</th>
</tr>
</thead>
<tbody><tr id="row583452627165048"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.1.3.1.1 "><p id="entry747665129165048p0"><a name="entry747665129165048p0"></a><a name="entry747665129165048p0"></a>moduleName</p>
</td>
<td class="cellrowborder" valign="top" width="50%" headers="mcps1.1.3.1.2 "><p id="entry912596825165048p0"><a name="entry912596825165048p0"></a><a name="entry912596825165048p0"></a>It has a fixed value of <strong id="b581184091716"><a name="b581184091716"></a><a name="b581184091716"></a>HDF_PLATFORM_ADC_MANAGER</strong>.</p>
</td>
</tr>
<tr id="row218211231165048"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.1.3.1.1 "><p id="entry568759156165048p0"><a name="entry568759156165048p0"></a><a name="entry568759156165048p0"></a>serviceName</p>
</td>
<td class="cellrowborder" valign="top" width="50%" headers="mcps1.1.3.1.2 "><p id="entry1349637957165048p0"><a name="entry1349637957165048p0"></a><a name="entry1349637957165048p0"></a>–</p>
</td>
</tr>
<tr id="row1166331861165048"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.1.3.1.1 "><p id="entry1142726988165048p0"><a name="entry1142726988165048p0"></a><a name="entry1142726988165048p0"></a>policy</p>
</td>
<td class="cellrowborder" valign="top" width="50%" headers="mcps1.1.3.1.2 "><p id="entry781016408165048p0"><a name="entry781016408165048p0"></a><a name="entry781016408165048p0"></a>The value is <strong id="b229121291813"><a name="b229121291813"></a><a name="b229121291813"></a>0</strong>, which indicates that no service is published.</p>
</td>
</tr>
<tr id="row1822624516165048"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.1.3.1.1 "><p id="entry982991296165048p0"><a name="entry982991296165048p0"></a><a name="entry982991296165048p0"></a>deviceMatchAttr</p>
</td>
<td class="cellrowborder" valign="top" width="50%" headers="mcps1.1.3.1.2 "><p id="entry367170471165048p0"><a name="entry367170471165048p0"></a><a name="entry367170471165048p0"></a>This parameter is not used.</p>
</td>
</tr>
</tbody>
</table>
Configure ADC controller information from the second node. This node specifies a type of ADC controllers rather than an ADC controller. In this example, there is only one ADC device. If there are multiple ADC devices, you need to add the **deviceNode** information to the **device\_info** file and add the corresponding device attributes to the **adc\_config** file.
- **device\_info.hcs** configuration reference
2. Add **deviceNode** to the **device_info.hcs** file, and configure the device attributes in the **adc_config.hcs** file.<br> The **deviceNode** information is related to registration of the driver entry. The device attribute values are closely related to the driver implementation and the default values or restriction ranges of the **AdcDevice** members at the core layer.
In the unified service mode, the first device node in the **device_info** file must be the ADC manager. The parameters must be set as follows:
| Parameter| Value|
| -------- | -------- |
| moduleName | **HDF_PLATFORM_ADC_MANAGER**|
| serviceName | –|
| policy | **0**, which indicates that no service is published.|
| deviceMatchAttr | Reserved.|
Configure ADC controller information from the second node. This node specifies a type of ADC controllers rather than an ADC controller. In this example, there is only one ADC device. If there are multiple ADC devices, add the **deviceNode** information to the **device_info** file and add the corresponding device attributes to the **adc_config** file for each device.
- **device_info.hcs** configuration example
```
root {
......@@ -177,20 +113,20 @@ The following uses **adc\_hi35xx.c** as an example to present the contents that
serviceName = "HDF_PLATFORM_ADC_MANAGER";
}
device1 :: deviceNode {
policy = 0; // The value 0 indicates that no service needs to be published.
priority = 55; // Driver startup priority
permission = 0644; // Permission to create device nodes for the driver
moduleName = "hi35xx_adc_driver"; // (Mandatory) Driver name, which must be the same as the moduleName in the driver entry.
serviceName = "HI35XX_ADC_DRIVER"; // (Mandatory) Unique name of the service published by the driver
deviceMatchAttr = "hisilicon_hi35xx_adc";// (Mandatory) Used to configure the private data of the controller. The value must be the same as the controller in adc_config.hcs.
} // The specific controller information is in adc_config.hcs.
policy = 0; // The value 0 indicates that no service is published.
priority = 55; // Driver startup priority.
permission = 0644; // Permission to create device nodes for the driver.
moduleName = "hi35xx_adc_driver"; // (Mandatory) Driver name, which must be the same as moduleName in the driver entry.
serviceName = "HI35XX_ADC_DRIVER"; // (Mandatory) Unique name of the service published by the driver.
deviceMatchAttr = "hisilicon_hi35xx_adc";// (Mandatory) Private data of the controller. The value must be the same as that of the corresponding controller in adc_config.hcs.
} // The specific controller information is configured in adc_config.hcs.
}
}
}
}
```
- **adc_config.hcs** configuration example
- adc\_config.hcs configuration reference
```
root {
......@@ -198,13 +134,13 @@ The following uses **adc\_hi35xx.c** as an example to present the contents that
adc_config_hi35xx {
match_attr = "hisilicon_hi35xx_adc";
template adc_device {
regBasePhy = 0x120e0000;// Physical base address of the register
regSize = 0x34; // Bit width of the register
deviceNum = 0; // Device number
validChannel = 0x1; // Valid channel
dataWidth = 10; // Data bit width of received signals
scanMode = 1; // Scan mode
delta = 0; // Delta parameter
regBasePhy = 0x120e0000;// Physical base address of the register.
regSize = 0x34; // Bit width of the register.
deviceNum = 0; // Device number.
validChannel = 0x1; // Valid channel.
dataWidth = 10; // Bit width of the received signal.
scanMode = 1; // Scan mode.
delta = 0; // Delta parameter.
deglitch = 0;
glitchSample = 5000;
rate = 20000;
......@@ -218,29 +154,30 @@ The following uses **adc\_hi35xx.c** as an example to present the contents that
}
```
3. Initialize the **AdcDevice** object at the core layer, including initializing the vendor custom structure \(transferring parameters and data\), instantiating **AdcMethod** \(used to call underlying functions of the driver\) in **AdcDevice**, and implementing the **HdfDriverEntry** member functions \(**Bind**, **Init**, and **Release**\).
- Custom structure reference
3. Initialize the **AdcDevice** object at the core layer, including defining a custom structure (to pass parameters and data) and implementing the **HdfDriverEntry** member functions (**Bind**, **Init** and **Release**) to instantiate **AdcMethod** in **AdcDevice** (so that the underlying driver functions can be called).
- Defining a custom structure
To the driver, the custom structure holds parameters and data. The **DeviceResourceIface()** function provided by the HDF reads **adc_config.hcs** to initialize the custom structure and passes some important parameters, such as the device number and bus number, to the **AdcDevice** object at the core layer.
To the driver, the custom structure carries parameters and data. The values in the **adc\_config.hcs** file are read by HDF, and the structure members are initialized through **DeviceResourceIface**. Some important values, such as the device number and bus number, are also passed to the **AdcDevice** object at the core layer.
```
struct Hi35xxAdcDevice {
struct AdcDevice device;// (Mandatory) Control object of the core layer. For details, see the description of AdcDevice.
volatile unsigned char *regBase;// (Mandatory) Base address of the register
volatile unsigned char *regBase;// (Mandatory) Base address of the register.
volatile unsigned char *pinCtrlBase;
uint32_t regBasePhy; // (Mandatory) Physical base address of the register
uint32_t regSize; // (mandatory) Bit width of the register
uint32_t deviceNum; // (Mandatory) Device number
uint32_t dataWidth; // (Mandatory) Data bit width of received signals
uint32_t validChannel; // (Mandatory) Valid channel
uint32_t scanMode; // (Mandatory) Scan mode
uint32_t regBasePhy; // (Mandatory) Physical base address of the register.
uint32_t regSize; // (Mandatory) Bit width of the register.
uint32_t deviceNum; // (Mandatory) Device number.
uint32_t dataWidth; // (Mandatory) Data bit width of received signals.
uint32_t validChannel; // (Mandatory) Valid channel.
uint32_t scanMode; // (Mandatory) Scan mode.
uint32_t delta;
uint32_t deglitch;
uint32_t glitchSample;
uint32_t rate; // (Mandatory) Sampling rate
uint32_t rate; // (Mandatory) Sampling rate.
};
// AdcDevice is the core layer controller structure. Its members are assigned with values by using the Init function.
// AdcDevice is the core layer controller structure. The **Init()** function assigns values to the members of AdcDevice.
struct AdcDevice {
const struct AdcMethod *ops;
OsalSpinlock spin;
......@@ -251,7 +188,8 @@ The following uses **adc\_hi35xx.c** as an example to present the contents that
};
```
- Instantiate the callback function structure **AdcMethod** in **AdcDevice**. The **AdcLockMethod** callback function structure is not implemented in this example. To instantiate the structure, refer to the I2C driver development. Other members are initialized in the **Init** function.
- Instantiating **AdcMethod** in **AdcDevice**<br>This example does not provide the implementation of the **AdcLockMethod** callback. For details, see I2C driver development. Other members are initialized by the **Init** function.
```
static const struct AdcMethod g_method = {
......@@ -260,56 +198,24 @@ The following uses **adc\_hi35xx.c** as an example to present the contents that
.start = Hi35xxAdcStart,
};
```
- **Init** function
Input parameter:
- Init function
Input parameters:
**HdfDeviceObject**, an interface parameter exposed by the driver, contains the .hcs configuration file information.
Return values:
HDF\_STATUS \(The following table lists some status. For details about other status, see **HDF\_STATUS** in the **//drivers/framework/include/utils/hdf\_base.h** file.\)
<a name="table127573104165048"></a>
<table><thead align="left"><tr id="row1932243367165048"><th class="cellrowborder" valign="top" width="50%" id="mcps1.1.3.1.1"><p id="entry405408385165048p0"><a name="entry405408385165048p0"></a><a name="entry405408385165048p0"></a>Status (Value)</p>
</th>
<th class="cellrowborder" valign="top" width="50%" id="mcps1.1.3.1.2"><p id="entry407875094165048p0"><a name="entry407875094165048p0"></a><a name="entry407875094165048p0"></a>Description</p>
</th>
</tr>
</thead>
<tbody><tr id="row1845195554165048"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.1.3.1.1 "><p id="entry144793493165048p0"><a name="entry144793493165048p0"></a><a name="entry144793493165048p0"></a>HDF_ERR_INVALID_OBJECT</p>
</td>
<td class="cellrowborder" valign="top" width="50%" headers="mcps1.1.3.1.2 "><p id="entry1933449399165048p0"><a name="entry1933449399165048p0"></a><a name="entry1933449399165048p0"></a>Invalid controller object</p>
</td>
</tr>
<tr id="row1203086670165048"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.1.3.1.1 "><p id="entry766973904165048p0"><a name="entry766973904165048p0"></a><a name="entry766973904165048p0"></a>HDF_ERR_INVALID_PARAM</p>
</td>
<td class="cellrowborder" valign="top" width="50%" headers="mcps1.1.3.1.2 "><p id="entry999036326165048p0"><a name="entry999036326165048p0"></a><a name="entry999036326165048p0"></a>Invalid parameter</p>
</td>
</tr>
<tr id="row1147526196165048"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.1.3.1.1 "><p id="entry271913224165048p0"><a name="entry271913224165048p0"></a><a name="entry271913224165048p0"></a>HDF_ERR_MALLOC_FAIL</p>
</td>
<td class="cellrowborder" valign="top" width="50%" headers="mcps1.1.3.1.2 "><p id="entry1349459344165048p0"><a name="entry1349459344165048p0"></a><a name="entry1349459344165048p0"></a>Failed to allocate memory</p>
</td>
</tr>
<tr id="row42206428165048"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.1.3.1.1 "><p id="entry826579070165048p0"><a name="entry826579070165048p0"></a><a name="entry826579070165048p0"></a>HDF_ERR_IO</p>
</td>
<td class="cellrowborder" valign="top" width="50%" headers="mcps1.1.3.1.2 "><p id="entry740637684165048p0"><a name="entry740637684165048p0"></a><a name="entry740637684165048p0"></a>I/O error</p>
</td>
</tr>
<tr id="row456623770165048"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.1.3.1.1 "><p id="entry2126491887165048p0"><a name="entry2126491887165048p0"></a><a name="entry2126491887165048p0"></a>HDF_SUCCESS</p>
</td>
<td class="cellrowborder" valign="top" width="50%" headers="mcps1.1.3.1.2 "><p id="entry1585614415165048p0"><a name="entry1585614415165048p0"></a><a name="entry1585614415165048p0"></a>Transmission successful</p>
</td>
</tr>
<tr id="row2048732992165048"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.1.3.1.1 "><p id="entry131232818165048p0"><a name="entry131232818165048p0"></a><a name="entry131232818165048p0"></a>HDF_FAILURE</p>
</td>
<td class="cellrowborder" valign="top" width="50%" headers="mcps1.1.3.1.2 "><p id="entry42178503165048p0"><a name="entry42178503165048p0"></a><a name="entry42178503165048p0"></a>Transmission failed</p>
</td>
</tr>
</tbody>
</table>
**HdfDeviceObject**, an interface parameter exposed by the driver, contains the .hcs information.
Return value:
HDF_STATUS<br/>The table below describes some status. For more information, see **HDF_STATUS** in the **/drivers/framework/include/utils/hdf_base.h** file.
| Status| Description|
| -------- | -------- |
| HDF_ERR_INVALID_OBJECT | Invalid controller object.|
| HDF_ERR_INVALID_PARAM | Invalid parameter.|
| HDF_ERR_MALLOC_FAIL | Failed to allocate memory.|
| HDF_ERR_IO | I/O error.|
| HDF_SUCCESS | Transmission successful.|
| HDF_FAILURE | Transmission failed.|
Function description:
......@@ -321,9 +227,9 @@ The following uses **adc\_hi35xx.c** as an example to present the contents that
int32_t ret;
struct DeviceResourceNode *childNode = NULL;
...
// Traverse and parse all nodes in adc_config.hcs and call the Hi35xxAdcParseInit function to initialize the devices separately.
// Traverse and parse all nodes in adc_config.hcs and call the **Hi35xxAdcParseInit** function to initialize the devices separately.
DEV_RES_NODE_FOR_EACH_CHILD_NODE(device->property, childNode) {
ret = Hi35xxAdcParseInit(device, childNode);// For details about the function definition, see the following description.
ret = Hi35xxAdcParseInit(device, childNode);// The function definition is as follows:
...
}
return ret;
......@@ -332,27 +238,27 @@ The following uses **adc\_hi35xx.c** as an example to present the contents that
static int32_t Hi35xxAdcParseInit(struct HdfDeviceObject *device, struct DeviceResourceNode *node)
{
int32_t ret;
struct Hi35xxAdcDevice *hi35xx = NULL; // (Mandatory) Custom structure object
struct Hi35xxAdcDevice *hi35xx = NULL; // (Mandatory) Custom structure object.
(void)device;
hi35xx = (struct Hi35xxAdcDevice *)OsalMemCalloc(sizeof(*hi35xx)); // (Mandatory) Memory allocation
hi35xx = (struct Hi35xxAdcDevice *)OsalMemCalloc(sizeof(*hi35xx)); // (Mandatory) Allocate memory.
...
ret = Hi35xxAdcReadDrs(hi35xx, node); // (Mandatory) Fill the default values in the adc_config file to the structure.
...
hi35xx->regBase = OsalIoRemap(hi35xx->regBasePhy, hi35xx->regSize);// (Mandatory) Address mapping
hi35xx->regBase = OsalIoRemap(hi35xx->regBasePhy, hi35xx->regSize);// (Mandatory) Address mapping.
...
hi35xx->pinCtrlBase = OsalIoRemap(HI35XX_ADC_IO_CONFIG_BASE, HI35XX_ADC_IO_CONFIG_SIZE);
...
Hi35xxAdcDeviceInit(hi35xx); // (Mandatory) Initialize the ADC.
hi35xx->device.priv = (void *)node; // (Mandatory) Store device attributes.
hi35xx->device.devNum = hi35xx->deviceNum;// (Mandatory) Initialize the AdcDevice members.
hi35xx->device.ops = &g_method; // (Mandatory) Mount the AdcMethod instance object.
ret = AdcDeviceAdd(&hi35xx->device); // (Mandatory and important) Call this function to set the structure of the core layer. The driver accesses the platform core layer only after a success signal is returned.
hi35xx->device.devNum = hi35xx->deviceNum;// (Mandatory) Initialize AdcDevice members.
hi35xx->device.ops = &g_method; // (Mandatory) Attach the AdcMethod instance object.
ret = AdcDeviceAdd(&hi35xx->device)); // (Mandatory) Call this function to set the structure of the core layer. The driver accesses the platform core layer only after a success signal is returned.
...
return HDF_SUCCESS;
__ERR__:
if (hi35xx != NULL) { // If the operation fails, you need to execute the initialization function reversely.
if (hi35xx != NULL) { // If the operation fails, execute the initialization function reversely.
if (hi35xx->regBase != NULL) {
OsalIoUnmap((void *)hi35xx->regBase);
hi35xx->regBase = NULL;
......@@ -363,20 +269,20 @@ The following uses **adc\_hi35xx.c** as an example to present the contents that
return ret;
}
```
- **Release** function
- Release function
Input parameter:
Input parameters:
**HdfDeviceObject**, an interface parameter exposed by the driver, contains the .hcs information.
**HdfDeviceObject**, an interface parameter exposed by the driver, contains the .hcs configuration file information.
Return value:
Return values:
No value is returned.
Function description:
Releases the memory and deletes the controller. This function assigns a value to the **Release** API in the driver entry structure. When the HDF fails to call the **Init** function to initialize the driver, the **Release** function can be called to release driver resources. All forced conversion operations for obtaining the corresponding object can be successful only when the **Init** function has the corresponding value assignment operations.
Releases the memory and deletes the controller. This function assigns values to the **Release** function in the driver entry structure. If the HDF fails to call the **Init** function to initialize the driver, the **Release** function can be called to release driver resources. All forced conversion operations for obtaining the corresponding object can be successful only when the **Init** function has the value assignment operations.
```
static void Hi35xxAdcRelease(struct HdfDeviceObject *device)
......@@ -406,7 +312,7 @@ The following uses **adc\_hi35xx.c** as an example to present the contents that
if (device != NULL && device->priv == node) {
AdcDevicePut(device);
AdcDeviceRemove(device); // (Mandatory) Remove the AdcDevice object from the driver manager.
hi35xx = (struct Hi35xxAdcDevice *)device;// (Mandatory) Obtain the custom object through forced conversion and release the object.
hi35xx = (struct Hi35xxAdcDevice *)device;// (Mandatory) Obtain the custom object through forced conversion and perform the release operation.
OsalIoUnmap((void *)hi35xx->regBase);
OsalMemFree(hi35xx);
}
......
# DAC
## Overview
## Overview<a name="1"></a>
### DAC<a name="2"></a>
### DAC
A digit-to-analog converter (DAC) is a device that converts a digital signal into an analog signal in electronics.
### Basic Concepts<a name="3"></a>
The DAC module supports development of digital-to-analog conversion. The DAC module provides the output channel for the process control computer system. It connects to the executor to implement automatic control of the production process. It is also an important module in the analog-to-digital converter using feedback technologies.
### Basic Concepts
- Resolution
The number of binary bits that can be converted by a DAC. A greater number of bits indicates a higher resolution.
......@@ -27,41 +26,40 @@ The DAC module supports development of digital-to-analog conversion. The DAC mod
The least significant byte (LSB) refers to bit 0 (the least significant bit) in a binary number.
### Working Principles<a name="4"></a>
### Working Principles
In the Hardware Driver Foundation (HDF), the DAC module uses the unified service mode for API adaptation. In this mode, a device service is used as the DAC manager to handle access requests from the devices of the same type in a unified manner. The unified service mode applies to the scenario where there are many 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 figure below shows the unified service mode.
The DAC module is divided into the following layers:
- The interface layer provides APIs for opening or closing a device and writing data.
- The core layer provides the capabilities of binding, initializing, and releasing devices.
- The adaptation layer implements other functions.
>![](../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.
![](../public_sys-resources/icon-note.gif)NOTE<br/>The core layer can call the APIs of the interface layer and uses hooks to call APIs of the adaptation layer. In this way, the adaptation layer can indirectly call the APIs of the interface layer, but the interface layer cannot call the APIs of the adaptation layer.
**Figure 1** Unified service mode<a name="fig14423182615525"></a>
**Figure 1** Unified service mode
![](figures/unified-service-mode.png "DAC unified service mode")
### Constraints<a name="5"></a>
### Constraints
Currently, the DAC module supports only the kernels (LiteOS) of mini and small systems.
## Development Guidelines<a name="6"></a>
## Development Guidelines
### When to Use<a name="7"></a>
### When to Use
The DAC module is used for digital-to-analog conversion, audio output, and motor control. The DAC driver is used when the digital signals input by the DAC module are converted into analog signals to output.
### Available APIs<a name="8"></a>
### Available APIs
The **DacMethod** is used to call the DAC driver functions.
The **DacMethod** is used to call the DAC driver APIs.
**DacMethod** definition:
**DacMethod**:
```
struct DacMethod {
......@@ -76,17 +74,17 @@ struct DacMethod {
**Table 1** Description of the DacMethod structure
<a name="table27410339187"></a>
| Function| Input Parameter | Output Parameter| Return Value | Description |
| -------- | ------------------------------------------------------------ | ---- | ------------------ | -------------- |
| write | **device**: pointer to the DAC controller at the core layer.<br>**channel**: channel ID, which is of the uint32_t type.<br>val: data to write, which is of the uint32_t type.| - | HDF_STATUS| Writes the target digit-to-analog (DA) value.|
| start | **device**: pointer to the DAC controller at the core layer. | - | HDF_STATUS| Starts a DAC device. |
| stop | **device**: pointer to the DAC controller at the core layer. | - | HDF_STATUS| Stops a device. |
| write | **device**: structure pointer to the DAC controller at the core layer.<br>**channel**: channel ID, which is of the uint32_t type.<br>**val**: data to write, which is of the uint32_t type.| - | HDF_STATUS| Writes the target digital-to-analog (DA) value.|
| start | **device**: structure pointer to the DAC controller at the core layer. | - | HDF_STATUS| Starts a DAC device. |
| stop | **device**: structure pointer to the DAC controller at the core layer. | - | HDF_STATUS| Stops a DAC device. |
### How to Develop<a name="9"></a>
### How to Develop
The DAC module adaptation procedure is as follows:
......@@ -97,7 +95,7 @@ The DAC module adaptation procedure is as follows:
1. Instantiate the driver entry.
Instantiate the driver entry. The driver entry must be a global variable of the **HdfDriverEntry** type (defined in **hdf_device_desc.h**), and the value of **moduleName** must be the same as that in **device_info.hcs**. In the HDF, the start address of each **HdfDriverEntry** object of all loaded drivers are collected to form a segment address space similar to an array for the upper layer to invoke.
The driver entry must be a global variable of the **HdfDriverEntry** type (defined in **hdf_device_desc.h**), and the value of **moduleName** must be the same as that in **device_info.hcs**. In the HDF, the start address of each **HdfDriverEntry** object of all loaded drivers is collected to form a segment address space similar to an array for the upper layer to invoke.
Generally, the HDF calls the **Init()** function to load the driver. If **Init()** fails to be called, the HDF calls **Release()** to release driver resources and exit.
......@@ -122,7 +120,7 @@ The DAC module adaptation procedure is as follows:
| Member | Value |
| --------------- | ------------------------------------------------------------ |
| policy | **0**, which indicates that no service is published.|
| priority | Driver startup priority. The value range is 0 to 200. A larger value indicates a lower priority. If the priorities are the same, the device loading sequence is not ensured.|
| priority | Driver startup priority. The value range is 0 to 200. A larger value indicates a lower priority. For the drivers with the same priority, the device loads them randomly.|
| permission | Driver permission.|
| moduleName | The value is **HDF_PLATFORM_DAC_MANAGER**.|
| serviceName | The value is **HDF_PLATFORM_DAC_MANAGER**.|
......@@ -130,12 +128,12 @@ The DAC module adaptation procedure is as follows:
Configure DAC controller information from the second node. This node specifies a type of DAC controllers rather than a specific DAC controller. In this example, there is only one DAC device. If there are multiple DAC devices, you need to add the **deviceNode** information to the **device_info** file and add the corresponding device attributes to the **dac_config** file.
**device_info.hcs** configuration reference
**device_info.hcs** configuration example
```
root {
device_dac :: device {
// device0 is the DAC manager.
// device0 is a DAC manager.
device0 :: deviceNode {
policy = 0;
priority = 52;
......@@ -144,7 +142,7 @@ The DAC module adaptation procedure is as follows:
moduleName = "HDF_PLATFORM_DAC_MANAGER";
}
}
// dac_virtual is the DAC controller.
// dac_virtual is a DAC controller.
dac_virtual :: deviceNode {
policy = 0;
priority = 56;
......@@ -178,9 +176,9 @@ The DAC module adaptation procedure is as follows:
}
```
3. Instantiate the APIs of the core layer.
3. Instantiate the core layer APIs.
- Initialize the **DacDevice** object.
- Initializing the **DacDevice** object
Initialize the **DacDevice** member in the **VirtualDacParseAndInit** function.
......@@ -201,28 +199,28 @@ The DAC module adaptation procedure is as follows:
{
// Define the return values.
int32_t ret;
// Virtual pointer to the DAC device
// Pointer to the virtual DAC device
struct VirtualDacDevice *virtual = NULL;
(void)device;
// Allocate space for the virtual pointer.
// Allocate space for this pointer.
virtual = (struct VirtualDacDevice *)OsalMemCalloc(sizeof(*virtual));
if (virtual == NULL) {
// If the value is null, return an error code.
HDF_LOGE("%s: Malloc virtual fail!", __func__);
return HDF_ERR_MALLOC_FAIL;
}
// Read the configuration of the attribute file.
// Read the content of the attribute file.
ret = VirtualDacReadDrs(virtual, node);
if (ret != HDF_SUCCESS) {
// Failed to read the file.
HDF_LOGE("%s: Read drs fail! ret:%d", __func__, ret);
// Release the virtual space.
// Release the space for the virtual DAC device.
OsalMemFree(virtual);
// Set the pointer to 0.
virtual = NULL;
return ret;
}
// Initialize the virtual pointer.
// Initialize the pointer to the virtual DAC device.
VirtualDacDeviceInit(virtual);
// Initialize the priv object in DacDevice.
virtual->device.priv = (void *)node;
......@@ -235,9 +233,9 @@ The DAC module adaptation procedure is as follows:
if (ret != HDF_SUCCESS) {
// Failed to add the device.
HDF_LOGE("%s: add Dac controller failed! ret = %d", __func__, ret);
// Release the virtual space.
// Release the space for the virtual DAC device.
OsalMemFree(virtual);
// Set the virtual pointer to null.
// Set this pointer to null.
virtual = NULL;
return ret;
}
......@@ -248,9 +246,9 @@ The DAC module adaptation procedure is as follows:
- Custom structure reference
- Defining a custom structure
The custom structure holds parameters and data for the driver. Define the custom structure based on the function parameters of the device. The HDF reads the values in the **dac_config.hcs** file using the **DacTestReadConfig()** function and initializes the structure members using **DeviceResourceIface()**. Some important values, such as the device number and bus number, are also passed to the **DacDevice** object at the core layer.
The custom structure holds parameters and data for the driver. Define the custom structure based on the function parameters of the device. The **DacTestReadConfig()** provided by the HDF reads the values in the **dac_config.hcs** file, and **DeviceResourceIface()** initializes the custom structure and passes some important parameters, such as the device number and bus number, to the **DacDevice** object at the core layer.
```
struct VirtualDacDevice {
......@@ -260,7 +258,7 @@ The DAC module adaptation procedure is as follows:
uint32_t rate; // (Mandatory) Sampling rate.
};
// DacDevice is the core layer controller structure. Its members are assigned with values in the Init() function.
// DacDevice is the core layer controller structure. The Init() function assigns values to the members of DacDevice.
struct DacDevice {
const struct DacMethod *ops;
OsalSpinlock spin; // Spinlock.
......@@ -271,8 +269,7 @@ The DAC module adaptation procedure is as follows:
};
```
- Instantiate the **DacDevice** in **DacMethod**.
- Instantiating **DacDevice** in **DacMethod**.
The **VirtualDacWrite**, **VirtualDacStop**, and **VirtualDacStart** functions are instantiated in **dac_virtual.c**.
......@@ -290,13 +287,13 @@ The DAC module adaptation procedure is as follows:
- **Init** function
Input parameters:
Input parameter:
**HdfDeviceObject**, an interface parameter exposed by the driver, contains the .hcs configuration file information.
**HdfDeviceObject**, an interface parameter exposed by the driver, contains the .hcs information.
Return value:
HDF_STATUS (The table below lists some status. For details about other status, see HDF_STATUS in the /drivers/framework/include/utils/hdf_base.h file.)
HDF_STATUS<br>The table below lists some status. For more information, see HDF_STATUS in the **/drivers/framework/include/utils/hdf_base.h** file.
| State | Description |
| ---------------------- | -------------- |
......@@ -355,7 +352,7 @@ The DAC module adaptation procedure is as follows:
if (virtual != NULL) {
// Release the memory.
OsalMemFree(virtual);
// Set the pointer to null.
// Set this pointer to null.
virtual = NULL;
}
......@@ -391,13 +388,13 @@ The DAC module adaptation procedure is as follows:
- **Release** function
Input parameters:
Input parameter:
**HdfDeviceObject**, an interface parameter exposed by the driver, contains the .hcs configuration file information.
**HdfDeviceObject**, an interface parameter exposed by the driver, contains the .hcs information.
Return value
Return value:
This function returns no value.
No value is returned.
Function description:
......@@ -410,15 +407,15 @@ The DAC module adaptation procedure is as follows:
int32_t ret;
// Define the DAC device number.
int16_t devNum;
// Pointer of the DacDevice structure.
// Pointer to the DacDevice structure.
struct DacDevice *device = NULL;
// Pointer of the VirtualDacDevice structure.
// Pointer to the VirtualDacDevice structure.
struct VirtualDacDevice *virtual = NULL;
// Pointer of the DeviceResourceIface structure.
// Pointer to the DeviceResourceIface structure.
struct DeviceResourceIface *drsOps = NULL;
// Obtain device resources through the instance entry.
drsOps = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE);
// Check the input parameter pointer.
//Check whether the return value and the member function of the return value are null.
if (drsOps == NULL || drsOps->GetUint32 == NULL) {
// The pointer is null.
HDF_LOGE("%s: invalid drs ops fail!", __func__);
......@@ -448,7 +445,7 @@ The DAC module adaptation procedure is as follows:
static void VirtualDacRelease(struct HdfDeviceObject *device)
{
// Define the child node structure pointer of the DeviceResourceNode.
// Define the child node structure pointer to the DeviceResourceNode.
const struct DeviceResourceNode *childNode = NULL;
// Check whether the input parameter pointer is null.
if (device == NULL || device->property == NULL) {
......@@ -467,3 +464,5 @@ The DAC module adaptation procedure is as follows:
4. Debug the driver.
(Optional) Verify the basic functions of the new driver, for example, whether the test cases are successful after the driver is loaded.
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册