2022-12-03 12:07:44 +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.
|
2024-07-15 12:06:17 +08:00
|
|
|
* 2024/07/08 Shell Using CPUTIME as tick
|
2022-12-03 12:07:44 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <rthw.h>
|
|
|
|
#include <rtthread.h>
|
|
|
|
|
2024-07-15 12:06:17 +08:00
|
|
|
#include <drivers/cputime.h>
|
2022-12-03 12:07:44 +08:00
|
|
|
#include <encoding.h>
|
|
|
|
#include "sbi.h"
|
|
|
|
|
2024-07-15 12:06:17 +08:00
|
|
|
#ifdef RT_USING_KTIME
|
|
|
|
#include <ktime.h>
|
|
|
|
#endif
|
2022-12-03 12:07:44 +08:00
|
|
|
|
2024-07-15 12:06:17 +08:00
|
|
|
static volatile unsigned long tick_cycles = 0;
|
2022-12-03 12:07:44 +08:00
|
|
|
|
|
|
|
int tick_isr(void)
|
|
|
|
{
|
|
|
|
rt_tick_increase();
|
2024-07-15 12:06:17 +08:00
|
|
|
sbi_set_timer(clock_cpu_gettime() + tick_cycles);
|
2022-12-03 12:07:44 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-07-15 12:06:17 +08:00
|
|
|
/* BSP should config clockbase frequency */
|
|
|
|
RT_STATIC_ASSERT(defined_clockbase_freq, CPUTIME_TIMER_FREQ != 0);
|
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
/* Sets and enable the timer interrupt */
|
|
|
|
int rt_hw_tick_init(void)
|
|
|
|
{
|
2024-07-15 12:06:17 +08:00
|
|
|
/* calculate the tick cycles */
|
|
|
|
tick_cycles = CPUTIME_TIMER_FREQ / RT_TICK_PER_SECOND;
|
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
/* Clear the Supervisor-Timer bit in SIE */
|
|
|
|
clear_csr(sie, SIP_STIP);
|
|
|
|
|
2024-07-15 12:06:17 +08:00
|
|
|
/* Init riscv timer */
|
|
|
|
riscv_cputime_init();
|
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
/* Set timer */
|
2024-07-15 12:06:17 +08:00
|
|
|
sbi_set_timer(clock_cpu_gettime() + tick_cycles);
|
2022-12-03 12:07:44 +08:00
|
|
|
|
2024-07-15 12:06:17 +08:00
|
|
|
#ifdef RT_USING_KTIME
|
|
|
|
rt_ktime_cputimer_init();
|
|
|
|
#endif
|
2022-12-03 12:07:44 +08:00
|
|
|
/* Enable the Supervisor-Timer bit in SIE */
|
|
|
|
set_csr(sie, SIP_STIP);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2024-04-24 05:40:22 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This function will delay for some us.
|
|
|
|
*
|
|
|
|
* @param us the delay time of us
|
|
|
|
*/
|
|
|
|
void rt_hw_us_delay(rt_uint32_t us)
|
|
|
|
{
|
|
|
|
unsigned long start_time;
|
|
|
|
unsigned long end_time;
|
|
|
|
unsigned long run_time;
|
|
|
|
|
2024-07-15 12:06:17 +08:00
|
|
|
start_time = clock_cpu_gettime();
|
|
|
|
end_time = start_time + us * (CPUTIME_TIMER_FREQ / 1000000);
|
2024-04-24 05:40:22 +08:00
|
|
|
do
|
|
|
|
{
|
2024-07-15 12:06:17 +08:00
|
|
|
run_time = clock_cpu_gettime();
|
2024-04-24 05:40:22 +08:00
|
|
|
} while(run_time < end_time);
|
2024-07-15 12:06:17 +08:00
|
|
|
}
|