diff options
author | Uros Majstorovic <majstor@majstor.org> | 2019-12-11 03:46:40 +0100 |
---|---|---|
committer | Uros Majstorovic <majstor@majstor.org> | 2019-12-11 03:46:40 +0100 |
commit | 404b1628d381cfff1bb737c5bc08b0fc5a3b74fe (patch) | |
tree | 7a88dd274534e60423f5868d3c7d93d63b954030 /code | |
parent | 602d0d803eb9607dcd949f3ea5ff27c14db4085b (diff) |
added eos_bufq to msgq; net changed accordingly
Diffstat (limited to 'code')
-rw-r--r-- | code/fe310/eos/cell.h | 4 | ||||
-rw-r--r-- | code/fe310/eos/msgq.c | 46 | ||||
-rw-r--r-- | code/fe310/eos/msgq.h | 13 | ||||
-rw-r--r-- | code/fe310/eos/net.c | 52 |
4 files changed, 62 insertions, 53 deletions
diff --git a/code/fe310/eos/cell.h b/code/fe310/eos/cell.h index badce3d..d6f6c35 100644 --- a/code/fe310/eos/cell.h +++ b/code/fe310/eos/cell.h @@ -7,8 +7,8 @@ #define EOS_CELL_MTYPE_DATA_START 2 #define EOS_CELL_MTYPE_DATA_STOP 3 -#define EOS_CELL_MTYPE_AUDIO_START 2 -#define EOS_CELL_MTYPE_AUDIO_STOP 3 +#define EOS_CELL_MTYPE_AUDIO_START 4 +#define EOS_CELL_MTYPE_AUDIO_STOP 5 #define EOS_CELL_MAX_MTYPE 2 diff --git a/code/fe310/eos/msgq.c b/code/fe310/eos/msgq.c index 35e0d15..2b20f31 100644 --- a/code/fe310/eos/msgq.c +++ b/code/fe310/eos/msgq.c @@ -5,24 +5,23 @@ #include "eos.h" #include "msgq.h" -#define EOS_MSGQ_IDX_MASK(IDX, SIZE) ((IDX) & ((SIZE) - 1)) - -#define EOS_MSGQ_IDX_HALF ((uint8_t)1 << (sizeof(uint8_t) * 8 - 1)) -#define EOS_MSGQ_IDX_LT(a,b) ((uint8_t)((uint8_t)(a) - (uint8_t)(b)) > EOS_MSGQ_IDX_HALF) -#define EOS_MSGQ_IDX_LTE(a,b) ((uint8_t)((uint8_t)(b) - (uint8_t)(a)) < EOS_MSGQ_IDX_HALF) +#define IDX_MASK(IDX, SIZE) ((IDX) & ((SIZE) - 1)) +#define IDX_HALF ((uint8_t)1 << (sizeof(uint8_t) * 8 - 1)) +#define IDX_LT(a,b) ((uint8_t)((uint8_t)(a) - (uint8_t)(b)) > IDX_HALF) +#define IDX_LTE(a,b) ((uint8_t)((uint8_t)(b) - (uint8_t)(a)) < IDX_HALF) void eos_msgq_init(EOSMsgQ *msgq, EOSMsgItem *array, uint8_t size) { msgq->idx_r = 0; msgq->idx_w = 0; - msgq->array = array; msgq->size = size; + msgq->array = array; } int eos_msgq_push(EOSMsgQ *msgq, unsigned char type, unsigned char *buffer, uint16_t len) { if ((uint8_t)(msgq->idx_w - msgq->idx_r) == msgq->size) return EOS_ERR_FULL; - uint8_t idx = EOS_MSGQ_IDX_MASK(msgq->idx_w, msgq->size); + uint8_t idx = IDX_MASK(msgq->idx_w, msgq->size); msgq->array[idx].type = type; msgq->array[idx].buffer = buffer; msgq->array[idx].len = len; @@ -34,8 +33,9 @@ void eos_msgq_pop(EOSMsgQ *msgq, unsigned char *type, unsigned char **buffer, ui if (msgq->idx_r == msgq->idx_w) { *type = 0; *buffer = NULL; + *len = 0; } else { - uint8_t idx = EOS_MSGQ_IDX_MASK(msgq->idx_r, msgq->size); + uint8_t idx = IDX_MASK(msgq->idx_r, msgq->size); *type = msgq->array[idx].type; *buffer = msgq->array[idx].buffer; *len = msgq->array[idx].len; @@ -52,7 +52,7 @@ int eos_msgq_get(EOSMsgQ *msgq, unsigned char type, unsigned char *selector, uin return 0; } - idx = EOS_MSGQ_IDX_MASK(msgq->idx_r, msgq->size); + idx = IDX_MASK(msgq->idx_r, msgq->size); if (type == msgq->array[idx].type) { *buffer = msgq->array[idx].buffer; *len = msgq->array[idx].len; @@ -61,14 +61,14 @@ int eos_msgq_get(EOSMsgQ *msgq, unsigned char type, unsigned char *selector, uin return 1; } } - for (i = msgq->idx_r + 1; EOS_MSGQ_IDX_LT(i, msgq->idx_w); i++) { - idx = EOS_MSGQ_IDX_MASK(i, msgq->size); + for (i = msgq->idx_r + 1; IDX_LT(i, msgq->idx_w); i++) { + idx = IDX_MASK(i, msgq->size); if (type== msgq->array[idx].type) { *buffer = msgq->array[idx].buffer; *len = msgq->array[idx].len; if ((selector == NULL) || (sel_len == 0) || ((sel_len <= *len) && (memcmp(selector, *buffer, sel_len) == 0))) { - for (j = i + 1; EOS_MSGQ_IDX_LT(j, msgq->idx_w); j++) { - msgq->array[EOS_MSGQ_IDX_MASK(j - 1, msgq->size)] = msgq->array[EOS_MSGQ_IDX_MASK(j, msgq->size)]; + for (j = i + 1; IDX_LT(j, msgq->idx_w); j++) { + msgq->array[IDX_MASK(j - 1, msgq->size)] = msgq->array[IDX_MASK(j, msgq->size)]; } msgq->idx_w--; return 1; @@ -80,3 +80,23 @@ int eos_msgq_get(EOSMsgQ *msgq, unsigned char type, unsigned char *selector, uin return 0; } +void eos_bufq_init(EOSBufQ *bufq, unsigned char **array, uint8_t size) { + bufq->idx_r = 0; + bufq->idx_w = 0; + bufq->size = size; + bufq->array = array; +} + +int eos_bufq_push(EOSBufQ *bufq, unsigned char *buffer) { + if ((uint8_t)(bufq->idx_w - bufq->idx_r) == bufq->size) return EOS_ERR_FULL; + + bufq->array[IDX_MASK(bufq->idx_w++, bufq->size)] = buffer; + return EOS_OK; +} + +unsigned char *eos_bufq_pop(EOSBufQ *bufq) { + if (bufq->idx_r == bufq->idx_w) return NULL; + + return bufq->array[IDX_MASK(bufq->idx_r++, bufq->size)]; +} + diff --git a/code/fe310/eos/msgq.h b/code/fe310/eos/msgq.h index aea4273..f8c05c4 100644 --- a/code/fe310/eos/msgq.h +++ b/code/fe310/eos/msgq.h @@ -16,4 +16,15 @@ typedef struct EOSMsgQ { void eos_msgq_init(EOSMsgQ *msgq, EOSMsgItem *array, uint8_t size); 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); -int eos_msgq_get(EOSMsgQ *msgq, unsigned char type, unsigned char *selector, uint16_t sel_len, unsigned char **buffer, uint16_t *len);
\ No newline at end of file +int eos_msgq_get(EOSMsgQ *msgq, unsigned char type, unsigned char *selector, uint16_t sel_len, unsigned char **buffer, uint16_t *len); + +typedef struct EOSBufQ { + uint8_t idx_r; + uint8_t idx_w; + uint8_t size; + unsigned char **array; +} EOSBufQ; + +void eos_bufq_init(EOSBufQ *bufq, unsigned char **array, uint8_t size); +int eos_bufq_push(EOSBufQ *bufq, unsigned char *buffer); +unsigned char *eos_bufq_pop(EOSBufQ *bufq); diff --git a/code/fe310/eos/net.c b/code/fe310/eos/net.c index ac2084d..2285ef7 100644 --- a/code/fe310/eos/net.c +++ b/code/fe310/eos/net.c @@ -18,20 +18,14 @@ #define MIN(X, Y) (((X) < (Y)) ? (X) : (Y)) #define MAX(X, Y) (((X) > (Y)) ? (X) : (Y)) -#define NET_BUFQ_IDX_MASK(IDX) ((IDX) & (EOS_NET_SIZE_BUFQ - 1)) -typedef struct EOSNetBufQ { - uint8_t idx_r; - uint8_t idx_w; - unsigned char *array[EOS_NET_SIZE_BUFQ]; -} EOSNetBufQ; +static EOSBufQ net_buf_q; +static unsigned char *net_bufq_array[EOS_NET_SIZE_BUFQ]; +static unsigned char net_bufq_buffer[EOS_NET_SIZE_BUFQ][EOS_NET_SIZE_BUF]; static EOSMsgQ net_send_q; static EOSMsgItem net_sndq_array[EOS_NET_SIZE_BUFQ]; -static EOSNetBufQ net_buf_q; -static unsigned char net_bufq_array[EOS_NET_SIZE_BUFQ][EOS_NET_SIZE_BUF]; - static uint8_t net_state_flags = 0; static unsigned char net_state_type = 0; static uint32_t net_state_len_tx = 0; @@ -49,26 +43,6 @@ extern uint32_t _eos_spi_state_idx_tx; extern uint32_t _eos_spi_state_idx_rx; extern unsigned char *_eos_spi_state_buf; -static void net_bufq_init(void) { - int i; - - net_buf_q.idx_r = 0; - net_buf_q.idx_w = EOS_NET_SIZE_BUFQ; - for (i=0; i<EOS_NET_SIZE_BUFQ; i++) { - net_buf_q.array[i] = net_bufq_array[i]; - } -} - -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 *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 net_xchg_reset(void) { net_state_flags &= ~NET_STATE_FLAG_CTS; net_state_flags |= (NET_STATE_FLAG_RST | NET_STATE_FLAG_XCHG); @@ -114,7 +88,7 @@ static int net_xchg_next(unsigned char *_buffer) { if (type) { net_xchg_start(type, buffer, len); } else if (net_state_flags & NET_STATE_FLAG_RTS) { - if (_buffer == NULL) _buffer = net_bufq_pop(); + if (_buffer == NULL) _buffer = eos_bufq_pop(&net_buf_q); if (_buffer) { net_xchg_start(0, _buffer, 0); return 0; @@ -127,12 +101,12 @@ void eos_net_xchg_done(void) { SPI1_REG(SPI_REG_CSMODE) = SPI_CSMODE_AUTO; if (net_state_type) { int r = eos_evtq_push_isr(EOS_EVT_NET | net_state_type, _eos_spi_state_buf, net_state_len_rx); - if (r) net_bufq_push(_eos_spi_state_buf); + if (r) eos_bufq_push(&net_buf_q, _eos_spi_state_buf); } 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_STATE_FLAG_ONEW; } else { - net_bufq_push(_eos_spi_state_buf); + eos_bufq_push(&net_buf_q, _eos_spi_state_buf); } net_state_flags &= ~NET_STATE_FLAG_XCHG; } @@ -221,8 +195,12 @@ static void net_handler_evt(unsigned char type, unsigned char *buffer, uint16_t void eos_net_init(void) { int i; - net_bufq_init(); eos_msgq_init(&net_send_q, net_sndq_array, EOS_NET_SIZE_BUFQ); + eos_bufq_init(&net_buf_q, net_bufq_array, EOS_NET_SIZE_BUFQ); + for (i=0; i<EOS_NET_SIZE_BUFQ; i++) { + eos_bufq_push(&net_buf_q, net_bufq_buffer[i]); + } + for (i=0; i<EOS_NET_MAX_MTYPE; i++) { evt_handler[i] = eos_evtq_bad_handler; } @@ -322,7 +300,7 @@ int _eos_net_acquire(unsigned char reserved) { } } else { clear_csr(mstatus, MSTATUS_MIE); - if (net_state_next_buf == NULL) net_state_next_buf = net_bufq_pop(); + if (net_state_next_buf == NULL) net_state_next_buf = eos_bufq_pop(&net_buf_q); ret = (net_state_next_buf != NULL); if (!ret) net_state_next_cnt++; set_csr(mstatus, MSTATUS_MIE); @@ -338,7 +316,7 @@ void eos_net_acquire(void) { void eos_net_release(void) { clear_csr(mstatus, MSTATUS_MIE); if (!net_state_next_cnt && net_state_next_buf) { - net_bufq_push(net_state_next_buf); + eos_bufq_push(&net_buf_q, net_state_next_buf); net_state_next_buf = NULL; } set_csr(mstatus, MSTATUS_MIE); @@ -363,7 +341,7 @@ void eos_net_free(unsigned char *buffer, unsigned char more) { net_state_next_buf = buffer; } else { 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); + if (do_release) eos_bufq_push(&net_buf_q, buffer); } set_csr(mstatus, MSTATUS_MIE); } @@ -379,7 +357,7 @@ int eos_net_send(unsigned char type, unsigned char *buffer, uint16_t len, unsign net_xchg_start(type, buffer, len); } else { rv = eos_msgq_push(&net_send_q, type, buffer, len); - if (rv) net_bufq_push(buffer); + if (rv) eos_bufq_push(&net_buf_q, buffer); } set_csr(mstatus, MSTATUS_MIE); |