From ca2d9002fcbebcf37f48944dd8d67e90c186ce4f Mon Sep 17 00:00:00 2001 From: Uros Majstorovic Date: Sun, 13 Oct 2019 05:21:28 +0200 Subject: fixed msgq bug --- code/ecp/msgq.c | 2 +- code/esp32/components/eos/msgq.c | 4 ++-- code/fe310/eos/i2s.c | 4 ++-- code/fe310/eos/msgq.c | 17 ++++++++++++++--- code/fe310/eos/trap_entry.S | 2 ++ 5 files changed, 21 insertions(+), 8 deletions(-) (limited to 'code') diff --git a/code/ecp/msgq.c b/code/ecp/msgq.c index b928a2d..201af4a 100644 --- a/code/ecp/msgq.c +++ b/code/ecp/msgq.c @@ -76,7 +76,7 @@ int ecp_conn_msgq_push(ECPConnection *conn, ecp_seq_t seq, unsigned char mtype) if (msgq == NULL) return ECP_ERR; if (mtype >= ECP_MAX_MTYPE) return ECP_ERR_MAX_MTYPE; - if (msgq->idx_w[mtype] - msgq->idx_r[mtype] == ECP_MSGQ_MAX_MSG) return ECP_MSGQ_ERR_MAX_MSG; + if ((unsigned short)(msgq->idx_w[mtype] - msgq->idx_r[mtype]) == ECP_MSGQ_MAX_MSG) return ECP_MSGQ_ERR_MAX_MSG; if (msgq->idx_w[mtype] == msgq->idx_r[mtype]) pthread_cond_signal(&msgq->cond[mtype]); msgq->seq_msg[mtype][MSG_IDX_MASK(msgq->idx_w[mtype])] = seq; diff --git a/code/esp32/components/eos/msgq.c b/code/esp32/components/eos/msgq.c index ec89efe..0160bf5 100644 --- a/code/esp32/components/eos/msgq.c +++ b/code/esp32/components/eos/msgq.c @@ -4,7 +4,7 @@ #include "eos.h" #include "msgq.h" -#define EOS_MSGQ_IDX_MASK(IDX, SIZE) ((IDX) & ((SIZE) - 1)) +#define EOS_MSGQ_IDX_MASK(IDX, SIZE) ((IDX) & ((SIZE) - 1)) void eos_msgq_init(EOSMsgQ *msgq, EOSMsgItem *array, uint8_t size) { msgq->idx_r = 0; @@ -14,7 +14,7 @@ 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) { - if (msgq->idx_w - msgq->idx_r == msgq->size) return EOS_ERR_Q_FULL; + if ((uint8_t)(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; diff --git a/code/fe310/eos/i2s.c b/code/fe310/eos/i2s.c index 78bc2e6..cb25493 100644 --- a/code/fe310/eos/i2s.c +++ b/code/fe310/eos/i2s.c @@ -34,7 +34,7 @@ static void _abuf_init(EOSABuf *buf, uint8_t *array, uint16_t size) { } static int _abuf_push(EOSABuf *buf, uint8_t sample) { - if (buf->idx_w - buf->idx_r == buf->size) return EOS_ERR_Q_FULL; + if ((uint16_t)(buf->idx_w - buf->idx_r) == buf->size) return EOS_ERR_Q_FULL; buf->array[EOS_ABUF_IDX_MASK(buf->idx_w, buf->size)] = sample; buf->idx_w++; @@ -66,7 +66,7 @@ static uint16_t _abuf_write(EOSABuf *buf, uint8_t *sample, uint16_t ssize) { uint16_t i; for (i=0; iidx_w - buf->idx_r == buf->size) break; + if ((uint16_t)(buf->idx_w - buf->idx_r) == buf->size) break; buf->array[EOS_ABUF_IDX_MASK(buf->idx_w, buf->size)] = sample[i]; buf->idx_w++; } diff --git a/code/fe310/eos/msgq.c b/code/fe310/eos/msgq.c index 2c31662..867bf94 100644 --- a/code/fe310/eos/msgq.c +++ b/code/fe310/eos/msgq.c @@ -3,7 +3,12 @@ #include "eos.h" #include "msgq.h" -#define EOS_MSGQ_IDX_MASK(IDX, SIZE) ((IDX) & ((SIZE) - 1)) +#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) + void eos_msgq_init(EOSMsgQ *msgq, EOSMsgItem *array, uint8_t size) { msgq->idx_r = 0; @@ -13,7 +18,7 @@ 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) { - if (msgq->idx_w - msgq->idx_r == msgq->size) return EOS_ERR_Q_FULL; + if ((uint8_t)(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].type = type; @@ -37,6 +42,12 @@ void eos_msgq_pop(EOSMsgQ *msgq, unsigned char *type, unsigned char **buffer, ui } void eos_msgq_get(EOSMsgQ *msgq, unsigned char type, unsigned char **buffer, uint16_t *len) { + if (msgq->idx_r == msgq->idx_w) { + *buffer = NULL; + *len = 0; + return; + } + uint8_t idx = EOS_MSGQ_IDX_MASK(msgq->idx_r, msgq->size); unsigned char _type = msgq->array[idx].type; EOSMsgItem *tmp_item = &msgq->array[idx]; @@ -47,7 +58,7 @@ void eos_msgq_get(EOSMsgQ *msgq, unsigned char type, unsigned char **buffer, uin msgq->idx_r++; return; } - for (idx = msgq->idx_r + 1; idx < msgq->idx_w; idx++) { + for (idx = msgq->idx_r + 1; EOS_MSGQ_IDX_LT(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)].type = _type; msgq->array[EOS_MSGQ_IDX_MASK(idx, msgq->size)].buffer = *buffer; diff --git a/code/fe310/eos/trap_entry.S b/code/fe310/eos/trap_entry.S index 21cf2f8..432e84a 100644 --- a/code/fe310/eos/trap_entry.S +++ b/code/fe310/eos/trap_entry.S @@ -266,8 +266,10 @@ i2s_handler_sd_xchg: lhu x18, I2S_ABUF_OFF_IDXR(x9) lhu x19, I2S_ABUF_OFF_IDXW(x9) lhu x20, I2S_ABUF_OFF_SIZE(x9) + li x21, 0xffff sub x18, x19, x18 + and x18, x18, x21 beq x18, x20, 0f addi x19, x19, 1 -- cgit v1.2.3