• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# HDF Driver Coding Guide
2
3## About This Document
4
5### Purpose
6
7OpenHarmony aims to build an open, distributed OS framework for smart IoT devices in the full-scenario, full-connectivity, and full-intelligence era. It has the following technical features: hardware collaboration for resource sharing, one-time development for multi-device deployment, and a unified OS for flexible deployment.
8
9The Hardware Driver Foundation (HDF) provides the following driver framework capabilities: driver loading, driver service management, and driver message mechanism. This unified driver architecture system is designed to provide a more precise and efficient development environment, where you can perform one-time driver development for multi-system deployment.
10
11As such, certain coding specifications are required for the OpenHarmony driver implemented based on the HDF. This document stipulates the specifications on the driver code, helping you improve code standardization and portability.
12
13## Coding Guide
14
15### General Principles
16
17#### [Rule] Use the capabilities provided by the HDF to implement drivers.
18
19[Description] The HDF supports driver loading, driver service management, and driver message mechanism. It provides the Operating System Abstraction Layer (OSAL) and Platform Abstraction Layer (PAL) to support cross-system and cross-platform driver deployment. It also provides capabilities such as driver model abstraction, common tools, and peripheral component framework. You should develop drivers based on these capabilities to ensure that the drivers can be deployed on various devices powered by OpenHarmony.
20
21#### [Rule] Follow this coding guide to develop drivers that can run in both the kernel space and user space.
22
23[Description] Kernel-mode drivers are different from user-mode drivers in essence. They apply to different use cases. You must follow this guide during service design and development and use the HDF OSAL and PAL to shield the differences, so as to ensure that the drivers can run in both the kernel space and user space.
24
25#### [Rec] Include the drivers/framework/include directory instead of a subdirectory in the build script.
26
27[Description] The **drivers/framework/include** directory is the root directory of the header file exposed by the HDF externally. This directory contains multiple subdirectories to represent different modules such as the core framework, OSAL, and PAL. When using a header file, you are advised to include the **drivers/framework/include** directory in the build script. This avoids repeated inclusion when the script is referenced in the code.
28
29[Example]
30
31```gn
32config("xxxx_private_config") {
33  include_dirs = [
34    "//drivers/framework/include",
35    "//drivers/framework/include/core", # Not recommended.
36  ]
37}
38```
39
40```c
41#include <core/hdf_device_desc.h>
42#include <hdf_device_desc.h> // Not recommended.
43```
44
45### HDF Core Framework
46
47#### [Rule] Implement the Bind, Init, and Release methods based on the responsibility definitions in the HdfDriverEntry object.
48
49[Description] The **HdfDriverEntry** object is the entry of an HDF driver. The **Bind**, **Init**, and **Release** methods have their own responsibilities. You must implement the corresponding functions based on the responsibilities.
50
51```c
52struct HdfDriverEntry g_sampleDriverEntry = {
53    .moduleVersion = 1,
54    .moduleName = "sample_driver",
55    .Bind = SampleDriverBind, // Responsibility: Bind the service interface provided by the driver to the HDF.
56    .Init = SampleDriverInit, // Responsibility: Initialize the driver service.
57    .Release = SampleDriverRelease, // Responsibility: Release driver resources. It is invoked when an exception occurs.
58};
59
60HDF_INIT(g_sampleDriverEntry);
61```
62
63#### [Rule] The first member in the driver service structure must be of the IDeviceIoService type.
64
65[Description] The first member of the service interface defined by the driver must be of the **IDeviceIoService** type.
66
67[Example]
68
69```c
70struct ISampleDriverService {
71    struct IDeviceIoService ioService; // The first member must be of the IDeviceIoService type.
72    int32_t (*FunctionA)(void); // The first service interface of the driver.
73    int32_t (*FunctionB)(uint32_t inputCode); // The second service interface of the driver. More service interfaces can be added here.
74};
75```
76
77[Example]
78
79```c
80struct ISampleDriverService {
81    struct IDeviceIoService ioService; // The first member must be of the IDeviceIoService type.
82    void *instance; // A service instance can be encapsulated here to provide service interfaces.
83};
84```
85
86#### [Rule] All driver service interfaces must be bound using the Bind method of the HdfDriverEntry object. All service interfaces must be defined. They cannot be defined as null.
87
88[Description] The service interfaces defined by the driver are exposed externally. If a service interface is not defined or is defined as null, exceptions may occur during external invocation.
89
90[Example]
91
92```c
93int32_t SampleDriverBind(struct HdfDeviceObject *deviceObject)
94{
95    static struct ISampleDriverService sampleDriver = {
96        .FunctionA = SampleDriverServiceA,
97        .FunctionB = NULL, // The service interface cannot be defined as null.
98    };
99    // Bind ioService to the device object created by the HDF.
100    deviceObject->service = &sampleDriver.ioService;
101    return HDF_SUCCESS;
102}
103```
104
105#### [Rec] Call the HdfDeviceSetClass interface in the Init method of the HdfDriverEntry object to define the driver type.
106
107[Description] Based on the driver type, you can classify the drivers of the current device and query the driver capabilities of the current device. For better driver management, you are advised to call **HdfDeviceSetClass** to set the driver type.
108
109[Example]
110
111```c
112int32_t SampleDriverInit(struct HdfDeviceObject *deviceObject)
113{
114    // Set the driver type to DISPLAY.
115    if (!HdfDeviceSetClass(deviceObject, DEVICE_CLASS_DISPLAY)) {
116        HDF_LOGE("HdfDeviceSetClass failed");
117        return HDF_FAILURE;
118    }
119    return HDF_SUCCESS;
120}
121```
122
123### HCS
124
125HDF Configuration Source (HCS) describes the configuration source code of the HDF in the form of key-value pairs. It decouples configuration code from driver code, making it easy for you to manage the driver configuration.
126
127The driver configuration consists of the driver device description defined by the HDF and the private configuration of a driver.
128
129**Driver Device Description**
130
131The driver loading information required by the HDF comes from the driver device description. Therefore, you must add the driver device description to the **device_info.hcs** file defined by the HDF.
132
133#### [Rule] Before configuring a driver, determine the hardware to which the driver belongs and the deployment mode, and plan the directories and files to be configured.
134
135[Description] In the **vendor** directory of the OpenHarmony source code, plan the directories based on the chip vendor, development board, and configuration. The HDF driver configuration is stored in the **hdf\_config** directory. According to the hardware specifications, the **hdf\_config** directory stores kernel-mode configuration information or both kernel- and user-mode configuration information. You should determine the directory where the driver is to be configured based on the driver hardware and deployment mode.
136
137[Example]
138
139```bash
140$openharmony_src_root/vendor/hisilicon/hispark_taurus/hdf_config # Directory for storing the kernel-mode configuration file. There are no user-mode configuration files.
141
142$openharmony_src_root/vendor/hisilicon/hispark_taurus_standard/hdf_config/khdf # Directory for storing the kernel-mode configuration file.
143$openharmony_src_root/vendor/hisilicon/hispark_taurus_standard/hdf_config/uhdf # Directory for storing the user-mode configuration file.
144$openharmony_src_root/vendor/hisilicon/hispark_taurus_standard/hdf_config/khdf/device_info/device_info.hcs # Device description file of the kernel-mode driver.
145$openharmony_src_root/vendor/hisilicon/hispark_taurus_standard/hdf_config/khdf/lcd/lcd_config.hcs # Private configuration file of the kernel-mode driver.
146```
147
148#### [Rule] Use existing configuration information and inherit existing configuration templates during driver configuration.
149
150[Description] The **host**, **device**, and **deviceNode** templates have been configured in the **device_info.hcs** file. When configuring a driver, make full use of the existing configuration information and inherit HCS features to minimize your configuration workload.
151
152[Example]
153
154```
155root {
156    device_info {
157        match_attr = "hdf_manager";
158        template host { // host template
159            hostName = "";
160            priority = 100; // Host startup priority. The value ranges from 0 to 200. A larger value indicates a lower priority. The default value 100 is recommended. If the priorities are the same, the host loading sequence cannot be ensured.
161            template device { // device template
162                template deviceNode { // deviceNode template
163                    policy = 0; // Policy for publishing drive services.
164                    priority = 100; // Driver startup priority. The value ranges from 0 to 200. A larger value indicates a lower priority. The default value 100 is recommended. If the priorities are the same, the device loading sequence cannot be ensured.
165                    preload = 0; // The driver is loaded as required.
166                    permission = 0664; // Permission for the driver to create a device node.
167                    moduleName = "";
168                    serviceName = "";
169                    deviceMatchAttr = "";
170                }
171            }
172        }
173        // To use the default values in the template, the node fields can be not included.
174        sample_host :: host { // sample_host inherits the host template.
175            hostName = "host0"; // Host name. The host node is a container used to store a type of drivers.
176            device_sample :: device { // device_sample inherits the device template.
177                device0 :: deviceNode { // device0 inherits the deviceNode template.
178                    policy = 1; // Overwrite the policy in the template.
179                    moduleName = "sample_driver"; // Driver name. The value of this field must be the same as that of moduleName in the HdfDriverEntry structure.
180                    serviceName = "sample_service"; // Service name of the driver, which must be unique.
181                    deviceMatchAttr = "sample_config"; // Keyword for matching the private data of the driver. The value must be the same as that of match_attr in the private data configuration table of the driver.
182                }
183            }
184        }
185    }
186}
187```
188
189#### [Rule] Use the defined types during driver model design and classification. Do not configure hosts and devices repeatedly.
190
191[Description] The HDF places the same type of devices in the same host. You can develop and deploy the driver functionalities of the host by layer, so that one driver has multiple nodes. The following figure shows the HDF driver model.
192
193![hdf-driver-model.png]( ../device-dev/driver/figures/hdf-driver-model.png)
194
195Place devices of the same type in the same host. When adding a device, check whether the host of the same type already exists. If such a host already exists, configure the device in the host. Do not configure the same host again. A device belongs to only one driver. Therefore, do not configure the same device in different hosts.
196
197#### [Rule] Set publication policies for driver services based on service rules.
198
199[Description] The driver service is the object of capabilities provided by the driver to external systems and is managed by the HDF in a unified manner. The HDF uses the **policy** field in the configuration file to define policies for drivers to publish services externally. The values and meanings of this field are as follows:
200
201```c
202typedef enum {
203    /* The driver does not provide services.*/
204    SERVICE_POLICY_NONE = 0,
205    /* The driver provides services for kernel-space applications. */
206    SERVICE_POLICY_PUBLIC = 1,
207    /* The driver provides services for both kernel- and user-space applications. */
208    SERVICE_POLICY_CAPACITY = 2,
209    /** Driver services are not published externally but can be subscribed to. */
210    SERVICE_POLICY_FRIENDLY = 3,
211    /* Driver services are not published externally and cannot be subscribed to. */
212    SERVICE_POLICY_PRIVATE = 4,
213    /** Invalid policy. */
214    SERVICE_POLICY_INVALID
215} ServicePolicy;
216```
217
218You must set the policies based on service rules. Do not set unnecessary policies, for example, setting user-mode publication policies for kernel-mode drivers.
219
220[Example]
221
222```
223root {
224    device_info {
225        sample_host {
226            sample_device {
227                device0 {
228                    policy = 1; // The driver provides services for kernel-space applications.
229                    ...
230                }
231            }
232        }
233    }
234}
235```
236
237#### [Rule] The permission to create device nodes for a driver must match the publication policy of the driver.
238
239[Description] In the **device_info.hcs** file, the **permission** field specifies the permission used by the driver to create a device node. This field is a 4-digit octal number and uses the Unix file permissions, for example, 0644. This field takes effect only when the driver provides services for user-space applications (policy = 2).
240
241You must ensure that the publication policy of the driver service matches the device node permission. Otherwise, access to the driver service may fail or the permission of the device node may be inappropriate.
242
243[Example]
244
245```
246root {
247    device_info {
248        sample_host {
249            sample_device {
250                device0 {
251                    policy = 2; // The driver provides services for both kernel- and user-space applications.
252                    permission = 0640; // Recommended value
253                    ...
254                }
255            }
256        }
257    }
258}
259```
260
261[Counterexample]
262
263```
264root {
265    device_info {
266        sample_host {
267            sample_device {
268                device0 {
269                    policy = 2; // The driver provides services for both kernel- and user-space applications.
270                    permission = 0777; // Excessive permission.
271                    ...
272                }
273            }
274        }
275    }
276}
277```
278
279[Counterexample]
280
281```
282root {
283    device_info {
284        sample_host {
285            sample_device {
286                device0 {
287                    policy = 1; // The driver provides services for kernel-space applications but does not create a device node.
288                    permission = 0640; // Redundancy configuration
289                    ...
290                }
291            }
292        }
293    }
294}
295```
296
297#### [Rule] Configure whether to load a driver as required based on service requirements.
298
299[Description] In the **device_info.hcs**, **preload** specifies the driver loading mode. The values and meanings of this field are as follows:
300
301```c
302typedef enum {
303    /* The driver is loaded by default during the system boot process. */
304    DEVICE_PRELOAD_ENABLE = 0,
305    /* The driver is loaded after a quick start is complete if the system supports quick start. If the system does not support quick start, this value has the same meaning as DEVICE\_PRELOAD\_ENABLE. */
306    DEVICE_PRELOAD_ENABLE_STEP2,
307    /* The driver is not loaded during the system boot process. When a user-mode process requests the driver service, the HDF attempts to dynamically load the driver if the driver service does not exist. */
308    DEVICE_PRELOAD_DISABLE,
309    /** Invalid value. */
310    DEVICE_PRELOAD_INVALID
311} DevicePreload;
312```
313
314Set the **preload** field based on the service requirements.
315
316[Example]
317
318```
319root {
320    device_info {
321        sample_host {
322            sample_device {
323                device0 {
324                    preload = 2; // The driver is loaded as required.
325                    ...
326                }
327            }
328        }
329    }
330}
331```
332
333#### [Rec] When the preload field is set to 0, configure the loading priority based on the service requirements.
334
335[Description] In the **device_info.hcs** file, the **priority** field indicates the host and driver loading priority. The value of this field is an integer ranging from 0 to 200. For drivers in different hosts, a smaller priority value of the host indicates a higher driver loading priority. For drivers in the same host, a smaller priority value of the driver indicates a higher driver loading priority. The default value of the **priority** field is 100. If this field is not set or set to the same value for different drivers, the driver loading sequence cannot be ensured. You should configure the **priority** field based on the service requirements to ensure the driver loading sequence.
336
337[Example]
338
339```
340root {
341    device_info {
342        sample_host0 {
343        priority = 100;
344            sample_device {
345                device0 {
346                    preload = 0; // The driver is loaded by default.
347                    priority = 100; // The HDF ensures that the driver is loaded before device1.
348                    ...
349                }
350                device1 {
351                    preload = 0; // The driver is loaded by default.
352                    priority = 200; // The HDF ensures that the driver is loaded after device0.
353                    ...
354                }
355            }
356        }
357        sample_host1 {
358            priority = 100; // The HDF does not ensure the loading sequence because this host has the same priority as sample_host0.
359            ...
360        }
361    }
362}
363```
364
365**Private Configuration Information of the Driver**
366
367If a driver has private configurations, you can add a driver configuration file to fill in default configurations of the driver. When loading the driver, the HDF obtains the configuration information, saves it in the **property** field of **HdfDeviceObject**, and passes it to the driver through **Bind** and **Init**.
368
369#### [Rule] Store the private configuration files of the drivers in different directories according to the component type or module.
370
371[Description] You must properly plan the directory for storing private configuration files of drivers. Do not store them in the root directory.
372
373[Example]
374
375```bash
376$openharmony_src_root/vendor/hisilicon/hispark_taurus_standard/hdf_config/khdf/sample/sample_config.hcs # Correct. The private configuration file is stored in the sample directory.
377
378$openharmony_src_root/vendor/hisilicon/hispark_taurus_standard/hdf_config/khdf/sample_config.hcs # Incorrect. The private configuration file is placed in the root directory.
379```
380
381#### [Rule] Add the private configuration file of a driver to the hdf.hcs file in the hdf_config directory.
382
383[Description] The **hdf.hcs** file summarizes the configuration information. The HDF parses the file content and loads the private configuration information of the driver to the device node during the build and runtime. Include the private configuration file of the driver in the **hdf.hcs** file to trigger driver initialization.
384
385[Example]
386
387```c
388#include "device_info/device_info.hcs"
389#include "sample/sample_config.hcs" // The file contains the private configuration file of a driver.
390
391root {
392    module = "hisilicon,hi35xx_chip";
393}
394```
395
396#### [Rule] The value of the matchAttr field in the private configuration file of the driver must be the same as that of the deviceMatchAttr field in device_info.hcs.
397
398[Description] The HDF associates with the driver device through the **match_attr** field. A mismatch causes a failure to obtain the private configuration information.
399
400[Example]
401
402```
403root {
404    sample_config {
405        ...
406        match_attr = "sample_config"; // The value of this field must be the same as that of deviceMatchAttr in device_info.hcs.
407    }
408}
409```
410
411#### [Rule] Use underscores (_) in field names in the private configuration file.
412
413[Description] According to the naming rules in the C/C++ coding guide, use underscores (_) in field names in the private configuration file of a driver. In this way, the naming rule is satisfied during the definition of the private configuration data structure in the implementation code. It also makes unified management of the code and configuration files easier.
414
415[Example]
416
417```
418root {
419    sample_config {
420        sample_version = 1; // Use an underscore (_) in the field name.
421        sample_bus = "I2C_0";
422        match_attr = "sample_config";
423    }
424}
425```
426
427### HCS Macros
428
429The private configuration information of a driver is loaded to **property** of **HdfDeviceObject**. This occupies memory space, which should be avoided for mini- and small-system devices. To minimize the memory usage, the HDF provides HCS macros to parse the private configuration information.
430
431#### [Rule] Use HCS macros to parse the private configuration information in memory-sensitive or cross-system scenario.
432
433[Description] You should specify the use cases of drivers. In memory-sensitive scenarios or if a driver needs to be used in mini-, small-, and standard-system devices, use HCS macros to parse the private configuration information for higher performance and portability.
434
435[Example]
436
437```c
438#include <utils/hcs_macro.h>
439
440#define SAMPLE_CONFIG_NODE HCS_NODE(HCS_ROOT, sample_config)
441
442ASSERT_EQ(HCS_PROP(SAMPLE_CONFIG_NODE, sampleVersion), 1);
443ASSERT_EQ(HCS_PROP(SAMPLE_CONFIG_NODE, sample_bus), "I2C_0");
444ASSERT_EQ(HCS_PROP(SAMPLE_CONFIG_NODE, match_attr), "sample_config");
445```
446
447### HDF Tools
448
449#### [Rule] Determine the communication scenario and HdfSbuf type.
450
451[Description] **HdfSbuf** is a data structure used for data transfer. This structure is classified into different types based on the communication scenario. These types are defined in the **hdf_sbuf.h** header file.
452
453```c
454enum HdfSbufType {
455    SBUF_RAW = 0,   /* SBUF used for communication between the user space and the kernel space. */
456    SBUF_IPC,      /* SBUF used for inter-process communication (IPC). */
457    SBUF_IPC_HW,    /* Reserved for extension. */
458    SBUF_TYPE_MAX,  /* Maximum value of the SBUF type. */
459};
460```
461
462Determine whether the data transfer is IPC in the user space or between the user space and kernel space, and then create the corresponding **HdfSbuf** structure.
463
464[Example]
465
466```c
467void SampleDispatchBetweenUserAndKernel()
468{
469    int32_t ret;
470    /* Communication between the user space and kernel space. */
471    struct HdfSBuf *data = HdfSBufTypedObtain(SBUF_RAW);
472    ...
473    ret = sample->dispatcher->Dispatch(&sample->object, CMD_SAMPLE_DISPATCH, data, NULL);
474    ...
475    HdfSBufRecycle(data);
476}
477```
478
479[Example]
480
481```c++
482void SampleDispatchIpc()
483{
484    /* IPC */
485    struct HdfSBuf *data = HdfSBufTypedObtain(SBUF_IPC);
486    ...
487    int ret = sample->dispatcher->Dispatch(sample, CMD_SAMPLE_DISPATCH, data, nullptr);
488    ...
489    HdfSBufRecycle(data);
490}
491```
492
493#### [Rule] Define the HDF_LOG_TAG macro when using HDF logging.
494
495[Description] The HDF provides the **hdf_log.h** tool ,using which you can output driver run logs. The **HDF_LOG_TAG** macro specifies the log tag. You must define this macro before printing logs.
496
497[Example]
498
499```c
500#include <hdf_log.h>
501
502#define HDF_LOG_TAG sample_driver // Define the log tag.
503
504int32_t SampleDriverInit(struct HdfDeviceObject *deviceObject)
505{
506    HDF_LOGI("sample driver is initialized"); // Use the tool to print logs.
507    return HDF_SUCCESS;
508}
509```
510
511#### [Rule] Verify the return values of the HDF methods and use the error codes provided by the HDF.
512
513[Description] The HDF methods have specific return values. You should verify them rather than ignoring them. The return values correspond to error codes in the **hdf_base.h** header file. Use the error codes provided by the HDF when using the HDF or implementing custom methods.
514
515[Example]
516
517```c
518int32_t SampleDriverInit(struct HdfDeviceObject *deviceObject)
519{
520    int32_t ret;
521    // Check whether the device type is successfully set.
522    if (!HdfDeviceSetClass(deviceObject, DEVICE_CLASS_DISPLAY)) {
523        HDF_LOGE("HdfDeviceSetClass failed");
524        return HDF_FAILURE;
525    }
526    ret = InitDiver();
527    // A custom method uses an error code provided by the HDF.
528    if (ret != HDF_SUCCESS) {
529        HDF_LOGE("init driver is failed");
530        return ret;
531    }
532    return HDF_SUCCESS;
533}
534```
535
536### OSAL
537
538The HDF OSAL shields the interface differences between OpenHarmony subsystems and provides unified OS interfaces, including memory management, threads, mutexes, spin locks, semaphores, timers, files, IRQ, time, atoms, firmware, and I/O operation modules.
539
540#### [Rule] Use OS interfaces through the OSAL for drivers used across mini-, small-, and standard-system devices.
541
542[Description] The OSAL shields the differences between OS interfaces. You should operate these OS interfaces based on the OSAL to ensure that drivers can run on different types of systems.
543
544[Example]
545
546```c
547#include <osal/osal_mem.h>
548#include <util/hdf_log.h>
549
550struct DevHandle *SampleInit(void)
551{
552    struct DevHandle *handle = (struct DevHandle *)OsalMemCalloc(sizeof(struct DevHandle));
553    if (handle == NULL) {
554        HDF_LOGE("OsalMemCalloc handle failed");
555        return NULL;
556    }
557    return handle;
558}
559```
560
561[Example]
562
563```c
564#include <osal/osal_time.h>
565
566void SampleSleep(uint32_t timeMs)
567{
568    OsalMSleep(timeMs);
569}
570```
571
572### PAL
573
574The HDF PAL abstracts platform drivers and provides unified operation interfaces for modules such as the GPIO, I2C, SPI, UART, RTC, SDIO, eMMC, DSI, PWM, and watchdog.
575
576#### [Rule] Use platform drivers across mini, small, and standard systems through the PAL.
577
578[Description] The PAL masks the differences between platform driver interfaces of different system types. You should operate these interfaces based on PAL to ensure that drivers can run on different types of systems.
579
580[Example]
581
582```c
583#include <platform/gpio_if.h>
584#include <util/hdf_log.h>
585#include <osal/osal_irq.h>
586#include <osal/osal_time.h>
587
588static uint32_t g_irqCnt;
589
590/* Sample function of the GPIO IRQ service */
591static int32_t SampleGpioIrqHandler(uint16_t gpio, void *data)
592{
593    HDF_LOGE("%s: irq triggered, on gpio:%u, data=%p", __func__, gpio, data);
594    g_irqCnt++; /* If the IRQ function is triggered, the number of global counters is incremented by 1. */
595    return GpioDisableIrq(gpio);
596}
597
598/* GPIO sample function */
599static int32_t SampleGpioIrqEdge(void)
600{
601    int32_t ret;
602    uint16_t valRead;
603    uint16_t mode;
604    uint16_t gpio = 83; // Number of the GPIO pin to test
605    uint32_t timeout;
606
607    /* Set the output direction for the pin. */
608    ret = GpioSetDir(gpio, GPIO_DIR_OUT);
609    ...
610    /* Disable the IRP of this pin. */
611    ret = GpioDisableIrq(gpio);
612    ...
613    /* Set the IRR function for the pin. The trigger mode is both rising edge and falling edge. */
614    mode = OSAL_IRQF_TRIGGER_RISING | OSAL_IRQF_TRIGGER_FALLING;
615    ret = GpioSetIrq(gpio, mode, SampleGpioIrqHandler, NULL);
616    ...
617    /* Enable the IRQ for this pin. */
618    ret = GpioEnableIrq(gpio);
619    ...
620    g_irqCnt = 0; /* Reset the global counter. */
621    timeout = 0;  /* Reset the waiting time. */
622    /* Wait for the IRQ function of this pin to trigger. The timeout duration is 1000 ms. */
623    while (g_irqCnt <= 0 && timeout < 1000) {
624        ret = GpioRead(gpio, &valRead);
625        ...
626        ret = GpioWrite(gpio, (valRead == GPIO_VAL_LOW) ? GPIO_VAL_HIGH : GPIO_VAL_LOW);
627        ...
628        OsalMDelay(200); // Wait for an interrupt to be triggered.
629        timeout += 200;
630    }
631    ret = GpioUnSetIrq(gpio);
632    ...
633    return (g_irqCnt > 0) ? HDF_SUCCESS : HDF_FAILURE;
634}
635```
636