API Description

Last Updated on : 2024-02-27 07:19:58download

This topic describes how to use the APIs in the SDK and important considerations.

  • APIs simplify adapting to a specific platform by abstracting away complexities. You only need to implement the required APIs for your platform, so it is common if some APIs do not work on your platform.
  • This topic covers only the adapted APIs. See the API code comments for more information.

Driver API

  • Path: software\TuyaOS\include\components\tal_driver\include

  • Enablement definitions: software\TuyaOS\include\base\include\tuya_iot_config.h

ADC driver

Description

// TS24-U is only compatible with the ADC0 module, so unit_num defaults to 0. The ADC0 module corresponds to an I/O, so ch_num is also 0.
OPERATE_RET tal_adc_init(UINT32_T unit_num, TUYA_ADC_BASE_CFG_T *cfg);
OPERATE_RET tal_adc_read_single_channel(UINT32_T unit_num, UINT8_T ch_num, UINT32_T *buf);

// Get the number of bits per ADC sample to calculate the maximum sample value.
UINT8_T tal_adc_width_get(UINT32_T unit_num);

// Get the ADC reference voltage.
UINT32_T tal_adc_ref_voltage_get(VOID_T);

// Not adapted.
OPERATE_RET tal_adc_read_data(UINT32_T unit_num, UINT32_T *buff, UINT8_T len);
INT32_T tal_adc_temperature_get(VOID_T);

Example

#define BATTERY_ADC_BIT_CNT         (4095)
STATIC TUYA_ADC_BASE_CFG_T s_adc_info;
UINT32_T adc_raw_val = 0;
UINT8_T s_ch_id = GPIO_PORTB_2_PIN;// The I/O pin for the ADC channel.

s_adc_info.ch_nums = 1;
s_adc_info.type = TUYA_ADC_EXTERNAL_SAMPLE_VOL;
s_adc_info->ch_list = &s_ch_id;
tal_adc_init(0, &s_adc_info);
tal_adc_read_single_channel(0, 0, &adc_raw_val);
tal_adc_deinit(0);

UINT16_T adc_volt_val = 0;
adc_volt_val = tal_adc_ref_voltage_get() * adc_raw_val / BATTERY_ADC_BIT_CNT;

Flash driver

Operate the flash based on the address.
The SDK has divided the flash memory based on functionality. It is recommended to avoid accessing the flash memory by address, as doing so may affect its existing function.

GPIO driver

Description

// Find the pin definitions in software\TuyaOS\vendor\efr_matter\tuyaos\components\include\ty_modules.h
// The control GPIO. Set the pin_id to the mapped pin, such as GPIO_PORTB_4_PIN
OPERATE_RET tal_gpio_init(UINT32_T pin_id, TUYA_GPIO_BASE_CFG_T *cfg);

Example

TUYA_GPIO_BASE_CFG_T gpio;

gpio.mode = TUYA_GPIO_PUSH_PULL;
gpio.direct = TUYA_GPIO_OUTPUT;
gpio.level = TUYA_GPIO_LEVEL_LOW;
tal_gpio_init(GPIO_PORTB_4_PIN, &gpio);

I2C driver

Description

// baudrate for frequency, up to 100000
// port_num for I2C module. Up to one I2C channel is supported so port_num defaults to 0.
// The default I/O is SCL:PA7 SDA:PA8. To change it, use the API below.
OPERATE_RET tal_i2c_init(UINT32_T port_num, UINT32_T baudrate);

// If you change the GPIO, the API will no longer be general-purpose.
/**
* @brief  Change the default I/O for the SDK I2C driver, which must be used with tal_i2c_init, before init 
*
* @param[in] port: I2C channel
* @param[in] scl_io sda_io: I2C IO
* @return OPRT_OK on success. Others on error, please refer to tuya_error_code.h
*/
OPERATE_RET tkl_i2c_user_change_io(UINT32_T port, UINT8_T scl_io,UINT8_T sda_io);

PWM driver

Description

// The driver is designed to provide PWM capability. A channel denotes a specific PWM, so you don't need to set the I/O for each channel.
// There are 5 PWM pins available, with ch_id ranging from 0 to 4, corresponding the GPIOs PC2\PC1\PA4\PA5\PA6.
OPERATE_RET tal_pwm_init(UINT32_T ch_id, TUYA_PWM_BASE_CFG_T *cfg);

