1e41f4b71Sopenharmony_ci# Development Example for Platform Drivers<a name="EN-US_TOPIC_0000001157064271"></a>
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci## Overview<a name="section194201316174215"></a>
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ciThis document uses the I2C driver as an example to describe how to develop platform drivers based on the hardware driver foundation \(HDF\).
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ci>![](../public_sys-resources/icon-caution.gif) **CAUTION:** 
8e41f4b71Sopenharmony_ci>The sample code in this document is for reference only and cannot be directly used for commercial integration.
9e41f4b71Sopenharmony_ci
10e41f4b71Sopenharmony_ciThe HDF provides a standard driver framework for common peripherals. To use the APIs provided by the HDF to perform operations on peripherals, you only need to adapt the specific driver to the HDF.
11e41f4b71Sopenharmony_ci
12e41f4b71Sopenharmony_ciIn this example, an I2C driver is used.  [Figure 1](#fig9596628607)  shows the sequence diagram of the I2C driver.
13e41f4b71Sopenharmony_ci
14e41f4b71Sopenharmony_ci**Figure  1**  I2C driver sequence diagram<a name="fig9596628607"></a>  
15e41f4b71Sopenharmony_ci![](figures/i2c-driver-sequence-diagram.png "i2c-driver-sequence-diagram")
16e41f4b71Sopenharmony_ci
17e41f4b71Sopenharmony_ci## Preparations<a name="section6926133918422"></a>
18e41f4b71Sopenharmony_ci
19e41f4b71Sopenharmony_ciFollow the instructions in [Getting Started with the Standard System with Hi3516 (IDE Mode)](../quick-start/quickstart-appendix-hi3516-ide.md).
20e41f4b71Sopenharmony_ci
21e41f4b71Sopenharmony_ci>![](../public_sys-resources/icon-notice.gif) **NOTICE:** 
22e41f4b71Sopenharmony_ci>This development example applies to standard, small, and mini OpenHarmony systems. The following sections use the standard system as an example. You can refer to the specific guide for your system to set up the environment.
23e41f4b71Sopenharmony_ci
24e41f4b71Sopenharmony_ci## Development<a name="section65801539470"></a>
25e41f4b71Sopenharmony_ci
26e41f4b71Sopenharmony_ci### File Description<a name="section0708184454414"></a>
27e41f4b71Sopenharmony_ci
28e41f4b71Sopenharmony_ciThe following table lists the files involved in this example and their paths.
29e41f4b71Sopenharmony_ci
30e41f4b71Sopenharmony_ci**Table  1**  File description
31e41f4b71Sopenharmony_ci
32e41f4b71Sopenharmony_ci<a name="table15887645104012"></a>
33e41f4b71Sopenharmony_ci<table><thead align="left"><tr id="row198881452404"><th class="cellrowborder" align="center" valign="top" width="13.489999999999998%" id="mcps1.2.4.1.1"><p id="p158742406488"><a name="p158742406488"></a><a name="p158742406488"></a>File</p>
34e41f4b71Sopenharmony_ci</th>
35e41f4b71Sopenharmony_ci<th class="cellrowborder" align="center" valign="top" width="68.52000000000001%" id="mcps1.2.4.1.2"><p id="p6975142717432"><a name="p6975142717432"></a><a name="p6975142717432"></a>File Path</p>
36e41f4b71Sopenharmony_ci</th>
37e41f4b71Sopenharmony_ci<th class="cellrowborder" align="center" valign="top" width="17.990000000000002%" id="mcps1.2.4.1.3"><p id="p98891454405"><a name="p98891454405"></a><a name="p98891454405"></a>Remarks</p>
38e41f4b71Sopenharmony_ci</th>
39e41f4b71Sopenharmony_ci</tr>
40e41f4b71Sopenharmony_ci</thead>
41e41f4b71Sopenharmony_ci<tbody><tr id="row1088914458407"><td class="cellrowborder" align="left" valign="top" width="13.489999999999998%" headers="mcps1.2.4.1.1 "><p id="p78741540104813"><a name="p78741540104813"></a><a name="p78741540104813"></a>Sample file</p>
42e41f4b71Sopenharmony_ci</td>
43e41f4b71Sopenharmony_ci<td class="cellrowborder" align="left" valign="top" width="68.52000000000001%" headers="mcps1.2.4.1.2 "><p id="p1066541692916"><a name="p1066541692916"></a><a name="p1066541692916"></a>/drivers/adapter/khdf/linux/platform/i2c/i2c_sample.c</p>
44e41f4b71Sopenharmony_ci</td>
45e41f4b71Sopenharmony_ci<td class="cellrowborder" align="left" valign="top" width="17.990000000000002%" headers="mcps1.2.4.1.3 "><p id="p208891445144012"><a name="p208891445144012"></a><a name="p208891445144012"></a>New file</p>
46e41f4b71Sopenharmony_ci</td>
47e41f4b71Sopenharmony_ci</tr>
48e41f4b71Sopenharmony_ci<tr id="row1388984594013"><td class="cellrowborder" align="left" valign="top" width="13.489999999999998%" headers="mcps1.2.4.1.1 "><p id="p88741840104811"><a name="p88741840104811"></a><a name="p88741840104811"></a>Device service file</p>
49e41f4b71Sopenharmony_ci</td>
50e41f4b71Sopenharmony_ci<td class="cellrowborder" align="left" valign="top" width="68.52000000000001%" headers="mcps1.2.4.1.2 "><p id="p486417183298"><a name="p486417183298"></a><a name="p486417183298"></a>/drivers/adapter/khdf/linux/hcs/device_info/device_info.hcs</p>
51e41f4b71Sopenharmony_ci</td>
52e41f4b71Sopenharmony_ci<td class="cellrowborder" rowspan="3" align="left" valign="top" width="17.990000000000002%" headers="mcps1.2.4.1.3 "><p id="p128898458401"><a name="p128898458401"></a><a name="p128898458401"></a></p>
53e41f4b71Sopenharmony_ci<p id="p168904455404"><a name="p168904455404"></a><a name="p168904455404"></a>New content will be added to these files.</p>
54e41f4b71Sopenharmony_ci<p id="p7890124516405"><a name="p7890124516405"></a><a name="p7890124516405"></a></p>
55e41f4b71Sopenharmony_ci</td>
56e41f4b71Sopenharmony_ci</tr>
57e41f4b71Sopenharmony_ci<tr id="row9889164513406"><td class="cellrowborder" align="left" valign="top" headers="mcps1.2.4.1.1 "><p id="p138741640124812"><a name="p138741640124812"></a><a name="p138741640124812"></a>Configuration file</p>
58e41f4b71Sopenharmony_ci</td>
59e41f4b71Sopenharmony_ci<td class="cellrowborder" align="left" valign="top" headers="mcps1.2.4.1.2 "><p id="p26905191293"><a name="p26905191293"></a><a name="p26905191293"></a>/drivers/adapter/khdf/linux/hcs/platform/i2c_config.hcs</p>
60e41f4b71Sopenharmony_ci</td>
61e41f4b71Sopenharmony_ci</tr>
62e41f4b71Sopenharmony_ci<tr id="row1189044513404"><td class="cellrowborder" align="left" valign="top" headers="mcps1.2.4.1.1 "><p id="p1687424074814"><a name="p1687424074814"></a><a name="p1687424074814"></a>Build file</p>
63e41f4b71Sopenharmony_ci</td>
64e41f4b71Sopenharmony_ci<td class="cellrowborder" align="left" valign="top" headers="mcps1.2.4.1.2 "><p id="p1885032192917"><a name="p1885032192917"></a><a name="p1885032192917"></a>/drivers/adapter/khdf/linux/platform/i2c/Makefile</p>
65e41f4b71Sopenharmony_ci</td>
66e41f4b71Sopenharmony_ci</tr>
67e41f4b71Sopenharmony_ci<tr id="row10890144564011"><td class="cellrowborder" align="left" valign="top" width="13.489999999999998%" headers="mcps1.2.4.1.1 "><p id="p118752040104810"><a name="p118752040104810"></a><a name="p118752040104810"></a>Dependency</p>
68e41f4b71Sopenharmony_ci</td>
69e41f4b71Sopenharmony_ci<td class="cellrowborder" align="left" valign="top" width="68.52000000000001%" headers="mcps1.2.4.1.2 "><p id="p15821718182916"><a name="p15821718182916"></a><a name="p15821718182916"></a>/drivers/framework/include/core/hdf_device_desc.h</p>
70e41f4b71Sopenharmony_ci</td>
71e41f4b71Sopenharmony_ci<td class="cellrowborder" rowspan="2" align="left" valign="top" width="17.990000000000002%" headers="mcps1.2.4.1.3 "><p id="p989012451401"><a name="p989012451401"></a><a name="p989012451401"></a>Header file to be included</p>
72e41f4b71Sopenharmony_ci<p id="p1890134594014"><a name="p1890134594014"></a><a name="p1890134594014"></a></p>
73e41f4b71Sopenharmony_ci</td>
74e41f4b71Sopenharmony_ci</tr>
75e41f4b71Sopenharmony_ci<tr id="row6890164564015"><td class="cellrowborder" align="left" valign="top" headers="mcps1.2.4.1.1 "><p id="p128756401484"><a name="p128756401484"></a><a name="p128756401484"></a>Core-layer header file</p>
76e41f4b71Sopenharmony_ci</td>
77e41f4b71Sopenharmony_ci<td class="cellrowborder" align="left" valign="top" headers="mcps1.2.4.1.2 "><p id="p47681122152918"><a name="p47681122152918"></a><a name="p47681122152918"></a>/drivers/framework/support/platform/include/i2c_core.h</p>
78e41f4b71Sopenharmony_ci</td>
79e41f4b71Sopenharmony_ci</tr>
80e41f4b71Sopenharmony_ci<tr id="row1499682234817"><td class="cellrowborder" align="left" valign="top" width="13.489999999999998%" headers="mcps1.2.4.1.1 "><p id="p1187513403487"><a name="p1187513403487"></a><a name="p1187513403487"></a>HCS configuration entry file</p>
81e41f4b71Sopenharmony_ci</td>
82e41f4b71Sopenharmony_ci<td class="cellrowborder" align="left" valign="top" width="68.52000000000001%" headers="mcps1.2.4.1.2 "><p id="p499818225487"><a name="p499818225487"></a><a name="p499818225487"></a>/drivers/adapter/khdf/linux/hcs/hdf.hcs</p>
83e41f4b71Sopenharmony_ci</td>
84e41f4b71Sopenharmony_ci<td class="cellrowborder" align="left" valign="top" width="17.990000000000002%" headers="mcps1.2.4.1.3 "><p id="p3998152254820"><a name="p3998152254820"></a><a name="p3998152254820"></a>Entry to HCS configuration files</p>
85e41f4b71Sopenharmony_ci</td>
86e41f4b71Sopenharmony_ci</tr>
87e41f4b71Sopenharmony_ci</tbody>
88e41f4b71Sopenharmony_ci</table>
89e41f4b71Sopenharmony_ci
90e41f4b71Sopenharmony_ci>![](../public_sys-resources/icon-caution.gif) **CAUTION:** 
91e41f4b71Sopenharmony_ci>The file paths involved in this example are used for demonstration only. Determine the paths for storing the source files as required when developing your driver.
92e41f4b71Sopenharmony_ci
93e41f4b71Sopenharmony_ci### Instantiating the Driver Entry<a name="section85325864412"></a>
94e41f4b71Sopenharmony_ci
95e41f4b71Sopenharmony_ciInstantiate an  **HdfDriverEntry**  object as the driver entry. The driver entry must be a global variable of the  **HdfDriverEntry**  type \(which is defined in  **hdf\_device\_desc.h**\), and the value of  **moduleName**  must be the same as that in  **device\_info.hcs**. When loading the driver, the HDF calls the  **Bind**  function first and then the  **Init**  function. If an error occurred during the calling of the  **Init**  function, the HDF calls  **Release**  to release the driver resource and exit.
96e41f4b71Sopenharmony_ci
97e41f4b71Sopenharmony_ciThe  **Bind**  function is not implemented in the I2C driver because the I2C controller is managed by the I2C manager and the  **Bind**  function has been implemented in the manager. Therefore, services do not need to be bound in the I2C driver.
98e41f4b71Sopenharmony_ci
99e41f4b71Sopenharmony_ciThe sample code for instantiating the driver entry is as follows:
100e41f4b71Sopenharmony_ci
101e41f4b71Sopenharmony_ci```
102e41f4b71Sopenharmony_ci/* Define a driver entry object. It must be a global variable of the HdfDriverEntry type (defined in hdf_device_desc.h). */
103e41f4b71Sopenharmony_cistruct HdfDriverEntry g_sampleI2cDriverEntry = {
104e41f4b71Sopenharmony_ci    .moduleVersion = 1,
105e41f4b71Sopenharmony_ci    .Init = SampleI2cInit,
106e41f4b71Sopenharmony_ci    .Release = SampleI2cRelease,
107e41f4b71Sopenharmony_ci    .moduleName = "demo_i2c_driver",
108e41f4b71Sopenharmony_ci};
109e41f4b71Sopenharmony_ci/* Call HDF_INIT to register the driver entry with the HDF. */
110e41f4b71Sopenharmony_ciHDF_INIT(g_sampleI2cDriverEntry);
111e41f4b71Sopenharmony_ci```
112e41f4b71Sopenharmony_ci
113e41f4b71Sopenharmony_ci### Setting Related Parameters<a name="section8155172019453"></a>
114e41f4b71Sopenharmony_ci
115e41f4b71Sopenharmony_ciConfigure the  **device\_info.hcs**  file and obtain and parse device configuration parameters from the HCS to ensure that the driver can be correctly loaded.
116e41f4b71Sopenharmony_ci
117e41f4b71Sopenharmony_ci1.  \(Mandatory\) Add a device service node.
118e41f4b71Sopenharmony_ci
119e41f4b71Sopenharmony_ci    Edit the  **device\_info.hcs**  file and add a driver device service node under  **device\_i2c :: device**. The following is an example:
120e41f4b71Sopenharmony_ci
121e41f4b71Sopenharmony_ci    ```
122e41f4b71Sopenharmony_ci     root {
123e41f4b71Sopenharmony_ci        device_info {
124e41f4b71Sopenharmony_ci            match_attr = "hdf_manager";
125e41f4b71Sopenharmony_ci                device_i2c :: device {                        // I2C devices
126e41f4b71Sopenharmony_ci    		device2 :: deviceNode {                   // Device node of an I2C driver
127e41f4b71Sopenharmony_ci                        policy = 0;                           // Policy for releasing the driver service
128e41f4b71Sopenharmony_ci                        priority = 55;                        // Driver startup priority
129e41f4b71Sopenharmony_ci                        permission = 0644;                    // Permission for the driver to create a device node
130e41f4b71Sopenharmony_ci                        moduleName = "demo_i2c_driver";       // Driver name. The value of this field must be the same as that of moduleName in the driver entry data structure.
131e41f4b71Sopenharmony_ci                        serviceName = "DEMO_I2C_DRIVER";      // Name of the service released by the driver. The name must be unique.
132e41f4b71Sopenharmony_ci                        deviceMatchAttr = "demo_i2c_config";  // Keyword matching the private data of the driver. The value must be the same as that of
133e41f4b71Sopenharmony_ci                                                              // match_attr in the private configuration data table of the driver.
134e41f4b71Sopenharmony_ci                    }
135e41f4b71Sopenharmony_ci                }
136e41f4b71Sopenharmony_ci        }
137e41f4b71Sopenharmony_ci    }
138e41f4b71Sopenharmony_ci    
139e41f4b71Sopenharmony_ci    ```
140e41f4b71Sopenharmony_ci
141e41f4b71Sopenharmony_ci    >![](../public_sys-resources/icon-notice.gif) **NOTICE:** 
142e41f4b71Sopenharmony_ci    >The  **priority**  attribute \(an integer ranging from 0 to 200\) in the configuration file indicates the priority of a host or drivers. Drivers in a host with a smaller priority value have a higher loading priority than those in other hosts. The driver with a smaller priority value in a host has a higher loading priority than the other drivers in the host. The loading sequence is random for drivers with the same priority.
143e41f4b71Sopenharmony_ci
144e41f4b71Sopenharmony_ci2.  \(Optional\) Add configuration parameters.
145e41f4b71Sopenharmony_ci
146e41f4b71Sopenharmony_ci    The driver may require private configuration information to ensure that the register configuration meets the requirements of different products. If private configuration data is required, you can add a driver configuration file to store some default configuration information about the driver. When loading the driver, the HDF obtains the specified configuration information, saves it in the  **property**  attribute of  **HdfDeviceObject**, and passes it to the driver via  **Bind**  and  **Init**. For details about how to use  **Bind**  and  **Init**, see  [Driver Development](../driver/driver-overview-foundation.md). You can create a configuration file and reference it in the  **hdf.hcs**  file of the board-level driver. In this example, configuration parameters are directly added to the existing configuration file  **i2c\_config.hcs**.
147e41f4b71Sopenharmony_ci
148e41f4b71Sopenharmony_ci    The following configuration parameters are added to the  **i2c\_config.hcs**  file:
149e41f4b71Sopenharmony_ci
150e41f4b71Sopenharmony_ci    ```
151e41f4b71Sopenharmony_ci    root {
152e41f4b71Sopenharmony_ci        platform {
153e41f4b71Sopenharmony_ci                i2c_config_demo {                
154e41f4b71Sopenharmony_ci                match_attr = "demo_i2c_config";        // The value of this field must be the same as that of deviceMatchAttr in device_info.hcs.
155e41f4b71Sopenharmony_ci    
156e41f4b71Sopenharmony_ci                template i2c_controller {              // Parameter template
157e41f4b71Sopenharmony_ci                    bus = 0;
158e41f4b71Sopenharmony_ci                    reg_pbase = 0x120b0000;
159e41f4b71Sopenharmony_ci                    reg_size = 0xd1;
160e41f4b71Sopenharmony_ci                }
161e41f4b71Sopenharmony_ci    
162e41f4b71Sopenharmony_ci                controller_demo_0 :: i2c_controller {  // Two sample I2C controllers
163e41f4b71Sopenharmony_ci                    bus = 8;
164e41f4b71Sopenharmony_ci                }
165e41f4b71Sopenharmony_ci                controller_demo_1 :: i2c_controller {
166e41f4b71Sopenharmony_ci                    bus = 9;
167e41f4b71Sopenharmony_ci                }
168e41f4b71Sopenharmony_ci            }
169e41f4b71Sopenharmony_ci        }
170e41f4b71Sopenharmony_ci    }
171e41f4b71Sopenharmony_ci    ```
172e41f4b71Sopenharmony_ci
173e41f4b71Sopenharmony_ci    The value of  **match\_attr**  must be the same as that of  **deviceMatch\_Attr**  in the  **device\_info.hcs**  file. The  **match\_attr**  attribute is used to match the configured parameters in the configuration file \(**i2c\_config.hcs**  in this example\) with the particular driver, so that the driver can call  **DeviceResourceGetIfaceInstance\(\)**  in the  **Bind**  or  **Init**  function to obtain these configuration parameters.
174e41f4b71Sopenharmony_ci
175e41f4b71Sopenharmony_ci    If you create a new configuration file to set parameters, reference this file in the board-level configuration entry file  **hdf.hcs**. For example:
176e41f4b71Sopenharmony_ci
177e41f4b71Sopenharmony_ci    ```
178e41f4b71Sopenharmony_ci    #include "device_info/device_info.hcs"
179e41f4b71Sopenharmony_ci    #include "i2c/i2c_config.hcs"
180e41f4b71Sopenharmony_ci    ```
181e41f4b71Sopenharmony_ci
182e41f4b71Sopenharmony_ci    In this development example, we use an existing configuration file  **i2c\_config.hcs**  to add parameters, and therefore do not need to add it to the board-level configuration entry file.
183e41f4b71Sopenharmony_ci
184e41f4b71Sopenharmony_ci3.  Enable the driver to obtain configuration parameters from the HCS.
185e41f4b71Sopenharmony_ci
186e41f4b71Sopenharmony_ci    In this example, the driver needs to obtain configuration parameters, such as the physical base address of the register, register size, and bus number, from the HCS to correctly configure the controller.
187e41f4b71Sopenharmony_ci
188e41f4b71Sopenharmony_ci    ```
189e41f4b71Sopenharmony_ci    /* Obtain configuration parameters from the HCS. */
190e41f4b71Sopenharmony_ci    static int32_t SampleI2cReadDrs(struct SampleI2cCntlr *sampleCntlr, const struct DeviceResourceNode *node)
191e41f4b71Sopenharmony_ci    {
192e41f4b71Sopenharmony_ci        int32_t ret;
193e41f4b71Sopenharmony_ci        struct DeviceResourceIface *drsOps = NULL;
194e41f4b71Sopenharmony_ci    
195e41f4b71Sopenharmony_ci        drsOps = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE);
196e41f4b71Sopenharmony_ci        if (drsOps == NULL || drsOps->GetUint32 == NULL) {                         // Ensure that the GetUint32 function is available.
197e41f4b71Sopenharmony_ci            HDF_LOGE("%s: invalid drs ops fail!", __func__);
198e41f4b71Sopenharmony_ci            return HDF_FAILURE;
199e41f4b71Sopenharmony_ci        }
200e41f4b71Sopenharmony_ci    
201e41f4b71Sopenharmony_ci        ret = drsOps->GetUint32(node, "reg_pbase", &sampleCntlr->regBasePhy, 0);   // Read the physical base address from the HCS.
202e41f4b71Sopenharmony_ci        if (ret != HDF_SUCCESS) {
203e41f4b71Sopenharmony_ci            HDF_LOGE("%s: read regBase fail!", __func__);
204e41f4b71Sopenharmony_ci            return ret;
205e41f4b71Sopenharmony_ci        }
206e41f4b71Sopenharmony_ci    
207e41f4b71Sopenharmony_ci        ret = drsOps->GetUint16(node, "reg_size", &sampleCntlr->regSize, 0);       // Read the register size from the HCS.
208e41f4b71Sopenharmony_ci        if (ret != HDF_SUCCESS) {
209e41f4b71Sopenharmony_ci            HDF_LOGE("%s: read regsize fail!", __func__);
210e41f4b71Sopenharmony_ci            return ret;
211e41f4b71Sopenharmony_ci        }
212e41f4b71Sopenharmony_ci    
213e41f4b71Sopenharmony_ci        ret = drsOps->GetUint16(node, "bus", (uint16_t *)&sampleCntlr->bus, 0);    // Read the bus number from the HCS.
214e41f4b71Sopenharmony_ci        if (ret != HDF_SUCCESS) {
215e41f4b71Sopenharmony_ci            HDF_LOGE("%s: read bus fail!", __func__);
216e41f4b71Sopenharmony_ci            return ret;
217e41f4b71Sopenharmony_ci        }
218e41f4b71Sopenharmony_ci    
219e41f4b71Sopenharmony_ci        return HDF_SUCCESS;
220e41f4b71Sopenharmony_ci    }
221e41f4b71Sopenharmony_ci    ```
222e41f4b71Sopenharmony_ci
223e41f4b71Sopenharmony_ci
224e41f4b71Sopenharmony_ci### Adding a Controller<a name="section1335374114452"></a>
225e41f4b71Sopenharmony_ci
226e41f4b71Sopenharmony_ciInitialize the controller hardware, call core-layer APIs to add or delete devices to or from the core layer, and implement a hook.
227e41f4b71Sopenharmony_ci
228e41f4b71Sopenharmony_ci1.  Define an I2C controller structure, implement a hook, and assign the hook to the function pointer.
229e41f4b71Sopenharmony_ci
230e41f4b71Sopenharmony_ci    The  **I2cMethod**  structure is defined in the  **i2c\_core.h**  header file. This structure defines the functions to be implemented by the I2C driver by using function pointers. The  **SampleI2cTransfer**  function is a hook used for data transmission, which must be implemented in the driver and must be assigned to a function pointer.
231e41f4b71Sopenharmony_ci
232e41f4b71Sopenharmony_ci    The sample code is as follows:
233e41f4b71Sopenharmony_ci
234e41f4b71Sopenharmony_ci    ```
235e41f4b71Sopenharmony_ci    /* Custom device structure, which is inherited from I2cCntlr */
236e41f4b71Sopenharmony_ci    struct SampleI2cCntlr {
237e41f4b71Sopenharmony_ci        struct I2cCntlr cntlr;
238e41f4b71Sopenharmony_ci        OsalSpinlock spin;
239e41f4b71Sopenharmony_ci        volatile unsigned char  *regBase;
240e41f4b71Sopenharmony_ci        uint16_t regSize;
241e41f4b71Sopenharmony_ci        int16_t bus;
242e41f4b71Sopenharmony_ci        uint32_t regBasePhy;
243e41f4b71Sopenharmony_ci    };
244e41f4b71Sopenharmony_ci    
245e41f4b71Sopenharmony_ci    /* Message structure, which is inherited from I2cMsg */
246e41f4b71Sopenharmony_ci    struct SampleTransferData {
247e41f4b71Sopenharmony_ci        struct I2cMsg *msgs;
248e41f4b71Sopenharmony_ci        int16_t index;
249e41f4b71Sopenharmony_ci        int16_t count;
250e41f4b71Sopenharmony_ci    };
251e41f4b71Sopenharmony_ci    /* Hook implementation */
252e41f4b71Sopenharmony_ci    static int32_t SampleI2cTransfer(struct I2cCntlr *cntlr, struct I2cMsg *msgs, int16_t count)
253e41f4b71Sopenharmony_ci    {
254e41f4b71Sopenharmony_ci        int32_t ret = HDF_SUCCESS;
255e41f4b71Sopenharmony_ci        struct SampleI2cCntlr *sampleCntlr = NULL;
256e41f4b71Sopenharmony_ci        struct SampleTransferData td;
257e41f4b71Sopenharmony_ci    
258e41f4b71Sopenharmony_ci        if (cntlr == NULL || cntlr->priv == NULL) {
259e41f4b71Sopenharmony_ci            HDF_LOGE("SampleI2cTransfer: cntlr lor sampleCntlr is null!");
260e41f4b71Sopenharmony_ci            return HDF_ERR_INVALID_OBJECT;
261e41f4b71Sopenharmony_ci        }
262e41f4b71Sopenharmony_ci        sampleCntlr = (struct SampleI2cCntlr *)cntlr;
263e41f4b71Sopenharmony_ci    
264e41f4b71Sopenharmony_ci        if (msgs == NULL || count <= 0) {
265e41f4b71Sopenharmony_ci            HDF_LOGE("SampleI2cTransfer: err parms! count:%d", count);
266e41f4b71Sopenharmony_ci            return HDF_ERR_INVALID_PARAM;
267e41f4b71Sopenharmony_ci        }
268e41f4b71Sopenharmony_ci        td.msgs = msgs;
269e41f4b71Sopenharmony_ci        td.count = count;
270e41f4b71Sopenharmony_ci        td.index = 0;
271e41f4b71Sopenharmony_ci    
272e41f4b71Sopenharmony_ci        HDF_LOGE("Successfully transmitted!");  // Data transmission is successful.
273e41f4b71Sopenharmony_ci          
274e41f4b71Sopenharmony_ci        td.index = count;                       // The total number of sent messages is returned. The message handling process is not provided in this sample code.
275e41f4b71Sopenharmony_ci        return (td.index > 0) ? td.index : ret;
276e41f4b71Sopenharmony_ci    }
277e41f4b71Sopenharmony_ci    /* Assign the hook to a function pointer. */
278e41f4b71Sopenharmony_ci    static struct I2cMethod g_method = {
279e41f4b71Sopenharmony_ci        .transfer = SampleI2cTransfer,
280e41f4b71Sopenharmony_ci    };
281e41f4b71Sopenharmony_ci    ```
282e41f4b71Sopenharmony_ci
283e41f4b71Sopenharmony_ci2.  Write a driver initialization function.
284e41f4b71Sopenharmony_ci
285e41f4b71Sopenharmony_ci    This example uses  **SampleI2cInit**  as the name of the driver initialization function \(this function name is configurable\). This function must be assigned to the  **Init**  function in the driver entry structure so that the HDF can call it to initialize the driver. The driver initialization function needs to parse the configuration parameters obtained from the HCS and create a controller based on these parameters. The sample code is as follows:
286e41f4b71Sopenharmony_ci
287e41f4b71Sopenharmony_ci    ```
288e41f4b71Sopenharmony_ci    /* Parse parameters, apply for memory, and create a controller. */
289e41f4b71Sopenharmony_ci    static int32_t SampleI2cParseAndInit(struct HdfDeviceObject *device, const struct DeviceResourceNode *node)
290e41f4b71Sopenharmony_ci    {
291e41f4b71Sopenharmony_ci        int32_t ret;
292e41f4b71Sopenharmony_ci        struct SampleI2cCntlr *sampleCntlr = NULL;
293e41f4b71Sopenharmony_ci        (void)device;
294e41f4b71Sopenharmony_ci    
295e41f4b71Sopenharmony_ci        sampleCntlr = (struct SampleI2cCntlr *)OsalMemCalloc(sizeof(*sampleCntlr));
296e41f4b71Sopenharmony_ci        if (sampleCntlr == NULL) {
297e41f4b71Sopenharmony_ci            HDF_LOGE("%s: malloc sampleCntlr fail!", __func__);
298e41f4b71Sopenharmony_ci            return HDF_ERR_MALLOC_FAIL;
299e41f4b71Sopenharmony_ci        }
300e41f4b71Sopenharmony_ci    
301e41f4b71Sopenharmony_ci        ret = SampleI2cReadDrs(sampleCntlr, node);              // Obtain configuration parameters from the HCS.
302e41f4b71Sopenharmony_ci        if (ret != HDF_SUCCESS) {
303e41f4b71Sopenharmony_ci            HDF_LOGE("%s: read drs fail! ret:%d", __func__, ret);
304e41f4b71Sopenharmony_ci            goto __ERR__;
305e41f4b71Sopenharmony_ci        }
306e41f4b71Sopenharmony_ci    
307e41f4b71Sopenharmony_ci        sampleCntlr->regBase = OsalIoRemap(sampleCntlr->regBasePhy, sampleCntlr->regSize);
308e41f4b71Sopenharmony_ci        if (sampleCntlr->regBase == NULL) {
309e41f4b71Sopenharmony_ci            HDF_LOGE("%s: ioremap regBase fail!", __func__);
310e41f4b71Sopenharmony_ci            ret = HDF_ERR_IO;
311e41f4b71Sopenharmony_ci            goto __ERR__;
312e41f4b71Sopenharmony_ci        }
313e41f4b71Sopenharmony_ci    
314e41f4b71Sopenharmony_ci        HDF_LOGE("The controller has been initialized!");       // The controller has been initialized successfully. (The initialization process is not provided here.)
315e41f4b71Sopenharmony_ci    
316e41f4b71Sopenharmony_ci        sampleCntlr->cntlr.priv = (void *)node;
317e41f4b71Sopenharmony_ci        sampleCntlr->cntlr.busId = sampleCntlr->bus;
318e41f4b71Sopenharmony_ci        sampleCntlr->cntlr.ops = &g_method;
319e41f4b71Sopenharmony_ci        (void)OsalSpinInit(&sampleCntlr->spin);                 // Initialize the spin lock.
320e41f4b71Sopenharmony_ci        ret = I2cCntlrAdd(&sampleCntlr->cntlr);                 // Add a controller to the core layer.
321e41f4b71Sopenharmony_ci        if (ret != HDF_SUCCESS) {
322e41f4b71Sopenharmony_ci            HDF_LOGE("%s: add i2c controller fail:%d!", __func__, ret);
323e41f4b71Sopenharmony_ci            goto __ERR__;
324e41f4b71Sopenharmony_ci        }
325e41f4b71Sopenharmony_ci    
326e41f4b71Sopenharmony_ci        return HDF_SUCCESS;
327e41f4b71Sopenharmony_ci    __ERR__:                                                    // Handle errors.
328e41f4b71Sopenharmony_ci        if (sampleCntlr != NULL) {
329e41f4b71Sopenharmony_ci            if (sampleCntlr->regBase != NULL) {
330e41f4b71Sopenharmony_ci                OsalIoUnmap((void *)sampleCntlr->regBase);      // Cancel address mapping.
331e41f4b71Sopenharmony_ci                sampleCntlr->regBase = NULL;
332e41f4b71Sopenharmony_ci            }
333e41f4b71Sopenharmony_ci            OsalMemFree(sampleCntlr);                           // Release the memory.
334e41f4b71Sopenharmony_ci            sampleCntlr = NULL;
335e41f4b71Sopenharmony_ci        }
336e41f4b71Sopenharmony_ci        return ret;
337e41f4b71Sopenharmony_ci    }
338e41f4b71Sopenharmony_ci    /* Driver initialization function */
339e41f4b71Sopenharmony_ci    static int32_t SampleI2cInit(struct HdfDeviceObject *device)
340e41f4b71Sopenharmony_ci    {
341e41f4b71Sopenharmony_ci        int32_t ret;
342e41f4b71Sopenharmony_ci        const struct DeviceResourceNode *childNode = NULL;
343e41f4b71Sopenharmony_ci    
344e41f4b71Sopenharmony_ci        HDF_LOGE("%s: Enter", __func__);
345e41f4b71Sopenharmony_ci        if (device == NULL || device->property == NULL) {
346e41f4b71Sopenharmony_ci            HDF_LOGE("%s: device or property is NULL", __func__);
347e41f4b71Sopenharmony_ci            return HDF_ERR_INVALID_OBJECT;
348e41f4b71Sopenharmony_ci        }
349e41f4b71Sopenharmony_ci    
350e41f4b71Sopenharmony_ci        ret = HDF_SUCCESS;
351e41f4b71Sopenharmony_ci        DEV_RES_NODE_FOR_EACH_CHILD_NODE(device->property, childNode) {
352e41f4b71Sopenharmony_ci            ret = SampleI2cParseAndInit(device, childNode);    // Call the function for parsing parameters and creating a controller.
353e41f4b71Sopenharmony_ci            if (ret != HDF_SUCCESS) {
354e41f4b71Sopenharmony_ci                break;
355e41f4b71Sopenharmony_ci            }
356e41f4b71Sopenharmony_ci        }
357e41f4b71Sopenharmony_ci        return ret;
358e41f4b71Sopenharmony_ci    }
359e41f4b71Sopenharmony_ci    ```
360e41f4b71Sopenharmony_ci
361e41f4b71Sopenharmony_ci3.  Write a driver release function.
362e41f4b71Sopenharmony_ci
363e41f4b71Sopenharmony_ci    This example uses  **SampleI2cRelease**  as the name of the driver release function \(you can name your own function\). This function must be assigned to the  **Release**  function in the driver entry structure so that the HDF can call it to initialize the driver if the driver fails to be initialized via  **Init**. The driver release function must contain operations for releasing the memory and deleting the controller. The sample code is as follows:
364e41f4b71Sopenharmony_ci
365e41f4b71Sopenharmony_ci    ```
366e41f4b71Sopenharmony_ci    /* Function for deleting the controller */
367e41f4b71Sopenharmony_ci    static void SampleI2cRemoveByNode(const struct DeviceResourceNode *node)
368e41f4b71Sopenharmony_ci    {
369e41f4b71Sopenharmony_ci        int32_t ret;
370e41f4b71Sopenharmony_ci        int16_t bus;
371e41f4b71Sopenharmony_ci        struct I2cCntlr *cntlr = NULL;
372e41f4b71Sopenharmony_ci        struct SampleI2cCntlr *sampleCntlr = NULL;
373e41f4b71Sopenharmony_ci        struct DeviceResourceIface *drsOps = NULL;
374e41f4b71Sopenharmony_ci    
375e41f4b71Sopenharmony_ci        drsOps = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE);
376e41f4b71Sopenharmony_ci        if (drsOps == NULL || drsOps->GetUint32 == NULL) {
377e41f4b71Sopenharmony_ci            HDF_LOGE("%s: invalid drs ops fail!", __func__);
378e41f4b71Sopenharmony_ci            return;
379e41f4b71Sopenharmony_ci        }
380e41f4b71Sopenharmony_ci    
381e41f4b71Sopenharmony_ci        ret = drsOps->GetUint16(node, "bus", (uint16_t *)&bus, 0); // Obtain the I2C bus number from the HCS.
382e41f4b71Sopenharmony_ci        if (ret != HDF_SUCCESS) {
383e41f4b71Sopenharmony_ci            HDF_LOGE("%s: read bus fail!", __func__);
384e41f4b71Sopenharmony_ci            return;
385e41f4b71Sopenharmony_ci        }
386e41f4b71Sopenharmony_ci    
387e41f4b71Sopenharmony_ci        cntlr = I2cCntlrGet(bus);
388e41f4b71Sopenharmony_ci        if (cntlr != NULL && cntlr->priv == node) {                // Delete the controller based on the I2C bus number.
389e41f4b71Sopenharmony_ci            I2cCntlrPut(cntlr);
390e41f4b71Sopenharmony_ci            I2cCntlrRemove(cntlr);
391e41f4b71Sopenharmony_ci            sampleCntlr = (struct SampleI2cCntlr *)cntlr;
392e41f4b71Sopenharmony_ci            OsalIoUnmap((void *)sampleCntlr->regBase);
393e41f4b71Sopenharmony_ci            OsalMemFree(sampleCntlr);
394e41f4b71Sopenharmony_ci        }
395e41f4b71Sopenharmony_ci        return;
396e41f4b71Sopenharmony_ci    }
397e41f4b71Sopenharmony_ci    /* Release resources. */
398e41f4b71Sopenharmony_ci    static void SampleI2cRelease(struct HdfDeviceObject *device)
399e41f4b71Sopenharmony_ci    {
400e41f4b71Sopenharmony_ci        const struct DeviceResourceNode *childNode = NULL;
401e41f4b71Sopenharmony_ci    
402e41f4b71Sopenharmony_ci        HDF_LOGI("%s: enter", __func__);
403e41f4b71Sopenharmony_ci    
404e41f4b71Sopenharmony_ci        if (device == NULL || device->property == NULL) {
405e41f4b71Sopenharmony_ci            HDF_LOGE("%s: device or property is NULL", __func__);
406e41f4b71Sopenharmony_ci            return;
407e41f4b71Sopenharmony_ci        }
408e41f4b71Sopenharmony_ci    
409e41f4b71Sopenharmony_ci        DEV_RES_NODE_FOR_EACH_CHILD_NODE(device->property, childNode) {
410e41f4b71Sopenharmony_ci            SampleI2cRemoveByNode(childNode);                       // Delete a controller.
411e41f4b71Sopenharmony_ci        }
412e41f4b71Sopenharmony_ci    }
413e41f4b71Sopenharmony_ci    ```
414e41f4b71Sopenharmony_ci
415e41f4b71Sopenharmony_ci
416e41f4b71Sopenharmony_ci## Building Source Code and Burning Images<a name="section164824754712"></a>
417e41f4b71Sopenharmony_ci
418e41f4b71Sopenharmony_ci1.  Edit the Makefile and add a source file to it, as shown in the following example:
419e41f4b71Sopenharmony_ci
420e41f4b71Sopenharmony_ci    ```
421e41f4b71Sopenharmony_ci    include drivers/hdf/khdf/platform/platform.mk
422e41f4b71Sopenharmony_ci    
423e41f4b71Sopenharmony_ci    obj-y  += $(HDF_PLATFORM_FRAMEWORKS_ROOT)/src/i2c_core.o \
424e41f4b71Sopenharmony_ci              $(HDF_PLATFORM_FRAMEWORKS_ROOT)/src/i2c_if.o \
425e41f4b71Sopenharmony_ci              ./i2c_adapter.o \
426e41f4b71Sopenharmony_ci              ./i2c_sample.o
427e41f4b71Sopenharmony_ci    ```
428e41f4b71Sopenharmony_ci
429e41f4b71Sopenharmony_ci    **./i2c\_sample.o**  is the source file added to the Makefile in this example.
430e41f4b71Sopenharmony_ci
431e41f4b71Sopenharmony_ci2.  Build source code and burn images to the development board.
432e41f4b71Sopenharmony_ci
433e41f4b71Sopenharmony_ci    - For details about the operations using the installation package, see [Building Source Code](../quick-start/quickstart-appendix-hi3516-pkg.md#building-source-code) and [Burning an Image](../quick-start/quickstart-appendix-hi3516-pkg.md#burning-an-image).
434e41f4b71Sopenharmony_ci    
435e41f4b71Sopenharmony_ci    - For details about the operations in IDE mode, see [Building Source Code](../quick-start/quickstart-appendix-hi3516-ide.md#building-source-code) and [Burning an Image](../quick-start/quickstart-appendix-hi3516-ide.md#burning-an-image).
436e41f4b71Sopenharmony_ci
437