From patchwork Tue Dec 27 15:36:02 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahiro Yamada X-Patchwork-Id: 89107 Delivered-To: patch@linaro.org Received: by 10.140.20.101 with SMTP id 92csp5039785qgi; Tue, 27 Dec 2016 07:36:51 -0800 (PST) X-Received: by 10.194.69.230 with SMTP id h6mr33469876wju.63.1482853011863; Tue, 27 Dec 2016 07:36:51 -0800 (PST) Return-Path: Received: from theia.denx.de (theia.denx.de. [85.214.87.163]) by mx.google.com with ESMTP id p21si46576261wmb.29.2016.12.27.07.36.51; Tue, 27 Dec 2016 07:36:51 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of u-boot-bounces@lists.denx.de designates 85.214.87.163 as permitted sender) client-ip=85.214.87.163; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@nifty.com; spf=pass (google.com: best guess record for domain of u-boot-bounces@lists.denx.de designates 85.214.87.163 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id D41EAB38BB; Tue, 27 Dec 2016 16:36:40 +0100 (CET) Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id XfJ7BM647SGm; Tue, 27 Dec 2016 16:36:40 +0100 (CET) Received: from theia.denx.de (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id E6954B3912; Tue, 27 Dec 2016 16:36:38 +0100 (CET) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id CCC05B3905 for ; Tue, 27 Dec 2016 16:36:35 +0100 (CET) Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 7uUm4mN37GJ7 for ; Tue, 27 Dec 2016 16:36:35 +0100 (CET) X-policyd-weight: NOT_IN_SBL_XBL_SPAMHAUS=-1.5 NOT_IN_SPAMCOP=-1.5 NOT_IN_BL_NJABL=-1.5 (only DNSBL check requested) Received: from conuserg-12.nifty.com (conuserg-12.nifty.com [210.131.2.79]) by theia.denx.de (Postfix) with ESMTPS id D487DB38BF for ; Tue, 27 Dec 2016 16:36:29 +0100 (CET) Received: from grover.sesame (FL1-111-169-71-157.osk.mesh.ad.jp [111.169.71.157]) (authenticated) by conuserg-12.nifty.com with ESMTP id uBRFa5RK030807; Wed, 28 Dec 2016 00:36:13 +0900 DKIM-Filter: OpenDKIM Filter v2.10.3 conuserg-12.nifty.com uBRFa5RK030807 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nifty.com; s=dec2015msa; t=1482852973; bh=fFqttLmHjCr/72QCjQlkzd3KaBH9V/pnuQvFCL5zh6E=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=EsjKjcq/9HP+QJLatGJOBXwPc76cr8DbZNmLvyu0jnP30754Of/cc6rzrhoRiblMv PXQBz4NtpNOJW7Iclyh442xmzXu4XzBylvGM9kTumVtjnfs2v7rkUN/fbzSeG3tFD/ 1AoYyJ6cXgneaB7pVSjYa4UHFrZ8jIQcINlaWtbR0j44OHxfhdlnDDAqB/NKNxHs18 J7YtMfBO7a2Se3qcgGzosFL5Xzigbi3Wp9YZT3FUc6FFi2PUoQzdEb/lTqQURrop1+ /jOFOvJf6Q8/Jqd6OM02AxOOSV5+f9sag/DOWlM+xp49ySvopQamHy5MSUqwQmyEBh rBc4y+/KHq2jA== X-Nifty-SrcIP: [111.169.71.157] From: Masahiro Yamada To: u-boot@lists.denx.de Date: Wed, 28 Dec 2016 00:36:02 +0900 Message-Id: <1482852963-6887-5-git-send-email-yamada.masahiro@socionext.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1482852963-6887-1-git-send-email-yamada.masahiro@socionext.com> References: <1482852963-6887-1-git-send-email-yamada.masahiro@socionext.com> Subject: [U-Boot] [PATCH v2 4/5] time: import time_after, time_before and friends from Linux X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.15 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" It is not safe to compare timer values directly. On 32-bit systems, for example, timer_get_us() wraps around every 72 min. (2 ^ 32 / 1000000 =~ 4295 sec =~ 72 min). Depending on the get_ticks() implementation, it may wrap more frequently. The 72 min might be possible on the use of U-Boot. Let's borrow time_after, time_before, and friends to solve the wrap-around problem. These macros were copied from include/linux/jiffies.h of Linux 4.9. Signed-off-by: Masahiro Yamada --- Changes in v2: None include/time.h | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) -- 2.7.4 _______________________________________________ U-Boot mailing list U-Boot@lists.denx.de http://lists.denx.de/mailman/listinfo/u-boot Reviewed-by: Simon Glass diff --git a/include/time.h b/include/time.h index 5ed021f..5746ad9 100644 --- a/include/time.h +++ b/include/time.h @@ -5,6 +5,8 @@ #ifndef _TIME_H #define _TIME_H +#include + unsigned long get_timer(unsigned long base); /* @@ -13,4 +15,43 @@ unsigned long get_timer(unsigned long base); */ unsigned long timer_get_us(void); +/* + * These inlines deal with timer wrapping correctly. You are + * strongly encouraged to use them + * 1. Because people otherwise forget + * 2. Because if the timer wrap changes in future you won't have to + * alter your driver code. + * + * time_after(a,b) returns true if the time a is after time b. + * + * Do this with "<0" and ">=0" to only test the sign of the result. A + * good compiler would generate better code (and a really good compiler + * wouldn't care). Gcc is currently neither. + */ +#define time_after(a,b) \ + (typecheck(unsigned long, a) && \ + typecheck(unsigned long, b) && \ + ((long)((b) - (a)) < 0)) +#define time_before(a,b) time_after(b,a) + +#define time_after_eq(a,b) \ + (typecheck(unsigned long, a) && \ + typecheck(unsigned long, b) && \ + ((long)((a) - (b)) >= 0)) +#define time_before_eq(a,b) time_after_eq(b,a) + +/* + * Calculate whether a is in the range of [b, c]. + */ +#define time_in_range(a,b,c) \ + (time_after_eq(a,b) && \ + time_before_eq(a,c)) + +/* + * Calculate whether a is in the range of [b, c). + */ +#define time_in_range_open(a,b,c) \ + (time_after_eq(a,b) && \ + time_before(a,c)) + #endif /* _TIME_H */