summaryrefslogtreecommitdiff
path: root/FreeRTOS/Demo/RISC-V_RV32_SiFive_HiFive1-RevB_FreedomStudio/freedom-metal/src/timer.c
blob: f58413321c0a9447fed04d1f205bef4a69715b9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/* Copyright 2018 SiFive, Inc */
/* SPDX-License-Identifier: Apache-2.0 */

#include <sys/time.h>
#include <sys/times.h>
#include <metal/cpu.h>
#include <metal/timer.h>
#include <metal/machine.h>

#if defined(__METAL_DT_MAX_HARTS)
/* This implementation serves as a small shim that interfaces with the first
 * timer on a system. */
int metal_timer_get_cyclecount(int hartid, unsigned long long *mcc)
{
    struct metal_cpu *cpu = metal_cpu_get(hartid);

    if ( cpu ) {
        *mcc = metal_cpu_get_timer(cpu);
        return 0;
    }	
    return -1;
}

int metal_timer_get_timebase_frequency(int hartid, unsigned long long *timebase)
{
    struct metal_cpu *cpu = metal_cpu_get(hartid);

    if ( cpu ) {
        *timebase = metal_cpu_get_timebase(cpu);
        return 0;
    } 
    return -1;
}

int metal_timer_get_machine_time(int hartid)
{
    struct metal_cpu *cpu = metal_cpu_get(hartid);
       
    if ( cpu ) {
       return metal_cpu_get_mtime(cpu);
    }
    return 0;
}

int metal_timer_set_machine_time(int hartid, unsigned long long time)
{
    struct metal_cpu *cpu = metal_cpu_get(hartid);

    if ( cpu ) {
       return metal_cpu_set_mtimecmp(cpu, time);
    }
    return -1;
}

#else

/* This implementation of gettimeofday doesn't actually do anything, it's just there to
 * provide a shim and return 0 so we can ensure that everything can link to _gettimeofday.
 */
int nop_cyclecount(int id, unsigned long long *c) __attribute__((section(".text.metal.nop.cyclecount")));
int nop_cyclecount(int id, unsigned long long *c) { return -1; }
int nop_timebase(unsigned long long *t) __attribute__((section(".text.metal.nop.timebase")));
int nop_timebase(unsigned long long *t) { return -1; }
int nop_tick(int second) __attribute__((section(".text.metal.nop.tick")));
int nop_tick(int second) { return -1; }
int metal_timer_get_cyclecount(int hartid, unsigned long long *c) __attribute__((weak, alias("nop_cyclecount")))
{
#pragma message("There is no default timer device, metal_timer_get_cyclecount() will always return cyclecount -1.")
}
int metal_timer_get_timebase_frequency(unsigned long long *t) __attribute__((weak, alias("nop_timebase")))
{
#pragma message("There is no default timer device, metal_timer_get_timebase_frequency() will always return timebase -1.")
}
int metal_timer_set_tick(int second) __attribute__((weak, alias("nop_tick")))
{
#pragma message("There is no default timer device, metal_timer_set_tick) will always return -1.")
}

#endif