From patchwork Wed Apr 29 13:56:43 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 466059 X-Patchwork-Delegate: sjg@chromium.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from theia.denx.de (theia.denx.de [85.214.87.163]) by ozlabs.org (Postfix) with ESMTP id 22F281402B0 for ; Wed, 29 Apr 2015 23:57:19 +1000 (AEST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id D6C5C4BBB4; Wed, 29 Apr 2015 15:57:16 +0200 (CEST) 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 Rc_wUElxyCyb; Wed, 29 Apr 2015 15:57:16 +0200 (CEST) Received: from theia.denx.de (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id C9DDB4BB45; Wed, 29 Apr 2015 15:57:15 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 945B04BB45 for ; Wed, 29 Apr 2015 15:57:12 +0200 (CEST) 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 Hlv480tRwklJ for ; Wed, 29 Apr 2015 15:57:12 +0200 (CEST) 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 mail-ob0-f202.google.com (mail-ob0-f202.google.com [209.85.214.202]) by theia.denx.de (Postfix) with ESMTPS id 23CE14BB44 for ; Wed, 29 Apr 2015 15:57:08 +0200 (CEST) Received: by obbgq1 with SMTP id gq1so1673394obb.1 for ; Wed, 29 Apr 2015 06:57:07 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=+f9tslCShoENALpXYlJRjNXGfdWGQIfJ/D1iGPE4vTI=; b=FPcuUkPknoBA0fEwCBjJuf0HHEsbJ72HpXpEet+MXB6WgG6v9Y6P/pXKx5LK96aXCI efHaZ+M3xMj2VWCBH4CC3R9WvIuxq3vmm8ManW9rGmn+2d18sH5asF3KWup/gv3rB3AI ySaDMsYLwPdiDpP13bXWHej/mNKUi7hJFV1gXn797CTeRsdGTObEiG5Iil5gXD/wKKWk xnXz1Rc77cVU9JeZ1PWDrazeD7XBNUrEf8yYI0tokqbXZ0VK5TPv3IyImRUS5fW7FnIB TtIrL0tT7KayPBM1FqXUSUh38RHV2bnusY+HHmb/mIBs0pbNlBwA7bfEosrQJOqPtfk8 h1Hg== X-Gm-Message-State: ALoCoQm/8CAP+ZNZ0rdt4yxaSJoj2JkCJrMp6PU08iUdKn//+smZgDEucd0Twd7gdzrvgvMW85c0 X-Received: by 10.182.91.6 with SMTP id ca6mr37500342obb.29.1430315826840; Wed, 29 Apr 2015 06:57:06 -0700 (PDT) Received: from corpmail-nozzle1-2.hot.corp.google.com ([100.108.1.103]) by gmr-mx.google.com with ESMTPS id l36si1456221yhb.1.2015.04.29.06.57.06 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 29 Apr 2015 06:57:06 -0700 (PDT) Received: from kaki.bld.corp.google.com ([172.29.216.32]) by corpmail-nozzle1-2.hot.corp.google.com with ESMTP id qkzXEPya.1; Wed, 29 Apr 2015 06:57:06 -0700 Received: by kaki.bld.corp.google.com (Postfix, from userid 121222) id 2215922030C; Wed, 29 Apr 2015 07:57:06 -0600 (MDT) From: Simon Glass To: U-Boot Mailing List Date: Wed, 29 Apr 2015 07:56:43 -0600 Message-Id: <1430315817-17272-7-git-send-email-sjg@chromium.org> X-Mailer: git-send-email 2.2.0.rc0.207.ga3a616c Cc: Thomas Beaman Subject: [U-Boot] [PATCH v3 06/20] Add print_freq() to display frequencies nicely 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" Add a function similar to print_size() that works for frequencies. It can handle from Hz to GHz. Signed-off-by: Simon Glass Reviewed-by: Bin Meng Tested-by: Bin Meng --- Changes in v3: - Only display 2 digits of the fractional part, and round up when needed Changes in v2: - Correct bugs in number output include/display_options.h | 11 +++++++++++ lib/display_options.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/include/display_options.h b/include/display_options.h index 54bd41d..ac44c45 100644 --- a/include/display_options.h +++ b/include/display_options.h @@ -23,6 +23,17 @@ void print_size(uint64_t size, const char *suffix); /** + * print_freq() - Print a frequency with a suffix + * + * Print frequencies as "x.xx GHz", "xxx KHz", etc as needed; allow for + * optional trailing string (like "\n") + * + * @freq: Frequency to print in Hz + * @suffix String to print after the frequency + */ +void print_freq(uint64_t freq, const char *suffix); + +/** * print_buffer() - Print data buffer in hex and ascii form * * Data reads are buffered so that each memory address is only read once. diff --git a/lib/display_options.c b/lib/display_options.c index 3f32bcd..24d8f55 100644 --- a/lib/display_options.c +++ b/lib/display_options.c @@ -7,6 +7,7 @@ #include #include +#include #include #include #include @@ -22,6 +23,47 @@ int display_options (void) return 0; } +void print_freq(uint64_t freq, const char *s) +{ + unsigned long m = 0, n; + uint32_t f; + static const char names[] = {'G', 'M', 'K'}; + unsigned long d = 1e9; + char c = 0; + unsigned int i; + + for (i = 0; i < ARRAY_SIZE(names); i++, d /= 1000) { + if (freq >= d) { + c = names[i]; + break; + } + } + + if (!c) { + printf("%" PRIu64 " Hz%s", freq, s); + return; + } + + f = do_div(freq, d); + n = freq; + + /* If there's a remainder, show the first few digits */ + if (f) { + m = f; + while (m > 1000) + m /= 10; + while (m && !(m % 10)) + m /= 10; + if (m >= 100) + m = (m / 10) + (m % 100 >= 50); + } + + printf("%lu", n); + if (m) + printf(".%ld", m); + printf(" %cHz%s", c, s); +} + void print_size(uint64_t size, const char *s) { unsigned long m = 0, n;