summaryrefslogtreecommitdiff
path: root/code/fe310/eos/timer.c
blob: c62986612f188bc6e81f734f69d73eaee755d503 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <stdlib.h>
#include <stdint.h>

#include "encoding.h"
#include "platform.h"

#include "msgq.h"
#include "event.h"
#include "timer.h"

#define MIN(X, Y)               (((X) < (Y)) ? (X) : (Y))
#define MAX(X, Y)               (((X) > (Y)) ? (X) : (Y))

static eos_timer_fptr_t timer_handler[EOS_TIMER_MAX_ETYPE + 1];
static uint64_t timer_next[EOS_TIMER_MAX_ETYPE + 1];

static void timer_handler_evt(unsigned char type, unsigned char *buffer, uint16_t len) {
    unsigned char idx = (type & ~EOS_EVT_MASK) - 1;

    if ((idx < EOS_TIMER_MAX_ETYPE) && timer_handler[idx]) {
        timer_handler[idx](type);
    } else {
        eos_evtq_bad_handler(type, buffer, len);
    }
}

void _eos_timer_handle(void) {
    int i;
    volatile uint64_t *mtime = (uint64_t *) (CLINT_CTRL_ADDR + CLINT_MTIME);
    uint64_t *mtimecmp = (uint64_t *) (CLINT_CTRL_ADDR + CLINT_MTIMECMP);
    uint64_t now = *mtime;
    uint64_t next = 0;

    for (i = 0; i <= EOS_TIMER_MAX_ETYPE; i++) {
        if (timer_next[i] && (timer_next[i] <= now)) {
            timer_next[i] = 0;
            if (i == EOS_TIMER_MAX_ETYPE) {
                timer_handler[EOS_TIMER_MAX_ETYPE](0);
            } else {
                eos_evtq_push_isr(EOS_EVT_TIMER | i + 1, NULL, 0);
            }
        }
        next = next && timer_next[i] ? MIN(next, timer_next[i]) : (next ? next : timer_next[i]);
    }
    *mtimecmp = next;
    if (*mtimecmp == 0) clear_csr(mie, MIP_MTIP);
}

void eos_timer_init(void) {
    int i;
    uint64_t *mtimecmp = (uint64_t *) (CLINT_CTRL_ADDR + CLINT_MTIMECMP);

    clear_csr(mie, MIP_MTIP);
    *mtimecmp = 0;
    for (i=0; i<=EOS_TIMER_MAX_ETYPE; i++) {
        timer_next[i] = 0;
        timer_handler[i] = NULL;
    }
    eos_evtq_set_handler(EOS_EVT_TIMER, timer_handler_evt, 0);
}

void eos_timer_set_handler(unsigned char evt, eos_timer_fptr_t handler, uint8_t flags) {
    uint64_t *mtimecmp = (uint64_t *) (CLINT_CTRL_ADDR + CLINT_MTIMECMP);

    if (evt && (evt <= EOS_TIMER_MAX_ETYPE)) {
        evt--;
    } else if (evt == 0) {
        evt = EOS_TIMER_MAX_ETYPE;
    } else {
        return;
    }

    clear_csr(mie, MIP_MTIP);
    timer_handler[evt] = handler;
    if (*mtimecmp != 0) set_csr(mie, MIP_MTIP);

    if (evt != EOS_TIMER_MAX_ETYPE) eos_evtq_set_hflags(EOS_EVT_TIMER | evt + 1, flags);
}

void eos_timer_set_hflags(unsigned char evt, uint8_t flags) {
    if (evt && (evt < EOS_TIMER_MAX_ETYPE)) eos_evtq_set_hflags(EOS_EVT_TIMER | evt, flags);
}

uint64_t eos_timer_get(unsigned char evt) {
    uint64_t *mtimecmp = (uint64_t *) (CLINT_CTRL_ADDR + CLINT_MTIMECMP);
    uint64_t ret = 0;

    if (evt && (evt <= EOS_TIMER_MAX_ETYPE)) {
        evt--;
    } else if (evt == 0) {
        evt = EOS_TIMER_MAX_ETYPE;
    } else {
        return 0;
    }

    clear_csr(mie, MIP_MTIP);
    ret = timer_next[evt];
    if (*mtimecmp != 0) set_csr(mie, MIP_MTIP);

    return ret;
}

void eos_timer_set(uint32_t msec, unsigned char evt, unsigned char b) {
    int i;
    volatile uint64_t *mtime = (uint64_t *) (CLINT_CTRL_ADDR + CLINT_MTIME);
    uint64_t *mtimecmp = (uint64_t *) (CLINT_CTRL_ADDR + CLINT_MTIMECMP);
    uint64_t tick = *mtime + msec * (uint64_t)RTC_FREQ / 1000;
    uint64_t next = 0;

    if (evt && (evt <= EOS_TIMER_MAX_ETYPE)) {
        evt--;
    } else if (evt == 0) {
        evt = EOS_TIMER_MAX_ETYPE;
    } else {
        return;
    }

    clear_csr(mie, MIP_MTIP);
    if (!b || (timer_next[evt] == 0) || (tick < timer_next[evt])) {
        timer_next[evt] = tick;
        for (i = 0; i <= EOS_TIMER_MAX_ETYPE; i++) {
            next = next && timer_next[i] ? MIN(next, timer_next[i]) : (next ? next : timer_next[i]);
        }
        *mtimecmp = next;
    }
    if (*mtimecmp != 0) set_csr(mie, MIP_MTIP);
}

void eos_timer_clear(unsigned char evt) {
    int i;
    uint64_t *mtimecmp = (uint64_t *) (CLINT_CTRL_ADDR + CLINT_MTIMECMP);
    uint64_t next = 0;

    if (evt && (evt <= EOS_TIMER_MAX_ETYPE)) {
        evt--;
    } else if (evt == 0) {
        evt = EOS_TIMER_MAX_ETYPE;
    } else {
        return;
    }

    clear_csr(mie, MIP_MTIP);
    if (timer_next[evt]) {
        timer_next[evt] = 0;
        for (i = 0; i <= EOS_TIMER_MAX_ETYPE; i++) {
            next = next && timer_next[i] ? MIN(next, timer_next[i]) : (next ? next : timer_next[i]);
        }
        *mtimecmp = next;
    }
    if (*mtimecmp != 0) set_csr(mie, MIP_MTIP);
}

void eos_timer_sleep(uint32_t msec) {
    volatile uint64_t *mtime = (uint64_t *) (CLINT_CTRL_ADDR + CLINT_MTIME);

    uint64_t now_ms = *mtime * 1000 / RTC_FREQ;
    while (*mtime * 1000 / RTC_FREQ < now_ms + msec);
}