From 528d6adda9997b45af62f598d298bf674357c066 Mon Sep 17 00:00:00 2001 From: Stefan Adolfsson Date: Sun, 6 May 2018 21:57:02 +0200 Subject: npcx: CEC: Improve pulse-width measurements Take into account the time from the interrupt is triggered until the timer is set when recharging the timer. Signed-off-by: Stefan Adolfsson BUG=b:76467407 BRANCH=none TEST=Using temprorary debug-prints. CQ-DEPEND=CL:1030227 Reviewed-on: https://chromium-review.googlesource.com/1030228 Reviewed-by: Randall Spangler Change-Id: Ia36bd73ff5efcff719db7b7915212f30a8e555f3 Reviewed-on: https://chromium-review.googlesource.com/1055827 Tested-by: Stefan Adolfsson Reviewed-by: Daisuke Nojiri Commit-Queue: Stefan Adolfsson --- chip/npcx/cec.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/chip/npcx/cec.c b/chip/npcx/cec.c index 231c4f367e..9b94b281d2 100644 --- a/chip/npcx/cec.c +++ b/chip/npcx/cec.c @@ -229,6 +229,12 @@ static struct cec_rx_cb cec_rx_cb; /* Parameters and buffer for initiator (sender) state */ static struct cec_tx cec_tx; +/* + * Time between interrupt triggered and the next timer was + * set when measuring pulse width + */ +static int cap_delay; + /* Value charged into the capture timer on last capture start */ static int cap_charge; @@ -273,7 +279,13 @@ static void tmr_cap_start(enum cap_edge edge, int timeout) * the edge change. */ if (timeout > 0) { - cap_charge = timeout; + /* + * Store the time it takes from the interrupts starts to when we + * actually get here. This part of the pulse-width needs to be + * taken into account + */ + cap_delay = (0xffff - NPCX_TCNT1(mdl)); + cap_charge = timeout - cap_delay; NPCX_TCNT1(mdl) = cap_charge; SET_BIT(NPCX_TIEN(mdl), NPCX_TIEN_TCIEN); } else { @@ -301,7 +313,7 @@ static int tmr_cap_get(void) { int mdl = NPCX_MFT_MODULE_1; - return (cap_charge - NPCX_TCRA(mdl)); + return (cap_charge + cap_delay - NPCX_TCRA(mdl)); } static void tmr_oneshot_start(int timeout) @@ -447,6 +459,7 @@ void enter_state(enum cec_state new_state) memset(&cec_tx, 0, sizeof(struct cec_tx)); memset(&cec_rx_cb, 0, sizeof(struct cec_rx_cb)); cap_charge = 0; + cap_delay = 0; cec_events = 0; break; case CEC_STATE_IDLE: -- cgit v1.2.1