summaryrefslogtreecommitdiff
path: root/code/fe310/eos/event.c
diff options
context:
space:
mode:
Diffstat (limited to 'code/fe310/eos/event.c')
-rw-r--r--code/fe310/eos/event.c82
1 files changed, 45 insertions, 37 deletions
diff --git a/code/fe310/eos/event.c b/code/fe310/eos/event.c
index 25f1fa1..a1cf591 100644
--- a/code/fe310/eos/event.c
+++ b/code/fe310/eos/event.c
@@ -1,56 +1,65 @@
+#include <stdint.h>
#include <stdio.h>
+#include <unistd.h>
#include "encoding.h"
#include "platform.h"
-#include "eos.h"
-#include "msgq.h"
#include "net.h"
-#include "timer.h"
+#include "msgq.h"
#include "event.h"
-static EOSMsgQ event_q;
+EOSMsgQ _eos_event_q;
static EOSMsgItem event_q_array[EOS_EVT_SIZE_Q];
-static eos_evt_fptr_t evt_net_handler[EOS_NET_MAX_CMD];
-static eos_evt_fptr_t evt_timer_handler = NULL;
-static eos_evt_fptr_t evt_ui_handler = NULL;
-
-static void bad_handler(unsigned char cmd, unsigned char *buffer, uint16_t len) {
- printf("bad handler: %d\n", cmd);
-}
+static eos_evt_fptr_t evt_handler[EOS_EVT_MAX_EVT];
+static uint16_t evt_handler_wrapper_acq = 0;
+static uint16_t evt_handler_wrapper_en = 0;
void eos_evtq_init(void) {
int i;
- for (i=0; i<EOS_NET_MAX_CMD; i++) {
- evt_net_handler[i] = bad_handler;
+ for (i=0; i<EOS_EVT_MAX_EVT; i++) {
+ evt_handler[i] = eos_evtq_bad_handler;
}
- evt_timer_handler = bad_handler;
- evt_ui_handler = bad_handler;
- eos_msgq_init(&event_q, event_q_array, EOS_EVT_SIZE_Q);
+ eos_msgq_init(&_eos_event_q, event_q_array, EOS_EVT_SIZE_Q);
}
int eos_evtq_push(unsigned char cmd, unsigned char *buffer, uint16_t len) {
- return eos_msgq_push(&event_q, cmd, buffer, len);
+ return eos_msgq_push(&_eos_event_q, cmd, buffer, len);
}
void eos_evtq_pop(unsigned char *cmd, unsigned char **buffer, uint16_t *len) {
- eos_msgq_pop(&event_q, cmd, buffer, len);
+ eos_msgq_pop(&_eos_event_q, cmd, buffer, len);
+}
+
+void eos_evtq_bad_handler(unsigned char cmd, unsigned char *buffer, uint16_t len) {
+ write(1, "error\n", 6);
+}
+
+void eos_evtq_handler_wrapper(unsigned char cmd, unsigned char *buffer, uint16_t len, uint16_t *flags_acq, uint16_t flag, eos_evt_fptr_t f) {
+ int ok = eos_net_acquire(*flags_acq & flag);
+ if (ok) {
+ f(cmd, buffer, len);
+ eos_net_release(1);
+ *flags_acq &= ~flag;
+ } else {
+ *flags_acq |= flag;
+ eos_evtq_push(cmd, buffer, len);
+ }
}
void eos_evtq_handle(unsigned char cmd, unsigned char *buffer, uint16_t len) {
- if (cmd & EOS_EVT_MASK_NET) {
- cmd &= ~EOS_EVT_MASK_NET;
- if (cmd < EOS_NET_MAX_CMD) {
- evt_net_handler[cmd](cmd, buffer, len);
+ if (((cmd & EOS_EVT_MASK) >> 4) > EOS_EVT_MAX_EVT) {
+ eos_evtq_bad_handler(cmd, buffer, len);
+ } else {
+ unsigned char idx = ((cmd & EOS_EVT_MASK) >> 4) - 1;
+ uint16_t flag = (uint16_t)1 << idx;
+ if (flag & evt_handler_wrapper_en) {
+ eos_evtq_handler_wrapper(cmd, buffer, len, &evt_handler_wrapper_acq, flag, evt_handler[idx]);
} else {
- bad_handler(cmd, buffer, len);
+ evt_handler[idx](cmd, buffer, len);
}
- } else if (cmd == EOS_EVT_TIMER) {
- evt_timer_handler(cmd, buffer, len);
- } else if (cmd == EOS_EVT_UI) {
- evt_ui_handler(cmd, buffer, len);
}
}
@@ -58,7 +67,7 @@ void eos_evtq_loop(void) {
unsigned char cmd;
unsigned char *buffer;
uint16_t len;
- volatile int foo = 1;
+ int foo = 1;
while(foo) {
clear_csr(mstatus, MSTATUS_MIE);
@@ -68,18 +77,17 @@ void eos_evtq_loop(void) {
eos_evtq_handle(cmd, buffer, len);
clear_csr(mstatus, MSTATUS_MIE);
} else {
- // asm volatile ("wfi");
+ asm volatile ("wfi");
}
set_csr(mstatus, MSTATUS_MIE);
}
}
-void eos_evtq_set_handler(unsigned char cmd, eos_evt_fptr_t handler) {
- if (cmd & EOS_EVT_MASK_NET) {
- evt_net_handler[cmd & ~EOS_EVT_MASK_NET] = handler;
- } else if (cmd == EOS_EVT_TIMER) {
- evt_timer_handler = handler;
- } else if (cmd == EOS_EVT_UI) {
- evt_ui_handler = handler;
+void eos_evtq_set_handler(unsigned char cmd, eos_evt_fptr_t handler, uint8_t flags) {
+ if (flags & EOS_EVT_FLAG_WRAP) {
+ uint16_t flag = (uint16_t)1 << (((cmd & EOS_EVT_MASK) >> 4) - 1);
+ evt_handler_wrapper_en |= flag;
}
-} \ No newline at end of file
+ evt_handler[((cmd & EOS_EVT_MASK) >> 4) - 1] = handler;
+}
+