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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
|
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include "encoding.h"
#include "platform.h"
#include "eos.h"
#include "log.h"
#include "msgq.h"
#include "event.h"
EOSMsgQ _eos_event_q;
static EOSMsgItem event_q_array[EOS_EVT_SIZE_Q];
static eos_evt_handler_t evt_handler[EOS_EVT_MAX];
static eos_evt_handler_global_t evt_handler_global;
static eos_evt_loopf_t evt_loop_f;
static void evtq_handler(unsigned char type, unsigned char *buffer, uint16_t len, uint8_t _idx) {
unsigned char idx = (type & EOS_EVT_MASK) >> 4;
if (idx && (idx <= EOS_EVT_MAX)) {
evt_handler[idx - 1](type, buffer, len);
} else {
eos_evtq_bad_handler(type, buffer, len);
}
}
int eos_evtq_init(void) {
int i;
evt_handler_global = evtq_handler;
for (i=0; i<EOS_EVT_MAX; i++) {
evt_handler[i] = eos_evtq_bad_handler;
}
eos_msgq_init(&_eos_event_q, event_q_array, EOS_EVT_SIZE_Q);
return EOS_OK;
}
int eos_evtq_len(void) {
int rv;
clear_csr(mstatus, MSTATUS_MIE);
rv = eos_msgq_len(&_eos_event_q);
set_csr(mstatus, MSTATUS_MIE);
return rv;
}
int eos_evtq_push(unsigned char type, unsigned char *buffer, uint16_t len) {
int rv;
clear_csr(mstatus, MSTATUS_MIE);
rv = eos_msgq_push(&_eos_event_q, type, buffer, len);
set_csr(mstatus, MSTATUS_MIE);
return rv;
}
int eos_evtq_push_widx(unsigned char type, unsigned char *buffer, uint16_t len, uint8_t *idx) {
int rv;
clear_csr(mstatus, MSTATUS_MIE);
rv = eos_msgq_push_widx(&_eos_event_q, type, buffer, len, idx);
set_csr(mstatus, MSTATUS_MIE);
return rv;
}
int eos_evtq_push_isr(unsigned char type, unsigned char *buffer, uint16_t len) {
return eos_msgq_push(&_eos_event_q, type, buffer, len);
}
int eos_evtq_push_widx_isr(unsigned char type, unsigned char *buffer, uint16_t len, uint8_t *idx) {
return eos_msgq_push_widx(&_eos_event_q, type, buffer, len, idx);
}
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_pop_widx(unsigned char *type, unsigned char **buffer, uint16_t *len, uint8_t *idx) {
clear_csr(mstatus, MSTATUS_MIE);
eos_msgq_pop_widx(&_eos_event_q, type, buffer, len, idx);
set_csr(mstatus, MSTATUS_MIE);
}
void eos_evtq_pop_isr(unsigned char *type, unsigned char **buffer, uint16_t *len) {
eos_msgq_pop(&_eos_event_q, type, buffer, len);
}
void eos_evtq_pop_widx_isr(unsigned char *type, unsigned char **buffer, uint16_t *len, uint8_t *idx) {
eos_msgq_pop_widx(&_eos_event_q, type, buffer, len, idx);
}
int eos_evtq_get(unsigned char type, unsigned char **buffer, uint16_t *len) {
int rv = 0;
clear_csr(mstatus, MSTATUS_MIE);
rv = eos_msgq_find(&_eos_event_q, type, NULL, 0, buffer, len);
set_csr(mstatus, MSTATUS_MIE);
return rv;
}
int eos_evtq_find(unsigned char type, unsigned char *selector, uint16_t sel_len, unsigned char **buffer, uint16_t *len) {
int rv = 0;
clear_csr(mstatus, MSTATUS_MIE);
rv = eos_msgq_find(&_eos_event_q, type, selector, sel_len, buffer, len);
set_csr(mstatus, MSTATUS_MIE);
return rv;
}
int eos_evtq_wait(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_find(&_eos_event_q, type, selector, sel_len, buffer, len);
if (rv && (rv != EOS_ERR_NOTFOUND)) {
set_csr(mstatus, MSTATUS_MIE);
return rv;
}
if (rv) {
unsigned char _type;
unsigned char *_buffer;
uint16_t _len;
uint8_t idx;
eos_msgq_pop_widx(&_eos_event_q, &_type, &_buffer, &_len, &idx);
if (_type) {
set_csr(mstatus, MSTATUS_MIE);
evt_handler_global(_type, _buffer, _len, idx);
} else {
asm volatile ("wfi");
set_csr(mstatus, MSTATUS_MIE);
}
} else {
set_csr(mstatus, MSTATUS_MIE);
}
}
}
void eos_evtq_flush(void) {
clear_csr(mstatus, MSTATUS_MIE);
eos_evtq_flush_isr();
set_csr(mstatus, MSTATUS_MIE);
}
void eos_evtq_flush_isr(void) {
unsigned char type;
unsigned char *buffer;
uint16_t len;
uint8_t idx;
do {
eos_msgq_pop_widx(&_eos_event_q, &type, &buffer, &len, &idx);
if (type) {
set_csr(mstatus, MSTATUS_MIE);
evt_handler_global(type, buffer, len, idx);
clear_csr(mstatus, MSTATUS_MIE);
}
} while (type);
}
void eos_evtq_loop(void) {
int foo = 1;
while(foo) {
eos_evtq_exec();
if (evt_loop_f) evt_loop_f();
}
}
void eos_evtq_exec(void) {
unsigned char type;
unsigned char *buffer;
uint16_t len;
uint8_t idx;
clear_csr(mstatus, MSTATUS_MIE);
eos_msgq_pop_widx(&_eos_event_q, &type, &buffer, &len, &idx);
if (type) {
set_csr(mstatus, MSTATUS_MIE);
evt_handler_global(type, buffer, len, idx);
} else {
asm volatile ("wfi");
set_csr(mstatus, MSTATUS_MIE);
}
}
void eos_evtq_set_loopf(eos_evt_loopf_t loop_f) {
evt_loop_f = loop_f;
}
void eos_evtq_bad_handler(unsigned char type, unsigned char *buffer, uint16_t len) {
EOS_LOG(EOS_LOG_ERR, "EVT BAD HANDLER:0x%.2X\n", type);
}
void eos_evtq_set_handler(unsigned char type, eos_evt_handler_t handler) {
unsigned char idx = (type & EOS_EVT_MASK) >> 4;
if (handler == NULL) handler = eos_evtq_bad_handler;
if (idx && (idx <= EOS_EVT_MAX)) evt_handler[idx - 1] = handler;
}
eos_evt_handler_t eos_evtq_get_handler(unsigned char type) {
unsigned char idx = (type & EOS_EVT_MASK) >> 4;
if (idx && (idx <= EOS_EVT_MAX)) return evt_handler[idx - 1];
return NULL;
}
void eos_evtq_set_handler_global(eos_evt_handler_global_t handler) {
evt_handler_global = handler;
}
eos_evt_handler_global_t eos_evtq_get_handler_global(void) {
return evt_handler_global;
}
|