timer_sample.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Copyright (c) 2006-2021, 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. #if defined(BSP_USING_TIM14) && defined(BSP_USING_ADC2)
  12. #include <rtthread.h>
  13. #include <rtdevice.h>
  14. #define HWTIMER_DEV_NAME "timer14"
  15. #define HWADC_DEV_NAME "adc2"
  16. #define REFER_VOLTAGE 330 /* voltage reference */
  17. #define CONVERT_BITS (1 << 12) /* Conversion digit */
  18. #define ADC_DEV_CHANNEL 6
  19. static rt_adc_device_t adc_dev = RT_NULL;
  20. static rt_err_t timeout_cb(rt_device_t dev, rt_size_t size)
  21. {
  22. rt_uint32_t value = 0 , vol = 0;
  23. /* read adc value */
  24. value = rt_adc_read(adc_dev, ADC_DEV_CHANNEL);
  25. rt_kprintf("the value is :%d \n", value);
  26. vol = value * REFER_VOLTAGE / CONVERT_BITS;
  27. rt_kprintf("the voltage is :%d.%02d \n", vol / 100, vol % 100);
  28. return 0;
  29. }
  30. static int hwtimer_stop(void)
  31. {
  32. rt_err_t ret = RT_EOK;
  33. rt_device_t hw_dev = RT_NULL;
  34. hw_dev = rt_device_find(HWTIMER_DEV_NAME);
  35. if (hw_dev == RT_NULL)
  36. {
  37. rt_kprintf("hwtimer sample run failed! can't find %s device!\n", HWTIMER_DEV_NAME);
  38. return RT_ERROR;
  39. }
  40. ret = rt_device_close(hw_dev);
  41. if (ret != RT_EOK)
  42. {
  43. rt_kprintf("close %s device failed!\n", HWTIMER_DEV_NAME);
  44. return ret;
  45. }
  46. /* close adc channel */
  47. ret = rt_adc_disable(adc_dev, ADC_DEV_CHANNEL);
  48. return ret;
  49. }
  50. static int hwtimer_start(void)
  51. {
  52. rt_err_t ret = RT_EOK;
  53. rt_hwtimerval_t timeout_s;
  54. rt_device_t hw_dev = RT_NULL;
  55. rt_hwtimer_mode_t mode;
  56. hw_dev = rt_device_find(HWTIMER_DEV_NAME);
  57. if (hw_dev == RT_NULL)
  58. {
  59. rt_kprintf("hwtimer sample run failed! can't find %s device!\n", HWTIMER_DEV_NAME);
  60. return RT_ERROR;
  61. }
  62. /* find adc dev */
  63. adc_dev = (rt_adc_device_t)rt_device_find(HWADC_DEV_NAME);
  64. if (adc_dev == RT_NULL)
  65. {
  66. rt_kprintf("hwtimer sample run failed! can't find %s device!\n", HWADC_DEV_NAME);
  67. return RT_ERROR;
  68. }
  69. /* Open the device in read/write mode */
  70. ret = rt_device_open(hw_dev, RT_DEVICE_OFLAG_RDWR);
  71. if (ret != RT_EOK)
  72. {
  73. rt_kprintf("open %s device failed!\n", HWTIMER_DEV_NAME);
  74. return ret;
  75. }
  76. /* Set the timeout callback function */
  77. rt_device_set_rx_indicate(hw_dev, timeout_cb);
  78. /* Set the mode to periodic timer */
  79. mode = HWTIMER_MODE_PERIOD;
  80. ret = rt_device_control(hw_dev, HWTIMER_CTRL_MODE_SET, &mode);
  81. if (ret != RT_EOK)
  82. {
  83. rt_kprintf("set mode failed! ret is :%d\n", ret);
  84. return ret;
  85. }
  86. timeout_s.sec = 5;
  87. timeout_s.usec = 0;
  88. if (rt_device_write(hw_dev, 0, &timeout_s, sizeof(timeout_s)) != sizeof(timeout_s))
  89. {
  90. rt_kprintf("set timeout value failed\n");
  91. return RT_ERROR;
  92. }
  93. rt_thread_mdelay(3500);
  94. rt_device_read(hw_dev, 0, &timeout_s, sizeof(timeout_s));
  95. rt_kprintf("Read: Sec = %d, Usec = %d\n", timeout_s.sec, timeout_s.usec);
  96. /* enable adc channel */
  97. ret = rt_adc_enable(adc_dev, ADC_DEV_CHANNEL);
  98. return ret;
  99. }
  100. static int tim_sample(int argc, char *argv[])
  101. {
  102. if (argc > 1)
  103. {
  104. if (!rt_strcmp(argv[1], "start"))
  105. {
  106. rt_kprintf("tim14 will start\n");
  107. hwtimer_start();
  108. return RT_EOK;
  109. }
  110. else if (!rt_strcmp(argv[1], "stop"))
  111. {
  112. hwtimer_stop();
  113. rt_kprintf("stop tim14 success!\n");
  114. return RT_EOK;
  115. }
  116. else
  117. {
  118. goto _exit;
  119. }
  120. }
  121. _exit:
  122. {
  123. rt_kprintf("Usage:\n");
  124. rt_kprintf("tim_sample start - start TIM14 \n");
  125. rt_kprintf("tim_sample stop - stop TIM14 \n");
  126. }
  127. return RT_ERROR;
  128. }
  129. MSH_CMD_EXPORT(tim_sample, tim sample);
  130. #endif