Skip to content

Commit 30f3ac4

Browse files
committed
[rv64] feat: unify tick.c
The changes unify the tick.c implementations for all risc-v64 architectures, leveraging the CPUTIME feature. This refactoring was necessary to streamline the codebase, and ensure consistent timer handling across different platforms. Changes: - Updated `Kconfig` in `bsp/cvitek/cv18xx_risc-v` to fix formatting issues. - Added default values for `CPUTIME_TIMER_FREQ` in `components/drivers/cputime/Kconfig`. - Refactored `tick.c` and `tick.h` in `libcpu/risc-v/t-head/c906` and `libcpu/risc-v/virt64`: - Replaced direct use of `rdtime` with `clock_cpu_gettime`. - Removed redundant timer frequency definitions. - Added static assertions to check the value of `CPUTIME_TIMER_FREQ`. - Initialized `tick_cycles` based on `CPUTIME_TIMER_FREQ`. - Integrated `ktime` support for tick initialization. Signed-off-by: Shell <[email protected]>
1 parent 66ff624 commit 30f3ac4

File tree

6 files changed

+57
-45
lines changed

6 files changed

+57
-45
lines changed

Diff for: bsp/cvitek/cv18xx_risc-v/Kconfig

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ config C906_PLIC_PHY_ADDR
2626
default 0x70000000
2727

2828
config IRQ_MAX_NR
29-
int
29+
int
3030
default 64
3131

3232
config TIMER_CLK_FREQ
@@ -40,7 +40,7 @@ config GPIO_IRQ_BASE
4040
config SYS_GPIO_IRQ_BASE
4141
int
4242
default 70
43-
43+
4444
config __STACKSIZE__
4545
int "stack size for interrupt"
4646
default 4096
@@ -72,5 +72,5 @@ choice
7272
config BOARD_TYPE_MILKV_DUO256M_SPINOR
7373
select SOC_TYPE_SG2002
7474
bool "milkv-duo256m-spinor"
75-
75+
7676
endchoice

Diff for: components/drivers/cputime/Kconfig

+3
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,8 @@ if RT_USING_CPUTIME
3030
Some RISCV64 MCU Use rdtime instructions read CPU time.
3131
config CPUTIME_TIMER_FREQ
3232
int "CPUTIME timer freq"
33+
default 10000000 if BOARD_QEMU_VIRT_RV64
34+
default 24000000 if BOARD_allwinnerd1s
35+
default 25000000 if BSP_USING_CV18XX
3336
default 0
3437
endif

Diff for: libcpu/risc-v/t-head/c906/tick.c

+21-18
Original file line numberDiff line numberDiff line change
@@ -6,44 +6,47 @@
66
* Change Logs:
77
* Date Author Notes
88
* 2018/10/28 Bernard The unify RISC-V porting code.
9+
* 2024/07/08 Shell Using CPUTIME as tick
910
*/
1011

1112
#include <rthw.h>
1213
#include <rtthread.h>
1314

15+
#include <drivers/cputime.h>
1416
#include <encoding.h>
1517
#include "sbi.h"
16-
#include "tick.h"
1718

18-
static volatile uint64_t time_elapsed = 0;
19-
static volatile unsigned long tick_cycles = 0;
20-
21-
static unsigned long tick_delta = TIMER_CLK_FREQ / RT_TICK_PER_SECOND;
19+
#ifdef RT_USING_KTIME
20+
#include <ktime.h>
21+
#endif
2222

23-
static uint64_t get_ticks()
24-
{
25-
__asm__ __volatile__(
26-
"rdtime %0"
27-
: "=r"(time_elapsed));
28-
return time_elapsed;
29-
}
23+
static volatile unsigned long tick_cycles = 0;
3024

3125
int tick_isr(void)
3226
{
3327
rt_tick_increase();
34-
sbi_set_timer(get_ticks() + tick_delta);
28+
sbi_set_timer(clock_cpu_gettime() + tick_cycles);
3529
return 0;
3630
}
3731

