RT-Thread RTOS
An open source embedded real-time operating system
|
Hardware timers generally have two modes of operation, timer mode and counter mode. No matter which mode is operated, it works by counting the pulse signal counted by the internal counter module. Here are some important concepts of timers.
Timer mode: Counts the internal pulse. Timers are often used as timing clocks for timing detection, timing response, and timing control.
Counter mode: The counter can count up or down. The maximum count value of a 16-bit counter is 65535, and the maximum value of a 32-bit counter is 4 294 967 295.
**Counting frequency**:Since the input frequency is usually fixed, the time it takes for the counter to reach its desired count number can be calculated from just the given frequency - time = count value / count frequency
. For example, if the counting frequency is 1 MHz, the counter counts once every 1 / 1000000 seconds. That is, every 1 microsecond, the counter is incremented by one (or subtracted by one), at this time, the maximum timing capability of the 16-bit counter is 65535 microseconds, or 65.535 milliseconds.
The application accesses the hardware timer device through the I/O device management interface provided by RT-Thread. The related interfaces are as follows:
Function | Description |
---|---|
rt_device_find() | to look up the timer device |
rt_device_open() | to open the timer device in read-write mode |
rt_device_set_rx_indicate() | to set the timeout callback function |
rt_device_control() | to control the timer device, you can set the timing mode (single time /cycle),counting frequency, or stop the timer |
rt_device_write() | to set the timeout value of the timer. The timer then starts |
rt_device_read() | to get the current value of the timer |
rt_device_close() | to turn off the timer device. |
The application obtains the device handle based on the hardware timer device name, and thus can operate the hardware timer device. The device function is as follows:
Parameter | Description |
---|---|
name | hardware timer device name |
return | —— |
timer device handle | will return to the corresponding device handle if the corresponding device is found |
RT_NULL | No device found |
In general, the hardware timer device name registered to the system is timer0, timer1, etc. The usage examples are as follows:
With the device handle, the application can open the device. When the device is open, it will detect whether the device has been initialized. If it is not initialized, it will call the initialization interface to initialize the device by default. Open the device with the following function:
Parameter | Description |
---|---|
dev | hardware timer device handle |
oflags | device open mode, is generally opened in read and write mode, which is to take the value:RT_DEVICE_OFLAG_RDWR |
return | —— |
RT_EOK | device opened successfully |
other error code | device fail to open |
An example of use is as follows:
Set the timer timeout callback function with the following function - this is the function that will be called when the timer reaches its set count value:
Parameter | Description |
---|---|
dev | device handle |
rx_ind | timeout callback function, provided by the caller |
return | —— |
RT_EOK | success |
An example of use is as follows:
By sending control words, the application can configure the hardware timer device with the following function:
Parameter | Description |
---|---|
dev | device handle |
cmd | command control word |
arg | controlled parameter |
return | —— |
RT_EOK | function executed successfully |
-RT_ENOSYS | execution failed,dev is null |
other error code | execution failed |
The command control words available for the hardware timer device are as follows:
Control word | Description |
---|---|
HWTIMER_CTRL_FREQ_SET | set the counting frequency |
HWTIMER_CTRL_STOP | stop the timer |
HWTIMER_CTRL_INFO_GET | get timer feature information |
HWTIMER_CTRL_MODE_SET | set timer mode |
Get the timer parameter argument, which is a pointer to the structure struct rt_hwtimer_info, to save the obtained information.
>Setting frequency is valid only when the timer hardware and included driver set the counting frequency. Generally, the default frequency of the driving setting can be used.
When setting the timer mode, the parameter argument can take the following values:
An example of using the timer count frequency and timing mode is as follows:
The timer timeout value can be set by the following function:
Parameter | Description |
---|---|
dev | device handle |
pos | write data offset, unused now, can set 0 value |
buffer | pointer to the timer timeout structure |
size | timeout structure size |
return | —— |
The actual size of the written data | |
0 | fail |
The prototype of the timeout structure is shown below :
An example of using the timer timeout value is as follows:
The current value of the timer can be obtained by the following function:
Parameter | Description |
---|---|
dev | timer device handle |
pos | write data offset, unused now , can set 0 value |
buffer | output parameter, a pointer point to the timeout structure |
size | timeout structure size |
return | —— |
Timeout structure size | success |
0 | fail |
An example of use is shown below:
The timer device can be closed with the following function:
Parameter | Description |
---|---|
dev | timer device handle |
return | —— |
RT_EOK | close device successfully |
-RT_ERROR | the device has been completely shut down and cannot be closed repeatedly |
other error code | fail to close the device |
When a timer device has been used and is not necessary anymore, it should be closed, otherwise the device will remain in an open status.
An example of use is shown below:
>Timing errors may occur. Assume that the counter has a maximum value of 0xFFFF, a counting frequency of 1Mhz, and a timing time of 1 second and 1 microsecond. Since the timer can only count up to 65535us at a time, the timing requirement for 1000001us can be completed 20 times at 50000us, and the calculation error will be 1us.
The specific use of the hardware timer device can refer to the following sample code. The main steps of the sample code are as follows: