Timer

Last Updated on : 2025-04-27 06:36:47download

Overview

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.

API description

tkl_timer_init

OPERATE_RET tkl_timer_init(TUYA_TIMER_NUM_E timer_id, TUYA_TIMER_BASE_CFG_T *cfg);
  • Features:

    • Initialize the specified timer instance using the port number and basic configuration, and return the initialization result.
  • 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.
    • For more information, see tuya_error_code.h.

tkl_timer_deinit

OPERATE_RET tkl_timer_deinit(TUYA_TIMER_NUM_E timer_id);
  • Features:
    • Deinitialize the timer instance.
    • This interface will stop the timer and release related software and hardware resources.
  • Parameters:
    • timer_id: The port number.
  • Return value:
    • OPRT_OK: Success.
    • For more information, see tuya_error_code.h.

tkl_timer_start

OPERATE_RET tkl_timer_start(TUYA_TIMER_NUM_E timer_id, UINT_T us);
  • Features:

    • Start the timer.
  • Parameters:

    • timer_id: The port number.

    • us: The timing interval of the timer.

  • Return value:

    • OPRT_OK: Success.
    • For more information, see tuya_error_code.h.

tkl_timer_stop

OPERATE_RET tkl_timer_stop(TKL_TIMER_PORT_E port);
  • Features:
    • Stop the timer.
  • Parameters:
    • port: The port number.
  • Return value:
    • OPRT_OK: Success.
    • For more information, see tuya_error_code.h.

tkl_timer_get

OPERATE_RET tkl_timer_get(TKL_TIMER_PORT_E port, UINT_T *us);
  • Features:
    • Get the timing interval of the timer.
  • Parameters:
    • port: The port number.
    • us: The timing interval value, unit: μs, corresponding to the value set by tkl_timer_start.
  • Return value:
    • OPRT_OK: Success.
    • For more information, see tuya_error_code.h.

tkl_timer_get_current_value

OPERATE_RET tkl_timer_get_current_value(TUYA_TIMER_NUM_E timer_id, UINT_T *us);
  • Features:
    • Get the actual count value of the timer.
  • Parameters:
    • timer_id: The port number.
    • us: The actual count value, unit: μs.
  • Return value:
    • OPRT_OK: Success.
    • For more information, see tuya_error_code.h.

Example

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
    }
}