MCPcopy Create free account
hub / github.com/RT-Thread/rt-thread / hwtimer_sample

Function hwtimer_sample

bsp/hc32/tests/test_hwtimer.c:58–161  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

56}
57
58static int hwtimer_sample(int argc, char *argv[])
59{
60 rt_uint8_t i;
61 rt_err_t ret = RT_EOK;
62 rt_hwtimerval_t timeout_s; /* 定时器超时值 */
63 rt_hwtimer_mode_t mode = HWTIMER_MODE_ONESHOT; /* 定时器模式 */
64 rt_device_t hw_dev = RT_NULL; /* 定时器设备句柄 */
65 rt_hwtimer_t *hwtimer;
66 float t;
67 rt_uint8_t loop_cnt; /* 循环打印次数 */
68 rt_hwtimerval_t overflow_tv; /* 定时器超时值 */
69 rt_uint32_t timer_out_s;
70
71 if ((argc != 4) || (rt_strcmp("oneshot", argv[2]) && rt_strcmp("period", argv[2])))
72 {
73 _hwtimer_cmd_print_usage();
74 return -RT_ERROR;
75 }
76
77 /* 查找定时器设备 */
78 hw_dev = rt_device_find(argv[1]);
79 if (hw_dev == RT_NULL)
80 {
81 rt_kprintf("hwtimer sample run failed! can't find %s device!\n", argv[1]);
82 return -RT_ERROR;
83 }
84 else
85 {
86 hwtimer = (rt_hwtimer_t *)hw_dev;
87 }
88
89 /* 以读写方式打开设备 */
90 ret = rt_device_open(hw_dev, RT_DEVICE_OFLAG_RDWR);
91 if (ret != RT_EOK)
92 {
93 rt_kprintf("open %s device failed!\n", argv[1]);
94 return ret;
95 }
96
97 /* 设置模式 */
98 if (0 == rt_strcmp(argv[2], "oneshot"))
99 {
100 mode = HWTIMER_MODE_ONESHOT;
101 loop_cnt = 1;
102 }
103 else if (0 == rt_strcmp(argv[2], "period"))
104 {
105 mode = HWTIMER_MODE_PERIOD;
106 loop_cnt = 5;
107 }
108 rt_device_control(hw_dev, HWTIMER_CTRL_MODE_SET, &mode);
109
110 /* 设置超时回调函数 */
111 rt_device_set_rx_indicate(hw_dev, timeout_cb);
112
113 /* 设置定时器超时并启动定时器 */
114 timeout_s.sec = atoi(argv[3]) / 1000U; /* 秒 */
115 timeout_s.usec = (atoi(argv[3]) % 1000U) * 1000U; /* 微秒 */

Callers

nothing calls this directly

Calls 13

rt_strcmpFunction · 0.85
_hwtimer_cmd_print_usageFunction · 0.85
rt_device_findFunction · 0.85
rt_kprintfFunction · 0.85
rt_device_openFunction · 0.85
rt_device_controlFunction · 0.85
rt_device_writeFunction · 0.85
rt_tick_getFunction · 0.85
rt_thread_mdelayFunction · 0.85
rt_device_readFunction · 0.85
rt_device_closeFunction · 0.85

Tested by

no test coverage detected