tick.c 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. * 2018/10/28 Bernard The unify RISC-V porting code.
  9. */
  10. #include <rthw.h>
  11. #include <rtthread.h>
  12. #include <encoding.h>
  13. #include "sbi.h"
  14. #include "tick.h"
  15. static volatile uint64_t time_elapsed = 0;
  16. static volatile unsigned long tick_cycles = 0;
  17. static unsigned long tick_delta = TIMER_CLK_FREQ / RT_TICK_PER_SECOND;
  18. static uint64_t get_ticks()
  19. {
  20. __asm__ __volatile__(
  21. "rdtime %0"
  22. : "=r"(time_elapsed));
  23. return time_elapsed;
  24. }
  25. int tick_isr(void)
  26. {
  27. rt_tick_increase();
  28. sbi_set_timer(get_ticks() + tick_delta);
  29. return 0;
  30. }
  31. /* Sets and enable the timer interrupt */
  32. int rt_hw_tick_init(void)
  33. {
  34. /* Clear the Supervisor-Timer bit in SIE */
  35. clear_csr(sie, SIP_STIP);
  36. /* Set timer */
  37. sbi_set_timer(get_ticks() + tick_delta);
  38. /* Enable the Supervisor-Timer bit in SIE */
  39. set_csr(sie, SIP_STIP);
  40. return 0;
  41. }