This is the documentation for the latest (main) development branch. If you are looking for the documentation of previous releases, use the drop-down menu on the left and select the desired version.

TIMER

Introduction

A Timer is a hardware module used for precise time control and event counting. In embedded systems, timers can be used to generate accurate time delays, count events, generate timing interrupts, and more. They are essential tools for implementing real-time operating system clocks, PWM (Pulse Width Modulation) signal generation, pulse counting, and event timing.

The W800 chip supports multiple timers, each of which can independently configure parameters such as count period and auto-reload. The flexibility and high precision of the timer module make it widely used in various embedded applications.

Function List

  1. Initialization — Initializes the timer driver.

  2. Set Count Period — Sets the timer’s count period.

  3. Set Auto-Reload — Configures the timer’s auto-reload mode.

  4. Start Timer — Starts the timer.

  5. Stop Timer — Stops the timer.

  6. Read Counter Value — Reads the current count value of the timer.

  7. Register Interrupt Callback — Registers a timer interrupt callback function.

Function Overview

Set and Get Count Values: Specifies how timers count from a starting point and how to retrieve the count value at any time.

Multi-Channel Support: The W800 supports up to 6 independent timer channels.

Flexible Parameter Configuration: Timers support configuring parameters such as clock source, count period, and auto-reload.

High Precision: Timers provide high-precision time control capabilities, suitable for various precise timing applications.

Interrupts and Callbacks: Timers support interrupts and callback functions, which can be triggered when the timer overflows or reaches a specific count value.

Main Functions

Initializing Timer

Before using a timer, you need to call the wm_drv_timer_init() function to allocate resources for the timer, using the wm_device_t structure to receive the timer device identifier. Example:

wm_device_t *timer_dev;
timer_dev = wm_drv_timer_init("timer0");

The first parameter specifies the device name, defined in the device table, ranging from timer0 to timer5.

Warning

After initializing the timer, if wm_drv_timer_deinit() is not called, calling wm_drv_timer_init() again will return NULL.

Setting Count Period

Call the function wm_drv_timer_set_period() to set the timer’s count period, in microseconds. Example:

wm_device_t *timer_dev;
timer_dev = wm_drv_timer_init("timer0");

wm_drv_timer_set_period(timer_dev, 1000);

The first parameter is a pointer to the timer device, of type wm_device_t*.

The second parameter is an unsigned 32-bit integer representing the timer’s count period, in microseconds. In this example, 1000 means the timer’s count period is 1000 microseconds (i.e., 1 millisecond).

Setting Auto-Reload

Call the function wm_drv_timer_set_auto_reload() to configure the timer’s auto-reload mode. Example:

wm_device_t *timer_dev;
timer_dev = wm_drv_timer_init("timer0");

wm_drv_timer_set_auto_reload(timer_dev, true);

The first parameter is a pointer to the timer device, of type wm_device_t*.

The second parameter is the auto-reload option, of type bool. Setting it to true enables the auto-reload function, and setting it to false disables the auto-reload function.

Starting the Timer

Call the function wm_drv_timer_start() to start the timer. Example:

wm_device_t *timer_dev;
timer_dev = wm_drv_timer_init("timer0");

wm_drv_timer_cfg_t timer_cfg = {
.unit = WM_HAL_TIMER_UNIT_US,
.period = 1000,
.auto_reload = true
    };
wm_drv_timer_start(timer_dev, timer_cfg);

The first parameter is a pointer to the timer device, of type wm_device_t*.

The second parameter is used to configure the timer’s related parameters, of type wm_drv_timer_cfg_t. Here, unit specifies the timer’s unit, of type wm_hal_timer_unit_t. It is set to WM_HAL_TIMER_UNIT_US, indicating that the timer’s unit is microseconds (us). period specifies the timer’s period, of type uint32_t, here set to 1000, indicating the timer’s period is 1000 microseconds (1 millisecond). auto_reload sets whether the timer auto-reloads, of type bool. Here, it is set to true, indicating that the timer will automatically reload and start counting again when the period ends.

Stopping the Timer

Call the function wm_drv_timer_stop() to stop the timer. Example:

wm_device_t *timer_dev;
timer_dev = wm_drv_timer_init("timer0");

wm_drv_timer_stop(timer_dev);

The parameter is a pointer to the timer device, of type wm_device_t*.

Reading Counter Value

Call the function wm_drv_timer_get_counter() to read the current count value of the timer. Example:

wm_device_t *timer_dev;
timer_dev = wm_drv_timer_init("timer0");

uint32_t counter;
wm_drv_timer_get_counter(timer_dev, &counter);

The first parameter is a pointer to the timer device, of type wm_device_t*.

The second parameter is a pointer to a uint32_t type, used to store the current count value of the timer.

Registering Interrupt Callback

Call the function wm_drv_timer_register_callback() to register a timer interrupt callback function. Example:

wm_device_t *timer_dev;
timer_dev = wm_drv_timer_init("timer0");

void timer_callback(void *arg) {
    printf("Timer interrupt triggered!\n");
}

wm_drv_timer_register_callback(timer_dev, timer_callback, NULL);

The first parameter is a pointer to the timer device, of type wm_device_t*.

The second parameter is the callback function, of type wm_drv_timer_callback_t. This function is called when the timer interrupt is triggered.

The third parameter is private data passed to the callback function, of type void *.

Application Example

For basic examples of using timers, refer to: examples/peripheral/timer

API Reference

For related TIMER APIs, refer to:

TIMER API Reference