涂鸦Zigbee SDK开发系列教程——7.HAL API 接口示例

HAL 层 API 是涂鸦抽象各芯片硬件外设封装的一层标准接口,您无需接入涂鸦平台的各种芯片硬件外设底层实现逻辑,只需调用统一的 API,填写相应参数,即可快速使用相关外设,完成产品功能,加快产品的开发速度。

当前 SDK 版本支持 UART、**Software **(软件定时)、ADCPWM等外设接口,用户可参考以下各外设 API 示例 demo 来实现相关功能,外设 demo 示例可点击此链接参考实现。

HAL 层列表如下:

名称功能说明
UARTUART 外设相关函数的使用说明
TIMERTIMER 外设相关函数的使用说明
ADCADC 外设相关函数的使用说明
PWMPWM 外设相关函数的使用说明

UART

UART 接口文件为TuyaOS\include\components\tal_driver\include\tal_uart.h 文件。

API 列表

函数名称功能描述
tal_uart_init串口初始化
tal_uart_read串口接收
tal_uart_write串口发送
tal_uart_deinit关闭串口

API 说明

  • 串口初始化函数:初始化串口,申请资源

    函数原型:

    OPERATE_RET tal_uart_init(UINT32_T port_id, TAL_UART_CFG_T *cfg);
    

    参数说明:

    参数名称参数类型说明
    port_idUINT32_T串口id
    *cfgTAL_UART_CFG_TTAL_UART_CFG_T枚举中的值
  • 串口接收函数

    函数原型:

    INT_T tal_uart_read(UINT32_T port_id, UINT8_T *data, UINT32_T len);
    

    参数说明:

    参数名称参数类型说明
    port_idUINT32_T串口id
    *dataUINT8_T接收数据存放地址的指针
    lenUINT32_T接收数据的最大长度
  • 串口发送函数

    函数原型:

    INT_T tal_uart_write(UINT32_T port_id, CONST UINT8_T *data, UINT32_T len);
    

    参数说明:

    参数名称参数类型说明
    port_idUINT32_T串口 id
    *dataUINT8_T发送数据的指针
    lenUINT32_T发送数据长度
  • 串口关闭函数

    函数原型:

    OPERATE_RET tal_uart_deinit(UINT32_T port_id);
    

    参数说明:

    参数名称参数类型说明
    port_idUINT32_T串口 id

串口使用示例

tal_uart.h 文件有详细的接口定义,串口使用有一定的限制,需要在 tuya_init_first 或以后再使用,下面通过一个使用串口接收和发送信息的例子来展示使用方法。

OPERATE_RET uart_demo(VOID_T)
{
    UINT32_T op_ret;
    UINT8_T uart0_rx_buf[BUFFER_SIZE];
    TAL_UART_CFG_T uart_cfg = {
        .rx_buffer_size = 256,
        .open_mode = 0,
        {
            .baudrate = 115200,
            .parity = TUYA_UART_PARITY_TYPE_NONE,
            .databits = TUYA_UART_DATA_LEN_8BIT,
            .stopbits = TUYA_UART_STOP_LEN_1BIT,
            .flowctrl = TUYA_UART_FLOWCTRL_NONE,
        }
    };
    tal_uart_init(USER_UART0, &uart_cfg);

    tal_uart_write(USER_UART0, "Hello uart0 init!\r\n", 20);

    op_ret = tal_uart_read(USER_UART0, uart0_rx_buf, strlen(uart0_rx_buf));

    TAL_PR_DEBUG("uart0_rx_buf : %s\r\n");

    if (op_ret > 0) {

         tal_uart_write(USER_UART0, uart0_rx_buf, strlen(uart0_rx_buf));

     }   else{

         tal_uart_write(USER_UART0, "no recv data\r\n", 15);

     }

    return OPRT_OK;
}

Timer

API 列表

函数名称功能描述
tal_sw_timer_init软件定时器初始化
tal_sw_timer_create创建软件定时器
tal_sw_timer_delete删除软件定时器
tal_sw_timer_stop停止软件定时器
tal_sw_timer_is_running软件定时器正在运行
tal_sw_timer_start启动软件定时器
tal_sw_timer_trigger触发软件定时器
tal_sw_timer_release释放软件定时器所有资源

