Last Updated on : 2024-11-20 02:14:53download
API files
The pin API is located in ty_pin.h
. The driver code for different chip platforms is located in ty_pin_xxxx.c
.
tuya_ble_sdk_demo
└── board
├── include
| └── ty_pin.h
├── xxxx /* xxxx represents the chip platform, such as TLSR825x */
| └── ty_board_xxxx /* xxxx represents the chip platform, such as ty_board_tlsr825x */
| └── ty_pin_xxxx.c /* xxxx represents the chip platform, such as ty_pin_tlsr825x.c */
└── board.h
Demo files
File structure of tuya_board_api_demo:
tuya_ble_sdk_demo
├── app /* API example */
| ├── include
| | └── ty_board_demo
| | | ├── demo_config.h /* Demo configuration file */
| | | └── ty_pin_demo.h /* Pin sample code */
| | ├── tuya_ble_board_api_demo.h /* Entry point to Board API example */
| | └── tuya_ble_sdk_demo.h /* Entry point to tuya_ble_sdk application */
| └── src
| ├── ty_board_demo
| | └── ty_pin_demo.c /* Pin sample code */
| ├── tuya_ble_board_api_demo.c /* Entry point to Board API example */
| └── tuya_ble_sdk_demo.c /* Entry point to tuya_ble_sdk application */
└── board /* Example of modifying API code */
Modify demo_config.h
to toggle to the pin example.
#define BOARD_API_DEMO BOARD_API_PIN
Function name | Description |
---|---|
ty_pin_init | Initialize pin modes. |
ty_pin_set | Set the pin level. |
ty_pin_get | Read the pin level. |
ty_pin_control | Control pins. |
ty_pin_uninit | Disable pins. |
Function name | ty_pin_init |
---|---|
Function prototype | uint32_t ty_pin_init(uint8_t pin, ty_pin_mode_t mode); |
Description | Initialize pin modes. |
Parameter | pin [in]: The pin number. mode [in]: The pin mode, defined by ty_pin_mode_t . |
Return value | 0 : Success. Others: Failure. |
Notes | You can add or modify functions, parameters, and return values as needed. |
Function name | ty_pin_set |
---|---|
Function prototype | uint32_t ty_pin_set(uint8_t pin, ty_pin_level_t level); |
Description | Set the pin level. |
Parameter | pin [in]: The pin number. level [in]: The pin level, defined by ty_pin_level_t . |
Return value | 0 : Success. Others: Failure. |
Notes | You can add or modify functions, parameters, and return values as needed. |
Function name | ty_pin_get |
---|---|
Function prototype | uint32_t ty_pin_get(uint8_t pin, ty_pin_level_t* p_level); |
Description | Set the pin level. |
Parameter | pin [in]: The pin number. p_level [out]: The pin level, defined by ty_pin_level_t . |
Return value | 0 : Success. Others: Failure. |
Notes | You can add or modify functions, parameters, and return values as needed. |
Function name | ty_pin_control |
---|---|
Function prototype | uint32_t ty_pin_control(uint8_t pin, uint8_t cmd, void* arg); |
Description | Control a pin. |
Parameter | pin [in]: The pin number. cmd [in]: The control command. arg [in]: The parameter of the command. |
Return value | 0 : Success. Others: Failure. |
Notes | You can add or modify functions, parameters, and return values as needed. |
Function name | ty_pin_uninit |
---|---|
Function prototype | uint32_t ty_pin_uninit(uint8_t pin, ty_pin_mode_t mode); |
Description | Set the pin level. |
Parameter | pin [in]: The pin number. mode [in]: The pin mode. |
Return value | 0 : Success. Others: Failure. |
Notes | You can add or modify functions, parameters, and return values as needed. |
/* ! Some code is omitted here for simplicity */
typedef enum {
//PU -> pull up
//PD -> pull down
//FL -> floating
//PP -> push pull
//OD -> open drain
//hiz -> high-impedance level
/* Input */
TY_PIN_MODE_IN_PU, /* Pull-up input */
TY_PIN_MODE_IN_PD, /* Pull-down input */
TY_PIN_MODE_IN_FL, /* Floating input */
/* External interrupt */
TY_PIN_MODE_IN_IRQ_RISE, /* Rising edge trigger */
TY_PIN_MODE_IN_IRQ_FALL, /* Falling edge trigger */
TY_PIN_MODE_IN_IRQ_RISE_FALL, /* Dual-edge trigger */
TY_PIN_MODE_IN_IRQ_LOW, /* Low-level trigger */
TY_PIN_MODE_IN_IRQ_HIGH, /* High-level trigger */
/* Push-pull output */
TY_PIN_MODE_OUT_PP_LOW, /* Initial low level */
TY_PIN_MODE_OUT_PP_HIGH, /* Initial high level */
/* Push-pull output, with pull-up capability */
TY_PIN_MODE_OUT_PP_PU_LOW, /* Initial low level */
TY_PIN_MODE_OUT_PP_PU_HIGH, /* Initial high level */
/* Push-pull output, with pull-down capability */
TY_PIN_MODE_OUT_PP_PD_LOW, /* Initial low level */
TY_PIN_MODE_OUT_PP_PD_HIGH, /* Initial high level */
/* Open drain output */
TY_PIN_MODE_OUT_OD_LOW, /* Initial low level */
TY_PIN_MODE_OUT_OD_HIZ, /* Initial high impedance */
/* Open drain output, with pull-up capability */
TY_PIN_MODE_OUT_OD_PU_LOW, /* Initial low level */
TY_PIN_MODE_OUT_OD_PU_HIGH, /* Initial high level */
} ty_pin_mode_t;
typedef enum {
TY_PIN_LOW = 0, /* Low level */
TY_PIN_HIGH /* High level */
} ty_pin_level_t;
Use TLSR825x as an example. You can add pin API functions or call APIs provided by the chip vendor in the application code.
ty_pin.h
/* ! The type of pin defaults to uint8_t, which can be modified to your needs. */
uint32_t ty_pin_init(uint16_t pin, ty_pin_mode_t mode);
uint32_t ty_pin_set(uint16_t pin, ty_pin_level_t level);
uint32_t ty_pin_get(uint16_t pin, ty_pin_level_t* p_level);
ty_pin_tlsr825x.c
uint32_t ty_pin_init(uint16_t pin, ty_pin_mode_t mode)
{
gpio_set_func(pin, AS_GPIO);
if ((mode & TY_PIN_INOUT_MASK) <= TY_PIN_IN_IRQ) {
gpio_set_input_en(pin, 1);
gpio_set_output_en(pin, 0);
} else {
gpio_set_input_en(pin, 0);
gpio_set_output_en(pin, 1);
}
switch (mode) {
case TY_PIN_MODE_IN_PU:
gpio_setup_up_down_resistor(pin, PM_PIN_PULLUP_10K);
break;
case TY_PIN_MODE_IN_PD:
gpio_setup_up_down_resistor(pin, PM_PIN_PULLDOWN_100K);
break;
case TY_PIN_MODE_IN_FL:
gpio_setup_up_down_resistor(pin, PM_PIN_UP_DOWN_FLOAT);
break;
case TY_PIN_MODE_OUT_PP_LOW:
gpio_write(pin, 0);
break;
case TY_PIN_MODE_OUT_PP_HIGH:
gpio_write(pin, 1);
break;
default:
break;
}
#if(GPIO_WAKEUP_MODULE_POLARITY == 1)
cpu_set_gpio_wakeup (WAKEUP_MODULE_GPIO, Level_High, 1);
GPIO_WAKEUP_MODULE_LOW;
#else
cpu_set_gpio_wakeup (WAKEUP_MODULE_GPIO, Level_Low, 1);
GPIO_WAKEUP_MODULE_HIGH;
#endif
return 0;
}
uint32_t ty_pin_set(uint16_t pin, ty_pin_level_t level)
{
gpio_write(pin, level);
return 0;
}
uint32_t ty_pin_get(uint16_t pin, ty_pin_level_t* p_level)
{
*p_level = gpio_read(pin);
return 0;
}
Description
Press the button (low) to turn on the LED (high). Release the button (high) to turn off the LED (low).
Sample code
The sample code uses the TLSR825x platform:
#include "ty_pin_demo.h"
#include "ty_pin.h"
#include "tuya_ble_log.h"
#include "tuya_ble_port.h"
/***********************************************************
************************micro define************************
***********************************************************/
#define KEY_PIN GPIO_PA0
#define LED_PIN GPIO_PD7
#define KEY_SCAN_TIME_MS 10
/***********************************************************
***********************variable define**********************
***********************************************************/
static tuya_ble_timer_t sg_pin_timer;
/***********************************************************
***********************function define**********************
***********************************************************/
/**
* @brief pin timer callback
* @param none
* @return none
*/
void __pin_timer_cb(void)
{
ty_pin_level_t pin_level = 0;
ty_pin_get(KEY_PIN, &pin_level);
ty_pin_set(LED_PIN, !pin_level);
}
/**
* @brief ty_pin api demo init
* @param none
* @return none
*/
void ty_pin_demo_init(void)
{
uint32_t res;
/* key pin init */
res = ty_pin_init(KEY_PIN, TY_PIN_MODE_IN_PU);
if (res) {
TUYA_APP_LOG_ERROR("ty_pin_init KEY_PIN failed, error code: %d", res);
return;
}
/* led pin init */
res = ty_pin_init(LED_PIN, TY_PIN_MODE_OUT_PP_LOW);
if (res) {
TUYA_APP_LOG_ERROR("ty_pin_init KEY_PIN failed, error code: %d", res);
return;
}
/* timer init */
tuya_ble_timer_create(&sg_pin_timer, KEY_SCAN_TIME_MS, TUYA_BLE_TIMER_REPEATED, (tuya_ble_timer_handler_t)__pin_timer_cb);
tuya_ble_timer_start(sg_pin_timer);
}
Is this page helpful?
YesFeedbackIs this page helpful?
YesFeedback