GPIO

Last Updated on : 2025-04-25 05:59:57download

Overview

General-purpose input/output (GPIO) ports are pins that can output high or low levels, or read the status of the pins as high or low.

GPIO mode

Mode Enumeration
Pull-up input TUYA_GPIO_PULLUP
Pull-down input TUYA_GPIO_PULLDOWN
High impedance input TUYA_GPIO_HIGH_IMPEDANCE
Floating input TUYA_GPIO_FLOATING
Push-pull output TUYA_GPIO_PUSH_PULL
Open drain output TUYA_GPIO_OPENDRAIN
Open drain output with pull-up TUYA_GPIO_OPENDRAIN_PULLUP

The above modes depend on whether the chip itself supports them. This table only lists the maximum set of modes.

API description

tkl_gpio_init

OPERATE_RET tkl_gpio_init(TUYA_GPIO_NUM_E pin_id, CONST TUYA_GPIO_BASE_CFG_T *cfg);
  • Features:

    • Initialize a GPIO pin.
  • Parameters:

    • pin_id: The ID of the specified GPIO pin. This numbering differs from the chip’s native pin numbers, as Tuya sequentially numbers pins based on the chip’s PA, PB…PN groups.

      Name Definition Remarks
      TUYA_GPIO_NUM_0 Pin 0 Start number
      TUYA_GPIO_NUM_1 Pin 1 -
      TUYA_GPIO_NUM_3 Pin 2 -
      Pin n -
      TUYA_GPIO_NUM_60 Pin 60 Maximum number
    • cfg: Basic GPIO configuration. Valid values are as follows:

      typedef struct {
          TUYA_GPIO_MODE_E  mode;	// GPIO mode
          TUYA_GPIO_DRCT_E  direct;	// GPIO input and output direction
          TUYA_GPIO_LEVEL_E level;	// GPIO initialization level
      } TUYA_GPIO_BASE_CFG_T;
      

      The mode parameter is defined as follows.

      Name Definition
      TUYA_GPIO_PULLUP Pull-up input
      TUYA_GPIO_PULLDOWN Pull-down input
      TUYA_GPIO_HIGH_IMPEDANCE High impedance input
      TUYA_GPIO_FLOATING Floating input
      TUYA_GPIO_PUSH_PULL Push-pull output
      TUYA_GPIO_OPENDRAIN Open drain output
      TUYA_GPIO_OPENDRAIN_PULLUP Open drain output with pull-up

      The direct parameter is defined as follows.

      Name Definition
      TUYA_GPIO_INPUT Input mode
      TUYA_GPIO_OUTPUT Output mode

      The level parameter is defined as follows.

      Name Definition
      TUYA_GPIO_LEVEL_LOW Low level
      TUYA_GPIO_LEVEL_HIGH High level
  • Return value:

    • OPRT_OK: Success.
    • For more information, see the definitions of OPRT_OS_ADAPTER_GPIO_ERRCODE in the file tuya_error_code.h.

tkl_gpio_deinit

OPERATE_RET tkl_gpio_deinit(TUYA_GPIO_NUM_E pin_id)
  • Features:
    • Deinitialize the GPIO pin.
  • Parameters:
    • pin_id: The ID of the specified GPIO pin.
  • Return value:
    • OPRT_OK: Success.
    • For more information, see the definitions of OPRT_OS_ADAPTER_GPIO_ERRCODE in the file tuya_error_code.h.

tkl_gpio_write

OPERATE_RET tkl_gpio_write(TUYA_GPIO_NUM_E pin_id, TUYA_GPIO_LEVEL_E level);
  • Features:
    • The GPIO pin outputs a level.
  • Parameters:
    • pin_id: The ID of the specified GPIO pin.
    • level: The output level of the specified GPIO pin.
  • Return value:
    • OPRT_OK: Success.
    • For more information, see the definitions of OPRT_OS_ADAPTER_GPIO_ERRCODE in the file tuya_error_code.h.

tkl_gpio_read

OPERATE_RET tkl_gpio_read(TUYA_GPIO_NUM_E pin_id, TUYA_GPIO_LEVEL_E *level);
  • Features:
    • The GPIO pin reads a level.
  • Parameters:
    • pin_id: The ID of the specified GPIO pin.
    • *level: The returned level read by the specified GPIO pin.
  • Return value:
    • OPRT_OK: Success.
    • For more information, see the definitions of OPRT_OS_ADAPTER_GPIO_ERRCODE in the file tuya_error_code.h.