API 说明

  • 软件定时器初始化函数

    函数原型:

    /**
     * @brief Initializing the software timer
     *
     * @param VOID
     *
     * @note This API is used for initializing the software timer
     *
     * @return OPRT_OK on success. Others on error, please refer to tuya_error_code.h
     */
    OPERATE_RET tal_sw_timer_init(VOID_T);
    
  • 创建软件定时器函数

    函数原型:

    /**
     * @brief create a software timer
     *
     * @param[in] pTimerFunc: the processing function of the timer
     * @param[in] pTimerArg:  the parameater of the timer function
     * @param[out] p_timerID: timer id
     *
     * @note This API is used for create a software timer
     *
     * @return OPRT_OK on success. Others on error, please refer to tuya_error_code.h
     */
    OPERATE_RET tal_sw_timer_create(TAL_TIMER_CB func, VOID_T *arg, TIMER_ID *timer_id);
    

    参数说明:

    参数名称参数类型说明
    funcTAL_TIMER_CB定时器的处理功能
    *argVOID_T定时器函数的参数
    *timer_idTIMER_ID定时器ID
  • 删除软件定时器函数

    函数原型:

    /**
     * @brief Delete the software timer
     *
     * @param[in] timerID: timer id
     *
     * @note This API is used for deleting the software timer
     *
     * @return OPRT_OK on success. Others on error, please refer to tuya_error_code.h
     */
    OPERATE_RET tal_sw_timer_delete(TIMER_ID timer_id);
    

    参数说明:

    参数名称参数类型说明
    timer_idTIMER_ID定时器ID
  • 软件定时器启动函数

    函数原型:

    /**
     * @brief Start the software timer
     *
     * @param[in] timerID:    timer id
     * @param[in] timeCycle:  timer running cycle
     * @param[in] timer_type: timer type
     *
     * @note This API is used for starting the software timer
     *
     * @return OPRT_OK on success. Others on error, please refer to tuya_error_code.h
     */
    OPERATE_RET tal_sw_timer_start(TIMER_ID timer_id, TIME_MS time_ms, TIMER_TYPE timer_type);
    

    参数说明:

    参数名称参数类型说明
    timer_idTIMER_ID定时器ID
    time_msTIME_MS定时器定时时间
    timer_typeTIMER_TYPE定时器运行类型:单次/循环

    定时类型:

      typedef PVOID_T TIMER_ID;  // 定时器ID
      typedef VOID_T (* TAL_TIMER_CB)(TIMER_ID timer_id, VOID_T *arg);   //定时器回调函数类型
    
      /**
      * @brief the type of timer
      */
      typedef enum {
          TAL_TIMER_ONCE = 0,    //单次运行,定时器超时后停止计数
          TAL_TIMER_CYCLE,       //循环运行,定时器超时后重新开始计数
      }TIMER_TYPE;
    
  • 软件定时器停止函数

    函数原型:

    /**
     * @brief Stop the software timer
     *
     * @param[in] timerID: timer id
     *
     * @note This API is used for stopping the software timer
     *
     * @return OPRT_OK on success. Others on error, please refer to tuya_error_code.h
     */
    OPERATE_RET tal_sw_timer_stop(TIMER_ID timer_id);
    
    

    参数说明:

    参数名称参数类型说明
    timer_idTIMER_ID定时器ID
  • 软件定时器运行中函数

    函数原型:

    /**
     * @brief Identify the software timer is running
     *
     * @param[in] timerID: timer id
     *
     * @note This API is used to identify wheather the software timer is running
     *
     * @return TRUE or FALSE
     */
    BOOL_T tal_sw_timer_is_running(TIMER_ID timer_id);
    

    参数说明:

    参数名称参数类型说明
    timer_idTIMER_ID定时器ID
  • 触发软件定时器函数

    函数原型:

    /**
     * @brief Trigger the software timer
     *
     * @param[in] timerID: timer id
     *
     * @note This API is used for triggering the software timer instantly.
     *
     * @return OPRT_OK on success. Others on error, please refer to tuya_error_code.h
     */
    OPERATE_RET tal_sw_timer_trigger(TIMER_ID timer_id);
    

    参数说明:

    参数名称参数类型说明
    timer_idTIMER_ID定时器ID
  • 释放软件定时器函数

    函数原型:

    /**
     * @brief Release all resource of the software timer
     *
     * @param VOID
     *
     * @note This API is used for releasing all resource of the software timer
     *
     * @return OPRT_OK on success. Others on error, please refer to tuya_error_code.h
     */
    OPERATE_RET tal_sw_timer_release(VOID_T);
    

软件定时器使用示例

本节介绍 software timer 的使用方法, tal_sw_timer.h 中有详细的接口定义,下面是 software timer 的使用例子:创建定时器设置定时时间,当时间到之后打开串口输出打印信息。

