# GPIO Usage Guidelines - [How to Use](#section1583613406410) - [Determining a GPIO Pin Number](#section135943361443) - [Using APIs to Operate GPIO Pins](#section69151114115315) ## How to Use The GPIO APIs use the GPIO pin number to specify a pin. [Figure 1](#fig1399416053717) shows the general process of using a GPIO. **Figure 1** Process of using a GPIO ![](figures/en-us_image_0000001057342245.png) ## Determining a GPIO Pin Number The method for converting GPIO pin numbers varies according to the GPIO controller model, parameters, and controller driver of different system on chips \(SoCs\). - Hi3516D V300 A controller manages 12 groups of GPIO pins. Each group contains 8 GPIO pins. GPIO pin number = GPIO group index \(0-11\) x Number of GPIO pins in each group \(8\) + Offset in the group Example: GPIO number of GPIO10\_3 = 10 x 8 + 3 = 83 - Hi3518E V300 A controller manages 10 groups of GPIO pins. Each group contains 10 GPIO pins. GPIO pin number = GPIO group index \(0–9\) x Number of GPIO pins in each group \(10\) + Offset in the group Example: GPIO pin number of GPIO7\_3 = 7 x 10 + 3 = 73 ## Using APIs to Operate GPIO Pins - Set the direction for a GPIO pin. Before performing read/write operations on a GPIO pin, call the following function to set the direction: int32\_t GpioSetDir\(uint16\_t gpio, uint16\_t dir\); **Table 1** Description of GpioSetDir

Parameter

Description

gpio

GPIO pin number.

dir

Direction to set.

Return Value

Description

0

The setting is successful.

Negative value

The setting failed.

- Read or write the level value for a GPIO pin. To read the level value of a GPIO pin, call the following function: int32\_t GpioRead\(uint16\_t gpio, uint16\_t \*val\); **Table 2** Description of GpioRead

Parameter

Description

gpio

GPIO pin number.

val

Pointer to the level value.

Return Value

Description

0

Succeeded in reading the level value.

Negative value

Failed to read the level value.

To write the level value for a GPIO pin, call the following function: int32\_t GpioWrite\(uint16\_t gpio, uint16\_t val\); **Table 3** Description of GpioWrite

Parameter

Description

gpio

GPIO pin number.

val

Level value to write.

Return Value

Description

0

Succeeded in writing the level value.

Negative value

Failed to write the level value.

Example: ``` int32_t ret; uint16_t val; /* Set the output direction for GPIO3. */ ret = GpioSetDir(3, GPIO_DIR_OUT); if (ret != 0) { HDF_LOGE("GpioSerDir: failed, ret %d\n", ret); return; } /* Write the low level GPIO_VAL_LOW for GPIO3. */ ret = GpioWrite(3, GPIO_VAL_LOW); if (ret != 0) { HDF_LOGE("GpioWrite: failed, ret %d\n", ret); return; } /* Set the input direction for GPIO6. */ ret = GpioSetDir(6, GPIO_DIR_IN); if (ret != 0) { HDF_LOGE("GpioSetDir: failed, ret %d\n", ret); return; } /* Read the level value of GPIO6. */ ret = GpioRead(6, &val); ``` - Set the ISR function for a GPIO pin. To set the ISR function for a GPIO pin, call the following function: int32\_t GpioSetIrq\(uint16\_t gpio, uint16\_t mode, GpioIrqFunc func, void \*arg\); **Table 4** Description of GpioSetIrq

Parameter

Description

gpio

GPIO pin number.

mode

Interrupt trigger mode.

func

ISR function to set.

arg

Pointer to the parameters passed to the ISR function.

Return Value

Description

0

The setting is successful.

Negative value

The setting failed.

>![](public_sys-resources/icon-caution.gif) **CAUTION:** >Only one ISR function can be set for a GPIO pin at a time. If **GpioSetIrq** is called repeatedly, the previous IRS function will be replaced. If the ISR function is no longer required, call the following function to cancel the setting: int32\_t GpioUnSetIrq\(uint16\_t gpio\); **Table 5** Description of GpioUnSetIrq

Parameter

Description

gpio

GPIO pin number.

Return Value

Description

0

The ISR function is canceled.

Negative value

Failed to cancel the ISR function.

After the ISR function is set, call the following function to enable a GPIO interrupt: int32\_t GpioEnableIrq\(uint16\_t gpio\); **Table 6** Description of GpioEnableIrq

Parameter

Description

gpio

GPIO pin number.

Return Value

Description

0

The GPIO interrupt is enabled.

Negative value

Failed to enable a GPIO interrupt.

>![](public_sys-resources/icon-caution.gif) **CAUTION:** >The configured ISR function can be responded only after the ISR function is enabled. Use the following function to disable the GPIO interrupt: int32\_t GpioDisableIrq\(uint16\_t gpio\); **Table 7** Description of GpioDisableIrq

Parameter

Description

gpio

GPIO pin number.

Return Value

Description

0

The GPIO interrupt is disabled.

Negative value

Failed to disable the GPIO interrupt.

Example: ``` /* ISR function */ */ int32_t MyCallBackFunc(uint16_t gpio, void *data) { HDF_LOGI("%s: gpio:%u interrupt service in! data=%p\n", __func__, gpio, data); return 0; } int32_t ret; /* Set the ISR function to MyCallBackFunc, the parameter to NULL, and the interrupt trigger mode to rising edge. */ ret = GpioSetIrq(3, OSAL_IRQF_TRIGGER_RISING, MyCallBackFunc, NULL); if (ret != 0) { HDF_LOGE("GpioSetIrq: failed, ret %d\n", ret); return; } /* Enable an interrupt for GPIO3. */ ret = GpioEnableIrq(3); if (ret != 0) { HDF_LOGE("GpioEnableIrq: failed, ret %d\n", ret); return; } /* Disable the interrupt for GPIO3. */ ret = GpioDisableIrq(3); if (ret != 0) { HDF_LOGE("GpioDisableIrq: failed, ret %d\n", ret); return; } /* Cancel the ISR function for GPIO3. */ ret = GpioUnSetIrq(3); if (ret != 0) { HDF_LOGE("GpioUnSetIrq: failed, ret %d\n", ret); return; } ```