From patchwork Tue Dec 20 06:53:31 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahiro Yamada X-Patchwork-Id: 88548 Delivered-To: patch@linaro.org Received: by 10.140.20.101 with SMTP id 92csp1575315qgi; Mon, 19 Dec 2016 22:54:32 -0800 (PST) X-Received: by 10.194.94.166 with SMTP id dd6mr19077289wjb.88.1482216872001; Mon, 19 Dec 2016 22:54:32 -0800 (PST) Return-Path: Received: from theia.denx.de (theia.denx.de. [85.214.87.163]) by mx.google.com with ESMTP id j83si17765028wmj.10.2016.12.19.22.54.31; Mon, 19 Dec 2016 22:54:31 -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 B20C8A767A; Tue, 20 Dec 2016 07:54:22 +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 psTZSXwBxdwZ; Tue, 20 Dec 2016 07:54:22 +0100 (CET) Received: from theia.denx.de (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 7E8B3B38FD; Tue, 20 Dec 2016 07:54:14 +0100 (CET) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 295B6A75C3 for ; Tue, 20 Dec 2016 07:54:04 +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 1NP2nonDauWy for ; Tue, 20 Dec 2016 07:54:04 +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 21F24A7652 for ; Tue, 20 Dec 2016 07:53:59 +0100 (CET) Received: from pug.jp.socionext.com (p14092-ipngnfx01kyoto.kyoto.ocn.ne.jp [153.142.97.92]) (authenticated) by conuserg-12.nifty.com with ESMTP id uBK6ratM015897; Tue, 20 Dec 2016 15:53:39 +0900 DKIM-Filter: OpenDKIM Filter v2.10.3 conuserg-12.nifty.com uBK6ratM015897 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nifty.com; s=dec2015msa; t=1482216819; bh=8F9VApFdqXf2Q7H9++pB3GLknKfIu5ufHz112sgSklw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=OSb1e95lHbfdomYbR/hkzbXAiGzfXJgctMrfbFR3/7mCp/hrY9QcyN3FEjcPnIgnC F2RO3eUbTse+BWmw/7Btxj9Ccwut5rhXYNszSejz/SizkCI9jyMT1crkc0FUwvuSq0 e79BhvAsJqLOx5Z4GBXz9dBF3DJxV7c6TEvNHYHkNPzwBSAcdQnCqIasIJ4QiDmzGL 6yTx9PVTqoFxuyL+usE0K4d9/umQNf4+RG/ZxUWTZj1N/uxf5WsDXqDroEm3Tco22a Q9IkbujEhkWZUvHJSbUI7yNGWGnH4pyHh+N6BlvGikthw2idQHaWY3yreKxNYi1i7e Eqsx/iylCOP0A== X-Nifty-SrcIP: [153.142.97.92] From: Masahiro Yamada To: u-boot@lists.denx.de Date: Tue, 20 Dec 2016 15:53:31 +0900 Message-Id: <1482216812-18379-5-git-send-email-yamada.masahiro@socionext.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1482216812-18379-1-git-send-email-yamada.masahiro@socionext.com> References: <1482216812-18379-1-git-send-email-yamada.masahiro@socionext.com> Subject: [U-Boot] [PATCH 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 --- 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 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 */