summaryrefslogtreecommitdiff
path: root/ecp/src/platform/fe310
diff options
context:
space:
mode:
authorUros Majstorovic <majstor@majstor.org>2022-02-02 06:40:10 +0100
committerUros Majstorovic <majstor@majstor.org>2022-02-02 06:40:10 +0100
commit64b55e7e1236121ea4197d9a37cfec43b196cfe8 (patch)
treeeb16f4a2d3eae3d9485eccbd923812a56b627979 /ecp/src/platform/fe310
parenta4401c99c2a54ba9a964317cbff915d40d16e470 (diff)
moved ecp, platform -> src
Diffstat (limited to 'ecp/src/platform/fe310')
-rw-r--r--ecp/src/platform/fe310/Makefile19
-rw-r--r--ecp/src/platform/fe310/platform.mk3
-rw-r--r--ecp/src/platform/fe310/platform_obj.mk1
-rw-r--r--ecp/src/platform/fe310/time.c35
-rw-r--r--ecp/src/platform/fe310/transport.c116
-rw-r--r--ecp/src/platform/fe310/transport.h7
6 files changed, 181 insertions, 0 deletions
diff --git a/ecp/src/platform/fe310/Makefile b/ecp/src/platform/fe310/Makefile
new file mode 100644
index 0000000..1a3502a
--- /dev/null
+++ b/ecp/src/platform/fe310/Makefile
@@ -0,0 +1,19 @@
+include ../common.mk
+
+obj_tr = transport.o
+obj_tm = time.o
+
+
+%.o: %.c
+ $(CC) $(CFLAGS) -c $<
+
+all: libecptr.a libecptm.a
+
+libecptr.a: $(obj_tr)
+ $(AR) rcs libecptr.a $(obj_tr)
+
+libecptm.a: $(obj_tm)
+ $(AR) rcs libecptm.a $(obj_tm)
+
+clean:
+ rm -f *.o *.a
diff --git a/ecp/src/platform/fe310/platform.mk b/ecp/src/platform/fe310/platform.mk
new file mode 100644
index 0000000..0230f29
--- /dev/null
+++ b/ecp/src/platform/fe310/platform.mk
@@ -0,0 +1,3 @@
+fe310_dir = $(abspath $(src_dir)/../../fw/fe310)
+include $(fe310_dir)/platform.mk
+CFLAGS += -I$(fe310_dir) -DECP_WITH_VCONN=1 -DECP_DEBUG=1 -D__FE310__
diff --git a/ecp/src/platform/fe310/platform_obj.mk b/ecp/src/platform/fe310/platform_obj.mk
new file mode 100644
index 0000000..52e4bb1
--- /dev/null
+++ b/ecp/src/platform/fe310/platform_obj.mk
@@ -0,0 +1 @@
+subdirs += vconn
diff --git a/ecp/src/platform/fe310/time.c b/ecp/src/platform/fe310/time.c
new file mode 100644
index 0000000..3c74933
--- /dev/null
+++ b/ecp/src/platform/fe310/time.c
@@ -0,0 +1,35 @@
+#include <core.h>
+#include <tr.h>
+
+#include <eos/eos.h>
+#include <eos/net.h>
+#include <eos/event.h>
+#include <eos/timer.h>
+
+extern ECPSocket *_ecp_tr_sock;
+
+static void timer_handler(unsigned char type) {
+ ecp_cts_t next = ecp_timer_exe(_ecp_tr_sock);
+ if (next) {
+ eos_timer_set(next, EOS_TIMER_ETYPE_ECP);
+ }
+}
+
+int ecp_tm_init(ECPContext *ctx) {
+ eos_timer_set_handler(EOS_TIMER_ETYPE_ECP, timer_handler);
+ eos_net_acquire_for_evt(EOS_EVT_TIMER | EOS_TIMER_ETYPE_ECP, 1);
+ return ECP_OK;
+}
+
+ecp_cts_t ecp_tm_abstime_ms(ecp_cts_t msec) {
+ return eos_time_get_tick() * 1000 / EOS_TIMER_RTC_FREQ + msec;
+}
+
+void ecp_tm_sleep_ms(ecp_cts_t msec) {
+ eos_time_sleep(msec);
+}
+
+void ecp_tm_timer_set(ecp_cts_t next) {
+ uint32_t _next = eos_timer_get(EOS_TIMER_ETYPE_ECP);
+ if ((_next == EOS_TIMER_NONE) || (next < _next)) eos_timer_set(next, EOS_TIMER_ETYPE_ECP);
+}
diff --git a/ecp/src/platform/fe310/transport.c b/ecp/src/platform/fe310/transport.c
new file mode 100644
index 0000000..777a956
--- /dev/null
+++ b/ecp/src/platform/fe310/transport.c
@@ -0,0 +1,116 @@
+#include <stdlib.h>
+
+#include <core.h>
+#include <tr.h>
+
+#include <eos/eos.h>
+#include <eos/net.h>
+
+static unsigned char _flags = 0;
+
+ECPSocket *_ecp_tr_sock = NULL;
+
+static void packet_handler(unsigned char type, unsigned char *buffer, uint16_t len) {
+ ECPNetAddr addr;
+
+ ECP2Buffer bufs;
+ ECPBuffer packet;
+ ECPBuffer payload;
+ unsigned char pld_buf[ECP_MAX_PLD];
+
+ bufs.packet = &packet;
+ bufs.payload = &payload;
+
+ packet.buffer = buffer+EOS_SOCK_SIZE_UDP_HDR;
+ packet.size = ECP_MAX_PKT;
+ payload.buffer = pld_buf;
+ payload.size = ECP_MAX_PLD;
+
+ if ((buffer == NULL) || (len < EOS_SOCK_SIZE_UDP_HDR)) {
+ eos_net_free(buffer, 0);
+ return;
+ }
+
+ eos_sock_getfrom(buffer, &addr);
+ ssize_t rv = ecp_pkt_handle(_ecp_tr_sock, &addr, NULL, &bufs, len-EOS_SOCK_SIZE_UDP_HDR);
+#ifdef ECP_DEBUG
+ if (rv < 0) {
+ char b[16];
+ puts("ERR:");
+ puts(itoa(rv, b, 10));
+ puts("\n");
+ }
+#endif
+ if (bufs.packet->buffer) eos_net_free(buffer, 0);
+ eos_net_release();
+}
+
+int ecp_tr_init(ECPContext *ctx) {
+ return ECP_OK;
+}
+
+int ecp_tr_addr_eq(ECPNetAddr *addr1, ECPNetAddr *addr2) {
+ if (addr1->port != addr2->port) return 0;
+ if (memcmp(addr1->host, addr2->host, sizeof(addr1->host)) != 0) return 0;
+ return 1;
+}
+
+int ecp_tr_addr_set(ECPNetAddr *addr, void *addr_s) {
+ return ECP_ERR;
+}
+
+int ecp_tr_open(ECPSocket *sock, void *addr_s) {
+ sock->sock = eos_sock_open_udp(packet_handler);
+ if (sock->sock < 0) {
+ sock->sock = 0;
+ return ECP_ERR_SEND;
+ }
+ _ecp_tr_sock = sock;
+ return ECP_OK;
+}
+
+void ecp_tr_close(ECPSocket *sock) {
+ eos_sock_close(sock->sock);
+ _ecp_tr_sock = NULL;
+}
+
+ssize_t ecp_tr_send(ECPSocket *sock, ECPBuffer *packet, size_t msg_size, ECPNetAddr *addr, unsigned char flags) {
+ unsigned char *buf = NULL;
+ int rv;
+
+ flags |= _flags;
+ if (packet && packet->buffer) {
+ if (flags & ECP_SEND_FLAG_REPLY) {
+ buf = packet->buffer-EOS_SOCK_SIZE_UDP_HDR;
+ packet->buffer = NULL;
+ } else {
+ buf = eos_net_alloc();
+ memcpy(buf+EOS_SOCK_SIZE_UDP_HDR, packet->buffer, msg_size);
+ }
+ }
+ if (buf == NULL) return ECP_ERR;
+ rv = eos_sock_sendto(sock->sock, buf, msg_size+EOS_SOCK_SIZE_UDP_HDR, flags & ECP_SEND_FLAG_MORE, addr);
+ if (rv) return ECP_ERR_SEND;
+ return msg_size;
+}
+
+ssize_t ecp_tr_recv(ECPSocket *sock, ECPBuffer *packet, ECPNetAddr *addr, int timeout) {
+ return ECP_ERR;
+}
+
+void ecp_tr_release(ECPBuffer *packet, unsigned char more) {
+ if (packet && packet->buffer) {
+ eos_net_free(packet->buffer-EOS_SOCK_SIZE_UDP_HDR, more);
+ packet->buffer = NULL;
+ } else if (!more) {
+ eos_net_release();
+ }
+}
+
+void ecp_tr_flag_set(unsigned char flags) {
+ _flags |= flags;
+}
+
+void ecp_tr_flag_clear(unsigned char flags) {
+ _flags &= ~flags;
+}
diff --git a/ecp/src/platform/fe310/transport.h b/ecp/src/platform/fe310/transport.h
new file mode 100644
index 0000000..47ad482
--- /dev/null
+++ b/ecp/src/platform/fe310/transport.h
@@ -0,0 +1,7 @@
+#include <eos/sock.h>
+
+#define ECP_IPv4_ADDR_SIZE 4
+
+typedef EOSNetAddr ECPNetAddr;
+typedef int ECPNetSock;
+