tick.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. * 2024/07/08 Shell Using CPUTIME as tick
  10. */
  11. #include <rthw.h>
  12. #include <rtthread.h>
  13. #include <drivers/cputime.h>
  14. #include <encoding.h>
  15. #include "sbi.h"
  16. #ifdef RT_USING_KTIME
  17. #include <ktime.h>
  18. #endif
  19. static volatile unsigned long tick_cycles = 0;
  20. int tick_isr(void)
  21. {
  22. rt_tick_increase();
  23. sbi_set_timer(clock_cpu_gettime() + tick_cycles);
  24. return 0;
  25. }
  26. /* BSP should config clockbase frequency */
  27. RT_STATIC_ASSERT(defined_clockbase_freq, CPUTIME_TIMER_FREQ != 0);
  28. /* Sets and enable the timer interrupt */
  29. int rt_hw_tick_init(void)
  30. {
  31. /* calculate the tick cycles */
  32. tick_cycles = CPUTIME_TIMER_FREQ / RT_TICK_PER_SECOND;
  33. /* Clear the Supervisor-Timer bit in SIE */
  34. clear_csr(sie, SIP_STIP);
  35. /* Init riscv timer */
  36. riscv_cputime_init();
  37. /* Set timer */
  38. sbi_set_timer(clock_cpu_gettime() + tick_cycles);
  39. #ifdef RT_USING_KTIME
  40. rt_ktime_cputimer_init();
  41. #endif
  42. /* Enable the Supervisor-Timer bit in SIE */
  43. set_csr(sie, SIP_STIP);
  44. return 0;
  45. }
  46. /**
  47. * This function will delay for some us.
  48. *
  49. * @param us the delay time of us
  50. */
  51. void rt_hw_us_delay(rt_uint32_t us)
  52. {
  53. unsigned long start_time;
  54. unsigned long end_time;
  55. unsigned long run_time;
  56. start_time = clock_cpu_gettime();
  57. end_time = start_time + us * (CPUTIME_TIMER_FREQ / 1000000);
  58. do
  59. {
  60. run_time = clock_cpu_gettime();
  61. } while(run_time < end_time);
  62. }