chiark / gitweb /
wiringPi Version 2 - First commit (of v2)
[wiringPi.git] / examples / speed.c
1 /*
2  * speed.c:
3  *      Simple program to measure the speed of the various GPIO
4  *      access mechanisms.
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  ***********************************************************************
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
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 }
59
60
61 int main (void)
62 {
63   printf ("Raspberry Pi wiringPi GPIO speed test program\n") ;
64   printf ("=============================================\n") ;
65
66 // Start the standard way
67
68   printf ("\nNative wiringPi method: (%8d iterations)\n", FAST_COUNT) ;
69   wiringPiSetup () ;
70   pinMode (0, OUTPUT) ;
71   speedTest (0, FAST_COUNT) ;
72
73 // GPIO
74
75   printf ("\nNative GPIO method: (%8d iterations)\n", FAST_COUNT) ;
76   wiringPiSetupGpio () ;
77   pinMode (17, OUTPUT) ;
78   speedTest (17, FAST_COUNT) ;
79
80 // Phys
81
82   printf ("\nPhysical pin GPIO method: (%8d iterations)\n", FAST_COUNT) ;
83   wiringPiSetupPhys () ;
84   pinMode (11, OUTPUT) ;
85   speedTest (11, FAST_COUNT) ;
86
87 // Switch to SYS mode:
88
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) ;
93
94   return 0 ;
95 }