]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/RISC-V_RV32_SiFive_HiFive1_FreedomStudio/freedom-metal/src/timer.c
Update RISCC-V-RV32-SiFive_HiFive1_FreedomStudio project to latest tools and metal...
[freertos] / FreeRTOS / Demo / RISC-V_RV32_SiFive_HiFive1_FreedomStudio / freedom-metal / src / timer.c
1 /* Copyright 2018 SiFive, Inc */
2 /* SPDX-License-Identifier: Apache-2.0 */
3
4 #include <sys/time.h>
5 #include <sys/times.h>
6 #include <metal/cpu.h>
7 #include <metal/timer.h>
8 #include <metal/machine.h>
9
10 #if defined(__METAL_DT_MAX_HARTS)
11 /* This implementation serves as a small shim that interfaces with the first
12  * timer on a system. */
13 int metal_timer_get_cyclecount(int hartid, unsigned long long *mcc)
14 {
15     struct metal_cpu *cpu = metal_cpu_get(hartid);
16
17     if ( cpu ) {
18         *mcc = metal_cpu_get_timer(cpu);
19         return 0;
20     }   
21     return -1;
22 }
23
24 int metal_timer_get_timebase_frequency(int hartid, unsigned long long *timebase)
25 {
26     struct metal_cpu *cpu = metal_cpu_get(hartid);
27
28     if ( cpu ) {
29         *timebase = metal_cpu_get_timebase(cpu);
30         return 0;
31     } 
32     return -1;
33 }
34
35 int metal_timer_get_machine_time(int hartid)
36 {
37     struct metal_cpu *cpu = metal_cpu_get(hartid);
38        
39     if ( cpu ) {
40        return metal_cpu_get_mtime(cpu);
41     }
42     return 0;
43 }
44
45 int metal_timer_set_machine_time(int hartid, unsigned long long time)
46 {
47     struct metal_cpu *cpu = metal_cpu_get(hartid);
48
49     if ( cpu ) {
50        return metal_cpu_set_mtimecmp(cpu, time);
51     }
52     return -1;
53 }
54
55 #else
56
57 /* This implementation of gettimeofday doesn't actually do anything, it's just there to
58  * provide a shim and return 0 so we can ensure that everything can link to _gettimeofday.
59  */
60 int nop_cyclecount(int id, unsigned long long *c) __attribute__((section(".text.metal.nop.cyclecount")));
61 int nop_cyclecount(int id, unsigned long long *c) { return -1; }
62 int nop_timebase(unsigned long long *t) __attribute__((section(".text.metal.nop.timebase")));
63 int nop_timebase(unsigned long long *t) { return -1; }
64 int nop_tick(int second) __attribute__((section(".text.metal.nop.tick")));
65 int nop_tick(int second) { return -1; }
66 int metal_timer_get_cyclecount(int hartid, unsigned long long *c) __attribute__((weak, alias("nop_cyclecount")))
67 {
68 #pragma message("There is no default timer device, metal_timer_get_cyclecount() will always return cyclecount -1.")
69 }
70 int metal_timer_get_timebase_frequency(unsigned long long *t) __attribute__((weak, alias("nop_timebase")))
71 {
72 #pragma message("There is no default timer device, metal_timer_get_timebase_frequency() will always return timebase -1.")
73 }
74 int metal_timer_set_tick(int second) __attribute__((weak, alias("nop_tick")))
75 {
76 #pragma message("There is no default timer device, metal_timer_set_tick) will always return -1.")
77 }
78
79 #endif
80