timer_sample.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-07-27 thread-liu first version
  9. */
  10. #include <board.h>
  11. #include <rtthread.h>
  12. #include <rtdevice.h>
  13. #define HWTIMER_DEV_NAME "timer14"
  14. static rt_err_t timeout_cb(rt_device_t dev, rt_size_t size)
  15. {
  16. rt_kprintf("this is hwtimer timeout callback fucntion!\n");
  17. rt_kprintf("tick is :%d !\n", rt_tick_get());
  18. return 0;
  19. }
  20. static int hwtimer_stop(void)
  21. {
  22. rt_err_t ret = RT_EOK;
  23. rt_device_t hw_dev = RT_NULL;
  24. hw_dev = rt_device_find(HWTIMER_DEV_NAME);
  25. if (hw_dev == RT_NULL)
  26. {
  27. rt_kprintf("hwtimer sample run failed! can't find %s device!\n", HWTIMER_DEV_NAME);
  28. return RT_ERROR;
  29. }
  30. ret = rt_device_close(hw_dev);
  31. if (ret != RT_EOK)
  32. {
  33. rt_kprintf("close %s device failed!\n", HWTIMER_DEV_NAME);
  34. return ret;
  35. }
  36. return ret;
  37. }
  38. static int hwtimer_start(void)
  39. {
  40. rt_err_t ret = RT_EOK;
  41. rt_hwtimerval_t timeout_s;
  42. rt_device_t hw_dev = RT_NULL;
  43. rt_hwtimer_mode_t mode;
  44. hw_dev = rt_device_find(HWTIMER_DEV_NAME);
  45. if (hw_dev == RT_NULL)
  46. {
  47. rt_kprintf("hwtimer sample run failed! can't find %s device!\n", HWTIMER_DEV_NAME);
  48. return RT_ERROR;
  49. }
  50. /* Open the device in read/write mode */
  51. ret = rt_device_open(hw_dev, RT_DEVICE_OFLAG_RDWR);
  52. if (ret != RT_EOK)
  53. {
  54. rt_kprintf("open %s device failed!\n", HWTIMER_DEV_NAME);
  55. return ret;
  56. }
  57. /* Set the timeout callback function */
  58. rt_device_set_rx_indicate(hw_dev, timeout_cb);
  59. /* Set the mode to periodic timer */
  60. mode = HWTIMER_MODE_PERIOD;
  61. ret = rt_device_control(hw_dev, HWTIMER_CTRL_MODE_SET, &mode);
  62. if (ret != RT_EOK)
  63. {
  64. rt_kprintf("set mode failed! ret is :%d\n", ret);
  65. return ret;
  66. }
  67. timeout_s.sec = 5;
  68. timeout_s.usec = 0;
  69. if (rt_device_write(hw_dev, 0, &timeout_s, sizeof(timeout_s)) != sizeof(timeout_s))
  70. {
  71. rt_kprintf("set timeout value failed\n");
  72. return RT_ERROR;
  73. }
  74. rt_thread_mdelay(3500);
  75. rt_device_read(hw_dev, 0, &timeout_s, sizeof(timeout_s));
  76. rt_kprintf("Read: Sec = %d, Usec = %d\n", timeout_s.sec, timeout_s.usec);
  77. return ret;
  78. }
  79. static int tim_sample(int argc, char *argv[])
  80. {
  81. if (argc > 1)
  82. {
  83. if (!rt_strcmp(argv[1], "start"))
  84. {
  85. rt_kprintf("tim14 will start\n");
  86. hwtimer_start();
  87. return RT_EOK;
  88. }
  89. else if (!rt_strcmp(argv[1], "stop"))
  90. {
  91. hwtimer_stop();
  92. rt_kprintf("stop tim14 success!\n");
  93. return RT_EOK;
  94. }
  95. else
  96. {
  97. goto _exit;
  98. }
  99. }
  100. _exit:
  101. {
  102. rt_kprintf("Usage:\n");
  103. rt_kprintf("tim_sample start - start TIM14 \n");
  104. rt_kprintf("tim_sample stop - stop TIM14 \n");
  105. }
  106. return RT_ERROR;
  107. }
  108. MSH_CMD_EXPORT(tim_sample, tim sample);