From ca1a94d74c3d50847912e3744b7b2a9613a7c35d Mon Sep 17 00:00:00 2001 From: Martin Stein Date: Thu, 6 Jul 2023 21:01:39 +0200 Subject: [PATCH] base-hw imx epit-timer: improve code readability Improves the readability of the implementation of the Timer::_duration method. Ref #4959 --- repos/base-hw/src/core/spec/arm/imx_epit.cc | 23 +++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/repos/base-hw/src/core/spec/arm/imx_epit.cc b/repos/base-hw/src/core/spec/arm/imx_epit.cc index 2c174eda8d..70a3c193f8 100644 --- a/repos/base-hw/src/core/spec/arm/imx_epit.cc +++ b/repos/base-hw/src/core/spec/arm/imx_epit.cc @@ -94,8 +94,23 @@ time_t Timer::_max_value() const { time_t Timer::_duration() const { using Device = Board::Timer; - Device::Cnt::access_t const last = (Device::Cnt::access_t) _last_timeout_duration; - Device::Cnt::access_t const cnt = _device.read(); - return (_device.read()) ? _max_value() - _device.read() + last - : last - cnt; + Device::Cnt::access_t const initial_cnt { + (Device::Cnt::access_t)_last_timeout_duration }; + + /* + * There are two situations here: + * + * 1. SR.OCIF reads as 1, meaning, the timer IRQ has triggered. In + * this case we read CNT after having read SR.OCIF in order to + * ensure that we use a post-IRQ (wrapped) counter value. + * + * 2. SR.OCIF reads as 0, meaning, the timer IRQ hasn't triggered yet. In + * this case we read CNT before having read SR.OCIF in order to + * ensure that we use a pre-IRQ (non-wrapped) counter value. + */ + Device::Cnt::access_t const curr_cnt { _device.read() }; + if (_device.read()) + return _max_value() - _device.read() + initial_cnt; + else + return initial_cnt - curr_cnt; }