Gordon Henderson | 2c13e61 | 2012-07-10 13:37:06 +0100 | [diff] [blame] | 1 | /* |
| 2 | * speed.c: |
| 3 | * Simple program to measure the speed of the various GPIO |
| 4 | * access mechanisms. |
Gordon Henderson | 3fbc564 | 2013-03-24 20:04:07 +0000 | [diff] [blame] | 5 | * |
| 6 | * Copyright (c) 2012-2013 Gordon Henderson. <projects@drogon.net> |
| 7 | *********************************************************************** |
| 8 | * This file is part of wiringPi: |
| 9 | * https://projects.drogon.net/raspberry-pi/wiringpi/ |
| 10 | * |
| 11 | * wiringPi is free software: you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU Lesser General Public License as published by |
| 13 | * the Free Software Foundation, either version 3 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * wiringPi is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU Lesser General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU Lesser General Public License |
| 22 | * along with wiringPi. If not, see <http://www.gnu.org/licenses/>. |
| 23 | *********************************************************************** |
Gordon Henderson | 2c13e61 | 2012-07-10 13:37:06 +0100 | [diff] [blame] | 24 | */ |
| 25 | |
| 26 | #include <wiringPi.h> |
| 27 | |
| 28 | #include <stdio.h> |
| 29 | #include <stdlib.h> |
| 30 | #include <stdint.h> |
| 31 | |
| 32 | #define FAST_COUNT 10000000 |
| 33 | #define SLOW_COUNT 1000000 |
Phil Howard | 26c7fe3 | 2016-02-27 16:03:10 +0000 | [diff] [blame] | 34 | #define PASSES 5 |
| 35 | |
| 36 | void speedTest (int pin, int maxCount) |
| 37 | { |
| 38 | int count, sum, perSec, i ; |
| 39 | unsigned int start, end ; |
| 40 | |
| 41 | sum = 0 ; |
| 42 | |
| 43 | for (i = 0 ; i < PASSES ; ++i) |
| 44 | { |
| 45 | start = millis () ; |
| 46 | for (count = 0 ; count < maxCount ; ++count) |
| 47 | digitalWrite (pin, 1) ; |
| 48 | end = millis () ; |
| 49 | printf (" %6d", end - start) ; |
| 50 | fflush (stdout) ; |
| 51 | sum += (end - start) ; |
| 52 | } |
| 53 | |
| 54 | digitalWrite (pin, 0) ; |
| 55 | printf (". Av: %6dmS", sum / PASSES) ; |
| 56 | perSec = (int)(double)maxCount / (double)((double)sum / (double)PASSES) * 1000.0 ; |
| 57 | printf (": %7d/sec\n", perSec) ; |
| 58 | } |
Gordon Henderson | 2c13e61 | 2012-07-10 13:37:06 +0100 | [diff] [blame] | 59 | |
| 60 | |
| 61 | int main (void) |
| 62 | { |
Phil Howard | 26c7fe3 | 2016-02-27 16:03:10 +0000 | [diff] [blame] | 63 | printf ("Raspberry Pi wiringPi GPIO speed test program\n") ; |
| 64 | printf ("=============================================\n") ; |
Gordon Henderson | 2c13e61 | 2012-07-10 13:37:06 +0100 | [diff] [blame] | 65 | |
| 66 | // Start the standard way |
| 67 | |
Phil Howard | 26c7fe3 | 2016-02-27 16:03:10 +0000 | [diff] [blame] | 68 | printf ("\nNative wiringPi method: (%8d iterations)\n", FAST_COUNT) ; |
| 69 | wiringPiSetup () ; |
Gordon Henderson | 2c13e61 | 2012-07-10 13:37:06 +0100 | [diff] [blame] | 70 | pinMode (0, OUTPUT) ; |
Phil Howard | 26c7fe3 | 2016-02-27 16:03:10 +0000 | [diff] [blame] | 71 | speedTest (0, FAST_COUNT) ; |
Gordon Henderson | 2c13e61 | 2012-07-10 13:37:06 +0100 | [diff] [blame] | 72 | |
Phil Howard | 26c7fe3 | 2016-02-27 16:03:10 +0000 | [diff] [blame] | 73 | // GPIO |
Gordon Henderson | 2c13e61 | 2012-07-10 13:37:06 +0100 | [diff] [blame] | 74 | |
Phil Howard | 26c7fe3 | 2016-02-27 16:03:10 +0000 | [diff] [blame] | 75 | printf ("\nNative GPIO method: (%8d iterations)\n", FAST_COUNT) ; |
| 76 | wiringPiSetupGpio () ; |
Gordon Henderson | 2c13e61 | 2012-07-10 13:37:06 +0100 | [diff] [blame] | 77 | pinMode (17, OUTPUT) ; |
Phil Howard | 26c7fe3 | 2016-02-27 16:03:10 +0000 | [diff] [blame] | 78 | speedTest (17, FAST_COUNT) ; |
Gordon Henderson | 2c13e61 | 2012-07-10 13:37:06 +0100 | [diff] [blame] | 79 | |
Phil Howard | 26c7fe3 | 2016-02-27 16:03:10 +0000 | [diff] [blame] | 80 | // Phys |
Gordon Henderson | 2c13e61 | 2012-07-10 13:37:06 +0100 | [diff] [blame] | 81 | |
Phil Howard | 26c7fe3 | 2016-02-27 16:03:10 +0000 | [diff] [blame] | 82 | printf ("\nPhysical pin GPIO method: (%8d iterations)\n", FAST_COUNT) ; |
| 83 | wiringPiSetupPhys () ; |
| 84 | pinMode (11, OUTPUT) ; |
| 85 | speedTest (11, FAST_COUNT) ; |
Gordon Henderson | 2c13e61 | 2012-07-10 13:37:06 +0100 | [diff] [blame] | 86 | |
| 87 | // Switch to SYS mode: |
| 88 | |
Phil Howard | 26c7fe3 | 2016-02-27 16:03:10 +0000 | [diff] [blame] | 89 | system ("/usr/local/bin/gpio export 17 out") ; |
| 90 | printf ("\n/sys/class/gpio method: (%8d iterations)\n", SLOW_COUNT) ; |
| 91 | wiringPiSetupSys () ; |
| 92 | speedTest (17, SLOW_COUNT) ; |
Gordon Henderson | 2c13e61 | 2012-07-10 13:37:06 +0100 | [diff] [blame] | 93 | |
| 94 | return 0 ; |
| 95 | } |