// 1. 定义一个定时器变量ID,不需要初始化参数
TIMER_ID etimer_bright_delay;

// 2. 定义定时器回调函数
VOID_T app_light_bright_cb(TKL_TIMER_ID timer_id, VOID_T *arg)

{

    TAL_PR_DEBUG("--------app_light_bright_cb-------\r\n");

    tal_gpio_write(LED0_GPIO_NUM, LED_STATUS_OFF);

}

// 3. 在tuya_init_second或者之后两层中创建定时器,将定时器和回调函数绑定在一起
tal_sw_timer_create(app_light_bright_cb, NULL, &etimer_bright_delay);

// 4. 在需要调用定时器事件的时候开启定时器
//     第一个参数为定时器ID
//     第二个参数为定时器定时时间
//     第三个参数为flag,once:定时器只运行一次,cycle:周期运行

tal_sw_timer_start(etimer_bright_delay, POWER_UP_LED_ON_TIME, TAL_TIMER_ONCE);

// 5. 在需要关闭定时器的时候调用关闭的接口,参数为需要关闭的定时器ID。
tkl_sw_timer_stop(etimer_bright_delay);

ADC

API列表

函数名称功能描述
tal_adc_initADC初始化
tal_adc_deinit关闭ADC函数
tal_adc_read_dataADC读取函数
tal_adc_read_single_channelADC读取信号通道数
tal_adc_width_getADC获取宽度值
tal_adc_temperature_getADC获取温度值
tal_adc_ref_voltage_get获取ADC电压值

API 说明

  • ADC 初始化函数

    函数原型:

    /**
     * @brief tuya hal adc init
     * 
     * @param[in] unit_num: adc unit number
     * @param[in] cfg: adc config
     *
     * @return OPRT_OK on success. Others on error, please refer to tuya_error_code.h
     */
    OPERATE_RET tal_adc_init(UINT32_T unit_num, TUYA_ADC_BASE_CFG_T *cfg);
    

    参数说明:

    参数名称参数类型说明
    unit_numUINT32_T单元通道id
    *cfgTUYA_ADC_BASE_CFG_TTUYA_ADC_BASE_CFG_T结构体中的值

    数据类型:

    /**
     * ADC采样类型
     */
    typedef enum {
        TUYA_ADC_INNER_SAMPLE_VOL = 0,
        TUYA_ADC_EXTERNAL_SAMPLE_VOL
    } TUYA_ADC_TYPE_E;
    
    /**
     * ADC配置
     */
    typedef struct {
        UINT8_T *ch_list;       //ADC通道列表
        UINT8_T ch_nums;        //ADC通道数
        UINT8_T  width;         //采样宽度
        TUYA_ADC_TYPE_E type;   //类型
        UINT32_T   ref_vol;     //参考电压(bat: mv),如果不支持设置参考电压,忽略它
    } TUYA_ADC_BASE_CFG_T;
    
  • 关闭ADC函数

    函数原型:

    /**
     * @brief adc deinit
     * 
     * @param[in] unit_num: adc unit number
     * @param[in] ch_num: adc channel number
     *
     * @return OPRT_OK on success. Others on error, please refer to tuya_error_code.h
     */
    OPERATE_RET tal_adc_deinit(UINT32_T unit_num);
    

    参数说明:

    参数名称参数类型说明
    unit_numUINT32_T单元通道id
  • ADC读取函数

    函数原型:

    /**
     * @brief adc read
     * 
     * @param[in] unit_num: adc unit number
     * @param[out] buff: points to the list of data read from the ADC register
     * @param[out] len:  buff len
     *
     * @return OPRT_OK on success. Others on error, please refer to tuya_error_code.h
     */
    OPERATE_RET tal_adc_read_data(UINT32_T unit_num, UINT32_T *buff, UINT8_T len);
    

    参数说明:

    参数名称参数类型说明
    unit_numUINT32_T单元通道id
    *buffUINT32_T指向从ADC寄存器读取的数据列表
    lenUINT32_T数据长度
  • ADC读取信号通道数

    函数原型:

    /**
     * @brief read single channel
     *
     * @param[in] unit_num: adc unit number
     * @param[in] ch_num: channel number in one unit
     * @param[out] buf: convert result buffer
     *
     * @return OPRT_OK on success. Others on error, please refer to tuya_error_code.h
     *
     */
    OPERATE_RET tal_adc_read_single_channel(UINT32_T unit_num, UINT8_T ch_num, UINT32_T *buf);
    

    参数说明:

    参数名称参数类型说明
    unit_numUINT32_T单元通道id
    ch_numUINT8_T每个单元中的通道数
    *bufUINT32_T转换结果缓冲区
  • ADC获取宽度值

    函数原型:

    /**
     * @brief get adc width
     * 
     * @param[in] unit_num: adc unit number
    
     *
     * @return adc width
     */
    UINT8_T tal_adc_width_get(UINT32_T unit_num);
    

    参数说明:

    参数名称参数类型说明
    unit_numUINT32_T单元通道id
  • ADC获取温度值

    函数原型:

    /**
     * @brief adc get temperature
     *
     * @return temperature(bat: 'C)
     */
    INT32_T tal_adc_temperature_get(VOID_T);
    
  • 获取ADC电压值

    函数原型:

    /**
     * @brief get adc reference voltage
     * 
     * @param[in] port: adc port
    
     *
     * @return adc reference voltage(bat: mv)
     */
    UINT32_T tal_adc_ref_voltage_get(VOID_T);
    