tkl_gpio_irq_init

OPERATE_RET tkl_gpio_irq_init(TUYA_GPIO_NUM_E pin_id, CONST TUYA_GPIO_IRQ_T *cfg);
  • Features:

    • Initialize the GPIO interrupt.
  • Parameters:

    • pin_id: The ID of the specified GPIO pin.

    • *cfg: The GPIO interrupt configuration.

    • cfg: Basic GPIO configuration. Valid values are as follows:

      typedef struct {
          TUYA_GPIO_IRQ_E      mode;	// Interrupt mode
          TUYA_GPIO_IRQ_CB     cb;	// Interrupt callback
          VOID_T              *arg;	// Callback parameters
      } TUYA_GPIO_IRQ_T;
      

      The mode parameter is defined as follows.

      Name Definition
      TUYA_GPIO_IRQ_RISE Rising edge mode
      TUYA_GPIO_IRQ_FALL Falling edge mode
      TUYA_GPIO_IRQ_RISE_FALL Dual-edge mode
      TUYA_GPIO_IRQ_LOW Low level mode
      TUYA_GPIO_IRQ_HIGH High level mode

      The cb parameter is defined as follows.

      typedef VOID_T (*TUYA_GPIO_IRQ_CB)(VOID_T *args);
      
  • Return value:

    • OPRT_OK: Success.
    • For more information, see the definitions of OPRT_OS_ADAPTER_GPIO_ERRCODE in the file tuya_error_code.h.

tkl_gpio_irq_enable

OPERATE_RET tkl_gpio_irq_enable(TUYA_GPIO_NUM_E pin_id);
  • Features:
    • Enable GPIO interrupt.
  • Parameters:
    • pin_id: The ID of the specified GPIO pin.
  • Return value:
    • OPRT_OK: Success.
    • For more information, see the definitions of OPRT_OS_ADAPTER_GPIO_ERRCODE in the file tuya_error_code.h.

tkl_gpio_irq_disable

OPERATE_RET tkl_gpio_irq_disable(TUYA_GPIO_NUM_E pin_id);
  • Features:
    • Disable GPIO interrupt.
  • Parameters:
    • pin_id: The ID of the specified GPIO pin.
  • Return value:
    • OPRT_OK: Success.
    • For more information, see the definitions of OPRT_OS_ADAPTER_GPIO_ERRCODE in the file tuya_error_code.h.

Example

Example 1

// GPIO initialization
VOID_T tuya_gpio_test(VOID_T)
{
    TUYA_GPIO_BASE_CFG_T cfg = {
        .mode = TUYA_GPIO_PUSH_PULL,
        .direct = TUYA_GPIO_OUTPUT,
        .level = TUYA_GPIO_LEVEL_LOW,
    };
	tkl_gpio_init(GPIO_NUM_3, &cfg);
    tkl_gpio_init(GPIO_NUM_4, &cfg);

    // GPIO output
    tkl_gpio_write(GPIO_NUM_3, TUYA_GPIO_LEVEL_HIGH);
    tkl_gpio_write(GPIO_NUM_4, TUYA_GPIO_LEVEL_HIGH);
}

Example 2

// Interrupt callback
STATIC VOID __gpio_irq_callback7(VOID *args)
{
    //To do
}

STATIC VOID __gpio_irq_callback8(VOID *args)
{
     //To do
}
// Initialize GPIO interrupt
VOID_T tuya_gpio_irq_test(VOID_T)
{
    TUYA_GPIO_IRQ_T irq_cfg_7 = {
        .mode = TUYA_GPIO_IRQ_RISE,
        .cb = __gpio_irq_callback7,
        .arg = NULL,
    };
    TUYA_GPIO_IRQ_T irq_cfg_8 = {
        .mode = TUYA_GPIO_IRQ_RISE,
        .cb = __gpio_irq_callback8,
        .arg = NULL,
    };
    tkl_gpio_irq_init(GPIO_NUM_7, &irq_cfg_7);
    tkl_gpio_irq_init(GPIO_NUM_8, &irq_cfg_8);
    tKl_gpio_irq_enable(GPIO_NUM_7);
    tKl_gpio_irq_enable(GPIO_NUM_8);
}