blob: 0a42b36291473907b133c0e67967900a0938eada [file] [log] [blame]
Gordon Henderson2c13e612012-07-10 13:37:06 +01001/*
2 * speed.c:
3 * Simple program to measure the speed of the various GPIO
4 * access mechanisms.
Gordon Henderson3fbc5642013-03-24 20:04:07 +00005 *
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 Henderson2c13e612012-07-10 13:37:06 +010024 */
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 Howard26c7fe32016-02-27 16:03:10 +000034#define PASSES 5
35
36void 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 Henderson2c13e612012-07-10 13:37:06 +010059
60
61int main (void)
62{
Phil Howard26c7fe32016-02-27 16:03:10 +000063 printf ("Raspberry Pi wiringPi GPIO speed test program\n") ;
64 printf ("=============================================\n") ;
Gordon Henderson2c13e612012-07-10 13:37:06 +010065
66// Start the standard way
67
Phil Howard26c7fe32016-02-27 16:03:10 +000068 printf ("\nNative wiringPi method: (%8d iterations)\n", FAST_COUNT) ;
69 wiringPiSetup () ;
Gordon Henderson2c13e612012-07-10 13:37:06 +010070 pinMode (0, OUTPUT) ;
Phil Howard26c7fe32016-02-27 16:03:10 +000071 speedTest (0, FAST_COUNT) ;
Gordon Henderson2c13e612012-07-10 13:37:06 +010072
Phil Howard26c7fe32016-02-27 16:03:10 +000073// GPIO
Gordon Henderson2c13e612012-07-10 13:37:06 +010074
Phil Howard26c7fe32016-02-27 16:03:10 +000075 printf ("\nNative GPIO method: (%8d iterations)\n", FAST_COUNT) ;
76 wiringPiSetupGpio () ;
Gordon Henderson2c13e612012-07-10 13:37:06 +010077 pinMode (17, OUTPUT) ;
Phil Howard26c7fe32016-02-27 16:03:10 +000078 speedTest (17, FAST_COUNT) ;
Gordon Henderson2c13e612012-07-10 13:37:06 +010079
Phil Howard26c7fe32016-02-27 16:03:10 +000080// Phys
Gordon Henderson2c13e612012-07-10 13:37:06 +010081
Phil Howard26c7fe32016-02-27 16:03:10 +000082 printf ("\nPhysical pin GPIO method: (%8d iterations)\n", FAST_COUNT) ;
83 wiringPiSetupPhys () ;
84 pinMode (11, OUTPUT) ;
85 speedTest (11, FAST_COUNT) ;
Gordon Henderson2c13e612012-07-10 13:37:06 +010086
87// Switch to SYS mode:
88
Phil Howard26c7fe32016-02-27 16:03:10 +000089 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 Henderson2c13e612012-07-10 13:37:06 +010093
94 return 0 ;
95}