Pin

Last Updated on : 2024-11-20 08:51:36download

API list

Function name Description
tuya_pin_init Initialize pin modes.
tuya_pin_write Set the pin level.
tuya_pin_read Read the pin level.
tuya_pin_irq_init Initialize the pin interrupt.
tuya_pin_irq_enable Enable the pin interrupt.
tuya_pin_irq_disable Disable the pin interrupt.
tuya_pin_control Control pins.

APIs

tuya_pin_init

Function prototype

int tuya_pin_init(tuya_pin_name_t pin, tuya_pin_mode_t  mode);

Parameters

Parameter name Parameter type Description
pin tuya_pin_name_t The values in the tuya_pin_name_t enum.
mode tuya_pin_mode_t The values in the tuya_pin_mode_t enum.

Return value

Return value Description
OPRT_OK Initialization succeeded.
Error codes Initialization failed.

tuya_pin_write

Function prototype

int tuya_pin_write(tuya_pin_name_t pin, tuya_pin_level_t level);

Parameters

Parameter name Parameter type Description
pin tuya_pin_name_t The values in the tuya_pin_name_t enum.
level tuya_pin_level_t The values in the tuya_pin_level_t enum.

Return value

Return value Description
OPRT_OK Initialization succeeded.
Error codes Initialization failed.

tuya_pin_read

Function prototype

int tuya_pin_read(tuya_pin_name_t pin)

Parameters

Parameter name Parameter type Description
pin tuya_pin_name_t The values in the tuya_pin_name_t enum.

Return value

Return value Description
TUYA_PIN_LOW Low level.
TUYA_PIN_HIGH High level.

tuya_pin_irq_init

Function prototype

int tuya_pin_irq_init(tuya_pin_name_t pin, tuya_pin_mode_t irq_mode, tuya_pin_irq_cb cb, void *arg);

Parameters

Parameter name Parameter type Description
pin tuya_pin_name_t The values in the tuya_pin_name_t enum.
irq_mode tuya_pin_mode_t The values in the tuya_pin_mode_t enum.
cb tuya_pin_irq_cb The registration callback. For more information, see the following function prototypes.
arg void The registered parameters.

Return value

Return value Description
OPRT_OK Initialization succeeded.
Error codes Execution failed.

tuya_pin_irq_enable

Function prototype

int tuya_pin_irq_enable(tuya_pin_name_t pin);

Parameters

Parameter name Parameter type Description
pin tuya_pin_name_t The values in the tuya_pin_name_t enum.

Return value

Return value Description
OPRT_OK Initialization succeeded.
Error codes Execution failed.

tuya_pin_irq_disable

Function prototype

int tuya_pin_irq_disable(tuya_pin_name_t pin);

Parameters

Parameter name Parameter type Description
pin tuya_pin_name_t The values in the tuya_pin_name_t enum.

Return value

Return value Description
OPRT_OK Initialization succeeded.
Error codes Execution failed.

tuya_pin_control

Function prototype

int tuya_pin_control(tuya_pin_name_t pin, uint8_t cmd, void *arg); 

Parameters

Parameter name Parameter type Description
pin tuya_pin_name_t The values in the tuya_pin_name_t enum.
cmd uint8_t The control commands.
*arg void The control parameters.

Return value

Return value Description
OPRT_OK Initialization succeeded.
Error codes Execution failed.

Structs

tuya_pin_name_t

Struct prototype

typedef enum {
    TUYA_PINS_NAME(TUYA_PA),     //!  TUYA_PA ->  TUYA_PA0 - TUYA_PA31
    TUYA_PINS_NAME(TUYA_PB),	 //!  TUYA_PB ->  TUYA_PB0 - TUYA_PB31
    TUYA_PINS_NAME(TUYA_PC),	 //!  TUYA_PC ->  TUYA_PC0 - TUYA_PC31
    TUYA_PINS_NAME(TUYA_PD),	 //!  TUYA_PD ->  TUYA_PD0 - TUYA_PD31
    TUYA_PINS_NAME(TUYA_PE),	 //!  TUYA_PE ->  TUYA_PE0 - TUYA_PE31
} tuya_pin_name_t;

tuya_pin_mode_t

Struct prototype