ADC 应用示例

读取 ADC 电压值并通过串口输出电压信息。

/*
 * @FileName: your project
 * @Author: Tuya
 * @Email:
 * @LastEditors: Tuya
 * @Date: 2022-03-07 14:24:24
 * @LastEditTime: 2022-03-16 15:58:12
 * @Copyright: HANGZHOU TUYA INFORMATION TECHNOLOGY CO.,LTD
 * @Company:  http://www.tuya.com
 * @Description:
 */

#include "tal_system.h"
#include "tal_gpio.h"
#include "tuya_adc_demo.h"
#include "tal_adc.h"
#include "tkl_adc.h"
#include "tal_uart.h"
#include "tal_log.h"
#include "tal_sw_timer.h"

/***********************************************************
************************micro define************************
***********************************************************/
#define adc_ch_num 1
#define adc_width 1000
#define ADC_LIST *GPIO_PIN
#define ADC_SAMPLE_TIME_MS 1000
#define adc_type TUYA_ADC_INNER_SAMPLE_VOL

TIMER_ID etimer_adc;
BOOL_T g_user_adc_init_flag = FALSE;
STATIC UCHAR_T sg_adc_channel = 0;
extern OPERATE_RET tkl_adc_mapping_to_gpio(UINT32_T ch_id, UINT32_T gpio_id);

VOID_T adc_ch_num_set(UCHAR_T channel_num)
{
    sg_adc_channel = channel_num;
}

OPERATE_RET tuya_adc_init(VOID_T)
{
    UINT32_T adc_data;
    OPERATE_RET v_ret = OPRT_COM_ERROR;

    TUYA_ADC_BASE_CFG_T adc_cfg = {
    .ch_list = ADC_LIST[0],
    .ch_nums = adc_ch_num,
    .width = adc_width,
    .type = adc_type,
};

    tkl_adc_mapping_to_gpio(0, 17);   //PD2;

    v_ret = tal_adc_init(0, &adc_cfg);


    if (v_ret != OPRT_OK) {
        TAL_PR_DEBUG("adc init error!");
    }

    g_user_adc_init_flag = TRUE;
    adc_ch_num_set(adc_ch_num);

    TAL_PR_DEBUG("adc init ok!");

    return v_ret;

}

PWM

API列表

函数名称功能描述
tal_pwm_initPWM初始化
tal_pwm_deinit关闭PWM
tal_pwm_startPAW启动函数
tal_pwm_stopPWM停止函数
tal_pwm_info_set设置PWM信息
tal_pwm_info_get获取

