Skip to main content

tkl_gipo | GPIO Driver

Brief Description

GPIO (General Purpose Input/Output Ports) are pins that can output high or low levels or read the state of the pin (high or low level).

GPIO Modes

ModeEnumeration
Pull-up InputTUYA_GPIO_PULLUP
Pull-down InputTUYA_GPIO_PULLDOWN
High-impedance InputTUYA_GPIO_HIGH_IMPEDANCE
Floating InputTUYA_GPIO_FLOATING
Push-pull OutputTUYA_GPIO_PUSH_PULL
Open-drain OutputTUYA_GPIO_OPENDRAIN
Open-drain with Pull-up OutputTUYA_GPIO_OPENDRAIN_PULLUP

The above modes depend on whether the chip itself supports them. This is just 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);
  • Function Description:

    • GPIO Initialization
  • Parameters:

    • pin_id: GPIO pin number, this number is different from the original chip pin number, it is numbered sequentially by Tuya according to the number of pins on the chip's PA, PB ... PN:

      NameDefinitionRemarks
      TUYA_GPIO_NUM_0Pin 0Starting number
      TUYA_GPIO_NUM_1Pin 1
      TUYA_GPIO_NUM_3Pin 2
      ...Pin n
      TUYA_GPIO_NUM_60Pin 60Maximum number
    • cfg: GPIO base configuration, values as follows:

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

      mode is defined as:

      NameDefinitionRemarks
      TUYA_GPIO_PULLUPPull-up Input
      TUYA_GPIO_PULLDOWNPull-down Input
      TUYA_GPIO_HIGH_IMPEDANCEHigh-impedance Input
      TUYA_GPIO_FLOATINGFloating Input
      TUYA_GPIO_PUSH_PULLPush-pull Output
      TUYA_GPIO_OPENDRAINOpen-drain Output
      TUYA_GPIO_OPENDRAIN_PULLUPOpen-drain with Pull-up Output

      direct is defined as:

      NameDefinitionRemarks
      TUYA_GPIO_INPUTInput Mode
      TUYA_GPIO_OUTPUTOutput Mode

      level is defined as:

      NameDefinitionRemarks
      TUYA_GPIO_LEVEL_LOWLow Level
      TUYA_GPIO_LEVEL_HIGHHigh Level
  • Return Value:

    • OPRT_OK - Success
    • Others, please refer to the OPRT_OS_ADAPTER_GPIO_ERRCODE section in the file tuya_error_code.h

tkl_gpio_deinit

OPERATE_RET tkl_gpio_deinit(TUYA_GPIO_NUM_E pin_id)
  • Function Description:
    • Restore GPIO to initial state
  • Parameters:
    • pin_id: GPIO pin number
  • Return Value:
    • OPRT_OK - Success
    • Others, please refer to the OPRT_OS_ADAPTER_GPIO_ERRCODE section 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);
  • Function Description:
    • GPIO output level
  • Parameters:
    • pin_id: GPIO pin number
    • level: GPIO output level
  • Return Value:
    • OPRT_OK - Success
    • Others, please refer to the OPRT_OS_ADAPTER_GPIO_ERRCODE section 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);
  • Function Description:
    • GPIO read level
  • Parameters:
    • pin_id: GPIO pin number
    • *level: GPIO read level return value
  • Return Value:
    • OPRT_OK - Success
    • Others, please refer to the OPRT_OS_ADAPTER_GPIO_ERRCODE section 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);
  • Function Description:

    • GPIO interrupt initialization
  • Parameters:

    • pin_id: GPIO pin number

    • *cfg: GPIO interrupt configuration

    • cfg: GPIO base configuration, values as follows:

      typedef struct {
      TUYA_GPIO_IRQ_E mode; // Interrupt mode
      TUYA_GPIO_IRQ_CB cb; // Interrupt callback function
      void *arg; // Callback function argument
      } TUYA_GPIO_IRQ_T;

      mode is defined as:

      NameDefinitionRemarks
      TUYA_GPIO_IRQ_RISERising Edge Mode
      TUYA_GPIO_IRQ_FALLFalling Edge Mode
      TUYA_GPIO_IRQ_RISE_FALLBoth Edges Mode
      TUYA_GPIO_IRQ_LOWLow Level Mode
      TUYA_GPIO_IRQ_HIGHHigh Level Mode

      cb is defined as:

      typedef void (*TUYA_GPIO_IRQ_CB)(void *args);
  • Return Value:

    • OPRT_OK - Success
    • Others, please refer to the OPRT_OS_ADAPTER_GPIO_ERRCODE section in the file tuya_error_code.h

tkl_gpio_irq_enable

OPERATE_RET tkl_gpio_irq_enable(TUYA_GPIO_NUM_E pin_id);
  • Function Description:
    • Enable GPIO interrupt
  • Parameters:
    • pin_id: GPIO pin number
  • Return Value:
    • OPRT_OK - Success
    • Others, please refer to the OPRT_OS_ADAPTER_GPIO_ERRCODE section in the file tuya_error_code.h

tkl_gpio_irq_disable

OPERATE_RET tkl_gpio_irq_disable(TUYA_GPIO_NUM_E pin_id);
  • Function Description:
    • Disable GPIO interrupt
  • Parameters:
    • pin_id: GPIO pin number
  • Return Value:
    • OPRT_OK - Success
    • Others, please refer to the OPRT_OS_ADAPTER_GPIO_ERRCODE section in the file tuya_error_code.h

Examples

Example 1

// GPIO Initialization
void tuya_gpio_test(void)
{
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 Function
static void __gpio_irq_callback7(void *args)
{
//to do
}

static void __gpio_irq_callback8(void *args)
{
//to do
}
// GPIO Interrupt Initialization
void tuya_gpio_irq_test(void)
{
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);
}