rt-thread-official/libcpu/risc-v/virt64/tick.c

70 lines
1.6 KiB
C
Raw Normal View History

/*
2021-05-21 18:39:41 +08:00
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2018/10/28 Bernard The unify RISC-V porting code.
*/
#include <rthw.h>
#include <rtthread.h>
#include "sbi.h"
2021-05-21 17:03:30 +08:00
#include "tick.h"
#include <riscv_io.h>
#include <encoding.h>
#define VIRT_CLINT_TIMEBASE_FREQ (10000000)
static volatile uint64_t time_elapsed = 0;
static volatile unsigned long tick_cycles = 0;
static uint64_t get_ticks()
{
__asm__ __volatile__(
"rdtime %0"
: "=r"(time_elapsed));
return time_elapsed;
}
int tick_isr(void)
{
2021-05-21 17:03:30 +08:00
int tick_cycles = VIRT_CLINT_TIMEBASE_FREQ / RT_TICK_PER_SECOND;
rt_tick_increase();
2021-05-21 17:03:30 +08:00
#ifdef RISCV_S_MODE
sbi_set_timer(get_ticks() + tick_cycles);
2021-05-21 17:03:30 +08:00
#else
2021-05-21 18:39:41 +08:00
*(uint64_t*)CLINT_MTIMECMP(r_mhartid()) = *(uint64_t*)CLINT_MTIME + tick_cycles;
2021-05-21 17:03:30 +08:00
#endif
return 0;
}
/* Sets and enable the timer interrupt */
int rt_hw_tick_init(void)
{
2021-05-21 17:03:30 +08:00
unsigned long interval = VIRT_CLINT_TIMEBASE_FREQ / RT_TICK_PER_SECOND;
2021-05-21 17:03:30 +08:00
#ifdef RISCV_S_MODE
/* Clear the Supervisor-Timer bit in SIE */
clear_csr(sie, SIP_STIP);
/* calculate the tick cycles */
// tick_cycles = interval * sysctl_clock_get_freq(SYSCTL_CLOCK_CPU) / CLINT_CLOCK_DIV / 1000ULL - 1;
tick_cycles = 40000;
/* Set timer */
sbi_set_timer(get_ticks() + tick_cycles);
2021-05-21 18:39:41 +08:00
/* Enable the Supervisor-Timer bit in SIE */
set_csr(sie, SIP_STIP);
2021-05-21 17:03:30 +08:00
#else
clear_csr(mie, MIP_MTIP);
clear_csr(mip, MIP_MTIP);
2021-05-21 18:39:41 +08:00
*(uint64_t*)CLINT_MTIMECMP(r_mhartid()) = *(uint64_t*)CLINT_MTIME + interval;
2021-05-21 17:03:30 +08:00
set_csr(mie, MIP_MTIP);
#endif
return 0;
}