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