This article introduces the usage of TImer of Lanxun ab32vg1, ab32vg1 has 5 TImers, of which TImer0, TImer1, timer2 only support 32-bit timer function, timer3, timer4, timer5 can be configured as timer mode, counter mode, Enter capture mode and pwm mode. This article first introduces the api functions provided by rt-thread, then lists the detailed steps for using sdk, and finally analyzes the code.

1. API of timer provided by rt-thread

rt-thread manages the peripherals on the soc through the I/O device model, which is divided into three layers from top to bottom: the I/O device management layer, the device driver framework layer and the device driver layer. The HAL library of stm32 belongs to the device driver layer. For example, the well-known peripheral drivers of i2c and spi are ready when the code is generated with cubemx. The device driver of the ab32vg1 of Lanxun has been implemented in the sdk by the engineers of Lanxun. The device driver framework layer and the device I/O management layer above the device driver layer should be explained: the device driver framework layer provides some interfaces for device driver developers to implement, only needed when doing driver transplantation, as Ordinary users only need to care about the I/O management layer. The I/O management layer of rt-thread provides APIs similar to file IO in linux, such as rt_device_find, rt_device_open, rt_device_read, rt_device_close and so on. The APIs of hwtimer are listed below, combined with examples to understand how to use these APIs to implement timer functions.

rt_device_trt_device_find(const char* name): Find the device, name is the device name.

rt_err_trt_device_open(rt_device_t dev, rt_uint16_t oflags): open the timer device, dev is the timer device handle, oflags: open mode, generally take RT_DEVICE_OFLAG_RDWR.

rt_err_trt_device_set_rx_indicate(rt_device_t dev, rt_err_t (*rx_ind)(rt_device_tdev,rt_size_t size)): set timeout callback, dev: timer device handle rx_ind: timeout callback function

rt_err_trt_device_control(rt_device_t dev, rt_uint8_t cmd, void* arg): control timer, dev: timer device handle cmd: control command, the possible values ​​are as follows:

HWTIMER_CTRL_FREQ_SET sets the count frequency

HWTIMER_CTRL_STOP stops the timer

HWTIMER_CTRL_INFO_GET Get timer feature information

HWTIMER_CTRL_MODE_SET set timer mode

arg: When the control command parameter sets the timer mode, the possible values ​​are as follows:



rt_size_trt_device_write(rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_tsize): set the timer timeout value, dev: timer device handle pos: offset value, unused, can take 0 value buffer: point to the timeout time structure size: timeout time structure size

rt_size_trt_device_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size ): get the current value of the timer, dev: timer handle pos: offset value, unused, can take 0 value buffer: timeout structure size: timeout structure body size.

rt_err_trt_device_close(rt_device_t dev): Close the timer.

2. Application steps of hardware timer

The configuration process of the timer: first, use rt_device_find to find the timer handle according to the device name, use the timer handle to open the timer, then set the callback function of the timer, set the timer value after configuring the timer, and then start the timer. After that, every time the timer’s counter overflows, the timer’s callback function will be executed once.

3. Specific application cases

3.1 Use rt-thread studio to create a new BSP project, select the project based on the development board, select AB32VG1-AB-PROUGEN for the development model, and click Finish.

3.2 Configure rt-thread setting

Double-click RT-Thread Setting to enter the RT-Thread Setting configuration interface, as shown below

Then click on More Configurations

Check the timer under the hardware option to enable timer 1

Press the shortcut key Ctrl+S to update the package. Then compile the project and download the program to the development board. In the Downloader software window, msh command lsit_device, you can see the print result timer device timer1, where timer1 is the name of the timer, which is used when operating the timer.

3.3 Add test source files

Select applications, right-click to add the source file timer.c.

Then copy and paste the following sample code into timer.c,


* Copyright (c) 2006-2021, RT-ThreadDevelopment Team


* SPDX-License-Identifier: Apache-2.0


* Change Logs:

* Date           Author       Notes

* 2021-12-11     admin       the first version




#defineHWTIMER_DEV_NAME “timer1” /* timer name */

/* Timer timeout callback function */

staticrt_err_ttimeout_cb(rt_device_t dev, rt_size_t size)


rt_kprintf(“this is hwtimer timeoutcallback fucntion!\n”);

rt_kprintf(“tick is :%d !\n”, rt_tick_get());



static hwtimer_sample(intargc,char*argv[])


rt_err_t ret = RT_EOK;

rt_hwtimerval_t timeout_s; /* Timer timeout value*/

rt_device_t hw_dev = RT_NULL; /* Timer device handle*/

rt_hwtimer_mode_t mode; /* Timer mode*/

/* Find the timer device */

hw_dev = rt_device_find(HWTIMER_DEV_NAME);

    if(hw_dev == RT_NULL)


rt_kprintf(“hwtimer sample run failed!can’t find %s device!\n”, HWTIMER_DEV_NAME);



/* Open the device for reading and writing */

ret = rt_device_open(hw_dev,RT_DEVICE_OFLAG_RDWR);

    if(right != RT_EOK)


rt_kprintf(“open %s device failed!\n”, HWTIMER_DEV_NAME);



/* Set the timeout callback function */


/* set mode to periodic timer */


ret = rt_device_control(hw_dev, HWTIMER_CTRL_MODE_SET, &mode);

    if(right != RT_EOK)


rt_kprintf(“set mode failed! ret is:%d\n”, ret);



/* Set the timer timeout value to 5s and start the timer */

timeout_s.sec = 5; /* seconds*/

timeout_s.usec = 0; /* microseconds*/

    if(rt_device_write(hw_dev, 0, &timeout_s,sizeof(timeout_s)) !=sizeof(timeout_s))


rt_kprintf(“set timeout value failed\n”);



/* Delay 3500ms */


/* Read the current value of the timer */

    rt_device_read(hw_dev, 0, &timeout_s,sizeof(timeout_s));

rt_kprintf(“Read: Sec = %d, Usec =%d\n”, timeout_s.sec, timeout_s.usec);



/* Export to msh command list */

MSH_CMD_EXPORT(hwtimer_sample,hwtimer sample);

3.4 Compile the project

3.5 Download Project

3.6 Running effect

This routine exports the hwtimer_sample command to the control terminal. The command call format is hwtimer_sample. You only need to input hwtimer_sample in the msh window to start the timer. The hardware timer timeout callback function periodically prints the current tick value, and the difference between the two tick values ​​is converted to time equal to the timing value. The print content of running is as follows: the value of two ticks is 5000ms, that is, the timing time is 5s.

4. Chapter Summary

Using rt-threadstudio for sdk development is a very efficient thing. After creating a new bsp project, you only need to configure the required hardware functions in the rt-thread setting, and then you can use the device I/O management interface provided by rt-thread to control the underlying SoC peripherals are controlled.

Reviewing Editor: Liu Qing

Leave a Reply

Your email address will not be published.