40 lines
1.0 KiB
C
40 lines
1.0 KiB
C
#include "FreeRTOS.h"
|
|
#include "task.h"
|
|
|
|
#define CLINT_BASE 0x14000000
|
|
|
|
extern uint64_t ullNextTime;
|
|
extern volatile uint64_t *pullMachineTimerCompareRegister;
|
|
|
|
static inline uint64_t timer_get_current(void) {
|
|
uint64_t ret;
|
|
|
|
asm volatile("csrr %0, time" : "=r"(ret));
|
|
|
|
return ret;
|
|
}
|
|
|
|
static inline void timer_set_match(uint64_t match) {
|
|
*(uint32_t *)(CLINT_BASE + 0x4004) = match >> 32;
|
|
*(uint32_t *)(CLINT_BASE + 0x4000) = match;
|
|
}
|
|
|
|
static inline void enable_timer_interrupt(void) {
|
|
uint64_t mie;
|
|
|
|
asm volatile("csrr %0, mie" : "=r"(mie));
|
|
|
|
mie |= (1U << 7); /* Machine timer interrupt */
|
|
|
|
asm volatile("csrw mie, %0" : : "r"(mie));
|
|
}
|
|
|
|
void vPortSetupTimerInterrupt(void) {
|
|
uint64_t cur_time = timer_get_current();
|
|
uint64_t match_inc = configSYSTICK_CLOCK_HZ / configTICK_RATE_HZ;
|
|
ullNextTime = cur_time + match_inc;
|
|
pullMachineTimerCompareRegister = (volatile uint64_t *)(CLINT_BASE + 0x4000);
|
|
|
|
timer_set_match(ullNextTime);
|
|
enable_timer_interrupt();
|
|
} |