test_ts.c 4.1 KB

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