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 <rtdevice.h>
34 #include <rtdef.h>
35 #include <stdlib.h>
36 #include "utest.h"
37 #include <math.h>
38 #include "drv_ts.h"
39 #include <page.h>
40
41 /*
42 * 测试温度传感器驱动的读取功能
43 *
44 * 1. 查找名为 "ts" 的温度传感器设备。
45 * 2. 打开设备,并确保设备成功初始化。
46 * 3. 通过 rt_device_read 接口读取温度数据:
47 * - 每次读取的数据类型为 double;
48 * - 读取操作执行 5 次,以连续获取温度值;
49 * - 每次读取后打印温度值,格式为 "Temperature = XX.XX C"。
50 * 4. 读取完成后,关闭设备并释放申请的内存。
51 *
52 * 硬件说明:
53 * - 本测试基于 K230-01studio 开发板;
54 * - 温度传感器为板载设备;
55 * - 驱动已将硬件寄存器读取的原始 ADC 值转换为摄氏度的 double 值;
56 * - 测试过程中,可在串口终端实时观察温度变化。
57 */
58
59 #define TS_DEV_NAME "ts"
60 rt_device_t ts_dev = RT_NULL;
61
test_ts_read(void)62 static void test_ts_read(void)
63 {
64 rt_uint32_t reval;
65 rt_uint32_t cnt;
66 rt_err_t ret = RT_EOK;
67 double temp = 0;
68 ts_dev = (rt_device_t)rt_device_find(TS_DEV_NAME);
69 uassert_not_null(ts_dev);
70 ret = rt_device_open(ts_dev, RT_DEVICE_OFLAG_RDWR);
71 uassert_int_equal(ret, RT_EOK);
72
73 for(cnt = 0; cnt < 5; cnt++)
74 {
75 reval = rt_device_read(ts_dev, 0, &temp, sizeof(double));
76 uassert_true(reval > 0);
77 LOG_I("Temperature = %.2f C\n", temp);
78 rt_thread_mdelay(1000);
79 }
80 rt_device_close(ts_dev);
81 }
82
test_ts_control(void)83 static void test_ts_control(void)
84 {
85 rt_err_t ret = RT_EOK;
86 uint8_t val;
87
88 ts_dev = (rt_device_t)rt_device_find(TS_DEV_NAME);
89 uassert_not_null(ts_dev);
90 ret = rt_device_open(ts_dev, RT_DEVICE_OFLAG_RDWR);
91 uassert_int_equal(ret, RT_EOK);
92
93 /* SET_MODE */
94 val = 1;
95 ret = rt_device_control(ts_dev, RT_DEVICE_TS_CTRL_SET_MODE, &val);
96 uassert_int_equal(ret, RT_EOK);
97
98 /* GET_MODE */
99 val = 0xFF;
100 ret = rt_device_control(ts_dev, RT_DEVICE_TS_CTRL_GET_MODE, &val);
101 uassert_int_equal(ret, RT_EOK);
102 LOG_I("Current MODE = %d\n", val);
103
104 /* SET_TRIM */
105 val = 2;
106 ret = rt_device_control(ts_dev, RT_DEVICE_TS_CTRL_SET_TRIM, &val);
107 uassert_int_equal(ret, RT_EOK);
108
109 /* GET_TRIM */
110 val = 0xFF;
111 ret = rt_device_control(ts_dev, RT_DEVICE_TS_CTRL_GET_TRIM, &val);
112 uassert_int_equal(ret, RT_EOK);
113 LOG_I("Current TRIM = %d\n", val);
114
115 rt_device_close(ts_dev);
116 }
117
utest_tc_init(void)118 static rt_err_t utest_tc_init(void)
119 {
120 return RT_EOK;
121 }
122
utest_tc_cleanup(void)123 static rt_err_t utest_tc_cleanup(void)
124 {
125 return RT_EOK;
126 }
127
testcase(void)128 static void testcase(void)
129 {
130 UTEST_UNIT_RUN(test_ts_read);
131 UTEST_UNIT_RUN(test_ts_control);
132 }
133 UTEST_TC_EXPORT(testcase, "ts", utest_tc_init, utest_tc_cleanup, 100);