test_timer.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 <drivers/hwtimer.h>
  32. #include "../interdrv/hwtimer/drv_timer.h"
  33. #include "utest.h"
  34. /*
  35. * This test case is designed to test the hardware timer driver.
  36. * It will:
  37. * 1. Find two hardware timer devices.
  38. * 2. Open both devices.
  39. * 3. Set a custom frequency for timer0 and use the default frequency for timer1.
  40. * 4. Start both timers with different timeout values.
  41. * 5. Poll and print the current value of each timer every second.
  42. * 6. Trigger the interrupt callback when the timer times out and print a message.
  43. */
  44. #define DEVICE_NAME0 "hwtimer0"
  45. #define DEVICE_NAME1 "hwtimer1"
  46. static rt_device_t tmr_dev_0;
  47. static rt_device_t tmr_dev_1;
  48. #define TIMEOUT_SEC_0 10
  49. #define TIMEOUT_SEC_1 5
  50. #define MAX_TIMEOUT_SEC \
  51. (TIMEOUT_SEC_0 > TIMEOUT_SEC_1 ? TIMEOUT_SEC_0 : TIMEOUT_SEC_1)
  52. static rt_err_t tmr_timeout_cb(rt_device_t dev, rt_size_t size)
  53. {
  54. struct rt_hwtimer_device *rt_timer = rt_container_of(dev, struct rt_hwtimer_device, parent);
  55. struct k230_timer *kd_timer = rt_container_of(rt_timer, struct k230_timer, device);
  56. LOG_I("---> [%s] timeout callback fucntion!\n", kd_timer->name);
  57. return RT_EOK;
  58. }
  59. static void test_hwtimer(void)
  60. {
  61. rt_hwtimerval_t timerval;
  62. rt_hwtimer_mode_t mode;
  63. rt_size_t tsize;
  64. rt_uint32_t freq = 25000000; /* Frequency options: 12.5M 25M 50M 100M */
  65. rt_err_t ret;
  66. rt_ssize_t size;
  67. int loop_count = 0;
  68. LOG_I("test_hwtimer start");
  69. tmr_dev_0 = rt_device_find(DEVICE_NAME0);
  70. uassert_not_null(tmr_dev_0);
  71. tmr_dev_1 = rt_device_find(DEVICE_NAME1);
  72. uassert_not_null(tmr_dev_1);
  73. ret = rt_device_open(tmr_dev_0, RT_DEVICE_OFLAG_RDWR);
  74. uassert_int_equal(ret, RT_EOK);
  75. ret = rt_device_open(tmr_dev_1, RT_DEVICE_OFLAG_RDWR);
  76. uassert_int_equal(ret, RT_EOK);
  77. ret = rt_device_control(tmr_dev_0, HWTIMER_CTRL_FREQ_SET, &freq);
  78. uassert_int_equal(ret, RT_EOK);
  79. ret = rt_device_set_rx_indicate(tmr_dev_0, tmr_timeout_cb);
  80. uassert_int_equal(ret, RT_EOK);
  81. ret = rt_device_set_rx_indicate(tmr_dev_1, tmr_timeout_cb);
  82. uassert_int_equal(ret, RT_EOK);
  83. timerval.sec = TIMEOUT_SEC_0;
  84. timerval.usec = 0;
  85. tsize = sizeof(timerval);
  86. mode = HWTIMER_MODE_ONESHOT;
  87. ret = rt_device_control(tmr_dev_0, HWTIMER_CTRL_MODE_SET, &mode);
  88. uassert_int_equal(ret, RT_EOK);
  89. size = rt_device_write(tmr_dev_0, 0, &timerval, tsize);
  90. uassert_int_equal(size, tsize);
  91. LOG_I("timer0 start: [%d:%d]\n", timerval.sec, timerval.usec);
  92. timerval.sec = TIMEOUT_SEC_1;
  93. timerval.usec = 0;
  94. tsize = sizeof(timerval);
  95. mode = HWTIMER_MODE_ONESHOT;
  96. ret = rt_device_control(tmr_dev_1, HWTIMER_CTRL_MODE_SET, &mode);
  97. uassert_int_equal(ret, RT_EOK);
  98. size = rt_device_write(tmr_dev_1, 0, &timerval, tsize);
  99. uassert_int_equal(size, tsize);
  100. LOG_I("timer1 start: [%d:%d]\n", timerval.sec, timerval.usec);
  101. while (loop_count++ < MAX_TIMEOUT_SEC + 1)
  102. {
  103. size = rt_device_read(tmr_dev_0, 0, &timerval, sizeof(timerval));
  104. uassert_int_equal(size, sizeof(timerval));
  105. LOG_I("timer0: [%d:%d]\n", timerval.sec, timerval.usec);
  106. size = rt_device_read(tmr_dev_1, 0, &timerval, sizeof(timerval));
  107. uassert_int_equal(size, sizeof(timerval));
  108. LOG_I("timer1: [%d:%d]\n", timerval.sec, timerval.usec);
  109. rt_thread_mdelay(1000);
  110. }
  111. ret = rt_device_close(tmr_dev_0);
  112. uassert_int_equal(ret, RT_EOK);
  113. ret = rt_device_close(tmr_dev_1);
  114. uassert_int_equal(ret, RT_EOK);
  115. LOG_I("test_hwtimer end");
  116. }
  117. static void hw_timer_testcase(void)
  118. {
  119. UTEST_UNIT_RUN(test_hwtimer);
  120. }
  121. static rt_err_t utest_tc_init(void)
  122. {
  123. return RT_EOK;
  124. }
  125. static rt_err_t utest_tc_cleanup(void)
  126. {
  127. return RT_EOK;
  128. }
  129. UTEST_TC_EXPORT(hw_timer_testcase, "timer", utest_tc_init, utest_tc_cleanup, 10);