From ab1006d27417cc831150877ec46fbc13ffeddb53 Mon Sep 17 00:00:00 2001 From: Uros Majstorovic Date: Wed, 11 Mar 2020 16:28:27 +0100 Subject: timer fix --- code/ecp/Makefile.fe310 | 2 +- code/ecp/fe310/time.c | 15 +++++---------- code/ecp/timer.c | 26 +++++++++++++------------- code/fe310/eos/timer.c | 49 ++++++++++++++++++++++++++++++------------------- code/fe310/eos/timer.h | 11 ++++++++--- 5 files changed, 57 insertions(+), 46 deletions(-) diff --git a/code/ecp/Makefile.fe310 b/code/ecp/Makefile.fe310 index f921f33..5b670c1 100644 --- a/code/ecp/Makefile.fe310 +++ b/code/ecp/Makefile.fe310 @@ -5,4 +5,4 @@ vconn=vconn rbuf_obj= include $(FE310_HOME)/common.mk -CFLAGS += -DECP_WITH_VCONN=1 -DECP_DEBUG=1 -I$(FE310_HOME) -I$(FE310_HOME)/include +CFLAGS += -DECP_WITH_VCONN=1 -DECP_DEBUG=1 -I$(FE310_HOME) diff --git a/code/ecp/fe310/time.c b/code/ecp/fe310/time.c index 8458a2d..3c74933 100644 --- a/code/ecp/fe310/time.c +++ b/code/ecp/fe310/time.c @@ -6,15 +6,12 @@ #include #include -#include "encoding.h" -#include "platform.h" - extern ECPSocket *_ecp_tr_sock; static void timer_handler(unsigned char type) { ecp_cts_t next = ecp_timer_exe(_ecp_tr_sock); if (next) { - eos_timer_set(next, EOS_TIMER_ETYPE_ECP, 0); + eos_timer_set(next, EOS_TIMER_ETYPE_ECP); } } @@ -25,16 +22,14 @@ int ecp_tm_init(ECPContext *ctx) { } ecp_cts_t ecp_tm_abstime_ms(ecp_cts_t msec) { - volatile uint64_t *mtime = (uint64_t *) (CLINT_CTRL_ADDR + CLINT_MTIME); - - uint64_t now_ms = *mtime * 1000 / RTC_FREQ; - return now_ms + msec; + return eos_time_get_tick() * 1000 / EOS_TIMER_RTC_FREQ + msec; } void ecp_tm_sleep_ms(ecp_cts_t msec) { - eos_timer_sleep(msec); + eos_time_sleep(msec); } void ecp_tm_timer_set(ecp_cts_t next) { - eos_timer_set(next, EOS_TIMER_ETYPE_ECP, 1); + uint32_t _next = eos_timer_get(EOS_TIMER_ETYPE_ECP); + if ((_next == EOS_TIMER_NONE) || (next < _next)) eos_timer_set(next, EOS_TIMER_ETYPE_ECP); } diff --git a/code/ecp/timer.c b/code/ecp/timer.c index 695d42a..f091464 100644 --- a/code/ecp/timer.c +++ b/code/ecp/timer.c @@ -9,7 +9,7 @@ int ecp_timer_create(ECPTimer *timer) { rv = pthread_mutex_init(&timer->mutex, NULL); if (rv) return ECP_ERR; #endif - + return ECP_OK; } @@ -21,17 +21,17 @@ void ecp_timer_destroy(ECPTimer *timer) { int ecp_timer_item_init(ECPTimerItem *ti, ECPConnection *conn, unsigned char mtype, short cnt, ecp_cts_t timeout) { if ((mtype & ECP_MTYPE_MASK) >= ECP_MAX_MTYPE) return ECP_ERR_MAX_MTYPE; - + if (ti == NULL) return ECP_ERR; if (conn == NULL) return ECP_ERR; - + ti->conn = conn; ti->mtype = mtype; ti->cnt = cnt-1; ti->timeout = timeout; ti->abstime = 0; ti->retry = NULL; - + return ECP_OK; } @@ -39,7 +39,7 @@ int ecp_timer_push(ECPTimerItem *ti) { int i, is_reg, rv = ECP_OK; ECPConnection *conn = ti->conn; ECPTimer *timer = &conn->sock->timer; - + ti->abstime = ecp_tm_abstime_ms(ti->timeout); #ifdef ECP_WITH_PTHREAD @@ -51,12 +51,11 @@ int ecp_timer_push(ECPTimerItem *ti) { #ifdef ECP_WITH_PTHREAD pthread_mutex_unlock(&conn->mutex); #endif - + if (timer->head == ECP_MAX_TIMER-1) rv = ECP_ERR_MAX_TIMER; if (!rv && !is_reg) rv = ECP_ERR_CLOSED; if (!rv) { - ecp_tm_timer_set(ti->timeout); for (i=timer->head; i>=0; i--) { if (ECP_CTS_LTE(ti->abstime, timer->item[i].abstime)) { if (i != timer->head) memmove(timer->item+i+2, timer->item+i+1, sizeof(ECPTimerItem) * (timer->head-i)); @@ -70,8 +69,9 @@ int ecp_timer_push(ECPTimerItem *ti) { timer->item[0] = *ti; timer->head++; } + ecp_tm_timer_set(ti->timeout); } - + #ifdef ECP_WITH_PTHREAD pthread_mutex_unlock(&timer->mutex); #endif @@ -98,11 +98,11 @@ void ecp_timer_pop(ECPConnection *conn, unsigned char mtype) { } #ifdef ECP_WITH_PTHREAD pthread_mutex_lock(&conn->mutex); -#endif +#endif conn->refcount--; #ifdef ECP_WITH_PTHREAD pthread_mutex_unlock(&conn->mutex); -#endif +#endif timer->head--; break; } @@ -111,7 +111,7 @@ void ecp_timer_pop(ECPConnection *conn, unsigned char mtype) { #ifdef ECP_WITH_PTHREAD pthread_mutex_unlock(&timer->mutex); #endif - + } void ecp_timer_remove(ECPConnection *conn) { @@ -145,7 +145,7 @@ void ecp_timer_remove(ECPConnection *conn) { #ifdef ECP_WITH_PTHREAD pthread_mutex_unlock(&timer->mutex); #endif - + } ecp_cts_t ecp_timer_exe(ECPSocket *sock) { @@ -185,7 +185,7 @@ ecp_cts_t ecp_timer_exe(ECPSocket *sock) { unsigned char mtype = to_exec[i].mtype; ecp_timer_retry_t *retry = to_exec[i].retry; ecp_conn_handler_msg_t *handler = conn->sock->ctx->handler[conn->type] ? conn->sock->ctx->handler[conn->type]->msg[mtype & ECP_MTYPE_MASK] : NULL; - + if (to_exec[i].cnt > 0) { ssize_t _rv = 0; to_exec[i].cnt--; diff --git a/code/fe310/eos/timer.c b/code/fe310/eos/timer.c index 4eb80b4..e07b9a6 100644 --- a/code/fe310/eos/timer.c +++ b/code/fe310/eos/timer.c @@ -62,37 +62,42 @@ void eos_timer_init(void) { void eos_timer_set_handler(unsigned char evt, eos_timer_handler_t handler) { uint64_t *mtimecmp = (uint64_t *) (CLINT_CTRL_ADDR + CLINT_MTIMECMP); - clear_csr(mie, MIP_MTIP); + if (!evt && (*mtimecmp != 0)) clear_csr(mie, MIP_MTIP); timer_handler[evt] = handler; - if (*mtimecmp != 0) set_csr(mie, MIP_MTIP); + if (!evt && (*mtimecmp != 0)) set_csr(mie, MIP_MTIP); } -uint64_t eos_timer_get(unsigned char evt) { +uint32_t eos_timer_get(unsigned char evt) { + volatile uint64_t *mtime = (uint64_t *) (CLINT_CTRL_ADDR + CLINT_MTIME); uint64_t *mtimecmp = (uint64_t *) (CLINT_CTRL_ADDR + CLINT_MTIMECMP); - uint64_t ret = 0; + uint64_t now; + uint32_t ret; - clear_csr(mie, MIP_MTIP); - ret = timer_next[evt]; + if (*mtimecmp != 0) clear_csr(mie, MIP_MTIP); + now = *mtime; + if (timer_next[evt]) { + ret = (timer_next[evt] > now) ? (timer_next[evt] - now) * 1000 / EOS_TIMER_RTC_FREQ : 0; + } else { + ret = EOS_TIMER_NONE; + } if (*mtimecmp != 0) set_csr(mie, MIP_MTIP); return ret; } -void eos_timer_set(uint32_t msec, unsigned char evt, unsigned char b) { +void eos_timer_set(uint32_t msec, unsigned char evt) { int i; volatile uint64_t *mtime = (uint64_t *) (CLINT_CTRL_ADDR + CLINT_MTIME); uint64_t *mtimecmp = (uint64_t *) (CLINT_CTRL_ADDR + CLINT_MTIMECMP); - uint64_t tick = *mtime + msec * (uint64_t)RTC_FREQ / 1000; + uint64_t tick = *mtime + msec * (uint64_t)EOS_TIMER_RTC_FREQ / 1000; uint64_t next = 0; - clear_csr(mie, MIP_MTIP); - if (!b || (timer_next[evt] == 0) || (tick < timer_next[evt])) { - timer_next[evt] = tick; - for (i = 0; i <= EOS_TIMER_MAX_ETYPE; i++) { - next = next && timer_next[i] ? MIN(next, timer_next[i]) : (next ? next : timer_next[i]); - } - *mtimecmp = next; + if (*mtimecmp != 0) clear_csr(mie, MIP_MTIP); + timer_next[evt] = tick; + for (i = 0; i <= EOS_TIMER_MAX_ETYPE; i++) { + next = next && timer_next[i] ? MIN(next, timer_next[i]) : (next ? next : timer_next[i]); } + *mtimecmp = next; if (*mtimecmp != 0) set_csr(mie, MIP_MTIP); } @@ -101,7 +106,7 @@ void eos_timer_clear(unsigned char evt) { uint64_t *mtimecmp = (uint64_t *) (CLINT_CTRL_ADDR + CLINT_MTIMECMP); uint64_t next = 0; - clear_csr(mie, MIP_MTIP); + if (*mtimecmp != 0) clear_csr(mie, MIP_MTIP); if (timer_next[evt]) { timer_next[evt] = 0; for (i = 0; i <= EOS_TIMER_MAX_ETYPE; i++) { @@ -112,9 +117,15 @@ void eos_timer_clear(unsigned char evt) { if (*mtimecmp != 0) set_csr(mie, MIP_MTIP); } -void eos_timer_sleep(uint32_t msec) { + +void eos_time_sleep(uint32_t msec) { volatile uint64_t *mtime = (uint64_t *) (CLINT_CTRL_ADDR + CLINT_MTIME); + uint64_t now_ms = *mtime * 1000 / EOS_TIMER_RTC_FREQ; - uint64_t now_ms = *mtime * 1000 / RTC_FREQ; - while (*mtime * 1000 / RTC_FREQ < now_ms + msec); + while (*mtime * 1000 / EOS_TIMER_RTC_FREQ < now_ms + msec); +} + +uint64_t eos_time_get_tick(void) { + volatile uint64_t *mtime = (uint64_t *) (CLINT_CTRL_ADDR + CLINT_MTIME); + return *mtime; } diff --git a/code/fe310/eos/timer.h b/code/fe310/eos/timer.h index edf422d..2ac53f7 100644 --- a/code/fe310/eos/timer.h +++ b/code/fe310/eos/timer.h @@ -6,12 +6,17 @@ #define EOS_TIMER_MAX_ETYPE 4 +#define EOS_TIMER_NONE 0xffffffff +#define EOS_TIMER_RTC_FREQ 32768 + typedef void (*eos_timer_handler_t) (unsigned char); void eos_timer_init(void); void eos_timer_set_handler(unsigned char evt, eos_timer_handler_t handler); -uint64_t eos_timer_get(unsigned char evt); -void eos_timer_set(uint32_t msec, unsigned char evt, unsigned char b); +uint32_t eos_timer_get(unsigned char evt); +void eos_timer_set(uint32_t msec, unsigned char evt); void eos_timer_clear(unsigned char evt); -void eos_timer_sleep(uint32_t msec); + +void eos_time_sleep(uint32_t msec); +uint64_t eos_time_get_tick(void); -- cgit v1.2.3