summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--fw/fe310/eos/dev/net.c80
1 files changed, 36 insertions, 44 deletions
diff --git a/fw/fe310/eos/dev/net.c b/fw/fe310/eos/dev/net.c
index d287e5a..6114485 100644
--- a/fw/fe310/eos/dev/net.c
+++ b/fw/fe310/eos/dev/net.c
@@ -146,10 +146,10 @@ static int net_xchg_next(unsigned char *_buffer) {
}
static void net_handle_xchg(void) {
- volatile uint32_t r1, r2, r3;
- uint32_t len;
-
if (net_state_flags & NET_STATE_FLAG_INIT) {
+ volatile uint32_t r1, r2, r3;
+ uint32_t len;
+
net_state_flags &= ~NET_STATE_FLAG_INIT;
r1 = SPI1_REG(SPI_REG_RXFIFO);
@@ -167,13 +167,6 @@ static void net_handle_xchg(void) {
net_state_len_rx |= (r3 & 0xFF);
len = MAX(net_state_len_tx, net_state_len_rx);
- if (len > EOS_NET_MTU) {
- net_state_flags &= ~NET_STATE_FLAG_XCHG;
- SPI1_REG(SPI_REG_CSMODE) = SPI_CSMODE_AUTO;
- SPI1_REG(SPI_REG_IE) = 0x0;
- return;
- }
-
// esp32 dma workaraund
if (len < 8 - NET_SIZE_HDR) {
len = 8 - NET_SIZE_HDR;
@@ -181,6 +174,13 @@ static void net_handle_xchg(void) {
len = ((len + NET_SIZE_HDR)/4 + 1) * 4 - NET_SIZE_HDR;
}
+ if (len > EOS_NET_MTU) {
+ net_state_flags &= ~NET_STATE_FLAG_XCHG;
+ SPI1_REG(SPI_REG_CSMODE) = SPI_CSMODE_AUTO;
+ SPI1_REG(SPI_REG_IE) = 0x0;
+ return;
+ }
+
_eos_spi_xchg_init(net_state_buf, len, 0);
SPI1_REG(SPI_REG_TXCTRL) = SPI_TXWM(SPI_SIZE_WM);
SPI1_REG(SPI_REG_IE) = SPI_IP_TXWM;
@@ -191,7 +191,7 @@ static void net_handle_xchg(void) {
if (SPI1_REG(SPI_REG_CSMODE) == SPI_CSMODE_AUTO) { // exchange done
if (!(net_state_flags & NET_STATE_FLAG_SYNC)) {
if (net_state_type) {
- int r = eos_evtq_push_isr(EOS_EVT_NET | net_state_type, net_state_buf, net_state_len_rx);
+ int r = eos_evtq_push_isr(EOS_EVT_NET | (net_state_type & ~EOS_NET_MTYPE_FLAG_MASK), net_state_buf, net_state_len_rx);
if (r) eos_bufq_push(&net_buf_q, net_state_buf);
} else if (((net_state_flags & NET_STATE_FLAG_ONEW) || net_state_next_cnt) && (net_state_next_buf == NULL)) {
net_state_next_buf = net_state_buf;
@@ -292,8 +292,25 @@ static void evt_handler(unsigned char type, unsigned char *buffer, uint16_t len)
}
}
+static void net_wait4xchg(void) {
+ while (net_state_flags & NET_STATE_FLAG_XCHG) {
+ asm volatile ("wfi");
+ set_csr(mstatus, MSTATUS_MIE);
+ clear_csr(mstatus, MSTATUS_MIE);
+ }
+}
+
+static void net_wait4cts(void) {
+ while (!(net_state_flags & NET_STATE_FLAG_CTS)) {
+ asm volatile ("wfi");
+ set_csr(mstatus, MSTATUS_MIE);
+ clear_csr(mstatus, MSTATUS_MIE);
+ }
+}
+
static void net_pause(void) {
net_state_flags &= ~NET_STATE_FLAG_RUN;
+ net_wait4xchg();
}
static void net_resume(void) {
@@ -376,23 +393,10 @@ void eos_net_start(void) {
}
void eos_net_stop(void) {
- uint8_t done = 0;
-
clear_csr(mstatus, MSTATUS_MIE);
- if (net_state_flags & NET_STATE_FLAG_RUN) {
- net_state_flags &= ~NET_STATE_FLAG_RUN;
- done = !(net_state_flags & NET_STATE_FLAG_XCHG);
- } else {
- done = 1;
- }
+ net_pause();
set_csr(mstatus, MSTATUS_MIE);
- while (!done) {
- clear_csr(mstatus, MSTATUS_MIE);
- done = !(net_state_flags & NET_STATE_FLAG_XCHG);
- if (!done) asm volatile ("wfi");
- set_csr(mstatus, MSTATUS_MIE);
- }
net_stop();
}
@@ -477,7 +481,7 @@ void eos_net_release(void) {
unsigned char *eos_net_alloc(void) {
unsigned char *ret = NULL;
- while (!ret) {
+ while (ret == NULL) {
clear_csr(mstatus, MSTATUS_MIE);
if (net_state_next_buf) {
ret = net_state_next_buf;
@@ -534,31 +538,19 @@ static int net_xchg(unsigned char *type, unsigned char *buffer, uint16_t *len, u
if (_sync) {
net_pause();
- while (!(net_state_flags & NET_STATE_FLAG_CTS)) {
- asm volatile ("wfi");
- set_csr(mstatus, MSTATUS_MIE);
- clear_csr(mstatus, MSTATUS_MIE);
- }
+ net_wait4cts();
if (flags & EOS_NET_FLAG_SYNC) {
net_state_flags |= NET_STATE_FLAG_SYNC;
}
net_xchg_start(_type, buffer, _len);
if (flags & EOS_NET_FLAG_SYNC) {
if (flags & EOS_NET_FLAG_REPL) {
- while (!(net_state_flags & NET_STATE_FLAG_CTS)) {
- asm volatile ("wfi");
- set_csr(mstatus, MSTATUS_MIE);
- clear_csr(mstatus, MSTATUS_MIE);
- }
+ net_wait4cts();
net_xchg_start(0, buffer, 0);
}
- while (net_state_flags & NET_STATE_FLAG_XCHG) {
- asm volatile ("wfi");
- set_csr(mstatus, MSTATUS_MIE);
- clear_csr(mstatus, MSTATUS_MIE);
- }
+ net_wait4xchg();
net_state_flags &= ~NET_STATE_FLAG_SYNC;
- *type = net_state_type;
+ *type = (net_state_type & ~EOS_NET_MTYPE_FLAG_MASK);
*len = net_state_len_rx;
}
net_resume();
@@ -591,8 +583,8 @@ int eos_net_send_async(unsigned char type, unsigned char *buffer, uint16_t len,
int _eos_net_send(unsigned char type, unsigned char *buffer, uint16_t len, unsigned char async, unsigned char more) {
if (async) {
- eos_net_send_async(type, buffer, len, more);
+ return eos_net_send_async(type, buffer, len, more);
} else {
- eos_net_send(type, buffer, len);
+ return eos_net_send(type, buffer, len);
}
}