Timer

Last Updated on : 2022-11-24 09:20:23

API list

Function name Description
hardware_timer_enable Start a timer.
timer_hardware_start_with_id Create a timer.
timer_hardware_stop_100us Cancel a timer.
dev_current_millisecond_ticks_get Get the ticks (ms) of the system clock.

API description

hardware_timer_enable

Function prototype

void hardware_timer_enable(void);

timer_hardware_start_with_id

Function prototype

void timer_hardware_start_with_id(TIMER_ID_T i, uint32_t t, TIMER_RELOAD_FLAG_T flag, hardware_timer_func_t func);

Parameter description

Parameter name Parameter type Description
i TIMER_ID_T The values in the TIMER_ID_T enum.
flag TIMER_RELOAD_FLAG_T The values in the TIMER_RELOAD_FLAG_T enum, indicating whether to reload a timer.
func hardware_timer_func_t The callback.

timer_hardware_stop_100us

Function prototype

void timer_hardware_stop_100us(TIMER_ID_T id);

Parameter description

Parameter name Parameter type Description
id TIMER_ID_T The values in the TIMER_ID_T enum.

dev_current_millisecond_ticks_gets

Function prototype

uint32_t  dev_current_millisecond_ticks_get(void);

Parameter description

Parameter name Parameter type Description
id TIMER_ID_T The values in the TIMER_ID_T enum.

How to use a timer

Here is an example of using the SysTick timer. For more information about the interface definition, see hal_systick_timer.h.

void __hardware_timer_func_t(TIMER_ID_T timer_id)
{
    static uint8_t test_times = 100;
    
    switch(timer_id) {
        case V_TIMER0: { /// Start a repeating timer that is called within less than 1,000 ms.
            if((--test_times) == 0) {
                timer_hardware_stop_100us(timer_id); /// The timer stops after it is run 100 times.
            }
            if(timer_hardware_is_active(timer_id)) {
                //TODO: The timer is valid.
            }
            else {
                //TODO:TODO: The timer is invalid.
            }
            
            //TODO:
            break;
        }
        
        case V_TIMER1: { /// Start a one-time timer that is called within less than 500 ms.
            break;
        }
    }
}
 
static void systick_timer_demo()
{
    hardware_timer_enable();
    
    timer_hardware_start_with_id(V_TIMER0, 10000, HARDWARE_TIMER_AUTO_RELOAD_ENABLE, __hardware_timer_func_t);
    timer_hardware_start_with_id(V_TIMER1, 5000, HARDWARE_TIMER_AUTO_RELOAD_DISABLE, __hardware_timer_func_t);
}