From 97968cb17dee6d1d0e68149fe2fb71362cdc5851 Mon Sep 17 00:00:00 2001 From: Uros Majstorovic Date: Sat, 19 Oct 2019 19:40:16 +0200 Subject: network sockets added --- code/fe310/eos/Makefile | 2 +- code/fe310/eos/ecp.c | 57 ++----------------------------------- code/fe310/eos/ecp.h | 6 ++-- code/fe310/eos/eos.h | 1 + code/fe310/eos/event.c | 2 +- code/fe310/eos/event.h | 2 +- code/fe310/eos/net.c | 73 +++++++++++++++++++++++------------------------- code/fe310/eos/net.h | 8 ++---- code/fe310/eos/net_def.h | 14 ++++++---- 9 files changed, 55 insertions(+), 110 deletions(-) (limited to 'code') diff --git a/code/fe310/eos/Makefile b/code/fe310/eos/Makefile index 94eb89a..b7f401c 100644 --- a/code/fe310/eos/Makefile +++ b/code/fe310/eos/Makefile @@ -7,7 +7,7 @@ AR = $(FE310_HOME)/work/build/riscv-gnu-toolchain/riscv64-unknown-elf/prefix/bin CFLAGS = $(CFLAGS_PL) -I../.. -obj = trap_entry.o eos.o msgq.o event.o interrupt.o timer.o i2s.o spi.o net.o ecp.o +obj = trap_entry.o eos.o msgq.o event.o interrupt.o timer.o i2s.o spi.o net.o sock.o ecp.o %.o: %.c %.h diff --git a/code/fe310/eos/ecp.c b/code/fe310/eos/ecp.c index 9611208..f151d1b 100644 --- a/code/fe310/eos/ecp.c +++ b/code/fe310/eos/ecp.c @@ -3,70 +3,19 @@ #include #include +#include +#include + #include "encoding.h" #include "platform.h" -#include "event.h" -#include "timer.h" -#include "net.h" - #include "ecp.h" -static ECPSocket *_sock = NULL; - -static void timer_handler(unsigned char type) { - ecp_cts_t next = ecp_timer_exe(_sock); - if (next) { - volatile uint64_t *mtime = (uint64_t *) (CLINT_CTRL_ADDR + CLINT_MTIME); - uint64_t tick = *mtime + next * (uint64_t)RTC_FREQ / 1000; - eos_timer_set(tick, EOS_TIMER_ETYPE_ECP, 0); - } -} - -static void packet_handler(unsigned char type, unsigned char *buffer, uint16_t len) { - ECPNetAddr addr; - size_t addr_len = sizeof(addr.host) + sizeof(addr.port); - - ECP2Buffer bufs; - ECPBuffer packet; - ECPBuffer payload; - unsigned char pld_buf[ECP_MAX_PLD]; - - bufs.packet = &packet; - bufs.payload = &payload; - - packet.buffer = buffer+addr_len; - packet.size = ECP_MAX_PKT; - payload.buffer = pld_buf; - payload.size = ECP_MAX_PLD; - - memcpy(addr.host, buffer, sizeof(addr.host)); - memcpy(&addr.port, buffer+sizeof(addr.host), sizeof(addr.port)); - ssize_t rv = ecp_pkt_handle(_sock, &addr, NULL, &bufs, len-addr_len); -#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_init(ECPContext *ctx) { int rv; rv = ecp_ctx_create_vconn(ctx); if (rv) return rv; - eos_timer_set_handler(EOS_TIMER_ETYPE_ECP, timer_handler, EOS_EVT_FLAG_NET_BUF_ACQ); - /* XXX */ - // eos_net_set_handler(EOS_NET_DATA_PKT, packet_handler, 0); return ECP_OK; } - -void ecp_sock_set(ECPSocket *s) { - _sock = s; -} \ No newline at end of file diff --git a/code/fe310/eos/ecp.h b/code/fe310/eos/ecp.h index 0da4947..3dd9b72 100644 --- a/code/fe310/eos/ecp.h +++ b/code/fe310/eos/ecp.h @@ -1,5 +1,3 @@ -#include -#include +struct ECPContext; -int ecp_init(ECPContext *ctx); -void ecp_sock_set(ECPSocket *s); \ No newline at end of file +int ecp_init(struct ECPContext *ctx); diff --git a/code/fe310/eos/eos.h b/code/fe310/eos/eos.h index 575a457..3314906 100644 --- a/code/fe310/eos/eos.h +++ b/code/fe310/eos/eos.h @@ -1,5 +1,6 @@ #define EOS_OK 0 #define EOS_ERR_Q_FULL -10 #define EOS_ERR_Q_EMPTY -11 +#define EOS_ERR_NET -20 void eos_init(void); diff --git a/code/fe310/eos/event.c b/code/fe310/eos/event.c index bae110e..0335df1 100644 --- a/code/fe310/eos/event.c +++ b/code/fe310/eos/event.c @@ -92,7 +92,7 @@ void eos_evtq_set_busy(char busy) { evt_busy = busy; } -void eos_evtq_wait(unsigned char type, unsigned char *selector, uint16_t sel_len, unsigned char **buffer, uint16_t *len) { +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) { diff --git a/code/fe310/eos/event.h b/code/fe310/eos/event.h index 3d9692d..8a16dfd 100644 --- a/code/fe310/eos/event.h +++ b/code/fe310/eos/event.h @@ -10,6 +10,6 @@ void eos_evtq_pop(unsigned char *type, unsigned char **buffer, uint16_t *len); void eos_evtq_bad_handler(unsigned char type, unsigned char *buffer, uint16_t len); void eos_evtq_set_handler(unsigned char type, eos_evt_fptr_t handler); void eos_evtq_set_flags(unsigned char type, uint8_t flags); -void eos_evtq_wait(unsigned char type, unsigned char *selector, uint16_t sel_len, unsigned char **buffer, uint16_t *len); +void eos_evtq_get(unsigned char type, unsigned char *selector, uint16_t sel_len, unsigned char **buffer, uint16_t *len); void eos_evtq_loop(void); diff --git a/code/fe310/eos/net.c b/code/fe310/eos/net.c index a198955..b6f5722 100644 --- a/code/fe310/eos/net.c +++ b/code/fe310/eos/net.c @@ -71,8 +71,8 @@ static unsigned char *net_bufq_pop(void) { } static void net_xchg_reset(void) { - net_state_flags &= ~NET_FLAG_CTS; - net_state_flags |= NET_FLAG_RST; + net_state_flags &= ~NET_STATE_FLAG_CTS; + net_state_flags |= NET_STATE_FLAG_RST; // before starting a transaction, set SPI peripheral to desired mode SPI1_REG(SPI_REG_CSMODE) = SPI_CSMODE_HOLD; @@ -84,11 +84,11 @@ static void net_xchg_reset(void) { } static void net_xchg_start(unsigned char type, unsigned char *buffer, uint16_t len) { - net_state_flags &= ~NET_FLAG_CTS; - net_state_flags |= NET_FLAG_INIT; + net_state_flags &= ~NET_STATE_FLAG_CTS; + net_state_flags |= NET_STATE_FLAG_INIT; - if (net_state_next_cnt && (net_state_next_buf == NULL)) type |= EOS_NET_MTYPE_FLAG_ONEW; - if (type & EOS_NET_MTYPE_FLAG_ONEW) net_state_flags |= NET_FLAG_ONEW; + if (net_state_next_cnt && (net_state_next_buf == NULL)) type |= NET_MTYPE_FLAG_ONEW; + if (type & NET_MTYPE_FLAG_ONEW) net_state_flags |= NET_STATE_FLAG_ONEW; net_state_type = type; net_state_len_tx = len; @@ -109,23 +109,23 @@ static void net_xchg_start(unsigned char type, unsigned char *buffer, uint16_t l static void net_xchg_handler(void) { volatile uint32_t r1, r2; - if (net_state_flags & NET_FLAG_RST) { - net_state_flags &= ~NET_FLAG_RST; + if (net_state_flags & NET_STATE_FLAG_RST) { + net_state_flags &= ~NET_STATE_FLAG_RST; r1 = SPI1_REG(SPI_REG_RXFIFO); SPI1_REG(SPI_REG_CSMODE) = SPI_CSMODE_AUTO; SPI1_REG(SPI_REG_IE) = 0x0; return; - } else if (net_state_flags & NET_FLAG_INIT) { - net_state_flags &= ~NET_FLAG_INIT; + } else if (net_state_flags & NET_STATE_FLAG_INIT) { + net_state_flags &= ~NET_STATE_FLAG_INIT; SPI1_REG(SPI_REG_TXCTRL) = SPI_TXWM(SPI_SIZE_WM); SPI1_REG(SPI_REG_IE) = SPI_IP_TXWM; r1 = SPI1_REG(SPI_REG_RXFIFO); r2 = SPI1_REG(SPI_REG_RXFIFO); - if (net_state_type & EOS_NET_MTYPE_FLAG_ONEW) { + if (net_state_flags & NET_STATE_FLAG_ONEW) { r1 = 0; r2 = 0; } @@ -163,7 +163,7 @@ static int net_xchg_next(unsigned char *_buffer) { eos_msgq_pop(&net_send_q, &type, &buffer, &len); if (type) { net_xchg_start(type, buffer, len); - } else if (net_state_flags & NET_FLAG_RTS) { + } else if (net_state_flags & NET_STATE_FLAG_RTS) { if (_buffer == NULL) _buffer = net_bufq_pop(); if (_buffer) { net_xchg_start(0, _buffer, 0); @@ -177,9 +177,9 @@ void eos_net_xchg_done(void) { if (net_state_type) { int r = eos_msgq_push(&_eos_event_q, EOS_EVT_NET | net_state_type, _eos_spi_state_buf, net_state_len_rx); if (r) net_bufq_push(_eos_spi_state_buf); - } else if (((net_state_flags & NET_FLAG_ONEW) || net_state_next_cnt) && (net_state_next_buf == NULL)) { + } else if (((net_state_flags & NET_STATE_FLAG_ONEW) || net_state_next_cnt) && (net_state_next_buf == NULL)) { net_state_next_buf = _eos_spi_state_buf; - net_state_flags &= ~NET_FLAG_ONEW; + net_state_flags &= ~NET_STATE_FLAG_ONEW; } else { net_bufq_push(_eos_spi_state_buf); } @@ -187,9 +187,9 @@ void eos_net_xchg_done(void) { static void net_handler_cts(void) { GPIO_REG(GPIO_RISE_IP) = (1 << NET_PIN_CTS); - net_state_flags |= NET_FLAG_CTS; + net_state_flags |= NET_STATE_FLAG_CTS; - if (net_state_flags & NET_FLAG_RUN) { + if (net_state_flags & NET_STATE_FLAG_RUN) { net_xchg_next(NULL); } } @@ -198,11 +198,11 @@ static void net_handler_rts(void) { uint32_t rts_offset = (1 << NET_PIN_RTS); if (GPIO_REG(GPIO_RISE_IP) & rts_offset) { GPIO_REG(GPIO_RISE_IP) = rts_offset; - net_state_flags |= NET_FLAG_RTS; - if ((net_state_flags & NET_FLAG_RUN) && (net_state_flags & NET_FLAG_CTS)) net_xchg_reset(); + net_state_flags |= NET_STATE_FLAG_RTS; + if ((net_state_flags & NET_STATE_FLAG_RUN) && (net_state_flags & NET_STATE_FLAG_CTS)) net_xchg_reset(); } else if (GPIO_REG(GPIO_FALL_IP) & rts_offset) { GPIO_REG(GPIO_FALL_IP) = rts_offset; - net_state_flags &= ~NET_FLAG_RTS; + net_state_flags &= ~NET_STATE_FLAG_RTS; } } @@ -270,8 +270,8 @@ void eos_net_start(void) { SPI1_REG(SPI_REG_SCKDIV) = SPI_DIV_NET; SPI1_REG(SPI_REG_CSID) = SPI_CS_IDX_NET; - net_state_flags |= NET_FLAG_RUN; - if (net_state_flags & NET_FLAG_CTS) net_xchg_next(NULL); + net_state_flags |= NET_STATE_FLAG_RUN; + if (net_state_flags & NET_STATE_FLAG_CTS) net_xchg_next(NULL); } void eos_net_stop(void) { @@ -279,8 +279,8 @@ void eos_net_stop(void) { while (!done) { clear_csr(mstatus, MSTATUS_MIE); - net_state_flags &= ~NET_FLAG_RUN; - done = net_state_flags & NET_FLAG_CTS; + net_state_flags &= ~NET_STATE_FLAG_RUN; + done = net_state_flags & NET_STATE_FLAG_CTS; if (!done) asm volatile ("wfi"); set_csr(mstatus, MSTATUS_MIE); } @@ -310,17 +310,13 @@ int eos_net_acquire(unsigned char reserved) { return ret; } -int eos_net_release(void) { - int rv = EOS_OK; - +void eos_net_release(void) { clear_csr(mstatus, MSTATUS_MIE); if (!net_state_next_cnt && net_state_next_buf) { - rv = net_bufq_push(net_state_next_buf); - if (!rv) net_state_next_buf = NULL; + net_bufq_push(net_state_next_buf); + net_state_next_buf = NULL; } set_csr(mstatus, MSTATUS_MIE); - - return rv; } unsigned char *eos_net_alloc(void) { @@ -340,30 +336,31 @@ unsigned char *eos_net_alloc(void) { return ret; } -int eos_net_free(unsigned char *buffer, unsigned char more) { - int rv = EOS_OK; +void eos_net_free(unsigned char *buffer, unsigned char more) { uint8_t do_release = 1; clear_csr(mstatus, MSTATUS_MIE); if ((more || net_state_next_cnt) && (net_state_next_buf == NULL)) { net_state_next_buf = buffer; } else { - if ((net_state_flags & NET_FLAG_RUN) && (net_state_flags & NET_FLAG_CTS)) do_release = net_xchg_next(buffer); - if (do_release) rv = net_bufq_push(buffer); + if ((net_state_flags & NET_STATE_FLAG_RUN) && (net_state_flags & NET_STATE_FLAG_CTS)) do_release = net_xchg_next(buffer); + if (do_release) net_bufq_push(buffer); } set_csr(mstatus, MSTATUS_MIE); - - return rv; } -int eos_net_send(unsigned char type, unsigned char *buffer, uint16_t len) { +int eos_net_send(unsigned char type, unsigned char *buffer, uint16_t len, unsigned char more) { int rv = EOS_OK; + if (more) { + type |= NET_MTYPE_FLAG_ONEW; + } clear_csr(mstatus, MSTATUS_MIE); - if ((net_state_flags & NET_FLAG_RUN) && (net_state_flags & NET_FLAG_CTS)) { + if ((net_state_flags & NET_STATE_FLAG_RUN) && (net_state_flags & NET_STATE_FLAG_CTS)) { net_xchg_start(type, buffer, len); } else { rv = eos_msgq_push(&net_send_q, type, buffer, len); + if (rv) net_bufq_push(buffer); } set_csr(mstatus, MSTATUS_MIE); diff --git a/code/fe310/eos/net.h b/code/fe310/eos/net.h index 4005fe5..ace05f1 100644 --- a/code/fe310/eos/net.h +++ b/code/fe310/eos/net.h @@ -4,8 +4,6 @@ #define EOS_NET_FLAG_BUF_FREE 0x1 #define EOS_NET_FLAG_BUF_ACQ 0x2 -#define EOS_NET_MTYPE_FLAG_ONEW 0x10 - #define EOS_NET_MTYPE_SOCK 1 #define EOS_NET_MTYPE_AUDIO 2 @@ -23,7 +21,7 @@ void eos_net_start(void); void eos_net_stop(void); void eos_net_set_handler(unsigned char type, eos_evt_fptr_t handler, uint8_t flags); int eos_net_acquire(unsigned char reserved); -int eos_net_release(void); +void eos_net_release(void); unsigned char *eos_net_alloc(void); -int eos_net_free(unsigned char *buffer, unsigned char more); -int eos_net_send(unsigned char type, unsigned char *buffer, uint16_t len); +void eos_net_free(unsigned char *buffer, unsigned char more); +int eos_net_send(unsigned char type, unsigned char *buffer, uint16_t len, unsigned char more); diff --git a/code/fe310/eos/net_def.h b/code/fe310/eos/net_def.h index 87e3fbe..473714c 100644 --- a/code/fe310/eos/net_def.h +++ b/code/fe310/eos/net_def.h @@ -4,10 +4,12 @@ #define NET_PIN_RTS 20 #define NET_PIN_CTS 22 -#define NET_FLAG_RUN 0x01 -#define NET_FLAG_RST 0x02 -#define NET_FLAG_RTS 0x04 -#define NET_FLAG_CTS 0x08 -#define NET_FLAG_INIT 0x10 -#define NET_FLAG_ONEW 0x20 +#define NET_STATE_FLAG_RUN 0x01 +#define NET_STATE_FLAG_RST 0x02 +#define NET_STATE_FLAG_RTS 0x04 +#define NET_STATE_FLAG_CTS 0x08 +#define NET_STATE_FLAG_INIT 0x10 +#define NET_STATE_FLAG_ONEW 0x20 + +#define NET_MTYPE_FLAG_ONEW 0x10 -- cgit v1.2.3