drv_timer.c 3.7 KB

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