1e41f4b71Sopenharmony_ci# ADC 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci## Overview 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci### Function 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ciAn analog-to-digital converter (ADC) is a device that converts analog signals into digital signals. 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci### Basic Concepts 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci- Resolution 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci The number of binary bits that can be converted by an ADC. A greater number of bits indicates a higher resolution. 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci- Conversion error 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_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. 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ci- Transition time 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ci Time required by an ADC to perform a complete conversion. 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci 24e41f4b71Sopenharmony_ci### Working Principles 25e41f4b71Sopenharmony_ci 26e41f4b71Sopenharmony_ciIn 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 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 in this case, more device nodes need to be configured and more memory resources will be consumed. The following figure illustrates the unified service mode of the ADC module. 27e41f4b71Sopenharmony_ci 28e41f4b71Sopenharmony_ciThe ADC module is divided into the following layers: 29e41f4b71Sopenharmony_ci 30e41f4b71Sopenharmony_ci- Interface layer: provides the capabilities of opening a device, writing data, and closing a device. 31e41f4b71Sopenharmony_ci- Core layer: binds services, initializes and releases the PlatformManager, and provides the capabilities of adding, deleting, and obtaining controllers. 32e41f4b71Sopenharmony_ci- Adaptation layer: implements hardware-related functions, such as controller initialization. 33e41f4b71Sopenharmony_ci 34e41f4b71Sopenharmony_ciIn the unified service mode, the core layer manages all controllers in a unified manner and publishes a service for the interface layer. That is, the driver does not need to publish a service for each controller. 35e41f4b71Sopenharmony_ci 36e41f4b71Sopenharmony_ci**Figure 1** Unified service mode 37e41f4b71Sopenharmony_ci 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_ci 40e41f4b71Sopenharmony_ci## Usage Guidelines 41e41f4b71Sopenharmony_ci 42e41f4b71Sopenharmony_ci### When to Use 43e41f4b71Sopenharmony_ci 44e41f4b71Sopenharmony_ciADC devices are used to convert analog voltage 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. Before using ADC devices with OpenHarmony, you need to adapt the ADC driver to OpenHarmony. The following describes how to do it. 45e41f4b71Sopenharmony_ci 46e41f4b71Sopenharmony_ci### Available APIs 47e41f4b71Sopenharmony_ci 48e41f4b71Sopenharmony_ciTo enable the upper layer to successfully operate the hardware by calling the ADC APIs, hook functions are defined in **//drivers/hdf_core/framework/support/platform/include/adc/adc_core.h** for the core layer. You need to implement these hook functions at the adaptation layer and hook them to implement the interaction between the interface layer and the core layer. 49e41f4b71Sopenharmony_ci 50e41f4b71Sopenharmony_ciDefinitions of **AdcMethod** and **AdcLockMethod**: 51e41f4b71Sopenharmony_ci 52e41f4b71Sopenharmony_ci```c 53e41f4b71Sopenharmony_cistruct AdcMethod { 54e41f4b71Sopenharmony_ci int32_t (*read)(struct AdcDevice *device, uint32_t channel, uint32_t *val); 55e41f4b71Sopenharmony_ci int32_t (*start)(struct AdcDevice *device); 56e41f4b71Sopenharmony_ci int32_t (*stop)(struct AdcDevice *device); 57e41f4b71Sopenharmony_ci}; 58e41f4b71Sopenharmony_ci 59e41f4b71Sopenharmony_cistruct AdcLockMethod { 60e41f4b71Sopenharmony_ci int32_t (*lock)(struct AdcDevice *device); 61e41f4b71Sopenharmony_ci void (*unlock)(struct AdcDevice *device); 62e41f4b71Sopenharmony_ci}; 63e41f4b71Sopenharmony_ci 64e41f4b71Sopenharmony_ci``` 65e41f4b71Sopenharmony_ci 66e41f4b71Sopenharmony_ciAt the adaptation layer, **AdcMethod** must be implemented, and **AdcLockMethod** can be implemented based on service requirements. The core layer provides the default **AdcLockMethod**, in which a spinlock is used to protect the critical section. 67e41f4b71Sopenharmony_ci 68e41f4b71Sopenharmony_ci```c 69e41f4b71Sopenharmony_cistatic int32_t AdcDeviceLockDefault(struct AdcDevice *device) 70e41f4b71Sopenharmony_ci{ 71e41f4b71Sopenharmony_ci if (device == NULL) { 72e41f4b71Sopenharmony_ci return HDF_ERR_INVALID_OBJECT; 73e41f4b71Sopenharmony_ci } 74e41f4b71Sopenharmony_ci return OsalSpinLock(&device->spin); 75e41f4b71Sopenharmony_ci} 76e41f4b71Sopenharmony_ci 77e41f4b71Sopenharmony_cistatic void AdcDeviceUnlockDefault(struct AdcDevice *device) 78e41f4b71Sopenharmony_ci{ 79e41f4b71Sopenharmony_ci if (device == NULL) { 80e41f4b71Sopenharmony_ci return; 81e41f4b71Sopenharmony_ci } 82e41f4b71Sopenharmony_ci (void)OsalSpinUnlock(&device->spin); 83e41f4b71Sopenharmony_ci} 84e41f4b71Sopenharmony_ci 85e41f4b71Sopenharmony_cistatic const struct AdcLockMethod g_adcLockOpsDefault = { 86e41f4b71Sopenharmony_ci .lock = AdcDeviceLockDefault, 87e41f4b71Sopenharmony_ci .unlock = AdcDeviceUnlockDefault, 88e41f4b71Sopenharmony_ci}; 89e41f4b71Sopenharmony_ci 90e41f4b71Sopenharmony_ci``` 91e41f4b71Sopenharmony_ci 92e41f4b71Sopenharmony_ciIf spinlock cannot be used, you can use another type of lock to implement **AdcLockMethod**. The custom **AdcLockMethod** will replace the default **AdcLockMethod**. 93e41f4b71Sopenharmony_ci 94e41f4b71Sopenharmony_ci **Table 1** Hook functions in **AdcMethod** 95e41f4b71Sopenharmony_ci 96e41f4b71Sopenharmony_ci| Function| Input Parameter| Output Parameter| Return Value| Description| 97e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | -------- | 98e41f4b71Sopenharmony_ci| read | **device**: structure pointer to the ADC controller at the core layer.<br>**channel**: channel number, 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.| 99e41f4b71Sopenharmony_ci| stop | **device**: structure pointer to the ADC controller at the core layer.| –| HDF_STATUS| Stops an ADC device.| 100e41f4b71Sopenharmony_ci| start | **device**: structure pointer to the ADC controller at the core layer.| –| HDF_STATUS| Starts an ADC device.| 101e41f4b71Sopenharmony_ci 102e41f4b71Sopenharmony_ci**Table 2** Functions in **AdcLockMethod** 103e41f4b71Sopenharmony_ci 104e41f4b71Sopenharmony_ci| Function| Input Parameter| Output Parameter| Return Value| Description| 105e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | -------- | 106e41f4b71Sopenharmony_ci| lock | **device**: structure pointer to the ADC device object at the core layer.| –| HDF_STATUS| Acquires the critical section lock.| 107e41f4b71Sopenharmony_ci| unlock | **device**: structure pointer to the ADC device object at the core layer.| –| HDF_STATUS| Releases the critical section lock.| 108e41f4b71Sopenharmony_ci 109e41f4b71Sopenharmony_ci### How to Develop 110e41f4b71Sopenharmony_ci 111e41f4b71Sopenharmony_ciThe ADC module adaptation involves the following steps: 112e41f4b71Sopenharmony_ci 113e41f4b71Sopenharmony_ci1. Instantiate the driver entry. 114e41f4b71Sopenharmony_ci - Instantiate the **HdfDriverEntry** structure. 115e41f4b71Sopenharmony_ci - Call **HDF_INIT** to register the **HdfDriverEntry** instance with the HDF. 116e41f4b71Sopenharmony_ci 117e41f4b71Sopenharmony_ci2. Configure attribute files. 118e41f4b71Sopenharmony_ci - Add the **deviceNode** information to the **device_info.hcs** file. 119e41f4b71Sopenharmony_ci - (Optional) Add the **adc_config.hcs** file. 120e41f4b71Sopenharmony_ci 121e41f4b71Sopenharmony_ci3. Instantiate the core layer APIs. 122e41f4b71Sopenharmony_ci - Initialize **AdcDevice**. 123e41f4b71Sopenharmony_ci - Instantiate **AdcMethod** in the **AdcDevice** object. 124e41f4b71Sopenharmony_ci >  **NOTE**<br> 125e41f4b71Sopenharmony_ci > For details about the functions in **AdcMethod**, see [Available APIs](#available-apis). 126e41f4b71Sopenharmony_ci 127e41f4b71Sopenharmony_ci### Example 128e41f4b71Sopenharmony_ci 129e41f4b71Sopenharmony_ciThe following uses the Hi3516D V300 driver **//device/soc/hisilicon/common/platform/adc/adc_hi35xx.c** as an example to describe how to perform the ADC driver adaptation. 130e41f4b71Sopenharmony_ci 131e41f4b71Sopenharmony_ci1. Instantiate the driver entry. 132e41f4b71Sopenharmony_ci 133e41f4b71Sopenharmony_ci 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. 134e41f4b71Sopenharmony_ci 135e41f4b71Sopenharmony_ci 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. 136e41f4b71Sopenharmony_ci 137e41f4b71Sopenharmony_ci ADC driver entry example: 138e41f4b71Sopenharmony_ci 139e41f4b71Sopenharmony_ci Multiple devices may connect to the ADC controller. In the HDF, a manager object needs to be created for this type of devices. When a device needs to be started, the manager object locates the target device based on the specified parameters. 140e41f4b71Sopenharmony_ci 141e41f4b71Sopenharmony_ci You do not need to implement the driver of the ADC manager, which is implemented by the core layer. However, the **AdcDeviceAdd** function of the core layer must be invoked in the **Init** function to implement the related features. 142e41f4b71Sopenharmony_ci 143e41f4b71Sopenharmony_ci ```c 144e41f4b71Sopenharmony_ci static struct HdfDriverEntry g_hi35xxAdcDriverEntry = { 145e41f4b71Sopenharmony_ci .moduleVersion = 1, 146e41f4b71Sopenharmony_ci .Init = Hi35xxAdcInit, 147e41f4b71Sopenharmony_ci .Release = Hi35xxAdcRelease, 148e41f4b71Sopenharmony_ci .moduleName = "hi35xx_adc_driver", // (Mandatory) The value must be the same as the module name in the device_info.hcs file. 149e41f4b71Sopenharmony_ci }; 150e41f4b71Sopenharmony_ci HDF_INIT(g_hi35xxAdcDriverEntry); // Call HDF_INIT to register the driver entry with the HDF. 151e41f4b71Sopenharmony_ci 152e41f4b71Sopenharmony_ci /* Driver entry of the adc_core.c manager service at the core layer */ 153e41f4b71Sopenharmony_ci struct HdfDriverEntry g_adcManagerEntry = { 154e41f4b71Sopenharmony_ci .moduleVersion = 1, 155e41f4b71Sopenharmony_ci .Init = AdcManagerInit, 156e41f4b71Sopenharmony_ci .Release = AdcManagerRelease, 157e41f4b71Sopenharmony_ci .moduleName = "HDF_PLATFORM_ADC_MANAGER", // The value must be that of device0 in the device_info.hcs file. 158e41f4b71Sopenharmony_ci }; 159e41f4b71Sopenharmony_ci HDF_INIT(g_adcManagerEntry); 160e41f4b71Sopenharmony_ci ``` 161e41f4b71Sopenharmony_ci 162e41f4b71Sopenharmony_ci2. Add the **deviceNode** information to the **//vendor/hisilicon/hispark_taurus/hdf_config/device_info/device_info.hcs** file and configure the device attributes in **adc_config.hcs**. 163e41f4b71Sopenharmony_ci 164e41f4b71Sopenharmony_ci The **deviceNode** information is related to the driver entry registration. 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. 165e41f4b71Sopenharmony_ci 166e41f4b71Sopenharmony_ci In the unified service mode, the first device node in the **device_info.hcs** file must be the ADC manager. The parameters must be set as follows: 167e41f4b71Sopenharmony_ci 168e41f4b71Sopenharmony_ci | Parameter| Value| 169e41f4b71Sopenharmony_ci | -------- | -------- | 170e41f4b71Sopenharmony_ci | moduleName | **HDF_PLATFORM_ADC_MANAGER**| 171e41f4b71Sopenharmony_ci | serviceName | –| 172e41f4b71Sopenharmony_ci | policy | **0**, which indicates that no service is published.| 173e41f4b71Sopenharmony_ci | deviceMatchAttr | Reserved.| 174e41f4b71Sopenharmony_ci 175e41f4b71Sopenharmony_ci 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.hcs** file and add the corresponding device attributes to the **adc_config** file for each device. 176e41f4b71Sopenharmony_ci 177e41f4b71Sopenharmony_ci - **device_info.hcs** example 178e41f4b71Sopenharmony_ci 179e41f4b71Sopenharmony_ci ```c 180e41f4b71Sopenharmony_ci root { 181e41f4b71Sopenharmony_ci device_info { 182e41f4b71Sopenharmony_ci platform :: host { 183e41f4b71Sopenharmony_ci device_adc :: device { 184e41f4b71Sopenharmony_ci device0 :: deviceNode { 185e41f4b71Sopenharmony_ci policy = 0; 186e41f4b71Sopenharmony_ci priority = 50; 187e41f4b71Sopenharmony_ci permission = 0644; 188e41f4b71Sopenharmony_ci moduleName = "HDF_PLATFORM_ADC_MANAGER"; 189e41f4b71Sopenharmony_ci serviceName = "HDF_PLATFORM_ADC_MANAGER"; 190e41f4b71Sopenharmony_ci } 191e41f4b71Sopenharmony_ci device1 :: deviceNode { 192e41f4b71Sopenharmony_ci policy = 0; // The value 0 indicates that no service is published. 193e41f4b71Sopenharmony_ci priority = 55; // Driver startup priority. 194e41f4b71Sopenharmony_ci permission = 0644; // Permission for the device node created. 195e41f4b71Sopenharmony_ci moduleName = "hi35xx_adc_driver"; // (Mandatory) Driver name, which must be the same as moduleName in the driver entry. 196e41f4b71Sopenharmony_ci serviceName = "HI35XX_ADC_DRIVER"; // (Mandatory) Unique name of the service published by the driver. 197e41f4b71Sopenharmony_ci deviceMatchAttr = "hisilicon_hi35xx_adc"; // (Mandatory) Private data of the controller. The value must be the same as that of the controller in adc_config.hcs. 198e41f4b71Sopenharmony_ci // The specific controller information is in adc_config.hcs. 199e41f4b71Sopenharmony_ci } 200e41f4b71Sopenharmony_ci } 201e41f4b71Sopenharmony_ci } 202e41f4b71Sopenharmony_ci } 203e41f4b71Sopenharmony_ci } 204e41f4b71Sopenharmony_ci ``` 205e41f4b71Sopenharmony_ci 206e41f4b71Sopenharmony_ci - **adc_config.hcs** example 207e41f4b71Sopenharmony_ci 208e41f4b71Sopenharmony_ci The following uses Hi3516D V300 as an example. Some fields are unique to Hi3516D V300. You can delete or add fields as required. 209e41f4b71Sopenharmony_ci 210e41f4b71Sopenharmony_ci ```c 211e41f4b71Sopenharmony_ci root { 212e41f4b71Sopenharmony_ci platform { 213e41f4b71Sopenharmony_ci adc_config_hi35xx { 214e41f4b71Sopenharmony_ci match_attr = "hisilicon_hi35xx_adc"; 215e41f4b71Sopenharmony_ci template adc_device { 216e41f4b71Sopenharmony_ci regBasePhy = 0x120e0000; // Physical base address of the register. 217e41f4b71Sopenharmony_ci regSize = 0x34; // Bit width of the register. 218e41f4b71Sopenharmony_ci deviceNum = 0; // Device number. 219e41f4b71Sopenharmony_ci validChannel = 0x1; // Valid channel. 220e41f4b71Sopenharmony_ci dataWidth = 10; // Data width after AD conversion, that is, the resolution. 221e41f4b71Sopenharmony_ci scanMode = 1; // Scan mode. 222e41f4b71Sopenharmony_ci delta = 0; // Error range of the conversion result. 223e41f4b71Sopenharmony_ci deglitch = 0; // Setting of the deglitch. 224e41f4b71Sopenharmony_ci glitchSample = 5000; // Deglitch time window. 225e41f4b71Sopenharmony_ci rate = 20000; // Conversion rate. 226e41f4b71Sopenharmony_ci } 227e41f4b71Sopenharmony_ci device_0 :: adc_device { 228e41f4b71Sopenharmony_ci deviceNum = 0; 229e41f4b71Sopenharmony_ci validChannel = 0x2; 230e41f4b71Sopenharmony_ci } 231e41f4b71Sopenharmony_ci } 232e41f4b71Sopenharmony_ci } 233e41f4b71Sopenharmony_ci } 234e41f4b71Sopenharmony_ci ``` 235e41f4b71Sopenharmony_ci 236e41f4b71Sopenharmony_ci After the **adc_config.hcs** file is configured, include the file in the **hdf.hcs** file. Otherwise, the configuration file cannot take effect. 237e41f4b71Sopenharmony_ci 238e41f4b71Sopenharmony_ci For example, if the **adc_config.hcs** file is in **//device/soc/hisilicon/hi3516dv300/sdk_liteos/hdf_config/adc/**, add the following statement to **hdf.hcs** of the product: 239e41f4b71Sopenharmony_ci 240e41f4b71Sopenharmony_ci ```c 241e41f4b71Sopenharmony_ci #include "../../../../device/soc/hisilicon/hi3516dv300/sdk_liteos/hdf_config/adc/adc_config.hcs" // Relative path of the configuration file 242e41f4b71Sopenharmony_ci ``` 243e41f4b71Sopenharmony_ci 244e41f4b71Sopenharmony_ci This example is based on the Hi3516D V300 development board that runs the LiteOS. The corresponding **hdf.hcs** file is in **vendor/hisilicon/hispark_taurus/hdf_config/hdf.hcs** and **//device/hisilicon/hispark_taurus/sdk_liteos/hdf_config/hdf.hcs**. You can modify the file as required. 245e41f4b71Sopenharmony_ci 246e41f4b71Sopenharmony_ci3. 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). 247e41f4b71Sopenharmony_ci 248e41f4b71Sopenharmony_ci - Define a custom structure. 249e41f4b71Sopenharmony_ci 250e41f4b71Sopenharmony_ci 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. 251e41f4b71Sopenharmony_ci 252e41f4b71Sopenharmony_ci ```c 253e41f4b71Sopenharmony_ci struct Hi35xxAdcDevice { 254e41f4b71Sopenharmony_ci struct AdcDevice device; // (Mandatory) Control object at the core layer. It must be the first member of the custom structure. For details, see the following description. 255e41f4b71Sopenharmony_ci volatile unsigned char *regBase; // (Mandatory) Register base address. 256e41f4b71Sopenharmony_ci volatile unsigned char *pinCtrlBase; 257e41f4b71Sopenharmony_ci uint32_t regBasePhy; // (Mandatory) Physical base address of the register. 258e41f4b71Sopenharmony_ci uint32_t regSize; // (Mandatory) Register bit width. 259e41f4b71Sopenharmony_ci uint32_t deviceNum; // (Mandatory) Device number. 260e41f4b71Sopenharmony_ci uint32_t dataWidth; // (Mandatory) Data bit width of received signals. 261e41f4b71Sopenharmony_ci uint32_t validChannel; // (Mandatory) Valid channel. 262e41f4b71Sopenharmony_ci uint32_t scanMode; // (Mandatory) Scan mode. 263e41f4b71Sopenharmony_ci uint32_t delta; 264e41f4b71Sopenharmony_ci uint32_t deglitch; 265e41f4b71Sopenharmony_ci uint32_t glitchSample; 266e41f4b71Sopenharmony_ci uint32_t rate; // (Mandatory) Sampling rate. 267e41f4b71Sopenharmony_ci }; 268e41f4b71Sopenharmony_ci 269e41f4b71Sopenharmony_ci /* AdcDevice is the core layer controller structure. The **Init()** function assigns values to the members of AdcDevice. */ 270e41f4b71Sopenharmony_ci struct AdcDevice { 271e41f4b71Sopenharmony_ci const struct AdcMethod *ops; 272e41f4b71Sopenharmony_ci OsalSpinlock spin; 273e41f4b71Sopenharmony_ci uint32_t devNum; 274e41f4b71Sopenharmony_ci uint32_t chanNum; 275e41f4b71Sopenharmony_ci const struct AdcLockMethod *lockOps; 276e41f4b71Sopenharmony_ci void *priv; 277e41f4b71Sopenharmony_ci }; 278e41f4b71Sopenharmony_ci ``` 279e41f4b71Sopenharmony_ci 280e41f4b71Sopenharmony_ci - Instantiate the hook function structure **AdcMethod** of **AdcDevice**. 281e41f4b71Sopenharmony_ci 282e41f4b71Sopenharmony_ci The **AdcLockMethod** is not implemented in this example. To instantiate the structure, refer to the I2C driver development. Other members are initialized in the **Init** function. 283e41f4b71Sopenharmony_ci 284e41f4b71Sopenharmony_ci ```c 285e41f4b71Sopenharmony_ci static const struct AdcMethod g_method = { 286e41f4b71Sopenharmony_ci .read = Hi35xxAdcRead, 287e41f4b71Sopenharmony_ci .stop = Hi35xxAdcStop, 288e41f4b71Sopenharmony_ci .start = Hi35xxAdcStart, 289e41f4b71Sopenharmony_ci }; 290e41f4b71Sopenharmony_ci ``` 291e41f4b71Sopenharmony_ci 292e41f4b71Sopenharmony_ci - Implement the **Init** function. 293e41f4b71Sopenharmony_ci 294e41f4b71Sopenharmony_ci Input parameter: 295e41f4b71Sopenharmony_ci 296e41f4b71Sopenharmony_ci **HdfDeviceObject**, an interface parameter provided by the driver, contains the .hcs information. 297e41f4b71Sopenharmony_ci 298e41f4b71Sopenharmony_ci Return value: 299e41f4b71Sopenharmony_ci 300e41f4b71Sopenharmony_ci **HDF_STATUS**<br/>The table below describes some status. For more information, see **HDF_STATUS** in the **//drivers/hdf_core/framework/include/utils/hdf_base.h** file. 301e41f4b71Sopenharmony_ci 302e41f4b71Sopenharmony_ci | Status| Description| 303e41f4b71Sopenharmony_ci | -------- | -------- | 304e41f4b71Sopenharmony_ci | HDF_ERR_INVALID_OBJECT | Invalid controller object.| 305e41f4b71Sopenharmony_ci | HDF_ERR_INVALID_PARAM | Invalid parameter.| 306e41f4b71Sopenharmony_ci | HDF_ERR_MALLOC_FAIL | Failed to allocate memory.| 307e41f4b71Sopenharmony_ci | HDF_ERR_IO | I/O error.| 308e41f4b71Sopenharmony_ci | HDF_SUCCESS | Transmission successful.| 309e41f4b71Sopenharmony_ci | HDF_FAILURE | Transmission failed.| 310e41f4b71Sopenharmony_ci 311e41f4b71Sopenharmony_ci Function description: 312e41f4b71Sopenharmony_ci 313e41f4b71Sopenharmony_ci Initializes the custom structure object and **AdcDevice**, and calls the **AdcDeviceAdd** function at the core layer. 314e41f4b71Sopenharmony_ci 315e41f4b71Sopenharmony_ci ```c 316e41f4b71Sopenharmony_ci static int32_t Hi35xxAdcInit(struct HdfDeviceObject *device) 317e41f4b71Sopenharmony_ci { 318e41f4b71Sopenharmony_ci int32_t ret; 319e41f4b71Sopenharmony_ci struct DeviceResourceNode *childNode = NULL; 320e41f4b71Sopenharmony_ci ... 321e41f4b71Sopenharmony_ci /* Traverse and parse all nodes in adc_config.hcs and call the **Hi35xxAdcParseInit** function to initialize the devices separately. */ 322e41f4b71Sopenharmony_ci DEV_RES_NODE_FOR_EACH_CHILD_NODE(device->property, childNode) { 323e41f4b71Sopenharmony_ci ret = Hi35xxAdcParseInit(device, childNode); // The function definition is as follows: 324e41f4b71Sopenharmony_ci ... 325e41f4b71Sopenharmony_ci } 326e41f4b71Sopenharmony_ci return ret; 327e41f4b71Sopenharmony_ci } 328e41f4b71Sopenharmony_ci 329e41f4b71Sopenharmony_ci static int32_t Hi35xxAdcParseInit(struct HdfDeviceObject *device, struct DeviceResourceNode *node) 330e41f4b71Sopenharmony_ci { 331e41f4b71Sopenharmony_ci int32_t ret; 332e41f4b71Sopenharmony_ci struct Hi35xxAdcDevice *hi35xx = NULL; // (Mandatory) Custom structure object. 333e41f4b71Sopenharmony_ci (void)device; 334e41f4b71Sopenharmony_ci 335e41f4b71Sopenharmony_ci hi35xx = (struct Hi35xxAdcDevice *)OsalMemCalloc(sizeof(*hi35xx)); // (Mandatory) Allocate memory. 336e41f4b71Sopenharmony_ci ... 337e41f4b71Sopenharmony_ci ret = Hi35xxAdcReadDrs(hi35xx, node); // (Mandatory) Use the default values in the adc_config file to fill in the structure. The function definition is as follows. 338e41f4b71Sopenharmony_ci ... 339e41f4b71Sopenharmony_ci hi35xx->regBase = OsalIoRemap(hi35xx->regBasePhy, hi35xx->regSize); // (Mandatory) Address mapping. 340e41f4b71Sopenharmony_ci ... 341e41f4b71Sopenharmony_ci hi35xx->pinCtrlBase = OsalIoRemap(HI35XX_ADC_IO_CONFIG_BASE, HI35XX_ADC_IO_CONFIG_SIZE); 342e41f4b71Sopenharmony_ci ... 343e41f4b71Sopenharmony_ci Hi35xxAdcDeviceInit(hi35xx); // (Mandatory) Initialize the ADC. 344e41f4b71Sopenharmony_ci hi35xx->device.priv = (void *)node; // (Mandatory) Save device attributes. 345e41f4b71Sopenharmony_ci hi35xx->device.devNum = hi35xx->deviceNum;// (Mandatory) Initialize AdcDevice. 346e41f4b71Sopenharmony_ci hi35xx->device.ops = &g_method; // (Mandatory) Attach the AdcMethod instance object. 347e41f4b71Sopenharmony_ci ret = AdcDeviceAdd(&hi35xx->device)); // (Mandatory) Call this function to set the structure at the core layer. The driver can access the platform core layer only after a success signal is returned. 348e41f4b71Sopenharmony_ci ... 349e41f4b71Sopenharmony_ci return HDF_SUCCESS; 350e41f4b71Sopenharmony_ci 351e41f4b71Sopenharmony_ci __ERR__: 352e41f4b71Sopenharmony_ci if (hi35xx != NULL) { // If the operation fails, deinitialize related functions. 353e41f4b71Sopenharmony_ci if (hi35xx->regBase != NULL) { 354e41f4b71Sopenharmony_ci OsalIoUnmap((void *)hi35xx->regBase); 355e41f4b71Sopenharmony_ci hi35xx->regBase = NULL; 356e41f4b71Sopenharmony_ci } 357e41f4b71Sopenharmony_ci AdcDeviceRemove(&hi35xx->device); 358e41f4b71Sopenharmony_ci OsalMemFree(hi35xx); 359e41f4b71Sopenharmony_ci } 360e41f4b71Sopenharmony_ci return ret; 361e41f4b71Sopenharmony_ci } 362e41f4b71Sopenharmony_ci 363e41f4b71Sopenharmony_ci static int32_t Hi35xxAdcReadDrs(struct Hi35xxAdcDevice *hi35xx, const struct DeviceResourceNode *node) 364e41f4b71Sopenharmony_ci { 365e41f4b71Sopenharmony_ci int32_t ret; 366e41f4b71Sopenharmony_ci struct DeviceResourceIface *drsOps = NULL; 367e41f4b71Sopenharmony_ci 368e41f4b71Sopenharmony_ci /* Obtain the drsOps method. */ 369e41f4b71Sopenharmony_ci drsOps = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE); 370e41f4b71Sopenharmony_ci if (drsOps == NULL || drsOps->GetUint32 == NULL) { 371e41f4b71Sopenharmony_ci HDF_LOGE("%s: invalid drs ops", __func__); 372e41f4b71Sopenharmony_ci return HDF_ERR_NOT_SUPPORT; 373e41f4b71Sopenharmony_ci } 374e41f4b71Sopenharmony_ci /* Read the configuration parameters in sequence and fill them in the structure. */ 375e41f4b71Sopenharmony_ci ret = drsOps->GetUint32(node, "regBasePhy", &hi35xx->regBasePhy, 0); 376e41f4b71Sopenharmony_ci if (ret != HDF_SUCCESS) { 377e41f4b71Sopenharmony_ci HDF_LOGE("%s: read regBasePhy failed", __func__); 378e41f4b71Sopenharmony_ci return ret; 379e41f4b71Sopenharmony_ci } 380e41f4b71Sopenharmony_ci ret = drsOps->GetUint32(node, "regSize", &hi35xx->regSize, 0); 381e41f4b71Sopenharmony_ci if (ret != HDF_SUCCESS) { 382e41f4b71Sopenharmony_ci HDF_LOGE("%s: read regSize failed", __func__); 383e41f4b71Sopenharmony_ci return ret; 384e41f4b71Sopenharmony_ci } 385e41f4b71Sopenharmony_ci ··· 386e41f4b71Sopenharmony_ci return HDF_SUCCESS; 387e41f4b71Sopenharmony_ci } 388e41f4b71Sopenharmony_ci ``` 389e41f4b71Sopenharmony_ci 390e41f4b71Sopenharmony_ci - Implement the **Release** function. 391e41f4b71Sopenharmony_ci 392e41f4b71Sopenharmony_ci Input parameter: 393e41f4b71Sopenharmony_ci 394e41f4b71Sopenharmony_ci **HdfDeviceObject**, an interface parameter provided by the driver, contains the .hcs information. 395e41f4b71Sopenharmony_ci 396e41f4b71Sopenharmony_ci Return value: 397e41f4b71Sopenharmony_ci 398e41f4b71Sopenharmony_ci No value is returned. 399e41f4b71Sopenharmony_ci 400e41f4b71Sopenharmony_ci Function description: 401e41f4b71Sopenharmony_ci 402e41f4b71Sopenharmony_ci 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. 403e41f4b71Sopenharmony_ci 404e41f4b71Sopenharmony_ci ```c 405e41f4b71Sopenharmony_ci static void Hi35xxAdcRelease(struct HdfDeviceObject *device) 406e41f4b71Sopenharmony_ci { 407e41f4b71Sopenharmony_ci const struct DeviceResourceNode *childNode = NULL; 408e41f4b71Sopenharmony_ci ... 409e41f4b71Sopenharmony_ci /* Traverse and parse all nodes in adc_config.hcs and perform the release operation on each node. */ 410e41f4b71Sopenharmony_ci DEV_RES_NODE_FOR_EACH_CHILD_NODE(device->property, childNode) { 411e41f4b71Sopenharmony_ci Hi35xxAdcRemoveByNode(childNode);// The function definition is as follows: 412e41f4b71Sopenharmony_ci } 413e41f4b71Sopenharmony_ci } 414e41f4b71Sopenharmony_ci 415e41f4b71Sopenharmony_ci static void Hi35xxAdcRemoveByNode(const struct DeviceResourceNode *node) 416e41f4b71Sopenharmony_ci { 417e41f4b71Sopenharmony_ci int32_t ret; 418e41f4b71Sopenharmony_ci int32_t deviceNum; 419e41f4b71Sopenharmony_ci struct AdcDevice *device = NULL; 420e41f4b71Sopenharmony_ci struct Hi35xxAdcDevice *hi35xx = NULL; 421e41f4b71Sopenharmony_ci struct DeviceResourceIface *drsOps = NULL; 422e41f4b71Sopenharmony_ci 423e41f4b71Sopenharmony_ci drsOps = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE); 424e41f4b71Sopenharmony_ci ... 425e41f4b71Sopenharmony_ci ret = drsOps->GetUint32(node, "deviceNum", (uint32_t *)&deviceNum, 0); 426e41f4b71Sopenharmony_ci ... 427e41f4b71Sopenharmony_ci /* You can use AdcDeviceGet() to obtain the AdcDevice object based on deviceNum and use AdcDeviceRemove() to release the AdcDevice object. */ 428e41f4b71Sopenharmony_ci device = AdcDeviceGet(deviceNum); 429e41f4b71Sopenharmony_ci if (device != NULL && device->priv == node) { 430e41f4b71Sopenharmony_ci AdcDevicePut(device); 431e41f4b71Sopenharmony_ci AdcDeviceRemove(device); // (Mandatory) Remove the AdcDevice object from the driver manager. 432e41f4b71Sopenharmony_ci hi35xx = (struct Hi35xxAdcDevice *)device; // (Mandatory) Obtain the custom object through forcible conversion and perform the Release operation. To perform this operation, the device must be the first member of the custom structure. 433e41f4b71Sopenharmony_ci OsalIoUnmap((void *)hi35xx->regBase); 434e41f4b71Sopenharmony_ci OsalMemFree(hi35xx); 435e41f4b71Sopenharmony_ci } 436e41f4b71Sopenharmony_ci return; 437e41f4b71Sopenharmony_ci } 438e41f4b71Sopenharmony_ci ``` 439