From accb73d8df905813ebf2d0cb60b279c6234796ee Mon Sep 17 00:00:00 2001 From: luanxueguang <1600577905@qq.com> Date: Sat, 21 Nov 2020 17:44:52 +0800 Subject: [PATCH] =?UTF-8?q?[update]=20=E8=8E=B7=E5=8F=96timer=20count?= =?UTF-8?q?=E6=97=B6=EF=BC=8C=E9=98=B2=E6=AD=A2=E5=9B=A0=E4=B8=AD=E6=96=AD?= =?UTF-8?q?=E5=88=B0=E6=9D=A5=E5=AF=BC=E8=87=B4overflow=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/drivers/hwtimer/hwtimer.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/components/drivers/hwtimer/hwtimer.c b/components/drivers/hwtimer/hwtimer.c index 3953919fae..976d8bbf32 100644 --- a/components/drivers/hwtimer/hwtimer.c +++ b/components/drivers/hwtimer/hwtimer.c @@ -10,6 +10,7 @@ #include #include +#include rt_inline rt_uint32_t timeout_calc(rt_hwtimer_t *timer, rt_hwtimerval_t *tv) { @@ -144,19 +145,25 @@ static rt_size_t rt_hwtimer_read(struct rt_device *dev, rt_off_t pos, void *buff rt_hwtimer_t *timer; rt_hwtimerval_t tv; rt_uint32_t cnt; + rt_base_t level; + rt_int32_t overflow; float t; timer = (rt_hwtimer_t *)dev; if (timer->ops->count_get == RT_NULL) return 0; + level = rt_hw_interrupt_disable(); cnt = timer->ops->count_get(timer); + overflow = timer->overflow; + rt_hw_interrupt_enable(level); + if (timer->info->cntmode == HWTIMER_CNTMODE_DW) { cnt = (timer->freq * timer->period_sec) - cnt; } - t = timer->overflow * timer->period_sec + cnt/(float)timer->freq; + t = overflow * timer->period_sec + cnt/(float)timer->freq; tv.sec = t; tv.usec = (t - tv.sec) * 1000000; size = size > sizeof(tv)? sizeof(tv) : size;