API 说明

  • PWM 初始化函数

    函数原型:

    /**
     * @brief pwm init
     * 
     * @param[in] ch_id: pwm channal id,id index starts from 0
     * @param[in] cfg: pwm config
     *
     * @return OPRT_OK on success. Others on error, please refer to tuya_error_code.h
     */
    OPERATE_RET tal_pwm_init(UINT32_T ch_id, TUYA_PWM_BASE_CFG_T *cfg);
    

    参数说明:

    参数名称参数类型说明
    ch_idUINT32_TPWM通道id,索引从0开始
    *cfgTUYA_PWM_BASE_CFG_TTUYA_PWM_BASE_CFG_T结构体中的值
    /**
     * @brief pwm config
     */
    typedef struct {
        TUYA_PWM_POLARITY_E  polarity;    //PWM 输出极性
        UINT_T              duty;       // 占空比 (bet: 10000 == 1/100)
        UINT_T              frequency;  // 频率 (bet: Hz)
    } TUYA_PWM_BASE_CFG_T;
    
  • PWM 启动函数

    函数原型:

    /**
     * @brief pwm start
     * 
     * @param[in] ch_id: pwm channal id,id index starts at 0
     *
     * @return OPRT_OK on success. Others on error, please refer to tuya_error_code.h
     */
    OPERATE_RET tal_pwm_start(UINT32_T ch_id);
    

    参数说明:

    参数名称参数类型说明
    ch_idUINT32_TPWM通道id,索引从0开始
  • PWM 停止函数

    函数原型:

    /**
     * @brief pwm stop
     * 
     * @param[in] ch_id: pwm channal id,id index starts from 0
     *
     * @return OPRT_OK on success. Others on error, please refer to tuya_error_code.h
     */
    OPERATE_RET tal_pwm_stop(UINT32_T ch_id);
    

    参数说明:

    参数名称参数类型说明
    ch_idUINT32_TPWM通道id,索引从0开始
  • PWM 关闭函数

    函数原型:

    /**
     * @brief pwm deinit
     * 
     * @param[in] ch_id: pwm channal id,id index starts from 0
     *
     * @return OPRT_OK on success. Others on error, please refer to tuya_error_code.h
     */
    OPERATE_RET tal_pwm_deinit(UINT32_T ch_id);
    

    参数说明:

    参数名称参数类型说明
    ch_idUINT32_TPWM通道id,索引从0开始
  • 设置PWM 函数

    函数原型:

    /**
     * @brief set pwm info
     * 
     * @param[in] ch_id: pwm channal id,id index starts from 0
     * @param[in] info: pwm info
     *
     * @return OPRT_OK on success. Others on error, please refer to tuya_error_code.h
     */
    OPERATE_RET tal_pwm_info_set(UINT32_T ch_id, TUYA_PWM_BASE_CFG_T *info);
    

    参数说明:

    参数名称参数类型说明
    ch_idUINT32_TPWM通道id,索引从0开始
    *infoTUYA_PWM_BASE_CFG_TTUYA_PWM_BASE_CFG_T结构体中的值
  • 获取PWM 函数

    函数原型:

    /**
     * @brief get pwm info
     * 
     * @param[in] ch_id: pwm channal id,id index starts from 0
     * @param[out] info: pwm info
     *
     * @return OPRT_OK on success. Others on error, please refer to tuya_error_code.h
     */
    OPERATE_RET tal_pwm_info_get(UINT32_T ch_id, TUYA_PWM_BASE_CFG_T *info);
    

    参数说明:

    参数名称参数类型说明
    ch_idUINT32_TPWM通道id,索引从0开始
    *infoTUYA_PWM_BASE_CFG_TTUYA_PWM_BASE_CFG_T结构体中的值

PWM 使用示例

  • tal_pwm.h 文件中有详细的接口定义,下面通过一个 PWM 输出控制灯闪烁的例子来说明使用方法。

    #define pwm_ch_num 2
    #define pwm_duty 500
    #define pwm_frequency 1000
    #define pwm_polarity TUYA_PWM_POSITIVE
    
    BOOL_T g_pwm_bPolarity = FALSE;
    BOOL_T g_user_pwm_init_flag = FALSE;
    
    /**
     * @brief pwm config
     */
    typedef struct {
        TUYA_PWM_POLARITY_E  polarity;
        UINT_T              duty;       // (bet: 10000 == 1/100)
        UINT_T              frequency;  // (bet: Hz)
    } TUYA_PWM_BASE_CFG_T;
    
    
    OPERATE_RET app_pwm_init(VOID_T)
    {
        OPERATE_RET v_ret = OPRT_COM_ERROR;
    
        TUYA_PWM_BASE_CFG_T v_cfg = {
            .duty = 0,
            .frequency = pwm_frequency,
            .polarity = pwm_polarity,
        };
        tkl_pwm_mapping_to_gpio(0, 8);//PB1;
    
        v_ret = tal_pwm_init(0, &v_cfg);
        v_ret = tal_pwm_start(0);
        if(v_ret!=OPRT_OK) {
            TAL_PR_DEBUG("pwm init error!");
        }
        g_user_pwm_init_flag = TRUE;
    
        g_pwm_bPolarity = pwm_polarity;
        app_light_ctrl_ch_num_set(pwm_ch_num);
        TAL_PR_DEBUG("pwm init ok!");
        return v_ret;
    }
    
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值