timer.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * This file is part of FH8620 BSP for RT-Thread distribution.
  3. *
  4. * Copyright (c) 2016 Shanghai Fullhan Microelectronics Co., Ltd.
  5. * All rights reserved
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. *
  21. * Visit http://www.fullhan.com to get contact with Fullhan.
  22. *
  23. * Change Logs:
  24. * Date Author Notes
  25. */
  26. #include "timer.h"
  27. #include <rtdevice.h>
  28. #include "fh_arch.h"
  29. #include "libraries/inc/fh_timer.h"
  30. //#include "fh_pmu.h"
  31. //#include "chip_reg.h"
  32. //NEED_CAUTION.
  33. #define TIMER_CLOCK 1000000
  34. #define CONFIG_PAE_PTS_CLOCK (1000000)
  35. #define TICKS_PER_USEC (CONFIG_PAE_PTS_CLOCK / 1000000)
  36. #define REG_PAE_PTS_REG (0xec100000 + 0x0040)
  37. static unsigned long lastdec;
  38. static unsigned long long timestamp;
  39. rt_uint32_t read_pts(void)
  40. {
  41. return GET_REG(REG_PAE_PTS_REG);
  42. }
  43. unsigned long long get_ticks(void)
  44. {
  45. rt_uint32_t now = read_pts();
  46. if (now >= lastdec) {
  47. /* normal mode */
  48. timestamp += now - lastdec;
  49. } else {
  50. now = read_pts();
  51. if (now >= lastdec)
  52. timestamp += now - lastdec;
  53. else {
  54. /* we have an overflow ... */
  55. timestamp += now + 0xffffffff - lastdec;
  56. }
  57. }
  58. lastdec = now;
  59. return timestamp / (TICKS_PER_USEC * 10);
  60. }
  61. void udelay(unsigned long usec)
  62. {
  63. unsigned long long tmp;
  64. rt_uint32_t tmo;
  65. tmo = (usec + 9) / 10;
  66. tmp = get_ticks() + tmo; /* get current timestamp */
  67. while (get_ticks() < tmp)
  68. /* loop till event */
  69. /*NOP*/;
  70. }
  71. void rt_timer_handler(int vector, void *param)
  72. {
  73. timer *tim = param;
  74. rt_interrupt_enter();
  75. timer_get_eoi(tim);
  76. rt_tick_increase();
  77. rt_interrupt_leave();
  78. }
  79. /**
  80. * This function will init pit for system ticks
  81. */
  82. void rt_hw_timer_init()
  83. {
  84. timer *tim = (timer *) TMR_REG_BASE;
  85. timer_init(tim);
  86. /* install interrupt handler */
  87. rt_hw_interrupt_install(TMR0_IRQn, rt_timer_handler, (void *) tim,
  88. "sys_tick");
  89. rt_hw_interrupt_umask(TMR0_IRQn);
  90. timer_set_mode(tim, TIMER_MODE_PERIODIC);
  91. timer_set_period(tim, RT_TICK_PER_SECOND, TIMER_CLOCK);
  92. //timer_set_period(tim, RT_TIMER_TICK_PER_SECOND, TIMER_CLOCK);
  93. timer_enable_irq(tim);
  94. timer_enable(tim);
  95. }