added function to read system time (MTIME) via time[h] CSRs

This commit is contained in:
stnolting 2020-07-12 22:40:00 +02:00
parent 13f0c7f034
commit a1aeb68464
2 changed files with 32 additions and 0 deletions

View file

@ -49,6 +49,7 @@ uint64_t neorv32_cpu_get_cycle(void);
void neorv32_cpu_set_mcycle(uint32_t value);
uint64_t neorv32_cpu_get_instret(void);
void neorv32_cpu_set_minstret(uint32_t value);
uint64_t neorv32_cpu_get_systime(void);
void neorv32_cpu_delay_ms(uint32_t time_ms);

View file

@ -183,6 +183,37 @@ void neorv32_cpu_set_minstret(uint32_t value) {
}
/**********************************************************************//**
* Get current system time from time[h] CSR.
*
* @note This function requires the MTIME system timer to be implemented.
*
* @return Current system time (64 bit).
**************************************************************************/
uint64_t neorv32_cpu_get_systime(void) {
union {
uint64_t uint64;
uint32_t uint32[sizeof(uint64_t)/2];
} cycles;
uint32_t tmp1, tmp2, tmp3;
while(1) {
tmp1 = neorv32_cpu_csr_read(CSR_TIMEH);
tmp2 = neorv32_cpu_csr_read(CSR_TIME);
tmp3 = neorv32_cpu_csr_read(CSR_TIMEH);
if (tmp1 == tmp3) {
break;
}
}
cycles.uint32[0] = tmp2;
cycles.uint32[1] = tmp3;
return cycles.uint64;
}
/**********************************************************************//**
* Simple delay function (not very precise) using busy wait.
*