From patchwork Tue Mar 21 15:48:46 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frode Isaksen X-Patchwork-Id: 9636999 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id DC51E602CC for ; Tue, 21 Mar 2017 15:50:03 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id CE96C206AC for ; Tue, 21 Mar 2017 15:50:03 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C3F9A2841F; Tue, 21 Mar 2017 15:50:03 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1D36F206AC for ; Tue, 21 Mar 2017 15:50:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1757307AbdCUPtj (ORCPT ); Tue, 21 Mar 2017 11:49:39 -0400 Received: from mail-wr0-f177.google.com ([209.85.128.177]:34495 "EHLO mail-wr0-f177.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932613AbdCUPsz (ORCPT ); Tue, 21 Mar 2017 11:48:55 -0400 Received: by mail-wr0-f177.google.com with SMTP id l37so114768987wrc.1 for ; Tue, 21 Mar 2017 08:48:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=f/MVPiMcY9biHtGoFPQXY4H1HkcFUTgzpEQ3nVAxQNM=; b=P34Mu/m534LLWZbV1cftslKEL4IYXNqjXAhmzsAdi/BmOdM4D1QpsihkzxC8PfBncO 49rGbWov3sIBf4Lder4slRiqSEbv2OIvJTpZcuB4WDdc6NBHWgJvHq+WZN7KDw2mk07M 1kRdmSSIijELCOylPbGn3HDTSe7seUx6snOVaOCX44zFzBOMcTlyu+wxYV/psUy74q4+ 6QOlM7+A1uwDLeQanFXc4uwyNKpUpjcURYnI0VvZmS4dLAJxwH/jHwkOJhD1CVDI2SO9 3qvAwyYqVIKSX+KY+xzAnJnVSMj75xEadt6yYexfi6rczEEMc6ViTGAB772yTMlj9Tzl q9Fw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=f/MVPiMcY9biHtGoFPQXY4H1HkcFUTgzpEQ3nVAxQNM=; b=q6Blsvg7yPElEsg99xlZYgHeCgNO5tsABqP0SMK/yiyqemF2cr0bzeWNl79S0LWZwM 7mTGvkTBnMQCEN6TN3Jul3ZJAK42ao9pyvy7bZ4tTGpmidcQFt+ywi+XQSH+5XzPtcIR wKBHWBqSmMG4rtc7/mx43B5PAByS/RtbH/ZndW/LhW3roDA6y2g/ybI3Mb/T8x813lhe fInbHvLpAfH1KHny5nlss4tKgPuxdd/RcOsaoxJYwCIPk5AmLtDrRnP/Ga/SEYoVWhXc qV9bFPJltjiBU4J/eEfNcYfuKHp4A7oEzHLWgmNjcUi5gHmk4V4NWgYsTGLPp6wgjaFY kWBA== X-Gm-Message-State: AFeK/H0SQVHujbBwTDK8XUSjTX1ge8wn9LS2neo9+PMkCibYlUK5RtDfIhFSiUhTQPhjiSSX X-Received: by 10.223.162.155 with SMTP id s27mr29262601wra.159.1490111333129; Tue, 21 Mar 2017 08:48:53 -0700 (PDT) Received: from localhost.localdomain ([90.63.244.31]) by smtp.gmail.com with ESMTPSA id g6sm18087625wmc.30.2017.03.21.08.48.52 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 21 Mar 2017 08:48:52 -0700 (PDT) From: Frode Isaksen To: nsekhar@ti.com, khilman@baylibre.com, ptitiano@baylibre.com, linux-spi@vger.kernel.org Cc: broonie@kernel.org, Frode Isaksen Subject: [PATCH v2] spi: spidev_test: add option to continuously transfer data Date: Tue, 21 Mar 2017 16:48:46 +0100 Message-Id: <1490111326-21215-2-git-send-email-fisaksen@baylibre.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1490111326-21215-1-git-send-email-fisaksen@baylibre.com> References: <1490111326-21215-1-git-send-email-fisaksen@baylibre.com> Sender: linux-spi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-spi@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Add option to send+recv bytes with iterations and show the transfer rate every 5 seconds. Example: rate: tx 4235.2kbps, rx 4235.2kbps total: tx 4882.8KB, rx 4882.8KB Signed-off-by: Frode Isaksen --- tools/spi/spidev_test.c | 93 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 88 insertions(+), 5 deletions(-) diff --git a/tools/spi/spidev_test.c b/tools/spi/spidev_test.c index 816f119..8c590cd 100644 --- a/tools/spi/spidev_test.c +++ b/tools/spi/spidev_test.c @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include @@ -40,6 +41,9 @@ static char *output_file; static uint32_t speed = 500000; static uint16_t delay; static int verbose; +static int transfer_size; +static int iterations; +static int interval = 5; /* interval in seconds for showing transfer rate */ uint8_t default_tx[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, @@ -156,13 +160,13 @@ static void transfer(int fd, uint8_t const *tx, uint8_t const *rx, size_t len) close(out_fd); } - if (verbose || !output_file) + if (verbose) hex_dump(rx, len, 32, "RX"); } static void print_usage(const char *prog) { - printf("Usage: %s [-DsbdlHOLC3]\n", prog); + printf("Usage: %s [-DsbdlHOLC3vpNR24SI]\n", prog); puts(" -D --device device to use (default /dev/spidev1.1)\n" " -s --speed max speed (Hz)\n" " -d --delay delay (usec)\n" @@ -180,7 +184,9 @@ static void print_usage(const char *prog) " -N --no-cs no chip select\n" " -R --ready slave pulls low to pause\n" " -2 --dual dual transfer\n" - " -4 --quad quad transfer\n"); + " -4 --quad quad transfer\n" + " -S --size transfer size\n" + " -I --iter iterations\n"); exit(1); } @@ -205,11 +211,13 @@ static void parse_opts(int argc, char *argv[]) { "dual", 0, 0, '2' }, { "verbose", 0, 0, 'v' }, { "quad", 0, 0, '4' }, + { "size", 1, 0, 'S' }, + { "iter", 1, 0, 'I' }, { NULL, 0, 0, 0 }, }; int c; - c = getopt_long(argc, argv, "D:s:d:b:i:o:lHOLC3NR24p:v", + c = getopt_long(argc, argv, "D:s:d:b:i:o:lHOLC3NR24p:vS:I:", lopts, NULL); if (c == -1) @@ -270,6 +278,12 @@ static void parse_opts(int argc, char *argv[]) case '4': mode |= SPI_TX_QUAD; break; + case 'S': + transfer_size = atoi(optarg); + break; + case 'I': + iterations = atoi(optarg); + break; default: print_usage(argv[0]); break; @@ -336,6 +350,57 @@ static void transfer_file(int fd, char *filename) close(tx_fd); } +static uint64_t _read_count; +static uint64_t _write_count; + +static void show_transfer_rate(void) +{ + static uint64_t prev_read_count, prev_write_count; + double rx_rate, tx_rate; + + rx_rate = ((_read_count - prev_read_count) * 8) / (interval*1000.0); + tx_rate = ((_write_count - prev_write_count) * 8) / (interval*1000.0); + + printf("rate: tx %.1fkbps, rx %.1fkbps\n", rx_rate, tx_rate); + + prev_read_count = _read_count; + prev_write_count = _write_count; +} + +static void transfer_buf(int fd, int len) +{ + uint8_t *tx; + uint8_t *rx; + int i; + + tx = malloc(len); + if (!tx) + pabort("can't allocate tx buffer"); + for (i = 0; i < len; i++) + tx[i] = random(); + + rx = malloc(len); + if (!rx) + pabort("can't allocate rx buffer"); + + transfer(fd, tx, rx, len); + + _write_count += len; + _read_count += len; + + if (mode & SPI_LOOP) { + if (memcmp(tx, rx, len)) { + fprintf(stderr, "transfer error !\n"); + hex_dump(tx, len, 32, "TX"); + hex_dump(rx, len, 32, "RX"); + exit(1); + } + } + + free(rx); + free(tx); +} + int main(int argc, char *argv[]) { int ret = 0; @@ -391,7 +456,25 @@ int main(int argc, char *argv[]) transfer_escaped_string(fd, input_tx); else if (input_file) transfer_file(fd, input_file); - else + else if (transfer_size) { + struct timespec last_stat; + + clock_gettime(CLOCK_MONOTONIC, &last_stat); + + while (iterations-- > 0) { + struct timespec current; + + transfer_buf(fd, transfer_size); + + clock_gettime(CLOCK_MONOTONIC, ¤t); + if (current.tv_sec - last_stat.tv_sec > interval) { + show_transfer_rate(); + last_stat = current; + } + } + printf("total: tx %.1fKB, rx %.1fKB\n", + _write_count/1024.0, _read_count/1024.0); + } else transfer(fd, default_tx, default_rx, sizeof(default_tx)); close(fd);