tick.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. static volatile uint64_t time_elapsed = 0;
  15. static volatile unsigned long tick_cycles = 0;
  16. static uint64_t get_ticks()
  17. {
  18. __asm__ __volatile__(
  19. "rdtime %0"
  20. : "=r"(time_elapsed));
  21. return time_elapsed;
  22. }
  23. int tick_isr(void)
  24. {
  25. // uint64_t core_id = current_coreid();
  26. int tick_cycles = 40000;
  27. // clint->mtimecmp[core_id] += tick_cycles;
  28. rt_tick_increase();
  29. sbi_set_timer(get_ticks() + tick_cycles);
  30. return 0;
  31. }
  32. /* Sets and enable the timer interrupt */
  33. int rt_hw_tick_init(void)
  34. {
  35. /* Read core id */
  36. // unsigned long core_id = current_coreid();
  37. unsigned long interval = 1000/RT_TICK_PER_SECOND;
  38. /* Clear the Supervisor-Timer bit in SIE */
  39. clear_csr(sie, SIP_STIP);
  40. /* calculate the tick cycles */
  41. // tick_cycles = interval * sysctl_clock_get_freq(SYSCTL_CLOCK_CPU) / CLINT_CLOCK_DIV / 1000ULL - 1;
  42. tick_cycles = 40000;
  43. /* Set timer */
  44. sbi_set_timer(get_ticks() + tick_cycles);
  45. /* Enable the Supervisor-Timer bit in SIE */
  46. set_csr(sie, SIP_STIP);
  47. return 0;
  48. }