drv_timer.c 3.6 KB

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