Last Updated on : 2025-04-27 06:36:47download
A timer is an on-chip peripheral in microprocessors used for time measurement. Depending on the actual configuration requirements, different timing precisions are typically available, such as 16-bit and 32-bit. During practical use, configure parameters including the timer period, counting mode, and interrupt service routine.
OPERATE_RET tkl_timer_init(TUYA_TIMER_NUM_E timer_id, TUYA_TIMER_BASE_CFG_T *cfg);
Features:
Parameters:
timer_id
: The port number.
cfg
: The basic configuration of the timer, including timing mode, callback function, and callback function parameters.
typedef struct {
TKL_TIMER_MODE_E mode;
TKL_TIMER_ISR_CB cb;
VOID *args;
} TUYA_TIMER_BASE_CFG_T;
TKL_TIMER_MODE_E
:
Name | Definition |
---|---|
TKL_TIMER_MODE_ONCE | One-shot timer |
TKL_TIMER_MODE_PERIOD | Periodic timer |
Return value:
OPRT_OK
: Success.tuya_error_code.h
.OPERATE_RET tkl_timer_deinit(TUYA_TIMER_NUM_E timer_id);
timer_id
: The port number.OPRT_OK
: Success.tuya_error_code.h
.OPERATE_RET tkl_timer_start(TUYA_TIMER_NUM_E timer_id, UINT_T us);
Features:
Parameters:
timer_id
: The port number.
us
: The timing interval of the timer.
Return value:
OPRT_OK
: Success.tuya_error_code.h
.OPERATE_RET tkl_timer_stop(TKL_TIMER_PORT_E port);
port
: The port number.OPRT_OK
: Success.tuya_error_code.h
.OPERATE_RET tkl_timer_get(TKL_TIMER_PORT_E port, UINT_T *us);
port
: The port number.us
: The timing interval value, unit: μs, corresponding to the value set by tkl_timer_start
.OPRT_OK
: Success.tuya_error_code.h
.OPERATE_RET tkl_timer_get_current_value(TUYA_TIMER_NUM_E timer_id, UINT_T *us);
timer_id
: The port number.us
: The actual count value, unit: μs.OPRT_OK
: Success.tuya_error_code.h
.static VOID tkl_timer_isr_cb_fun(VOID *args)
{
PR_NOTICE("hw_timer test");
}
void tuya_timer_test(void)
{
OPERATE_RET ret;
TUYA_TIMER_BASE_CFG_T cfg;
UINT_T interval_us;
UINT_T get_us;
cfg.mode = TUYA_TIMER_MODE_PERIOD;
cfg.cb = tkl_timer_isr_cb_fun;
cfg.arg = NULL;
ret = tkl_timer_init(TUYA_TIMER_NUM_0, &cfg);
if (ret != OPRT_OK) {
//Failed
return;
}
ret = tkl_timer_start(TUYA_TIMER_NUM_0, 1000);
if (ret != OPRT_OK) {
//Failed
return;
}
tkl_system_delay(5000);
ret = tkl_timer_stop(TUYA_TIMER_NUM_0);
if (ret != OPRT_OK) {
//Failed
return;
}
ret = tkl_timer_get(TUYA_TIMER_NUM_0, &interval_us);
if (ret != OPRT_OK) {
//Failed
return;
}
if(interval_us != 2000){
interval_us = 2000;
}
ret = tkl_timer_start(TUYA_TIMER_NUM_0, interval_us);
if (ret != OPRT_OK) {
//Failed
return;
}
tkl_system_delay(1000);
ret = tkl_timer_get_current_value(TUYA_TIMER_NUM_0, &get_us);
if (ret != OPRT_OK) {
//Failed
return;
}
PR_DEBUG("current run time:%d us", get_us);
tkl_system_delay(5000);
//Deinitialize the timer
ret = tkl_timer_deinit(TUYA_TIMER_NUM_0);
if (ret != 0) {
//Failed
}
}
Is this page helpful?
YesFeedbackIs this page helpful?
YesFeedback