1 /* Copyright (c) 2023, Canaan Bright Sight Co., Ltd
2  *
3  * Redistribution and use in source and binary forms, with or without
4  * modification, are permitted provided that the following conditions are met:
5  * 1. Redistributions of source code must retain the above copyright
6  * notice, this list of conditions and the following disclaimer.
7  * 2. Redistributions in binary form must reproduce the above copyright
8  * notice, this list of conditions and the following disclaimer in the
9  * documentation and/or other materials provided with the distribution.
10  *
11  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
12  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
13  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
14  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
15  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
16  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
17  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
18  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
21  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
22  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26  /*
27  * Copyright (c) 2006-2025, RT-Thread Development Team
28  *
29  * SPDX-License-Identifier: Apache-2.0
30  */
31 
32 #include <rtthread.h>
33 #include <drivers/hwtimer.h>
34 
35 #include "../interdrv/hwtimer/drv_timer.h"
36 #include "utest.h"
37 
38 /*
39  * This test case is designed to test the hardware timer driver.
40  * It will:
41  * 1. Find two hardware timer devices.
42  * 2. Open both devices.
43  * 3. Set a custom frequency for timer0 and use the default frequency for timer1.
44  * 4. Start both timers with different timeout values.
45  * 5. Poll and print the current value of each timer every second.
46  * 6. Trigger the interrupt callback when the timer times out and print a message.
47  */
48 
49 #define DEVICE_NAME0 "hwtimer0"
50 #define DEVICE_NAME1 "hwtimer1"
51 
52 static rt_device_t tmr_dev_0;
53 static rt_device_t tmr_dev_1;
54 
55 #define TIMEOUT_SEC_0 10
56 #define TIMEOUT_SEC_1 5
57 #define MAX_TIMEOUT_SEC \
58     (TIMEOUT_SEC_0 > TIMEOUT_SEC_1 ? TIMEOUT_SEC_0 : TIMEOUT_SEC_1)
59 
tmr_timeout_cb(rt_device_t dev,rt_size_t size)60 static rt_err_t tmr_timeout_cb(rt_device_t dev, rt_size_t size)
61 {
62     struct rt_hwtimer_device *rt_timer = rt_container_of(dev, struct rt_hwtimer_device, parent);
63     struct k230_timer *kd_timer = rt_container_of(rt_timer, struct k230_timer, device);
64 
65     LOG_I("---> [%s] timeout callback fucntion!\n", kd_timer->name);
66     return RT_EOK;
67 }
68 
test_hwtimer(void)69 static void test_hwtimer(void)
70 {
71     rt_hwtimerval_t timerval;
72     rt_hwtimer_mode_t mode;
73     rt_size_t tsize;
74     rt_uint32_t freq = 25000000; /* Frequency options: 12.5M 25M 50M 100M */
75     rt_err_t ret;
76     rt_ssize_t size;
77     int loop_count = 0;
78 
79     LOG_I("test_hwtimer start");
80 
81     tmr_dev_0 = rt_device_find(DEVICE_NAME0);
82     uassert_not_null(tmr_dev_0);
83     tmr_dev_1 = rt_device_find(DEVICE_NAME1);
84     uassert_not_null(tmr_dev_1);
85 
86     ret = rt_device_open(tmr_dev_0, RT_DEVICE_OFLAG_RDWR);
87     uassert_int_equal(ret, RT_EOK);
88     ret = rt_device_open(tmr_dev_1, RT_DEVICE_OFLAG_RDWR);
89     uassert_int_equal(ret, RT_EOK);
90 
91     ret = rt_device_control(tmr_dev_0, HWTIMER_CTRL_FREQ_SET, &freq);
92     uassert_int_equal(ret, RT_EOK);
93 
94     ret = rt_device_set_rx_indicate(tmr_dev_0, tmr_timeout_cb);
95     uassert_int_equal(ret, RT_EOK);
96     ret = rt_device_set_rx_indicate(tmr_dev_1, tmr_timeout_cb);
97     uassert_int_equal(ret, RT_EOK);
98 
99     timerval.sec = TIMEOUT_SEC_0;
100     timerval.usec = 0;
101     tsize = sizeof(timerval);
102     mode = HWTIMER_MODE_ONESHOT;
103     ret = rt_device_control(tmr_dev_0, HWTIMER_CTRL_MODE_SET, &mode);
104     uassert_int_equal(ret, RT_EOK);
105     size = rt_device_write(tmr_dev_0, 0, &timerval, tsize);
106     uassert_int_equal(size, tsize);
107     LOG_I("timer0 start: [%d:%d]\n", timerval.sec, timerval.usec);
108 
109     timerval.sec = TIMEOUT_SEC_1;
110     timerval.usec = 0;
111     tsize = sizeof(timerval);
112     mode = HWTIMER_MODE_ONESHOT;
113     ret = rt_device_control(tmr_dev_1, HWTIMER_CTRL_MODE_SET, &mode);
114     uassert_int_equal(ret, RT_EOK);
115     size = rt_device_write(tmr_dev_1, 0, &timerval, tsize);
116     uassert_int_equal(size, tsize);
117     LOG_I("timer1 start: [%d:%d]\n", timerval.sec, timerval.usec);
118 
119     while (loop_count++ < MAX_TIMEOUT_SEC + 1)
120     {
121         size = rt_device_read(tmr_dev_0, 0, &timerval, sizeof(timerval));
122         uassert_int_equal(size, sizeof(timerval));
123         LOG_I("timer0: [%d:%d]\n", timerval.sec, timerval.usec);
124 
125         size = rt_device_read(tmr_dev_1, 0, &timerval, sizeof(timerval));
126         uassert_int_equal(size, sizeof(timerval));
127         LOG_I("timer1: [%d:%d]\n", timerval.sec, timerval.usec);
128 
129         rt_thread_mdelay(1000);
130     }
131 
132     ret = rt_device_close(tmr_dev_0);
133     uassert_int_equal(ret, RT_EOK);
134     ret = rt_device_close(tmr_dev_1);
135     uassert_int_equal(ret, RT_EOK);
136     LOG_I("test_hwtimer end");
137 }
138 
hw_timer_testcase(void)139 static void hw_timer_testcase(void)
140 {
141     UTEST_UNIT_RUN(test_hwtimer);
142 }
143 
utest_tc_init(void)144 static rt_err_t utest_tc_init(void)
145 {
146     return RT_EOK;
147 }
utest_tc_cleanup(void)148 static rt_err_t utest_tc_cleanup(void)
149 {
150     return RT_EOK;
151 }
152 
153 UTEST_TC_EXPORT(hw_timer_testcase, "timer", utest_tc_init, utest_tc_cleanup, 10);