//If you change the GPIO, the API will no longer be general-purpose.
/**
* @brief  Change the default I/O for the SDK PWM driver, which must be used with tal_pwm_init, before init 
*
* @param[in] ch_id: PWM port ID
* @param[in] pwm_io: PWM IO
* @return OPRT_OK on success. Others on error, please refer to tuya_error_code.h
*/
OPERATE_RET tkl_pwm_user_change_io(UINT32_T ch_id, UINT8_T pwm_io);

SPI driver

Description

// port_num for the SPI module. Up to one SPI channel is supported so port_num defaults to 0.
// The default GPIOs for SPIs are not set. You need to first configure the I/O using the API below.
OPERATE_RET tal_spi_init(UINT_T port_num, TUYA_SPI_BASE_CFG_T *cfg);

/**
* @brief Change the default I/O for the SDK SPI driver, which must be used with tal_spi_init, before init
*
* @param[in] port: SPI channel
* @param[in] cs_io clk_io tx_io rx_io: SPI IO
* @return OPRT_OK on success. Others on error, please refer to tuya_error_code.h
*/
OPERATE_RET tkl_spi_user_change_io(UINT32_T port, UINT8_T cs_io,UINT8_T clk_io,UINT8_T tx_io,UINT8_T rx_io);

UART driver

Description

// Logging and testing are enabled by default, which occupies the serial port.
// Support 3 serial ports, with port_id assigned as 0, 1, and 2.
//port_id 0: Not used by the SDK.
//port_id 1: Used for logging by the SDK, with a baud rate of 921600
//port_id 2: Used for authorization by the SDK, with a baud rate of 115200. Use it with caution.
OPERATE_RET tal_uart_init(UINT32_T port_id, TUYA_UART_BASE_CFG_T *cfg);

//If you change the GPIO, the API will no longer be general-purpose.
/**
* @brief Change the default UART I/O for the SDK serial port driver, which must be used with tal_uart_init, before init 
*
* @param[in] port_id: UART port ID
* @param[in] tx_io: UART TX IO
* @param[in] rx_io: UART RX IO
* @return OPRT_OK on success. Others on error, please refer to tuya_error_code.h
*/
OPERATE_RET tkl_uart_user_change_io(UINT32_T port_id, UINT8_T tx_io, UINT8_T rx_io)

Software timer driver

The software timer functions similarly to the FreeRTOS timer.

Sleep timer driver

Not supported currently.

Watchdog driver

The watchdog is enabled by default. Disabling the watchdog and feeding is not supported currently.

RTC driver

Not supported currently.

Component API

  • Path: software\TuyaOS\vendor\efr_matter\tuyaos\components\include

  • ty_modules.h

    // Mappings between chip pins and API pins
    

Key driver

// Initialize the key component
//key_index: The light number.
//pin: Corresponding GPIO pin.
//high_valid: Specify whether to use high level active.
//long_ms: The press-and-hold duration to trigger an action.
//key_callback: The callback for button press.
void ty_key_init(uint8_t key_index,uint32_t pin,bool_t high_valid,uint32_t long_ms,key_callback_func_t key_callback);
//key disabled
void ty_key_deinit(uint8_t key_index);

LED driver

// Initialize the LED component.
//led_index: The light number. pin: The corresponding GPIO pin. level: The initial state. high_valid: Specify whether to use high level active.
void ty_led_init(uint8_t led_index,uint32_t pin,TUYA_GPIO_LEVEL_E level,bool_t high_valid);
// Control the LED.
// Turn on a specific light.
void ty_led_action_on(uint8_t led_index);
// Turn off a specific light.
void ty_led_action_off(uint8_t led_index);
// Instruct a specific light to blink.
void ty_led_action_blink(uint8_t led_index,uint32_t led_on_ms,uint32_t led_off_ms,uint16_t blink_times,LED_ON_OFF_T led_end_state);

Non-volatile memory (NVM) driver

Store data in flash memory using a key-value database to simplify flash operation when data changes.

//NVM_SAVE_TYPE_E enumeration can define nvm_id. Each nvm_id corresponds to a set of data to store.
// Write data to flash
OPERATE_RET flash_kv_write( IN UINT8_T nvm_id, IN UCHAR_T *data, IN USHORT_T len);

// Read data from flash. The return value is the length of the written data.
USHORT_T flash_kv_read(IN UINT8_T nvm_id, IN UCHAR_T *data, IN USHORT_T len);

Support and help

If you have any problems with TuyaOS development, you can post your questions in the Tuya Developer Forum.