1 Star 0 Fork 4.9K

fish / docs

forked from OpenHarmony / docs 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
gpiousage-guidelines.md 21.22 KB
一键复制 编辑 原始数据 按行查看 历史
NEEN 提交于 2021-03-12 17:59 . !197 Docs Update version 1.0.1

GPIO Usage Guidelines

How to Use

The GPIO APIs use the GPIO pin number to specify a pin. Figure 1 shows the general process of using a GPIO.

Figure 1 Process of using a GPIO

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.

    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.

    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;
    }
1
https://gitee.com/fish_neil/docs.git
git@gitee.com:fish_neil/docs.git
fish_neil
docs
docs
master

搜索帮助