Files
eb001/tb/sw/src/clint.c

55 lines
1.6 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include "clint.h"
volatile const uint32_t * const mtime_high = (volatile const uint32_t*)(CLINT_BASE + CLINT_MTIME_HIGH);
volatile const uint32_t * const mtime_low = (volatile const uint32_t*)(CLINT_BASE + CLINT_MTIME_LOW);
volatile uint32_t * const mtimecmp_high = (volatile const uint32_t*)(CLINT_BASE + CLINT_MTIMECMP_HIGH);
volatile uint32_t * const mtimecmp_low = (volatile const uint32_t*)(CLINT_BASE + CLINT_MTIMECMP_LOW);
volatile uint32_t mtimer_int_flag = 0;
void set_mtimecmp(uint64_t value)
{
*mtimecmp_high = 0xFFFFFFFF; // 先写入高 32 位为全 1禁用中断
*mtimecmp_low = (uint32_t)value; // 写入低 32 位
*mtimecmp_high = (uint32_t)(value >> 32); // 写入高 32 位
}
uint64_t read_mtimer()
{
uint32_t hi, lo;
do {
hi = *mtime_high; // 读取高 32 位
lo = *mtime_low; // 读取低 32 位
} while (hi != *mtime_high); // 检查高 32 位是否变化(避免进位)
return (((uint64_t)hi << 32) | lo);
}
void enable_mtimer_int()
{
__asm__ volatile ("csrs mie, %0" :: "r"(0x80)); // mie.MTIE = 1
__asm__ volatile ("csrs mstatus, %0" :: "r"(0x8)); // mstatus.MIE = 1
}
void disable_mtimer_int()
{
__asm__ volatile ("csrc mie, %0" :: "r"(0x80)); // mie.MTIE = 0
__asm__ volatile ("csrc mstatus, %0" :: "r"(0x8)); // mstatus.MIE = 0
}
void set_mtimer(uint32_t delta_t)
{
#if 0
uint64_t t;
t = read_mtimer();
t += delta_t;
set_mtimecmp(t);
#else
uint64_t t;
t = *(volatile uint64_t *)(CLINT_BASE + CLINT_MTIME_LOW);
t += delta_t;
*(volatile uint64_t *)(CLINT_BASE + CLINT_MTIMECMP_LOW) = t;
#endif
}