32+
/* BSP should config clockbase frequency */
33+
RT_STATIC_ASSERT(defined_clockbase_freq, CPUTIME_TIMER_FREQ != 0);
34+
3835
/* Sets and enable the timer interrupt */
3936
int rt_hw_tick_init(void)
4037
{
38+
/* calculate the tick cycles */
39+
tick_cycles = CPUTIME_TIMER_FREQ / RT_TICK_PER_SECOND;
40+
4141
/* Clear the Supervisor-Timer bit in SIE */
4242
clear_csr(sie, SIP_STIP);
4343

4444
/* Set timer */
45-
sbi_set_timer(get_ticks() + tick_delta);
45+
sbi_set_timer(clock_cpu_gettime() + tick_cycles);
4646

47+
#ifdef RT_USING_KTIME
48+
rt_ktime_cputimer_init();
49+
#endif
4750
/* Enable the Supervisor-Timer bit in SIE */
4851
set_csr(sie, SIP_STIP);
4952

@@ -61,10 +64,10 @@ void rt_hw_us_delay(rt_uint32_t us)
6164
unsigned long end_time;
6265
unsigned long run_time;
6366

64-
start_time = get_ticks();
65-
end_time = start_time + us * (TIMER_CLK_FREQ / 1000000);
67+
start_time = clock_cpu_gettime();
68+
end_time = start_time + us * (CPUTIME_TIMER_FREQ / 1000000);
6669
do
6770
{
68-
run_time = get_ticks();
71+
run_time = clock_cpu_gettime();
6972
} while(run_time < end_time);
70-
}
73+
}

Diff for: libcpu/risc-v/t-head/c906/tick.h

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2006-2023, RT-Thread Development Team
2+
* Copyright (c) 2006-2024, RT-Thread Development Team
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*
@@ -11,10 +11,6 @@
1111
#ifndef TICK_H__
1212
#define TICK_H__
1313

14-
/* timer clock is 24 MHZ */
15-
#ifndef TIMER_CLK_FREQ
16-
#define TIMER_CLK_FREQ (24000000)
17-
#endif
1814
int tick_isr(void);
1915
int rt_hw_tick_init(void);
2016

Diff for: libcpu/risc-v/virt64/tick.c

+28-18
Original file line numberDiff line numberDiff line change
@@ -6,52 +6,43 @@
66
* Change Logs:
77
* Date Author Notes
88
* 2018/10/28 Bernard The unify RISC-V porting code.
9+
* 2024/07/08 Shell Using CPUTIME as tick
910
*/
1011

1112
#include <rthw.h>
1213
#include <rtthread.h>
1314

15+
#include <drivers/cputime.h>
1416
#include <encoding.h>
1517
#include "sbi.h"
1618

1719
#ifdef RT_USING_KTIME
1820
#include <ktime.h>
1921
#endif
2022

21-
static volatile uint64_t time_elapsed = 0;
2223
static volatile unsigned long tick_cycles = 0;
2324

24-
static uint64_t get_ticks()
25-
{
26-
__asm__ __volatile__(
27-
"rdtime %0"
28-
: "=r"(time_elapsed));
29-
return time_elapsed;
30-
}
31-
3225
int tick_isr(void)
3326
{
34-
// uint64_t core_id = current_coreid();
35-
// clint->mtimecmp[core_id] += tick_cycles;
3627
rt_tick_increase();
37-
sbi_set_timer(get_ticks() + tick_cycles);
38-
28+
sbi_set_timer(clock_cpu_gettime() + tick_cycles);
3929
return 0;
4030
}
4131

32+
/* BSP should config clockbase frequency */
33+
RT_STATIC_ASSERT(defined_clockbase_freq, CPUTIME_TIMER_FREQ != 0);
34+
4235
/* Sets and enable the timer interrupt */
4336
int rt_hw_tick_init(void)
4437
{
45-
/* Read core id */
46-
// unsigned long core_id = current_coreid();
38+
/* calculate the tick cycles */
39+
tick_cycles = CPUTIME_TIMER_FREQ / RT_TICK_PER_SECOND;
4740

4841
/* Clear the Supervisor-Timer bit in SIE */
4942
clear_csr(sie, SIP_STIP);
5043

51-
/* calculate the tick cycles */
52-
tick_cycles = CPUTIME_TIMER_FREQ / RT_TICK_PER_SECOND;
5344
/* Set timer */
54-
sbi_set_timer(get_ticks() + tick_cycles);
45+
sbi_set_timer(clock_cpu_gettime() + tick_cycles);
5546

5647
#ifdef RT_USING_KTIME
5748
rt_ktime_cputimer_init();
@@ -61,3 +52,22 @@ int rt_hw_tick_init(void)
6152

6253
return 0;
6354
}
55+
56+
/**
57+
* This function will delay for some us.
58+
*
59+
* @param us the delay time of us
60+
*/
61+
void rt_hw_us_delay(rt_uint32_t us)
62+
{
63+
unsigned long start_time;
64+
unsigned long end_time;
65+
unsigned long run_time;
66+
67+
start_time = clock_cpu_gettime();
68+
end_time = start_time + us * (CPUTIME_TIMER_FREQ / 1000000);
69+
do
70+
{
71+
run_time = clock_cpu_gettime();
72+
} while(run_time < end_time);
73+
}

Diff for: libcpu/risc-v/virt64/tick.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2006-2021, RT-Thread Development Team
2+
* Copyright (c) 2006-2024, RT-Thread Development Team
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*

0 commit comments

Comments
 (0)