From 8c6c68e92d6b4c8aa5e2fcc784ed52da40845acc Mon Sep 17 00:00:00 2001 From: Uros Majstorovic Date: Sun, 13 Oct 2019 02:05:03 +0200 Subject: net refactor; rename cmd -> type --- code/fe310/eos/net.c | 238 ++++++++++++++++++++++++++------------------------- 1 file changed, 123 insertions(+), 115 deletions(-) (limited to 'code/fe310/eos/net.c') 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