summaryrefslogtreecommitdiff
path: root/code/fe310/eos/event.c
blob: bf11729f7f61499d56258d4a552f9964dd3f2973 (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
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <stdio.h>

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

#include "net.h"
#include "msgq.h"
#include "event.h"

EOSMsgQ _eos_event_q;
static EOSMsgItem event_q_array[EOS_EVT_SIZE_Q];

static eos_evt_fptr_t evt_handler[EOS_EVT_MAX_EVT];
static uint16_t evt_handler_wrapper_acq[EOS_EVT_MAX_EVT];
static uint16_t evt_handler_flags_buf_acq[EOS_EVT_MAX_EVT];

void eos_evtq_init(void) {
    int i;

    for (i=0; i<EOS_EVT_MAX_EVT; i++) {
        evt_handler[i] = eos_evtq_bad_handler;
    }
    eos_msgq_init(&_eos_event_q, event_q_array, EOS_EVT_SIZE_Q);
}

int eos_evtq_push(unsigned char type, unsigned char *buffer, uint16_t len) {
    clear_csr(mstatus, MSTATUS_MIE);
    int ret = eos_msgq_push(&_eos_event_q, type, buffer, len);
    set_csr(mstatus, MSTATUS_MIE);
    return ret;
}

int eos_evtq_push_isr(unsigned char type, unsigned char *buffer, uint16_t len) {
    return eos_msgq_push(&_eos_event_q, type, buffer, len);
}

void eos_evtq_pop(unsigned char *type, unsigned char **buffer, uint16_t *len) {
    clear_csr(mstatus, MSTATUS_MIE);
    eos_msgq_pop(&_eos_event_q, type, buffer, len);
    set_csr(mstatus, MSTATUS_MIE);
}

void eos_evtq_bad_handler(unsigned char type, unsigned char *buffer, uint16_t len) {
    printf("evt bad handler:%d\n", type);
}

static void evtq_handler_wrapper(unsigned char type, unsigned char *buffer, uint16_t len) {
    unsigned char idx = ((type & EOS_EVT_MASK) >> 4) - 1;
    uint16_t flag = (uint16_t)1 << ((type & ~EOS_EVT_MASK) - 1);
    int ok;

    ok = _eos_net_acquire(evt_handler_wrapper_acq[idx] & flag);
    if (ok) {
        evt_handler[idx](type, buffer, len);
        eos_net_release();
        evt_handler_wrapper_acq[idx] &= ~flag;
    } else {
        evt_handler_wrapper_acq[idx] |= flag;
        eos_evtq_push(type, buffer, len);
    }
}

static void evtq_handler(unsigned char type, unsigned char *buffer, uint16_t len) {
    unsigned char idx = ((type & EOS_EVT_MASK) >> 4) - 1;
    uint16_t flag = (uint16_t)1 << ((type & ~EOS_EVT_MASK) - 1);

    if (idx >= EOS_EVT_MAX_EVT) {
        eos_evtq_bad_handler(type, buffer, len);
        return;
    }
    if (flag & evt_handler_flags_buf_acq[idx]) {
        evtq_handler_wrapper(type, buffer, len);
    } else {
        evt_handler[idx](type, buffer, len);
    }
}

void eos_evtq_set_handler(unsigned char type, eos_evt_fptr_t handler, uint8_t flags) {
    unsigned char idx = ((type & EOS_EVT_MASK) >> 4) - 1;

    if (idx < EOS_EVT_MAX_EVT) {
        evt_handler[idx] = handler;
        eos_evtq_set_hflags(type, flags);
    }
}

void eos_evtq_set_hflags(unsigned char type, uint8_t flags) {
    unsigned char idx = ((type & EOS_EVT_MASK) >> 4) - 1;
    uint16_t flag = type & ~EOS_EVT_MASK ? (uint16_t)1 << ((type & ~EOS_EVT_MASK) - 1) : 0xFFFF;

    if (idx < EOS_EVT_MAX_EVT) {
        evt_handler_flags_buf_acq[idx] &= ~flag;
        if (flags & EOS_NET_FLAG_BACQ) evt_handler_flags_buf_acq[idx] |= flag;
    }
}

void eos_evtq_get(unsigned char type, unsigned char *selector, uint16_t sel_len, unsigned char **buffer, uint16_t *len) {
    int rv = 0;

    while(!rv) {
        clear_csr(mstatus, MSTATUS_MIE);
        rv = eos_msgq_get(&_eos_event_q, type, selector, sel_len, buffer, len);
        if (!rv) {
            unsigned char _type;
            unsigned char *_buffer;
            uint16_t _len;

            eos_msgq_pop(&_eos_event_q, &_type, &_buffer, &_len);
            if (_type) {
                set_csr(mstatus, MSTATUS_MIE);
                evtq_handler(_type, _buffer, _len);
            } else {
                asm volatile ("wfi");
                set_csr(mstatus, MSTATUS_MIE);
            }
        } else {
            set_csr(mstatus, MSTATUS_MIE);
        }
    }
}

void eos_evtq_loop(void) {
    unsigned char type;
    unsigned char *buffer;
    uint16_t len;
    int foo = 1;

    while(foo) {
        clear_csr(mstatus, MSTATUS_MIE);
        eos_msgq_pop(&_eos_event_q, &type, &buffer, &len);
        if (type) {
            set_csr(mstatus, MSTATUS_MIE);
            evtq_handler(type, buffer, len);
        } else {
            asm volatile ("wfi");
            set_csr(mstatus, MSTATUS_MIE);
        }
    }
}