//!  Initialize all required parameters.
typedef enum {
    //! PU  ->  pull up
    //! PD  ->  pull dowm
    //! FL  ->  floating
    //! PP  ->  push pull
    //! OD  ->  open drain
    //! hiz ->  high-impedance level
    TUYA_PIN_MODE_IN_PU,    		          //!  Pull-up input.
    TUYA_PIN_MODE_IN_PD,           		 //!  Pull-down input.
    TUYA_PIN_MODE_IN_FL,                        //!  Floating input.
	
    //!  Input the interrupt trigger method, set to pull-up by default.
    TUYA_PIN_MODE_IN_IRQ_RISE,      		 //!  Rising edge trigger.
    TUYA_PIN_MODE_IN_IRQ_FALL,          	//!  Falling edge trigger.
    TUYA_PIN_MODE_IN_IRQ_RISE_FALL,   	       //!  Dual-edge trigger.
    TUYA_PIN_MODE_IN_IRQ_LOW,          	      //!  Low-level trigger.
    TUYA_PIN_MODE_IN_IRQ_HIGH,               //!  High-level trigger.
    
    TUYA_PIN_MODE_OUT_PP_LOW,           	//!  Push-pull output low, without pull-up.
    TUYA_PIN_MODE_OUT_PP_HIGH,          	//!  Push-pull output high, without pull-up.

    TUYA_PIN_MODE_OUT_PP_PU_LOW,         	//!  Push-pull output low, with pull-up.
    TUYA_PIN_MODE_OUT_PP_PU_HIGH,      	       //!  Push-pull output high, with pull-up.

    TUYA_PIN_MODE_OUT_PP_PD_LOW,       	//!  Open-drain output low, with pull-down.
    TUYA_PIN_MODE_OUT_PP_PD_HIGH,      	//!  Open-drain output high, with pull-down.
                                  
    TUYA_PIN_MODE_OUT_OD_LOW,            	//!  Open-drain output low, without pull-up.
    TUYA_PIN_MODE_OUT_OD_HIZ,         	       //!  Open-drain output high impedance, without pull-up.

    TUYA_PIN_MODE_OUT_OD_PU_LOW,        	//!  Open-drain output low, with pull-up.
    TUYA_PIN_MODE_OUT_OD_PU_HIGH,       	//!  Open-drain output high, with pull-up.
} tuya_pin_mode_t;

tuya_pin_level_t

Struct prototype

typedef enum {
    TUYA_PIN_LOW  = 0,  					    //!  Set the pin to low level.
    TUYA_PIN_HIGH  						   //!  Set the pin to high level.
} tuya_pin_level_t;

Registration function prototype

tuya_pin_irq_cb

Function prototype

typedef void (*tuya_pin_irq_cb)(void *args);

Usage example

void tuya_pa5_irq_cb(void *arg)
{
    PR_NOTICE("TUYA_PA5 irq trigger");
    tuya_pin_irq_disable(TUYA_PA5);
}

void tuya_pa12_irq_cb(void *arg)
{
    PR_NOTICE("TUYA_PA12 irq trigger");
    //! enable tuya_pa5 irq;
    PR_NOTICE("TUYA_PA5 irq enable");
    tuya_pin_irq_enable(TUYA_PA5);
}

void tuya_pin_test(void)
{
    STATIC UINT8_T tick   = 0;
    STATIC UINT8_T stat   = TUYA_PIN_HIGH;

    //! output init high 
    tuya_pin_init(TUYA_PA22, TUYA_PIN_MODE_OUT_PP_HIGH);
    tuya_pin_init(TUYA_PA19, TUYA_PIN_MODE_OUT_PP_LOW);

    //! input pull up
    tuya_pin_init(TUYA_PA14, TUYA_PIN_MODE_IN_PU);

    //! input irq mode
    tuya_pin_irq_init(TUYA_PA5,  TUYA_PIN_MODE_IN_IRQ_FALL, tuya_pa5_irq_cb, NULL);
    tuya_pin_irq_disable(TUYA_PA5);

    //! TUYA_PA5 irq enable by TUYA_P12 irq
    tuya_pin_irq_init(TUYA_PA12, TUYA_PIN_MODE_IN_IRQ_FALL, tuya_pa12_irq_cb, TUYA_PA5);

    while (1) {
        tuya_hal_system_sleep(200);

        if (++tick >= 3) {
            tick = 0;
            //! led toggle
            stat = stat ? TUYA_PIN_LOW : TUYA_PIN_HIGH;
            tuya_pin_write(TUYA_PA22, stat);
        }

        //! led control by key
        if (tuya_pin_read(TUYA_PA14)) {
            tuya_pin_write(TUYA_PA19, TUYA_PIN_HIGH);
        } else {
            tuya_pin_write(TUYA_PA19, TUYA_PIN_LOW);
        }
    }
}