drv_timer.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. * 2019-07-29 zdzn first version
  9. */
  10. #include "drv_timer.h"
  11. #ifdef BSP_USING_SYSTIMER
  12. static void raspi_systimer_init(rt_hwtimer_t *hwtimer, rt_uint32_t state)
  13. {
  14. if (state == 0)
  15. hwtimer->ops->stop(hwtimer);
  16. }
  17. static rt_err_t raspi_systimer_start(rt_hwtimer_t *hwtimer, rt_uint32_t cnt, rt_hwtimer_mode_t mode)
  18. {
  19. rt_err_t result = RT_EOK;
  20. rt_systimer_t *timer = (rt_systimer_t *)hwtimer->parent.user_data;
  21. int timer_id = timer->timer_id;
  22. if (mode == HWTIMER_MODE_PERIOD)
  23. timer->cnt = cnt;
  24. else
  25. timer->cnt = 0;
  26. __sync_synchronize();
  27. if (timer_id == 1)
  28. {
  29. rt_hw_interrupt_umask(IRQ_SYSTEM_TIMER_1);
  30. STIMER_C1 = STIMER_CLO + cnt;
  31. }
  32. else if (timer_id == 3)
  33. {
  34. rt_hw_interrupt_umask(IRQ_SYSTEM_TIMER_3);
  35. STIMER_C3 = STIMER_CLO + cnt;
  36. }
  37. else
  38. result = -RT_ERROR;
  39. __sync_synchronize();
  40. return result;
  41. }
  42. static void raspi_systimer_stop(rt_hwtimer_t *hwtimer)
  43. {
  44. rt_systimer_t *timer = (rt_systimer_t *)hwtimer->parent.user_data;
  45. int timer_id = timer->timer_id;
  46. if (timer_id == 1)
  47. rt_hw_interrupt_mask(IRQ_SYSTEM_TIMER_1);
  48. else if (timer_id == 3)
  49. rt_hw_interrupt_mask(IRQ_SYSTEM_TIMER_3);
  50. }
  51. static rt_err_t raspi_systimer_ctrl(rt_hwtimer_t *timer, rt_uint32_t cmd, void *arg)
  52. {
  53. /* The frequency value is an immutable value. */
  54. if (cmd == HWTIMER_CTRL_FREQ_SET)
  55. {
  56. return RT_EOK;
  57. }
  58. else
  59. {
  60. return -RT_ENOSYS;
  61. }
  62. }
  63. void rt_device_systimer_isr(int vector, void *param)
  64. {
  65. rt_hwtimer_t *hwtimer = (rt_hwtimer_t *) param;
  66. rt_systimer_t *timer = (rt_systimer_t *)hwtimer->parent.user_data;
  67. RT_ASSERT(timer != RT_NULL);
  68. int timer_id = timer->timer_id;
  69. __sync_synchronize();
  70. if (timer_id == 1)
  71. {
  72. STIMER_CS = 0x2;
  73. STIMER_C1 = STIMER_CLO + timer->cnt;
  74. }
  75. else if (timer_id == 3)
  76. {
  77. STIMER_CS = 0x8;
  78. STIMER_C3 = STIMER_CLO + timer->cnt;
  79. }
  80. __sync_synchronize();
  81. rt_device_hwtimer_isr(hwtimer);
  82. }
  83. static struct rt_hwtimer_device _hwtimer1;
  84. static struct rt_hwtimer_device _hwtimer3;
  85. static rt_systimer_t _systimer1;
  86. static rt_systimer_t _systimer3;
  87. const static struct rt_hwtimer_ops systimer_ops =
  88. {
  89. raspi_systimer_init,
  90. raspi_systimer_start,
  91. raspi_systimer_stop,
  92. RT_NULL,
  93. raspi_systimer_ctrl
  94. };
  95. static const struct rt_hwtimer_info _info =
  96. {
  97. 1000000, /* the maxinum count frequency can be set */
  98. 1000000, /* the maxinum count frequency can be set */
  99. 0xFFFFFFFF, /* the maximum counter value */
  100. HWTIMER_CNTMODE_UP /* count mode (inc/dec) */
  101. };
  102. #endif
  103. int rt_hw_systimer_init(void)
  104. {
  105. #ifdef BSP_USING_SYSTIMER
  106. #ifdef RT_USING_SYSTIMER1
  107. _systimer1.timer_id =1;
  108. _hwtimer1.ops = &systimer_ops;
  109. _hwtimer1.info = &_info;
  110. rt_device_hwtimer_register(&_hwtimer1, "timer1",&_systimer1);
  111. rt_hw_interrupt_install(IRQ_SYSTEM_TIMER_1, rt_device_systimer_isr, &_hwtimer1, "systimer1");
  112. rt_hw_interrupt_umask(IRQ_SYSTEM_TIMER_1);
  113. #endif
  114. #ifdef RT_USING_SYSTIMER3
  115. _systimer3.timer_id =3;
  116. _hwtimer3.ops = &systimer_ops;
  117. _hwtimer3.info = &_info;
  118. rt_device_hwtimer_register(&_hwtimer3, "timer3",&_systimer3);
  119. rt_hw_interrupt_install(IRQ_SYSTEM_TIMER_3, rt_device_systimer_isr, &_hwtimer3, "systimer3");
  120. rt_hw_interrupt_umask(IRQ_SYSTEM_TIMER_3);
  121. #endif
  122. #endif
  123. return 0;
  124. }
  125. INIT_DEVICE_EXPORT(rt_hw_systimer_init);