1 /*
2  * Copyright (c) 2006-2023, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author         Notes
8  * 2023-04-01     wcx1024979076  first version.
9  */
10 /*
11  * 程序清单:这是一个 hwtimer 设备使用例程
12  * 例程导出了 hwtimer_sample 命令到控制终端
13  * 命令调用格式:hwtimer_sample
14  * 程序功能:硬件定时器超时回调函数周期性的打印当前tick值,2次tick值之差换算为时间等同于定时时间值。
15 */
16 
17 #include <rtthread.h>
18 #include <rtdevice.h>
19 
20 #ifdef BSP_USING_HWTIMER
21 
22 #define HWTIMER_DEV_NAME   "timer0"     /* 定时器名称 */
23 
24 /* 定时器超时回调函数 */
timeout_cb(rt_device_t dev,rt_size_t size)25 static rt_err_t timeout_cb(rt_device_t dev, rt_size_t size)
26 {
27     rt_kprintf("this is hwtimer timeout callback fucntion!\n");
28     rt_kprintf("tick is :%d !\n", rt_tick_get());
29 
30     return 0;
31 }
32 
hwtimer_sample(int argc,char * argv[])33 static int hwtimer_sample(int argc, char *argv[])
34 {
35     rt_err_t ret = RT_EOK;
36     rt_hwtimerval_t timeout_s;      /* 定时器超时值 */
37     rt_device_t hw_dev = RT_NULL;   /* 定时器设备句柄 */
38     rt_hwtimer_mode_t mode;         /* 定时器模式 */
39     rt_uint32_t freq = 10000;               /* 计数频率 */
40 
41     /* 查找定时器设备 */
42     hw_dev = rt_device_find(HWTIMER_DEV_NAME);
43     if (hw_dev == RT_NULL)
44     {
45         rt_kprintf("hwtimer sample run failed! can't find %s device!\n", HWTIMER_DEV_NAME);
46         return -RT_ERROR;
47     }
48 
49     /* 以读写方式打开设备 */
50     ret = rt_device_open(hw_dev, RT_DEVICE_OFLAG_RDWR);
51     if (ret != RT_EOK)
52     {
53         rt_kprintf("open %s device failed!\n", HWTIMER_DEV_NAME);
54         return ret;
55     }
56 
57     /* 设置超时回调函数 */
58     rt_device_set_rx_indicate(hw_dev, timeout_cb);
59 
60     /* 设置计数频率(若未设置该项,默认为1Mhz 或 支持的最小计数频率) */
61     rt_device_control(hw_dev, HWTIMER_CTRL_FREQ_SET, &freq);
62     /* 设置模式为周期性定时器(若未设置,默认是HWTIMER_MODE_ONESHOT)*/
63     mode = HWTIMER_MODE_PERIOD;
64     ret = rt_device_control(hw_dev, HWTIMER_CTRL_MODE_SET, &mode);
65     if (ret != RT_EOK)
66     {
67         rt_kprintf("set mode failed! ret is :%d\n", ret);
68         return ret;
69     }
70 
71     /* 设置定时器超时值为2s并启动定时器 */
72     timeout_s.sec = 2;      /* 秒 */
73     timeout_s.usec = 0;     /* 微秒 */
74     if (rt_device_write(hw_dev, 0, &timeout_s, sizeof(timeout_s)) != sizeof(timeout_s))
75     {
76         rt_kprintf("set timeout value failed\n");
77         return -RT_ERROR;
78     }
79 
80     /* 延时1500ms */
81     rt_thread_mdelay(1500);
82 
83     /* 读取定时器当前值 */
84     rt_device_read(hw_dev, 0, &timeout_s, sizeof(timeout_s));
85     rt_kprintf("Read: Sec = %d, Usec = %d\n", timeout_s.sec, timeout_s.usec);
86 
87     return ret;
88 }
89 /* 导出到 msh 命令列表中 */
90 MSH_CMD_EXPORT(hwtimer_sample, hwtimer sample);
91 
92 #endif /* RT_USING_HWTIMER */
93