diff options
author | Uros Majstorovic <majstor@majstor.org> | 2019-10-13 02:05:03 +0200 |
---|---|---|
committer | Uros Majstorovic <majstor@majstor.org> | 2019-10-13 02:05:03 +0200 |
commit | 8c6c68e92d6b4c8aa5e2fcc784ed52da40845acc (patch) | |
tree | 0251bea9be01701c488f9d8719b52eeb2a64855d | |
parent | 2a39fdf6d943f5738ba174368053a86304b0d8fa (diff) |
net refactor; rename cmd -> type
-rw-r--r-- | code/ecp/fe310/time.c | 7 | ||||
-rw-r--r-- | code/ecp/fe310/transport.c | 6 | ||||
-rw-r--r-- | code/fe310/eos/ecp.c | 7 | ||||
-rw-r--r-- | code/fe310/eos/event.c | 42 | ||||
-rw-r--r-- | code/fe310/eos/event.h | 8 | ||||
-rw-r--r-- | code/fe310/eos/i2s.c | 12 | ||||
-rw-r--r-- | code/fe310/eos/msgq.c | 22 | ||||
-rw-r--r-- | code/fe310/eos/msgq.h | 8 | ||||
-rw-r--r-- | code/fe310/eos/msgq_def.h | 2 | ||||
-rw-r--r-- | code/fe310/eos/net.c | 238 | ||||
-rw-r--r-- | code/fe310/eos/net.h | 19 | ||||
-rw-r--r-- | code/fe310/eos/net_def.h | 20 | ||||
-rw-r--r-- | code/fe310/eos/spi.h | 7 | ||||
-rw-r--r-- | code/fe310/eos/spi_def.h | 19 | ||||
-rw-r--r-- | code/fe310/eos/trap_entry.S | 2 |
15 files changed, 212 insertions, 207 deletions
diff --git a/code/ecp/fe310/time.c b/code/ecp/fe310/time.c index fedd23a..995b4f2 100644 --- a/code/ecp/fe310/time.c +++ b/code/ecp/fe310/time.c @@ -13,7 +13,12 @@ ecp_cts_t ecp_tm_abstime_ms(ecp_cts_t msec) { return now_ms + msec; } -void ecp_tm_sleep_ms(ecp_cts_t msec) {} +void ecp_tm_sleep_ms(ecp_cts_t msec) { + volatile uint64_t *mtime = (uint64_t *) (CLINT_CTRL_ADDR + CLINT_MTIME); + + uint64_t now_ms = *mtime * 1000 / RTC_FREQ; + while (*mtime * 1000 / RTC_FREQ < now_ms + msec); +} void ecp_tm_timer_set(ecp_cts_t next) { uint32_t tick = next * (uint64_t)RTC_FREQ / 1000; diff --git a/code/ecp/fe310/transport.c b/code/ecp/fe310/transport.c index f728792..61d3355 100644 --- a/code/ecp/fe310/transport.c +++ b/code/ecp/fe310/transport.c @@ -28,12 +28,12 @@ ssize_t ecp_tr_send(int *sock, ECPBuffer *packet, size_t msg_size, ECPNetAddr *a unsigned char *buf = NULL; size_t addr_len = sizeof(addr->host) + sizeof(addr->port); uint16_t buf_size = msg_size + addr_len; - unsigned char cmd = EOS_NET_CMD_PKT; + unsigned char type = EOS_NET_MTYPE_SOCK; int rv; flags |= _flags; if (flags & ECP_SEND_FLAG_MORE) { - cmd |= EOS_NET_CMD_FLAG_ONEW; + type |= EOS_NET_MTYPE_FLAG_ONEW; } if (flags & ECP_SEND_FLAG_REPLY) { if (packet && packet->buffer) { @@ -47,7 +47,7 @@ ssize_t ecp_tr_send(int *sock, ECPBuffer *packet, size_t msg_size, ECPNetAddr *a if (buf == NULL) return ECP_ERR; memcpy(buf, addr->host, sizeof(addr->host)); memcpy(buf+sizeof(addr->host), &addr->port, sizeof(addr->port)); - rv = eos_net_send(cmd, buf, buf_size); + rv = eos_net_send(type, buf, buf_size); if (rv) return ECP_ERR_SEND; return msg_size; } diff --git a/code/fe310/eos/ecp.c b/code/fe310/eos/ecp.c index 9297ccd..44d9a47 100644 --- a/code/fe310/eos/ecp.c +++ b/code/fe310/eos/ecp.c @@ -13,7 +13,7 @@ static ECPSocket *_sock = NULL; -static void timer_handler(unsigned char cmd, unsigned char *buffer, uint16_t len) { +static void timer_handler(unsigned char type, unsigned char *buffer, uint16_t len) { ecp_cts_t next = ecp_timer_exe(_sock); if (next) { uint32_t tick = next * (uint64_t)RTC_FREQ / 1000; @@ -21,7 +21,7 @@ static void timer_handler(unsigned char cmd, unsigned char *buffer, uint16_t len } } -static void packet_handler(unsigned char cmd, unsigned char *buffer, uint16_t len) { +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); @@ -59,8 +59,9 @@ int ecp_init(ECPContext *ctx) { rv = ecp_ctx_create_vconn(ctx); if (rv) return rv; + /* XXX */ eos_evtq_set_handler(EOS_EVT_TIMER, timer_handler, EOS_EVT_FLAG_NET_BUF_ACQ); - eos_net_set_handler(EOS_NET_CMD_PKT, packet_handler, 0); + // eos_net_set_handler(EOS_NET_DATA_PKT, packet_handler, 0); return ECP_OK; } diff --git a/code/fe310/eos/event.c b/code/fe310/eos/event.c index 2aa4ecf..66b1f4a 100644 --- a/code/fe310/eos/event.c +++ b/code/fe310/eos/event.c @@ -25,69 +25,69 @@ void eos_evtq_init(void) { 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) { +int eos_evtq_push(unsigned char type, unsigned char *buffer, uint16_t len) { clear_csr(mstatus, MSTATUS_MIE); - int ret = eos_msgq_push(&_eos_event_q, cmd, buffer, len); + int ret = eos_msgq_push(&_eos_event_q, type, buffer, len); set_csr(mstatus, MSTATUS_MIE); return ret; } -void eos_evtq_pop(unsigned char *cmd, unsigned char **buffer, uint16_t *len) { +void eos_evtq_pop(unsigned char *type, unsigned char **buffer, uint16_t *len) { clear_csr(mstatus, MSTATUS_MIE); - eos_msgq_pop(&_eos_event_q, cmd, buffer, len); + eos_msgq_pop(&_eos_event_q, type, buffer, len); set_csr(mstatus, MSTATUS_MIE); } -void eos_evtq_bad_handler(unsigned char cmd, unsigned char *buffer, uint16_t len) { +void eos_evtq_bad_handler(unsigned char type, unsigned char *buffer, uint16_t len) { write(1, "error\n", 6); } -static void evtq_handler_wrapper(unsigned char cmd, unsigned char *buffer, uint16_t len, uint16_t *flags_acq, uint16_t flag, eos_evt_fptr_t f) { +static void evtq_handler_wrapper(unsigned char type, 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); + f(type, buffer, len); eos_net_release(); *flags_acq &= ~flag; } else { *flags_acq |= flag; - eos_evtq_push(cmd, buffer, len); + eos_evtq_push(type, buffer, len); } } -static void evtq_handler(unsigned char cmd, unsigned char *buffer, uint16_t len) { - if (((cmd & EOS_EVT_MASK) >> 4) > EOS_EVT_MAX_EVT) { - eos_evtq_bad_handler(cmd, buffer, len); +static void evtq_handler(unsigned char type, unsigned char *buffer, uint16_t len) { + if (((type & EOS_EVT_MASK) >> 4) > EOS_EVT_MAX_EVT) { + eos_evtq_bad_handler(type, buffer, len); } else { - unsigned char idx = ((cmd & EOS_EVT_MASK) >> 4) - 1; + unsigned char idx = ((type & EOS_EVT_MASK) >> 4) - 1; uint16_t flag = (uint16_t)1 << idx; if (flag & evt_handler_flags_buf_acq) { - evtq_handler_wrapper(cmd, buffer, len, &evt_handler_wrapper_acq, flag, evt_handler[idx]); + evtq_handler_wrapper(type, buffer, len, &evt_handler_wrapper_acq, flag, evt_handler[idx]); } else { - evt_handler[idx](cmd, buffer, len); + evt_handler[idx](type, buffer, len); } } } -void eos_evtq_set_handler(unsigned char cmd, eos_evt_fptr_t handler, uint8_t flags) { +void eos_evtq_set_handler(unsigned char type, eos_evt_fptr_t handler, uint8_t flags) { if (flags) { - uint16_t flag = (uint16_t)1 << (((cmd & EOS_EVT_MASK) >> 4) - 1); + uint16_t flag = (uint16_t)1 << (((type & EOS_EVT_MASK) >> 4) - 1); if (flags & EOS_EVT_FLAG_NET_BUF_ACQ) evt_handler_flags_buf_acq |= flag; } - evt_handler[((cmd & EOS_EVT_MASK) >> 4) - 1] = handler; + evt_handler[((type & EOS_EVT_MASK) >> 4) - 1] = handler; } void eos_evtq_loop(void) { - unsigned char cmd; + unsigned char type; unsigned char *buffer; uint16_t len; int foo = 1; while(foo) { clear_csr(mstatus, MSTATUS_MIE); - eos_msgq_pop(&_eos_event_q, &cmd, &buffer, &len); - if (cmd) { + eos_msgq_pop(&_eos_event_q, &type, &buffer, &len); + if (type) { set_csr(mstatus, MSTATUS_MIE); - evtq_handler(cmd, buffer, len); + evtq_handler(type, buffer, len); clear_csr(mstatus, MSTATUS_MIE); } else { // asm volatile ("wfi"); diff --git a/code/fe310/eos/event.h b/code/fe310/eos/event.h index 8215d2b..653d5c4 100644 --- a/code/fe310/eos/event.h +++ b/code/fe310/eos/event.h @@ -5,8 +5,8 @@ typedef void (*eos_evt_fptr_t) (unsigned char, unsigned char *, uint16_t); void eos_evtq_init(void); -int eos_evtq_push(unsigned char cmd, unsigned char *buffer, uint16_t len); -void eos_evtq_pop(unsigned char *cmd, unsigned char **buffer, uint16_t *len); -void eos_evtq_bad_handler(unsigned char cmd, unsigned char *buffer, uint16_t len); -void eos_evtq_set_handler(unsigned char cmd, eos_evt_fptr_t handler, uint8_t flags); +int eos_evtq_push(unsigned char type, unsigned char *buffer, uint16_t len); +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, uint8_t flags); void eos_evtq_loop(void); diff --git a/code/fe310/eos/i2s.c b/code/fe310/eos/i2s.c index 2ff465e..78bc2e6 100644 --- a/code/fe310/eos/i2s.c +++ b/code/fe310/eos/i2s.c @@ -77,14 +77,14 @@ static uint16_t _abuf_len(EOSABuf *buf) { return buf->idx_w - buf->idx_r; } -static void audio_handler(unsigned char cmd, unsigned char *buffer, uint16_t len) { - cmd = cmd & ~EOS_EVT_MASK; +static void audio_handler(unsigned char type, unsigned char *buffer, uint16_t len) { + type = type & ~EOS_EVT_MASK; - if (cmd < I2S_MAX_HANDLER) { - evt_handler[cmd](cmd, buffer, len); - _eos_i2s_evt_enable[cmd] = 1; + if (type < I2S_MAX_HANDLER) { + evt_handler[type](type, buffer, len); + _eos_i2s_evt_enable[type] = 1; } else { - eos_evtq_bad_handler(cmd, buffer, len); + eos_evtq_bad_handler(type, buffer, len); } } diff --git a/code/fe310/eos/msgq.c b/code/fe310/eos/msgq.c index d242c69..2c31662 100644 --- a/code/fe310/eos/msgq.c +++ b/code/fe310/eos/msgq.c @@ -12,50 +12,50 @@ void eos_msgq_init(EOSMsgQ *msgq, EOSMsgItem *array, uint8_t size) { msgq->size = size; } -int eos_msgq_push(EOSMsgQ *msgq, unsigned char cmd, unsigned char *buffer, uint16_t len) { +int eos_msgq_push(EOSMsgQ *msgq, unsigned char type, unsigned char *buffer, uint16_t len) { if (msgq->idx_w - msgq->idx_r == msgq->size) return EOS_ERR_Q_FULL; uint8_t idx = EOS_MSGQ_IDX_MASK(msgq->idx_w, msgq->size); - msgq->array[idx].cmd = cmd; + msgq->array[idx].type = type; msgq->array[idx].buffer = buffer; msgq->array[idx].len = len; msgq->idx_w++; return EOS_OK; } -void eos_msgq_pop(EOSMsgQ *msgq, unsigned char *cmd, unsigned char **buffer, uint16_t *len) { +void eos_msgq_pop(EOSMsgQ *msgq, unsigned char *type, unsigned char **buffer, uint16_t *len) { if (msgq->idx_r == msgq->idx_w) { - *cmd = 0; + *type = 0; *buffer = NULL; } else { uint8_t idx = EOS_MSGQ_IDX_MASK(msgq->idx_r, msgq->size); - *cmd = msgq->array[idx].cmd; + *type = msgq->array[idx].type; *buffer = msgq->array[idx].buffer; *len = msgq->array[idx].len; msgq->idx_r++; } } -void eos_msgq_get(EOSMsgQ *msgq, unsigned char cmd, unsigned char **buffer, uint16_t *len) { +void eos_msgq_get(EOSMsgQ *msgq, unsigned char type, unsigned char **buffer, uint16_t *len) { uint8_t idx = EOS_MSGQ_IDX_MASK(msgq->idx_r, msgq->size); - unsigned char _cmd = msgq->array[idx].cmd; + unsigned char _type = msgq->array[idx].type; EOSMsgItem *tmp_item = &msgq->array[idx]; *buffer = msgq->array[idx].buffer; *len = msgq->array[idx].len; - if (_cmd == cmd) { + if (_type == type) { msgq->idx_r++; return; } for (idx = msgq->idx_r + 1; idx < msgq->idx_w; idx++) { *tmp_item = msgq->array[EOS_MSGQ_IDX_MASK(idx, msgq->size)]; - msgq->array[EOS_MSGQ_IDX_MASK(idx, msgq->size)].cmd = _cmd; + msgq->array[EOS_MSGQ_IDX_MASK(idx, msgq->size)].type = _type; msgq->array[EOS_MSGQ_IDX_MASK(idx, msgq->size)].buffer = *buffer; msgq->array[EOS_MSGQ_IDX_MASK(idx, msgq->size)].len = *len; - _cmd = tmp_item->cmd; + _type = tmp_item->type; *buffer = tmp_item->buffer; *len = tmp_item->len; - if (_cmd == cmd) { + if (_type == type) { msgq->idx_r++; return; } diff --git a/code/fe310/eos/msgq.h b/code/fe310/eos/msgq.h index 05a5d80..5a61a79 100644 --- a/code/fe310/eos/msgq.h +++ b/code/fe310/eos/msgq.h @@ -1,7 +1,7 @@ #include <stdint.h> typedef struct EOSMsgItem { - unsigned char cmd; + unsigned char type; unsigned char *buffer; uint16_t len; } EOSMsgItem; @@ -14,6 +14,6 @@ typedef struct EOSMsgQ { } EOSMsgQ; void eos_msgq_init(EOSMsgQ *msgq, EOSMsgItem *array, uint8_t size); -int eos_msgq_push(EOSMsgQ *msgq, unsigned char cmd, unsigned char *buffer, uint16_t len); -void eos_msgq_pop(EOSMsgQ *msgq, unsigned char *cmd, unsigned char **buffer, uint16_t *len); -void eos_msgq_get(EOSMsgQ *msgq, unsigned char cmd, unsigned char **buffer, uint16_t *len);
\ No newline at end of file +int eos_msgq_push(EOSMsgQ *msgq, unsigned char type, unsigned char *buffer, uint16_t len); +void eos_msgq_pop(EOSMsgQ *msgq, unsigned char *type, unsigned char **buffer, uint16_t *len); +void eos_msgq_get(EOSMsgQ *msgq, unsigned char type, unsigned char **buffer, uint16_t *len);
\ No newline at end of file diff --git a/code/fe310/eos/msgq_def.h b/code/fe310/eos/msgq_def.h index 733fc39..53d71f2 100644 --- a/code/fe310/eos/msgq_def.h +++ b/code/fe310/eos/msgq_def.h @@ -5,6 +5,6 @@ #define MSGQ_OFF_ARRAY 4 #define MSGQ_ITEM_SIZE 12 -#define MSGQ_ITEM_OFF_CMD 0 +#define MSGQ_ITEM_OFF_TYPE 0 #define MSGQ_ITEM_OFF_BUF 4 #define MSGQ_ITEM_OFF_SIZE 8 diff --git a/code/fe310/eos/net.c b/code/fe310/eos/net.c index 32b293a..0cbe9f6 100644 --- a/code/fe310/eos/net.c +++ b/code/fe310/eos/net.c @@ -11,49 +11,57 @@ #include "msgq.h" #include "interrupt.h" -#include "net.h" #include "spi.h" +#include "net.h" +#include "net_def.h" #define MIN(X, Y) (((X) < (Y)) ? (X) : (Y)) #define MAX(X, Y) (((X) > (Y)) ? (X) : (Y)) -#define SPI_BUFQ_IDX_MASK(IDX) ((IDX) & (SPI_SIZE_BUFQ - 1)) +#define NET_BUFQ_IDX_MASK(IDX) ((IDX) & (NET_SIZE_BUFQ - 1)) -EOSMsgQ _eos_spi_send_q; -static EOSMsgItem spi_sndq_array[SPI_SIZE_BUFQ]; +typedef struct EOSNetBufQ { + uint8_t idx_r; + uint8_t idx_w; + unsigned char *array[NET_SIZE_BUFQ]; +} EOSNetBufQ; -SPIBufQ _eos_spi_buf_q; -static unsigned char spi_bufq_array[SPI_SIZE_BUFQ][SPI_SIZE_BUF]; +static EOSMsgQ net_send_q; +static EOSMsgItem net_sndq_array[NET_SIZE_BUFQ]; + +static EOSNetBufQ net_buf_q; +static unsigned char net_bufq_array[NET_SIZE_BUFQ][NET_SIZE_BUF]; extern EOSMsgQ _eos_event_q; -uint8_t _eos_spi_state_flags = 0; uint32_t _eos_spi_state_len = 0; uint32_t _eos_spi_state_len_tx = 0; uint32_t _eos_spi_state_len_rx = 0; uint32_t _eos_spi_state_idx_tx = 0; uint32_t _eos_spi_state_idx_rx = 0; -unsigned char _eos_spi_state_cmd = 0; unsigned char *_eos_spi_state_buf = NULL; -uint8_t _eos_spi_state_next_cnt = 0; -unsigned char *_eos_spi_state_next_buf = NULL; -static eos_evt_fptr_t evt_handler[EOS_NET_MAX_CMD]; +static uint8_t net_state_flags = 0; +static unsigned char net_state_type = 0; +static uint8_t net_state_next_cnt = 0; +static unsigned char *net_state_next_buf = NULL; + +static eos_evt_fptr_t evt_handler[EOS_NET_MAX_MTYPE]; static uint16_t evt_handler_flags_buf_free = 0; static uint16_t evt_handler_flags_buf_acq = 0; -static int spi_bufq_push(unsigned char *buffer) { - _eos_spi_buf_q.array[SPI_BUFQ_IDX_MASK(_eos_spi_buf_q.idx_w++)] = buffer; +static int net_bufq_push(unsigned char *buffer) { + net_buf_q.array[NET_BUFQ_IDX_MASK(net_buf_q.idx_w++)] = buffer; return EOS_OK; } -static unsigned char *spi_bufq_pop(void) { - if (_eos_spi_buf_q.idx_r == _eos_spi_buf_q.idx_w) return NULL; - return _eos_spi_buf_q.array[SPI_BUFQ_IDX_MASK(_eos_spi_buf_q.idx_r++)]; +static unsigned char *net_bufq_pop(void) { + if (net_buf_q.idx_r == net_buf_q.idx_w) return NULL; + return net_buf_q.array[NET_BUFQ_IDX_MASK(net_buf_q.idx_r++)]; } -static void spi_xchg_reset(void) { - _eos_spi_state_flags &= ~SPI_FLAG_CTS; - _eos_spi_state_flags |= SPI_FLAG_RST; +static void net_xchg_reset(void) { + net_state_flags &= ~NET_FLAG_CTS; + net_state_flags |= NET_FLAG_RST; // before starting a transaction, set SPI peripheral to desired mode SPI1_REG(SPI_REG_CSMODE) = SPI_CSMODE_HOLD; @@ -64,14 +72,14 @@ static void spi_xchg_reset(void) { SPI1_REG(SPI_REG_IE) = SPI_IP_RXWM; } -static void spi_xchg_start(unsigned char cmd, unsigned char *buffer, uint16_t len) { - _eos_spi_state_flags &= ~SPI_FLAG_CTS; - _eos_spi_state_flags |= SPI_FLAG_INIT; +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; - if (_eos_spi_state_next_cnt && (_eos_spi_state_next_buf == NULL)) cmd |= EOS_NET_CMD_FLAG_ONEW; - if (cmd & EOS_NET_CMD_FLAG_ONEW) _eos_spi_state_flags |= SPI_FLAG_ONEW; + 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; - _eos_spi_state_cmd = cmd; + net_state_type = type; _eos_spi_state_buf = buffer; _eos_spi_state_len_tx = len; _eos_spi_state_len_rx = 0; @@ -81,57 +89,57 @@ static void spi_xchg_start(unsigned char cmd, unsigned char *buffer, uint16_t le // before starting a transaction, set SPI peripheral to desired mode SPI1_REG(SPI_REG_CSMODE) = SPI_CSMODE_HOLD; - SPI1_REG(SPI_REG_TXFIFO) = ((cmd << 3) | (len >> 8)) & 0xFF; + SPI1_REG(SPI_REG_TXFIFO) = ((type << 3) | (len >> 8)) & 0xFF; SPI1_REG(SPI_REG_TXFIFO) = len & 0xFF; SPI1_REG(SPI_REG_RXCTRL) = SPI_RXWM(1); SPI1_REG(SPI_REG_IE) = SPI_IP_RXWM; } -static int spi_xchg_next(unsigned char *_buffer) { - unsigned char cmd; +static int net_xchg_next(unsigned char *_buffer) { + unsigned char type; unsigned char *buffer = NULL; uint16_t len; - eos_msgq_pop(&_eos_spi_send_q, &cmd, &buffer, &len); - if (cmd) { - spi_xchg_start(cmd, buffer, len); - } else if (_eos_spi_state_flags & SPI_FLAG_RTS) { - if (_buffer == NULL) _buffer = spi_bufq_pop(); + 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) { + if (_buffer == NULL) _buffer = net_bufq_pop(); if (_buffer) { - spi_xchg_start(0, _buffer, 0); + net_xchg_start(0, _buffer, 0); return 0; } } return 1; } -static void spi_handler_xchg(void) { +static void net_handler_xchg(void) { volatile uint32_t r1, r2; int i; - if (_eos_spi_state_flags & SPI_FLAG_RST) { - _eos_spi_state_flags &= ~SPI_FLAG_RST; + if (net_state_flags & NET_FLAG_RST) { + net_state_flags &= ~NET_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 (_eos_spi_state_flags & SPI_FLAG_INIT) { - _eos_spi_state_flags &= ~SPI_FLAG_INIT; + } else if (net_state_flags & NET_FLAG_INIT) { + net_state_flags &= ~NET_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 (_eos_spi_state_cmd & EOS_NET_CMD_FLAG_ONEW) { + if (net_state_type & EOS_NET_MTYPE_FLAG_ONEW) { r1 = 0; r2 = 0; } - _eos_spi_state_cmd = ((r1 & 0xFF) >> 3); + net_state_type = ((r1 & 0xFF) >> 3); _eos_spi_state_len_rx = ((r1 & 0x07) << 8); _eos_spi_state_len_rx |= (r2 & 0xFF); _eos_spi_state_len = MAX(_eos_spi_state_len_tx, _eos_spi_state_len_rx); @@ -143,7 +151,7 @@ static void spi_handler_xchg(void) { _eos_spi_state_len = ((_eos_spi_state_len + 2)/4 + 1) * 4 - 2; } - if (_eos_spi_state_len > SPI_SIZE_BUF) { + if (_eos_spi_state_len > NET_SIZE_BUF) { SPI1_REG(SPI_REG_CSMODE) = SPI_CSMODE_AUTO; SPI1_REG(SPI_REG_IE) = 0x0; } @@ -169,14 +177,14 @@ static void spi_handler_xchg(void) { if (_eos_spi_state_idx_rx == _eos_spi_state_len) { SPI1_REG(SPI_REG_CSMODE) = SPI_CSMODE_AUTO; SPI1_REG(SPI_REG_IE) = 0x0; - if (_eos_spi_state_cmd) { - int r = eos_msgq_push(&_eos_event_q, EOS_EVT_NET | _eos_spi_state_cmd, _eos_spi_state_buf, _eos_spi_state_len_rx); - if (r) spi_bufq_push(_eos_spi_state_buf); - } else if (((_eos_spi_state_flags & SPI_FLAG_ONEW) || _eos_spi_state_next_cnt) && (_eos_spi_state_next_buf == NULL)) { - _eos_spi_state_next_buf = _eos_spi_state_buf; - _eos_spi_state_flags &= ~SPI_FLAG_ONEW; + if (net_state_type) { + int r = eos_msgq_push(&_eos_event_q, EOS_EVT_NET | net_state_type, _eos_spi_state_buf, _eos_spi_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)) { + net_state_next_buf = _eos_spi_state_buf; + net_state_flags &= ~NET_FLAG_ONEW; } else { - spi_bufq_push(_eos_spi_state_buf); + net_bufq_push(_eos_spi_state_buf); } } else if (_eos_spi_state_idx_tx == _eos_spi_state_len) { SPI1_REG(SPI_REG_RXCTRL) = SPI_RXWM(MIN(_eos_spi_state_len - _eos_spi_state_idx_rx - 1, SPI_SIZE_WM - 1)); @@ -184,35 +192,35 @@ static void spi_handler_xchg(void) { } } -static void spi_handler_cts(void) { - GPIO_REG(GPIO_RISE_IP) = (0x1 << SPI_PIN_CTS); - _eos_spi_state_flags |= SPI_FLAG_CTS; +static void net_handler_cts(void) { + GPIO_REG(GPIO_RISE_IP) = (0x1 << NET_PIN_CTS); + net_state_flags |= NET_FLAG_CTS; - if (_eos_spi_state_flags & SPI_FLAG_RDY) { - spi_xchg_next(NULL); + if (net_state_flags & NET_FLAG_RDY) { + net_xchg_next(NULL); } else { - uint32_t iof_mask = ((uint32_t)1 << SPI_PIN_CS); + uint32_t iof_mask = ((uint32_t)1 << NET_PIN_CS); GPIO_REG(GPIO_IOF_EN) &= ~iof_mask; } } -static void spi_handler_rts(void) { - uint32_t rts_offset = (0x1 << SPI_PIN_RTS); +static void net_handler_rts(void) { + uint32_t rts_offset = (0x1 << NET_PIN_RTS); if (GPIO_REG(GPIO_RISE_IP) & rts_offset) { GPIO_REG(GPIO_RISE_IP) = rts_offset; - _eos_spi_state_flags |= SPI_FLAG_RTS; - if ((_eos_spi_state_flags & SPI_FLAG_RDY) && (_eos_spi_state_flags & SPI_FLAG_CTS)) spi_xchg_reset(); + net_state_flags |= NET_FLAG_RTS; + if ((net_state_flags & NET_FLAG_RDY) && (net_state_flags & NET_FLAG_CTS)) net_xchg_reset(); } else if (GPIO_REG(GPIO_FALL_IP) & rts_offset) { GPIO_REG(GPIO_FALL_IP) = rts_offset; - _eos_spi_state_flags &= ~SPI_FLAG_RTS; + net_state_flags &= ~NET_FLAG_RTS; } } -static void net_handler(unsigned char cmd, unsigned char *buffer, uint16_t len) { - if ((cmd & ~EOS_EVT_MASK) > EOS_NET_MAX_CMD) { - eos_evtq_bad_handler(cmd, buffer, len); +static void net_handler_evt(unsigned char type, unsigned char *buffer, uint16_t len) { + if ((type & ~EOS_EVT_MASK) > EOS_NET_MAX_MTYPE) { + eos_evtq_bad_handler(type, buffer, len); } else { - unsigned char idx = (cmd & ~EOS_EVT_MASK) - 1; + unsigned char idx = (type & ~EOS_EVT_MASK) - 1; uint16_t buf_free = ((uint16_t)1 << idx) & evt_handler_flags_buf_free; uint16_t buf_acq = ((uint16_t)1 << idx) & evt_handler_flags_buf_acq; @@ -222,56 +230,56 @@ static void net_handler(unsigned char cmd, unsigned char *buffer, uint16_t len) len = 0; } - evt_handler[idx](cmd, buffer, len); + evt_handler[idx](type, buffer, len); if (buf_free && buf_acq) eos_net_release(); } } -void eos_net_set_handler(unsigned char cmd, eos_evt_fptr_t handler, uint8_t flags) { +void eos_net_set_handler(unsigned char type, eos_evt_fptr_t handler, uint8_t flags) { if (flags) { - uint16_t flag = (uint16_t)1 << ((cmd & ~EOS_EVT_MASK) - 1); + uint16_t flag = (uint16_t)1 << ((type & ~EOS_EVT_MASK) - 1); if (flags & EOS_NET_FLAG_BUF_FREE) evt_handler_flags_buf_free |= flag; if (flags & EOS_NET_FLAG_BUF_ACQ) evt_handler_flags_buf_acq |= flag; } - evt_handler[(cmd & ~EOS_EVT_MASK) - 1] = handler; + evt_handler[(type & ~EOS_EVT_MASK) - 1] = handler; } void eos_net_init(void) { int i; - _eos_spi_buf_q.idx_r = 0; - _eos_spi_buf_q.idx_w = 0; - for (i=0; i<SPI_SIZE_BUFQ; i++) { - spi_bufq_push(spi_bufq_array[i]); + net_buf_q.idx_r = 0; + net_buf_q.idx_w = 0; + for (i=0; i<NET_SIZE_BUFQ; i++) { + net_bufq_push(net_bufq_array[i]); } - eos_msgq_init(&_eos_spi_send_q, spi_sndq_array, SPI_SIZE_BUFQ); + eos_msgq_init(&net_send_q, net_sndq_array, NET_SIZE_BUFQ); GPIO_REG(GPIO_IOF_SEL) &= ~SPI_IOF_MASK; GPIO_REG(GPIO_IOF_EN) |= SPI_IOF_MASK; - eos_intr_set(INT_SPI1_BASE, 5, spi_handler_xchg); + eos_intr_set(INT_SPI1_BASE, 5, net_handler_xchg); - GPIO_REG(GPIO_OUTPUT_EN) &= ~(0x1 << SPI_PIN_CTS); - GPIO_REG(GPIO_PULLUP_EN) |= (0x1 << SPI_PIN_CTS); - GPIO_REG(GPIO_INPUT_EN) |= (0x1 << SPI_PIN_CTS); - GPIO_REG(GPIO_RISE_IE) |= (0x1 << SPI_PIN_CTS); - eos_intr_set(INT_GPIO_BASE + SPI_PIN_CTS, 4, spi_handler_cts); + GPIO_REG(GPIO_OUTPUT_EN) &= ~(0x1 << NET_PIN_CTS); + GPIO_REG(GPIO_PULLUP_EN) |= (0x1 << NET_PIN_CTS); + GPIO_REG(GPIO_INPUT_EN) |= (0x1 << NET_PIN_CTS); + GPIO_REG(GPIO_RISE_IE) |= (0x1 << NET_PIN_CTS); + eos_intr_set(INT_GPIO_BASE + NET_PIN_CTS, 4, net_handler_cts); - GPIO_REG(GPIO_OUTPUT_EN) &= ~(0x1 << SPI_PIN_RTS); - GPIO_REG(GPIO_PULLUP_EN) |= (0x1 << SPI_PIN_RTS); - GPIO_REG(GPIO_INPUT_EN) |= (0x1 << SPI_PIN_RTS); - GPIO_REG(GPIO_RISE_IE) |= (0x1 << SPI_PIN_RTS); - GPIO_REG(GPIO_FALL_IE) |= (0x1 << SPI_PIN_RTS); - eos_intr_set(INT_GPIO_BASE + SPI_PIN_RTS, 4, spi_handler_rts); - - for (i=0; i<EOS_NET_MAX_CMD; i++) { + GPIO_REG(GPIO_OUTPUT_EN) &= ~(0x1 << NET_PIN_RTS); + GPIO_REG(GPIO_PULLUP_EN) |= (0x1 << NET_PIN_RTS); + GPIO_REG(GPIO_INPUT_EN) |= (0x1 << NET_PIN_RTS); + GPIO_REG(GPIO_RISE_IE) |= (0x1 << NET_PIN_RTS); + GPIO_REG(GPIO_FALL_IE) |= (0x1 << NET_PIN_RTS); + eos_intr_set(INT_GPIO_BASE + NET_PIN_RTS, 4, net_handler_rts); + + for (i=0; i<EOS_NET_MAX_MTYPE; i++) { evt_handler[i] = eos_evtq_bad_handler; } - eos_evtq_set_handler(EOS_EVT_NET, net_handler, 0); + eos_evtq_set_handler(EOS_EVT_NET, net_handler_evt, 0); } void eos_net_start(uint32_t sckdiv) { - uint32_t iof_mask = ((uint32_t)1 << SPI_PIN_CS); + uint32_t iof_mask = ((uint32_t)1 << NET_PIN_CS); GPIO_REG(GPIO_IOF_SEL) &= ~iof_mask; GPIO_REG(GPIO_IOF_EN) |= iof_mask; @@ -284,14 +292,14 @@ void eos_net_start(uint32_t sckdiv) { SPI_FMT_LEN(8); // enable CS pin for selected channel/pin - SPI1_REG(SPI_REG_CSID) = SPI_IDX_SS; + SPI1_REG(SPI_REG_CSID) = NET_IDX_SS; // There is no way here to change the CS polarity. // SPI1_REG(SPI_REG_CSDEF) = 0xFFFF; clear_csr(mstatus, MSTATUS_MIE); - _eos_spi_state_flags |= SPI_FLAG_RDY; - if (_eos_spi_state_flags & SPI_FLAG_CTS) spi_xchg_next(NULL); + net_state_flags |= NET_FLAG_RDY; + if (net_state_flags & NET_FLAG_CTS) net_xchg_next(NULL); set_csr(mstatus, MSTATUS_MIE); } @@ -299,9 +307,9 @@ void eos_net_stop(void) { volatile uint8_t done = 0; clear_csr(mstatus, MSTATUS_MIE); - _eos_spi_state_flags &= ~SPI_FLAG_RDY; - if (_eos_spi_state_flags & SPI_FLAG_CTS) { - uint32_t iof_mask = ((uint32_t)1 << SPI_PIN_CS); + net_state_flags &= ~NET_FLAG_RDY; + if (net_state_flags & NET_FLAG_CTS) { + uint32_t iof_mask = ((uint32_t)1 << NET_PIN_CS); GPIO_REG(GPIO_IOF_EN) &= ~iof_mask; done = 1; } @@ -309,7 +317,7 @@ void eos_net_stop(void) { while (!done) { clear_csr(mstatus, MSTATUS_MIE); - done = _eos_spi_state_flags & SPI_FLAG_CTS; + done = net_state_flags & NET_FLAG_CTS; if (!done) asm volatile ("wfi"); set_csr(mstatus, MSTATUS_MIE); } @@ -321,9 +329,9 @@ int eos_net_acquire(unsigned char reserved) { if (reserved) { while (!ret) { clear_csr(mstatus, MSTATUS_MIE); - if (_eos_spi_state_next_buf) { + if (net_state_next_buf) { ret = 1; - _eos_spi_state_next_cnt--; + net_state_next_cnt--; } else { asm volatile ("wfi"); } @@ -331,9 +339,9 @@ int eos_net_acquire(unsigned char reserved) { } } else { clear_csr(mstatus, MSTATUS_MIE); - if (_eos_spi_state_next_buf == NULL) _eos_spi_state_next_buf = spi_bufq_pop(); - ret = (_eos_spi_state_next_buf != NULL); - if (!ret) _eos_spi_state_next_cnt++; + if (net_state_next_buf == NULL) net_state_next_buf = net_bufq_pop(); + ret = (net_state_next_buf != NULL); + if (!ret) net_state_next_cnt++; set_csr(mstatus, MSTATUS_MIE); } return ret; @@ -343,9 +351,9 @@ int eos_net_release(void) { int rv = EOS_OK; clear_csr(mstatus, MSTATUS_MIE); - if (!_eos_spi_state_next_cnt && _eos_spi_state_next_buf) { - rv = spi_bufq_push(_eos_spi_state_next_buf); - if (!rv) _eos_spi_state_next_buf = NULL; + if (!net_state_next_cnt && net_state_next_buf) { + rv = net_bufq_push(net_state_next_buf); + if (!rv) net_state_next_buf = NULL; } set_csr(mstatus, MSTATUS_MIE); @@ -357,9 +365,9 @@ unsigned char *eos_net_alloc(void) { while (ret == NULL) { clear_csr(mstatus, MSTATUS_MIE); - if (_eos_spi_state_next_buf) { - ret = _eos_spi_state_next_buf; - _eos_spi_state_next_buf = NULL; + if (net_state_next_buf) { + ret = net_state_next_buf; + net_state_next_buf = NULL; } else { asm volatile ("wfi"); } @@ -374,25 +382,25 @@ int eos_net_free(unsigned char *buffer, unsigned char more) { uint8_t do_release = 1; clear_csr(mstatus, MSTATUS_MIE); - if ((more || _eos_spi_state_next_cnt) && (_eos_spi_state_next_buf == NULL)) { - _eos_spi_state_next_buf = buffer; + if ((more || net_state_next_cnt) && (net_state_next_buf == NULL)) { + net_state_next_buf = buffer; } else { - if ((_eos_spi_state_flags & SPI_FLAG_RDY) && (_eos_spi_state_flags & SPI_FLAG_CTS)) do_release = spi_xchg_next(buffer); - if (do_release) rv = spi_bufq_push(buffer); + if ((net_state_flags & NET_FLAG_RDY) && (net_state_flags & NET_FLAG_CTS)) do_release = net_xchg_next(buffer); + if (do_release) rv = net_bufq_push(buffer); } set_csr(mstatus, MSTATUS_MIE); return rv; } -int eos_net_send(unsigned char cmd, unsigned char *buffer, uint16_t len) { +int eos_net_send(unsigned char type, unsigned char *buffer, uint16_t len) { int rv = EOS_OK; clear_csr(mstatus, MSTATUS_MIE); - if ((_eos_spi_state_flags & SPI_FLAG_RDY) && (_eos_spi_state_flags & SPI_FLAG_CTS)) { - spi_xchg_start(cmd, buffer, len); + if ((net_state_flags & NET_FLAG_RDY) && (net_state_flags & NET_FLAG_CTS)) { + net_xchg_start(type, buffer, len); } else { - rv = eos_msgq_push(&_eos_spi_send_q, EOS_EVT_NET | cmd, buffer, len); + rv = eos_msgq_push(&net_send_q, EOS_EVT_NET | type, buffer, len); } set_csr(mstatus, MSTATUS_MIE); diff --git a/code/fe310/eos/net.h b/code/fe310/eos/net.h index d09cd96..d50ee94 100644 --- a/code/fe310/eos/net.h +++ b/code/fe310/eos/net.h @@ -1,14 +1,27 @@ #include <stdint.h> #include "event.h" -#include "net_def.h" +#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 + +#define EOS_NET_MTYPE_WIFI 5 +#define EOS_NET_MTYPE_CELL 6 +#define EOS_NET_MTYPE_SIP 7 +#define EOS_NET_MTYPE_APP 8 + +#define EOS_NET_MAX_MTYPE 8 void eos_net_init(void); void eos_net_start(uint32_t sckdiv); void eos_net_stop(void); -void eos_net_set_handler(unsigned char cmd, eos_evt_fptr_t handler, uint8_t flags); +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); unsigned char *eos_net_alloc(void); int eos_net_free(unsigned char *buffer, unsigned char more); -int eos_net_send(unsigned char cmd, unsigned char *buffer, uint16_t len); +int eos_net_send(unsigned char type, unsigned char *buffer, uint16_t len); diff --git a/code/fe310/eos/net_def.h b/code/fe310/eos/net_def.h index a7766f2..2df2833 100644 --- a/code/fe310/eos/net_def.h +++ b/code/fe310/eos/net_def.h @@ -1,11 +1,15 @@ -#define EOS_NET_FLAG_BUF_FREE 0x1 -#define EOS_NET_FLAG_BUF_ACQ 0x2 +#define NET_SIZE_BUF 1500 +#define NET_SIZE_BUFQ 2 -#define EOS_NET_CMD_FLAG_ONEW 0x10 +#define NET_PIN_RTS 20 +#define NET_PIN_CTS 22 +#define NET_PIN_CS 10 +#define NET_IDX_SS 3 -#define EOS_NET_CMD_CONNECT 1 -#define EOS_NET_CMD_DISCONNECT 2 -#define EOS_NET_CMD_SCAN 3 -#define EOS_NET_CMD_PKT 4 +#define NET_FLAG_RDY 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 EOS_NET_MAX_CMD 8 diff --git a/code/fe310/eos/spi.h b/code/fe310/eos/spi.h index 7cad270..9ef3745 100644 --- a/code/fe310/eos/spi.h +++ b/code/fe310/eos/spi.h @@ -6,10 +6,3 @@ #include "spi_def.h" #define SPI_IOF_MASK (((uint32_t)1 << IOF_SPI1_SCK) | ((uint32_t)1 << IOF_SPI1_MOSI) | ((uint32_t)1 << IOF_SPI1_MISO)) - -typedef struct SPIBufQ { - uint8_t idx_r; - uint8_t idx_w; - unsigned char *array[SPI_SIZE_BUFQ]; -} SPIBufQ; - diff --git a/code/fe310/eos/spi_def.h b/code/fe310/eos/spi_def.h index 44d3030..0c90b6d 100644 --- a/code/fe310/eos/spi_def.h +++ b/code/fe310/eos/spi_def.h @@ -1,6 +1,3 @@ -#define SPI_SIZE_BUF 1500 -#define SPI_SIZE_BUFQ 2 - #define SPI_MODE0 0x00 #define SPI_MODE1 0x01 #define SPI_MODE2 0x02 @@ -10,19 +7,3 @@ #define SPI_SIZE_CHUNK 4 #define SPI_SIZE_WM 2 -#define SPI_PIN_RTS 20 -#define SPI_PIN_CTS 22 -#define SPI_PIN_CS 10 -#define SPI_IDX_SS 3 - -#define SPI_FLAG_RDY 0x01 -#define SPI_FLAG_RST 0x02 -#define SPI_FLAG_RTS 0x04 -#define SPI_FLAG_CTS 0x08 -#define SPI_FLAG_INIT 0x10 -#define SPI_FLAG_ONEW 0x20 - -/* asm */ -#define SPI_BUFQ_OFF_IDXR 0 -#define SPI_BUFQ_OFF_IDXW 1 -#define SPI_BUFQ_OFF_ARRAY 4 diff --git a/code/fe310/eos/trap_entry.S b/code/fe310/eos/trap_entry.S index 26e93cb..21cf2f8 100644 --- a/code/fe310/eos/trap_entry.S +++ b/code/fe310/eos/trap_entry.S @@ -297,7 +297,7 @@ i2s_handler_sd_xchg: jal x20, evtq_push beqz x8, i2s_handler_sd_exit li x18, (EOS_EVT_AUDIO | I2S_EVT_MIC) - sb x18, MSGQ_ITEM_OFF_CMD(x8) + sb x18, MSGQ_ITEM_OFF_TYPE(x8) i2s_handler_sd_exit: # complete |